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