+typedef struct enc_dyn_arg {
+ void *buffer;
+ size_t length;
+ size_t allocated;
+} enc_dyn_arg;
+static int
+encode_dyn_cb(const void *buffer, size_t size, void *key) {
+ enc_dyn_arg *arg = key;
+ if(arg->length + size >= arg->allocated) {
+ size_t new_size = arg->allocated ? arg->allocated : 8;
+ void *p;
+
+ do {
+ new_size <<= 2;
+ } while(arg->length + size >= new_size);
+
+ p = REALLOC(arg->buffer, new_size);
+ if(!p) {
+ FREEMEM(arg->buffer);
+ memset(arg, 0, sizeof(*arg));
+ return -1;
+ }
+ arg->buffer = p;
+ arg->allocated = new_size;
+ }
+ memcpy(((char *)arg->buffer) + arg->length, buffer, size);
+ arg->length += size;
+ return 0;
+}
+ssize_t
+uper_encode_to_new_buffer(const asn_TYPE_descriptor_t *td,
+ const asn_per_constraints_t *constraints,
+ const void *sptr, void **buffer_r) {
+ asn_enc_rval_t er;
+ enc_dyn_arg key;
+
+ memset(&key, 0, sizeof(key));
+
+ er = uper_encode(td, constraints, sptr, encode_dyn_cb, &key);
+ switch(er.encoded) {
+ case -1:
+ FREEMEM(key.buffer);
+ return -1;
+ case 0:
+ FREEMEM(key.buffer);
+ key.buffer = MALLOC(1);
+ if(key.buffer) {
+ *(char *)key.buffer = '\0';
+ *buffer_r = key.buffer;
+ return 1;
+ } else {
+ return -1;
+ }
+ default:
+ *buffer_r = key.buffer;
+ ASN_DEBUG("Complete encoded in %ld bits", (long)er.encoded);
+ return ((er.encoded + 7) >> 3);
+ }