Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8096245
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T21:23:34+00:00 2026-06-05T21:23:34+00:00

I am using asn1c to de/encode DER encoded data. That’s working fine. However, for

  • 0

I am using asn1c to de/encode DER encoded data. That’s working fine. However, for logging it would be nice to be able to write the sent/received data in XER format (XML-like). The asn1c generated files contain routines to write XER encoded data to a FILE*, such as stdout. However, I would like to encode into a buffer, so that I can process the data before I print it. Can anyone tell me how to do that?

I would like a similar interface as encode_to_buffer(), which encodes a sructure in DER data.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T21:23:36+00:00Added an answer on June 5, 2026 at 9:23 pm

    I just found out how it can be done: The xer_encode() function generated by asn1c takes a callback, which gets a XER snippet and it’s size in bytes as well as a userdata pointer. By writing an appropriate callback, we can put the XER encoded data in a buffer.

    Below is how I solved my problem. I implemented a xer_buffer_t which holds a buffer, it’s size and tracks how full the buffer is so that the callback can realloc if necessary.

    void init_xer_buffer(xer_buffer_t* xer_buffer) {
        xer_buffer->buffer = malloc(1024);
        assert(xer_buffer->buffer != NULL);
        xer_buffer->buffer_size = 1024;
        xer_buffer->buffer_filled = 0;
    }
    
    void free_xer_buffer(xer_buffer_t* xer_buffer) {
        free(xer_buffer->buffer);
        xer_buffer->buffer_size = 0;
        xer_buffer->buffer_filled = 0;
    }
    
    static int xer_print2xerbuf_cb(const void *buffer, size_t size, void *app_key) {
        xer_buffer_t* xb = (xer_buffer_t*) app_key;
        while (xb->buffer_size - xb->buffer_filled <= size+1) {
            xb->buffer_size *= 2;
            xb->buffer_size += 1;
            xb->buffer = realloc(xb->buffer, xb->buffer_size);
            assert(xb->buffer != NULL);
        }
        memcpy(xb->buffer+xb->buffer_filled, buffer, size);
        xb->buffer_filled += size;
        *(xb->buffer+xb->buffer_filled) = '\0';
        return 1;
    }
    
    int xer_encode_to_buffer(xer_buffer_t* xb, asn_TYPE_descriptor_t *td, void *sptr) {
        asn_enc_rval_t er;
        if (!td || !sptr) return -1;
        er = xer_encode(td, sptr, XER_F_BASIC, xer_print2xerbuf_cb, xb);
        if (er.encoded == -1) return -1;
        return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Greetings, How can I simply encode some binary data into an ASN.1 DER-encoded blob?
I am using iText library to read PDF files. It's working fine for all
Using the Redis info command, I am able to get all the stats of
Using Rails 3.2.0 with haml and sass: I Would like to link an external
Using Android TelephonyManager an application can obtain the state of data activity over the
I am using openssl to get data about x509 certificate. Is there a way
Using Jenkins or Hudson I would like to create a pipeline of builds with
Using report builder 3.0, I have a report that queries a cube. How do
Using Entity Framework CodeFirst, how do I create a created datetime column that gets
I have some data that pyOpenSSL gave me, '0\r\x82\x0bexample.com' . This should be the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.