Lev Walkin | 523de9e | 2006-08-18 01:34:18 +0000 | [diff] [blame] | 1 | #include <asn_application.h> |
| 2 | #include <asn_internal.h> |
| 3 | #include <per_encoder.h> |
| 4 | |
Lev Walkin | 5615304 | 2017-10-24 00:47:03 -0700 | [diff] [blame^] | 5 | static int _uper_encode_flush_outp(asn_per_outp_t *po); |
Lev Walkin | 523de9e | 2006-08-18 01:34:18 +0000 | [diff] [blame] | 6 | |
| 7 | asn_enc_rval_t |
Lev Walkin | 5615304 | 2017-10-24 00:47:03 -0700 | [diff] [blame^] | 8 | uper_encode(const asn_TYPE_descriptor_t *td, |
| 9 | const asn_per_constraints_t *constraints, const void *sptr, |
Lev Walkin | 20696a4 | 2017-10-17 21:27:33 -0700 | [diff] [blame] | 10 | asn_app_consume_bytes_f *cb, void *app_key) { |
Lev Walkin | 5615304 | 2017-10-24 00:47:03 -0700 | [diff] [blame^] | 11 | asn_per_outp_t po; |
| 12 | asn_enc_rval_t er; |
| 13 | |
| 14 | /* |
| 15 | * Invoke type-specific encoder. |
| 16 | */ |
| 17 | if(!td || !td->op->uper_encoder) |
| 18 | ASN__ENCODE_FAILED; /* PER is not compiled in */ |
| 19 | |
| 20 | po.buffer = po.tmpspace; |
| 21 | po.nboff = 0; |
| 22 | po.nbits = 8 * sizeof(po.tmpspace); |
| 23 | po.output = cb; |
| 24 | po.op_key = app_key; |
| 25 | po.flushed_bytes = 0; |
| 26 | |
| 27 | er = td->op->uper_encoder(td, constraints, sptr, &po); |
| 28 | if(er.encoded != -1) { |
| 29 | size_t bits_to_flush; |
| 30 | |
| 31 | bits_to_flush = ((po.buffer - po.tmpspace) << 3) + po.nboff; |
| 32 | |
| 33 | /* Set number of bits encoded to a firm value */ |
| 34 | er.encoded = (po.flushed_bytes << 3) + bits_to_flush; |
| 35 | |
| 36 | if(_uper_encode_flush_outp(&po)) ASN__ENCODE_FAILED; |
| 37 | } |
| 38 | |
| 39 | return er; |
Lev Walkin | 523de9e | 2006-08-18 01:34:18 +0000 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | /* |
| 43 | * Argument type and callback necessary for uper_encode_to_buffer(). |
| 44 | */ |
| 45 | typedef struct enc_to_buf_arg { |
| 46 | void *buffer; |
| 47 | size_t left; |
| 48 | } enc_to_buf_arg; |
| 49 | static int encode_to_buffer_cb(const void *buffer, size_t size, void *key) { |
| 50 | enc_to_buf_arg *arg = (enc_to_buf_arg *)key; |
| 51 | |
| 52 | if(arg->left < size) |
| 53 | return -1; /* Data exceeds the available buffer size */ |
| 54 | |
| 55 | memcpy(arg->buffer, buffer, size); |
| 56 | arg->buffer = ((char *)arg->buffer) + size; |
| 57 | arg->left -= size; |
| 58 | |
| 59 | return 0; |
| 60 | } |
| 61 | |
| 62 | asn_enc_rval_t |
Lev Walkin | 5615304 | 2017-10-24 00:47:03 -0700 | [diff] [blame^] | 63 | uper_encode_to_buffer(const asn_TYPE_descriptor_t *td, |
| 64 | const asn_per_constraints_t *constraints, |
| 65 | const void *sptr, void *buffer, size_t buffer_size) { |
Lev Walkin | 20696a4 | 2017-10-17 21:27:33 -0700 | [diff] [blame] | 66 | enc_to_buf_arg key; |
Lev Walkin | 523de9e | 2006-08-18 01:34:18 +0000 | [diff] [blame] | 67 | |
Lev Walkin | 5615304 | 2017-10-24 00:47:03 -0700 | [diff] [blame^] | 68 | key.buffer = buffer; |
| 69 | key.left = buffer_size; |
Lev Walkin | 523de9e | 2006-08-18 01:34:18 +0000 | [diff] [blame] | 70 | |
Lev Walkin | 5615304 | 2017-10-24 00:47:03 -0700 | [diff] [blame^] | 71 | if(td) ASN_DEBUG("Encoding \"%s\" using UNALIGNED PER", td->name); |
Lev Walkin | 523de9e | 2006-08-18 01:34:18 +0000 | [diff] [blame] | 72 | |
Lev Walkin | 5615304 | 2017-10-24 00:47:03 -0700 | [diff] [blame^] | 73 | return uper_encode(td, constraints, sptr, encode_to_buffer_cb, &key); |
Lev Walkin | 523de9e | 2006-08-18 01:34:18 +0000 | [diff] [blame] | 74 | } |
| 75 | |
Lev Walkin | 62258e2 | 2007-06-23 23:50:25 +0000 | [diff] [blame] | 76 | typedef struct enc_dyn_arg { |
| 77 | void *buffer; |
| 78 | size_t length; |
| 79 | size_t allocated; |
| 80 | } enc_dyn_arg; |
| 81 | static int |
| 82 | encode_dyn_cb(const void *buffer, size_t size, void *key) { |
| 83 | enc_dyn_arg *arg = key; |
| 84 | if(arg->length + size >= arg->allocated) { |
| 85 | void *p; |
| 86 | arg->allocated = arg->allocated ? (arg->allocated << 2) : size; |
| 87 | p = REALLOC(arg->buffer, arg->allocated); |
| 88 | if(!p) { |
| 89 | FREEMEM(arg->buffer); |
| 90 | memset(arg, 0, sizeof(*arg)); |
| 91 | return -1; |
| 92 | } |
| 93 | arg->buffer = p; |
| 94 | } |
| 95 | memcpy(((char *)arg->buffer) + arg->length, buffer, size); |
| 96 | arg->length += size; |
| 97 | return 0; |
| 98 | } |
| 99 | ssize_t |
Lev Walkin | 20696a4 | 2017-10-17 21:27:33 -0700 | [diff] [blame] | 100 | uper_encode_to_new_buffer(const asn_TYPE_descriptor_t *td, |
| 101 | const asn_per_constraints_t *constraints, |
| 102 | const void *sptr, void **buffer_r) { |
Lev Walkin | 494fb70 | 2017-08-07 20:07:00 -0700 | [diff] [blame] | 103 | asn_enc_rval_t er; |
Lev Walkin | 62258e2 | 2007-06-23 23:50:25 +0000 | [diff] [blame] | 104 | enc_dyn_arg key; |
| 105 | |
| 106 | memset(&key, 0, sizeof(key)); |
| 107 | |
Lev Walkin | 5615304 | 2017-10-24 00:47:03 -0700 | [diff] [blame^] | 108 | er = uper_encode(td, constraints, sptr, encode_dyn_cb, &key); |
Lev Walkin | 62258e2 | 2007-06-23 23:50:25 +0000 | [diff] [blame] | 109 | switch(er.encoded) { |
| 110 | case -1: |
| 111 | FREEMEM(key.buffer); |
| 112 | return -1; |
| 113 | case 0: |
| 114 | FREEMEM(key.buffer); |
| 115 | key.buffer = MALLOC(1); |
| 116 | if(key.buffer) { |
| 117 | *(char *)key.buffer = '\0'; |
| 118 | *buffer_r = key.buffer; |
| 119 | return 1; |
| 120 | } else { |
| 121 | return -1; |
| 122 | } |
| 123 | default: |
| 124 | *buffer_r = key.buffer; |
Lev Walkin | fe1ffaf | 2010-10-25 21:07:59 -0700 | [diff] [blame] | 125 | ASN_DEBUG("Complete encoded in %ld bits", (long)er.encoded); |
Lev Walkin | 62258e2 | 2007-06-23 23:50:25 +0000 | [diff] [blame] | 126 | return ((er.encoded + 7) >> 3); |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | /* |
| 131 | * Internally useful functions. |
| 132 | */ |
| 133 | |
| 134 | /* Flush partially filled buffer */ |
Lev Walkin | 523de9e | 2006-08-18 01:34:18 +0000 | [diff] [blame] | 135 | static int |
| 136 | _uper_encode_flush_outp(asn_per_outp_t *po) { |
| 137 | uint8_t *buf; |
| 138 | |
| 139 | if(po->nboff == 0 && po->buffer == po->tmpspace) |
| 140 | return 0; |
| 141 | |
| 142 | buf = po->buffer + (po->nboff >> 3); |
| 143 | /* Make sure we account for the last, partially filled */ |
| 144 | if(po->nboff & 0x07) { |
| 145 | buf[0] &= 0xff << (8 - (po->nboff & 0x07)); |
| 146 | buf++; |
| 147 | } |
| 148 | |
Lev Walkin | 6cd0d56 | 2017-08-25 11:57:01 -0700 | [diff] [blame] | 149 | return po->output(po->tmpspace, buf - po->tmpspace, po->op_key); |
Lev Walkin | 523de9e | 2006-08-18 01:34:18 +0000 | [diff] [blame] | 150 | } |
Lev Walkin | 62258e2 | 2007-06-23 23:50:25 +0000 | [diff] [blame] | 151 | |