How to map c code to postgres c function format?
I'm trying to write the following c code in postgres c function style -
The argument passed is a value of a table column of type varchar,
Needed some help to map this pure C code in the postgres C format of the type -
Datum
func_name (PG_FUNCTION_ARGS)
Can somebody please help to map this and return the appropriate char value?
static uint32_t key = 0xOQ9426YP;
uint32_t tmpKey = 0x00000000;
int i = 0;
uint32_t *in = (uint32_t *) str;
uint32_t *out = (uint32_t *) malloc (256);
memset (out, 0, 256);
tmpKey = key;
for (i = 0; i < (256/sizeof (uint32_t)); i++)
{
out[i] = tmpKey ^ in[i];
tmpKey = out[i];
}
memcpy (output, (char *) out, 256);
Thanks & Regards,
VJ
c postgresql
add a comment |
I'm trying to write the following c code in postgres c function style -
The argument passed is a value of a table column of type varchar,
Needed some help to map this pure C code in the postgres C format of the type -
Datum
func_name (PG_FUNCTION_ARGS)
Can somebody please help to map this and return the appropriate char value?
static uint32_t key = 0xOQ9426YP;
uint32_t tmpKey = 0x00000000;
int i = 0;
uint32_t *in = (uint32_t *) str;
uint32_t *out = (uint32_t *) malloc (256);
memset (out, 0, 256);
tmpKey = key;
for (i = 0; i < (256/sizeof (uint32_t)); i++)
{
out[i] = tmpKey ^ in[i];
tmpKey = out[i];
}
memcpy (output, (char *) out, 256);
Thanks & Regards,
VJ
c postgresql
This code completely ignores endianness. Is that your intention?
– paddy
Nov 22 '18 at 4:24
On top of that,0xOQ9426YP
is not a valid hexadecimal literal.
– paddy
Nov 22 '18 at 4:27
add a comment |
I'm trying to write the following c code in postgres c function style -
The argument passed is a value of a table column of type varchar,
Needed some help to map this pure C code in the postgres C format of the type -
Datum
func_name (PG_FUNCTION_ARGS)
Can somebody please help to map this and return the appropriate char value?
static uint32_t key = 0xOQ9426YP;
uint32_t tmpKey = 0x00000000;
int i = 0;
uint32_t *in = (uint32_t *) str;
uint32_t *out = (uint32_t *) malloc (256);
memset (out, 0, 256);
tmpKey = key;
for (i = 0; i < (256/sizeof (uint32_t)); i++)
{
out[i] = tmpKey ^ in[i];
tmpKey = out[i];
}
memcpy (output, (char *) out, 256);
Thanks & Regards,
VJ
c postgresql
I'm trying to write the following c code in postgres c function style -
The argument passed is a value of a table column of type varchar,
Needed some help to map this pure C code in the postgres C format of the type -
Datum
func_name (PG_FUNCTION_ARGS)
Can somebody please help to map this and return the appropriate char value?
static uint32_t key = 0xOQ9426YP;
uint32_t tmpKey = 0x00000000;
int i = 0;
uint32_t *in = (uint32_t *) str;
uint32_t *out = (uint32_t *) malloc (256);
memset (out, 0, 256);
tmpKey = key;
for (i = 0; i < (256/sizeof (uint32_t)); i++)
{
out[i] = tmpKey ^ in[i];
tmpKey = out[i];
}
memcpy (output, (char *) out, 256);
Thanks & Regards,
VJ
c postgresql
c postgresql
asked Nov 22 '18 at 3:59
Being VJBeing VJ
41
41
This code completely ignores endianness. Is that your intention?
– paddy
Nov 22 '18 at 4:24
On top of that,0xOQ9426YP
is not a valid hexadecimal literal.
– paddy
Nov 22 '18 at 4:27
add a comment |
This code completely ignores endianness. Is that your intention?
– paddy
Nov 22 '18 at 4:24
On top of that,0xOQ9426YP
is not a valid hexadecimal literal.
– paddy
Nov 22 '18 at 4:27
This code completely ignores endianness. Is that your intention?
– paddy
Nov 22 '18 at 4:24
This code completely ignores endianness. Is that your intention?
– paddy
Nov 22 '18 at 4:24
On top of that,
0xOQ9426YP
is not a valid hexadecimal literal.– paddy
Nov 22 '18 at 4:27
On top of that,
0xOQ9426YP
is not a valid hexadecimal literal.– paddy
Nov 22 '18 at 4:27
add a comment |
1 Answer
1
active
oldest
votes
For data PostgreSQL uses own internal format and structures that should not be compatible with C language. For strings a varlena
format is used - in this format first N bytes are encoded length - and after these bytes are encoded data. The direct access is not recommended - for almost all work there are a macros.
For example - some small function hello
can looks like:
Datum
Hello(PG_FUNCTION_ARGS)
{
text *txt = PG_GETARG_TEXT_PP(0);
text *result;
char *str;
char *hello = "Hello, ";
int size;
int hello_size;
-- conversion to c string
str = text_to_cstring(txt);
-- show it on debug console
elog(NOTICE, "input string: %s", str);
hello_size = strlen(hello);
size = hello_size + VARSIZE_ANY_EXHDR(txt) + VARHDRSZ;
-- allocate memory for result, use palloc, not malloc!
result = palloc(size);
--set size of result varlena type
SET_VARSIZE(result, size);
-- set data
memcpy(VARDATA(result), str, hello_size);
memcpy(VARDATA(result) + hello_size,
VARDATA_ANY(txt), VARSIZE_ANY_EXHDR(txt));
-- returns data
PG_RETURN_TEXT_P(result);
}
You can see, this is C and it is not C. There are lot of macros, than can be used a) for portability purposes, b) for hiding complexity. It is good to start to read documentation or some presentation.
This topic is not too hard or too complex, but it is not intuitive - you cannot to start without reading documentation.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53423678%2fhow-to-map-c-code-to-postgres-c-function-format%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
For data PostgreSQL uses own internal format and structures that should not be compatible with C language. For strings a varlena
format is used - in this format first N bytes are encoded length - and after these bytes are encoded data. The direct access is not recommended - for almost all work there are a macros.
For example - some small function hello
can looks like:
Datum
Hello(PG_FUNCTION_ARGS)
{
text *txt = PG_GETARG_TEXT_PP(0);
text *result;
char *str;
char *hello = "Hello, ";
int size;
int hello_size;
-- conversion to c string
str = text_to_cstring(txt);
-- show it on debug console
elog(NOTICE, "input string: %s", str);
hello_size = strlen(hello);
size = hello_size + VARSIZE_ANY_EXHDR(txt) + VARHDRSZ;
-- allocate memory for result, use palloc, not malloc!
result = palloc(size);
--set size of result varlena type
SET_VARSIZE(result, size);
-- set data
memcpy(VARDATA(result), str, hello_size);
memcpy(VARDATA(result) + hello_size,
VARDATA_ANY(txt), VARSIZE_ANY_EXHDR(txt));
-- returns data
PG_RETURN_TEXT_P(result);
}
You can see, this is C and it is not C. There are lot of macros, than can be used a) for portability purposes, b) for hiding complexity. It is good to start to read documentation or some presentation.
This topic is not too hard or too complex, but it is not intuitive - you cannot to start without reading documentation.
add a comment |
For data PostgreSQL uses own internal format and structures that should not be compatible with C language. For strings a varlena
format is used - in this format first N bytes are encoded length - and after these bytes are encoded data. The direct access is not recommended - for almost all work there are a macros.
For example - some small function hello
can looks like:
Datum
Hello(PG_FUNCTION_ARGS)
{
text *txt = PG_GETARG_TEXT_PP(0);
text *result;
char *str;
char *hello = "Hello, ";
int size;
int hello_size;
-- conversion to c string
str = text_to_cstring(txt);
-- show it on debug console
elog(NOTICE, "input string: %s", str);
hello_size = strlen(hello);
size = hello_size + VARSIZE_ANY_EXHDR(txt) + VARHDRSZ;
-- allocate memory for result, use palloc, not malloc!
result = palloc(size);
--set size of result varlena type
SET_VARSIZE(result, size);
-- set data
memcpy(VARDATA(result), str, hello_size);
memcpy(VARDATA(result) + hello_size,
VARDATA_ANY(txt), VARSIZE_ANY_EXHDR(txt));
-- returns data
PG_RETURN_TEXT_P(result);
}
You can see, this is C and it is not C. There are lot of macros, than can be used a) for portability purposes, b) for hiding complexity. It is good to start to read documentation or some presentation.
This topic is not too hard or too complex, but it is not intuitive - you cannot to start without reading documentation.
add a comment |
For data PostgreSQL uses own internal format and structures that should not be compatible with C language. For strings a varlena
format is used - in this format first N bytes are encoded length - and after these bytes are encoded data. The direct access is not recommended - for almost all work there are a macros.
For example - some small function hello
can looks like:
Datum
Hello(PG_FUNCTION_ARGS)
{
text *txt = PG_GETARG_TEXT_PP(0);
text *result;
char *str;
char *hello = "Hello, ";
int size;
int hello_size;
-- conversion to c string
str = text_to_cstring(txt);
-- show it on debug console
elog(NOTICE, "input string: %s", str);
hello_size = strlen(hello);
size = hello_size + VARSIZE_ANY_EXHDR(txt) + VARHDRSZ;
-- allocate memory for result, use palloc, not malloc!
result = palloc(size);
--set size of result varlena type
SET_VARSIZE(result, size);
-- set data
memcpy(VARDATA(result), str, hello_size);
memcpy(VARDATA(result) + hello_size,
VARDATA_ANY(txt), VARSIZE_ANY_EXHDR(txt));
-- returns data
PG_RETURN_TEXT_P(result);
}
You can see, this is C and it is not C. There are lot of macros, than can be used a) for portability purposes, b) for hiding complexity. It is good to start to read documentation or some presentation.
This topic is not too hard or too complex, but it is not intuitive - you cannot to start without reading documentation.
For data PostgreSQL uses own internal format and structures that should not be compatible with C language. For strings a varlena
format is used - in this format first N bytes are encoded length - and after these bytes are encoded data. The direct access is not recommended - for almost all work there are a macros.
For example - some small function hello
can looks like:
Datum
Hello(PG_FUNCTION_ARGS)
{
text *txt = PG_GETARG_TEXT_PP(0);
text *result;
char *str;
char *hello = "Hello, ";
int size;
int hello_size;
-- conversion to c string
str = text_to_cstring(txt);
-- show it on debug console
elog(NOTICE, "input string: %s", str);
hello_size = strlen(hello);
size = hello_size + VARSIZE_ANY_EXHDR(txt) + VARHDRSZ;
-- allocate memory for result, use palloc, not malloc!
result = palloc(size);
--set size of result varlena type
SET_VARSIZE(result, size);
-- set data
memcpy(VARDATA(result), str, hello_size);
memcpy(VARDATA(result) + hello_size,
VARDATA_ANY(txt), VARSIZE_ANY_EXHDR(txt));
-- returns data
PG_RETURN_TEXT_P(result);
}
You can see, this is C and it is not C. There are lot of macros, than can be used a) for portability purposes, b) for hiding complexity. It is good to start to read documentation or some presentation.
This topic is not too hard or too complex, but it is not intuitive - you cannot to start without reading documentation.
answered Nov 22 '18 at 4:57
Pavel StehulePavel Stehule
22.6k34857
22.6k34857
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53423678%2fhow-to-map-c-code-to-postgres-c-function-format%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
This code completely ignores endianness. Is that your intention?
– paddy
Nov 22 '18 at 4:24
On top of that,
0xOQ9426YP
is not a valid hexadecimal literal.– paddy
Nov 22 '18 at 4:27