How to map c code to postgres c function format?












0















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










share|improve this question























  • 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
















0















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










share|improve this question























  • 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














0












0








0








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










share|improve this question














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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










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



















  • 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












1 Answer
1






active

oldest

votes


















1














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.






share|improve this answer























    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
    });


    }
    });














    draft saved

    draft discarded


















    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









    1














    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.






    share|improve this answer




























      1














      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.






      share|improve this answer


























        1












        1








        1







        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.






        share|improve this answer













        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.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 22 '18 at 4:57









        Pavel StehulePavel Stehule

        22.6k34857




        22.6k34857






























            draft saved

            draft discarded




















































            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.




            draft saved


            draft discarded














            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





















































            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







            Popular posts from this blog

            Wiesbaden

            To store a contact into the json file from server.js file using a class in NodeJS

            Marschland