Peter Szilagyi | fbc56f9 | 2019-07-23 19:29:46 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2005-2017 Lev Walkin <vlm@lionet.info>. |
| 3 | * All rights reserved. |
| 4 | * Redistribution and modifications are permitted subject to BSD license. |
| 5 | */ |
| 6 | #include <asn_system.h> |
| 7 | #include <asn_internal.h> |
| 8 | #include <asn_bit_data.h> |
| 9 | |
| 10 | /* |
| 11 | * Create a contiguous non-refillable bit data structure. |
| 12 | * Can be freed by FREEMEM(). |
| 13 | */ |
| 14 | asn_bit_data_t * |
| 15 | asn_bit_data_new_contiguous(const void *data, size_t size_bits) { |
| 16 | size_t size_bytes = (size_bits + 7) / 8; |
| 17 | asn_bit_data_t *pd; |
| 18 | uint8_t *bytes; |
| 19 | |
| 20 | /* Get the extensions map */ |
| 21 | pd = CALLOC(1, sizeof(*pd) + size_bytes + 1); |
| 22 | if(!pd) { |
| 23 | return NULL; |
| 24 | } |
| 25 | bytes = (void *)(((char *)pd) + sizeof(*pd)); |
| 26 | memcpy(bytes, data, size_bytes); |
| 27 | bytes[size_bytes] = 0; |
| 28 | pd->buffer = bytes; |
| 29 | pd->nboff = 0; |
| 30 | pd->nbits = size_bits; |
| 31 | |
| 32 | return pd; |
| 33 | } |
| 34 | |
| 35 | |
| 36 | char * |
| 37 | asn_bit_data_string(asn_bit_data_t *pd) { |
| 38 | static char buf[2][32]; |
| 39 | static int n; |
| 40 | n = (n+1) % 2; |
| 41 | snprintf(buf[n], sizeof(buf[n]), |
| 42 | "{m=%" ASN_PRI_SIZE " span %" ASN_PRI_SIZE "[%" ASN_PRI_SIZE |
| 43 | "..%" ASN_PRI_SIZE "] (%" ASN_PRI_SIZE ")}", |
| 44 | pd->moved, ((uintptr_t)(pd->buffer) & 0xf), pd->nboff, pd->nbits, |
| 45 | pd->nbits - pd->nboff); |
| 46 | return buf[n]; |
| 47 | } |
| 48 | |
| 49 | void |
| 50 | asn_get_undo(asn_bit_data_t *pd, int nbits) { |
| 51 | if((ssize_t)pd->nboff < nbits) { |
| 52 | assert((ssize_t)pd->nboff < nbits); |
| 53 | } else { |
| 54 | pd->nboff -= nbits; |
| 55 | pd->moved -= nbits; |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | /* |
| 60 | * Extract a small number of bits (<= 31) from the specified PER data pointer. |
| 61 | */ |
| 62 | int32_t |
| 63 | asn_get_few_bits(asn_bit_data_t *pd, int nbits) { |
| 64 | size_t off; /* Next after last bit offset */ |
| 65 | ssize_t nleft; /* Number of bits left in this stream */ |
| 66 | uint32_t accum; |
| 67 | const uint8_t *buf; |
| 68 | |
| 69 | if(nbits < 0) |
| 70 | return -1; |
| 71 | |
| 72 | nleft = pd->nbits - pd->nboff; |
| 73 | if(nbits > nleft) { |
| 74 | int32_t tailv, vhead; |
| 75 | if(!pd->refill || nbits > 31) return -1; |
| 76 | /* Accumulate unused bytes before refill */ |
| 77 | ASN_DEBUG("Obtain the rest %d bits (want %d)", |
| 78 | (int)nleft, (int)nbits); |
| 79 | tailv = asn_get_few_bits(pd, nleft); |
| 80 | if(tailv < 0) return -1; |
| 81 | /* Refill (replace pd contents with new data) */ |
| 82 | if(pd->refill(pd)) |
| 83 | return -1; |
| 84 | nbits -= nleft; |
| 85 | vhead = asn_get_few_bits(pd, nbits); |
| 86 | /* Combine the rest of previous pd with the head of new one */ |
| 87 | tailv = (tailv << nbits) | vhead; /* Could == -1 */ |
| 88 | return tailv; |
| 89 | } |
| 90 | |
| 91 | /* |
| 92 | * Normalize position indicator. |
| 93 | */ |
| 94 | if(pd->nboff >= 8) { |
| 95 | pd->buffer += (pd->nboff >> 3); |
| 96 | pd->nbits -= (pd->nboff & ~0x07); |
| 97 | pd->nboff &= 0x07; |
| 98 | } |
| 99 | pd->moved += nbits; |
| 100 | pd->nboff += nbits; |
| 101 | off = pd->nboff; |
| 102 | buf = pd->buffer; |
| 103 | |
| 104 | /* |
| 105 | * Extract specified number of bits. |
| 106 | */ |
| 107 | if(off <= 8) |
| 108 | accum = nbits ? (buf[0]) >> (8 - off) : 0; |
| 109 | else if(off <= 16) |
| 110 | accum = ((buf[0] << 8) + buf[1]) >> (16 - off); |
| 111 | else if(off <= 24) |
| 112 | accum = ((buf[0] << 16) + (buf[1] << 8) + buf[2]) >> (24 - off); |
| 113 | else if(off <= 31) |
| 114 | accum = (((uint32_t)buf[0] << 24) + (buf[1] << 16) |
| 115 | + (buf[2] << 8) + (buf[3])) >> (32 - off); |
| 116 | else if(nbits <= 31) { |
| 117 | asn_bit_data_t tpd = *pd; |
| 118 | /* Here are we with our 31-bits limit plus 1..7 bits offset. */ |
| 119 | asn_get_undo(&tpd, nbits); |
| 120 | /* The number of available bits in the stream allow |
| 121 | * for the following operations to take place without |
| 122 | * invoking the ->refill() function */ |
| 123 | accum = asn_get_few_bits(&tpd, nbits - 24) << 24; |
| 124 | accum |= asn_get_few_bits(&tpd, 24); |
| 125 | } else { |
| 126 | asn_get_undo(pd, nbits); |
| 127 | return -1; |
| 128 | } |
| 129 | |
| 130 | accum &= (((uint32_t)1 << nbits) - 1); |
| 131 | |
| 132 | ASN_DEBUG(" [PER got %2d<=%2d bits => span %d %+ld[%d..%d]:%02x (%d) => 0x%x]", |
| 133 | (int)nbits, (int)nleft, |
| 134 | (int)pd->moved, |
| 135 | (((long)pd->buffer) & 0xf), |
| 136 | (int)pd->nboff, (int)pd->nbits, |
| 137 | ((pd->buffer != NULL)?pd->buffer[0]:0), |
| 138 | (int)(pd->nbits - pd->nboff), |
| 139 | (int)accum); |
| 140 | |
| 141 | return accum; |
| 142 | } |
| 143 | |
| 144 | /* |
| 145 | * Extract a large number of bits from the specified PER data pointer. |
| 146 | */ |
| 147 | int |
| 148 | asn_get_many_bits(asn_bit_data_t *pd, uint8_t *dst, int alright, int nbits) { |
| 149 | int32_t value; |
| 150 | |
| 151 | if(alright && (nbits & 7)) { |
| 152 | /* Perform right alignment of a first few bits */ |
| 153 | value = asn_get_few_bits(pd, nbits & 0x07); |
| 154 | if(value < 0) return -1; |
| 155 | *dst++ = value; /* value is already right-aligned */ |
| 156 | nbits &= ~7; |
| 157 | } |
| 158 | |
| 159 | while(nbits) { |
| 160 | if(nbits >= 24) { |
| 161 | value = asn_get_few_bits(pd, 24); |
| 162 | if(value < 0) return -1; |
| 163 | *(dst++) = value >> 16; |
| 164 | *(dst++) = value >> 8; |
| 165 | *(dst++) = value; |
| 166 | nbits -= 24; |
| 167 | } else { |
| 168 | value = asn_get_few_bits(pd, nbits); |
| 169 | if(value < 0) return -1; |
| 170 | if(nbits & 7) { /* implies left alignment */ |
| 171 | value <<= 8 - (nbits & 7), |
| 172 | nbits += 8 - (nbits & 7); |
| 173 | if(nbits > 24) |
| 174 | *dst++ = value >> 24; |
| 175 | } |
| 176 | if(nbits > 16) |
| 177 | *dst++ = value >> 16; |
| 178 | if(nbits > 8) |
| 179 | *dst++ = value >> 8; |
| 180 | *dst++ = value; |
| 181 | break; |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | return 0; |
| 186 | } |
| 187 | |
| 188 | /* |
| 189 | * Put a small number of bits (<= 31). |
| 190 | */ |
| 191 | int |
| 192 | asn_put_few_bits(asn_bit_outp_t *po, uint32_t bits, int obits) { |
| 193 | size_t off; /* Next after last bit offset */ |
| 194 | size_t omsk; /* Existing last byte meaningful bits mask */ |
| 195 | uint8_t *buf; |
| 196 | |
| 197 | if(obits <= 0 || obits >= 32) return obits ? -1 : 0; |
| 198 | |
| 199 | ASN_DEBUG("[PER put %d bits %x to %p+%d bits]", |
| 200 | obits, (int)bits, (void *)po->buffer, (int)po->nboff); |
| 201 | |
| 202 | /* |
| 203 | * Normalize position indicator. |
| 204 | */ |
| 205 | if(po->nboff >= 8) { |
| 206 | po->buffer += (po->nboff >> 3); |
| 207 | po->nbits -= (po->nboff & ~0x07); |
| 208 | po->nboff &= 0x07; |
| 209 | } |
| 210 | |
| 211 | /* |
| 212 | * Flush whole-bytes output, if necessary. |
| 213 | */ |
| 214 | if(po->nboff + obits > po->nbits) { |
| 215 | size_t complete_bytes; |
| 216 | if(!po->buffer) po->buffer = po->tmpspace; |
| 217 | complete_bytes = (po->buffer - po->tmpspace); |
| 218 | ASN_DEBUG("[PER output %ld complete + %ld]", |
| 219 | (long)complete_bytes, (long)po->flushed_bytes); |
| 220 | if(po->output(po->tmpspace, complete_bytes, po->op_key) < 0) |
| 221 | return -1; |
| 222 | if(po->nboff) |
| 223 | po->tmpspace[0] = po->buffer[0]; |
| 224 | po->buffer = po->tmpspace; |
| 225 | po->nbits = 8 * sizeof(po->tmpspace); |
| 226 | po->flushed_bytes += complete_bytes; |
| 227 | } |
| 228 | |
| 229 | /* |
| 230 | * Now, due to sizeof(tmpspace), we are guaranteed large enough space. |
| 231 | */ |
| 232 | buf = po->buffer; |
| 233 | omsk = ~((1 << (8 - po->nboff)) - 1); |
| 234 | off = (po->nboff + obits); |
| 235 | |
| 236 | /* Clear data of debris before meaningful bits */ |
| 237 | bits &= (((uint32_t)1 << obits) - 1); |
| 238 | |
| 239 | ASN_DEBUG("[PER out %d %u/%x (t=%d,o=%d) %x&%x=%x]", obits, |
| 240 | (int)bits, (int)bits, |
| 241 | (int)po->nboff, (int)off, |
| 242 | buf[0], (int)(omsk&0xff), |
| 243 | (int)(buf[0] & omsk)); |
| 244 | |
| 245 | if(off <= 8) /* Completely within 1 byte */ |
| 246 | po->nboff = off, |
| 247 | bits <<= (8 - off), |
| 248 | buf[0] = (buf[0] & omsk) | bits; |
| 249 | else if(off <= 16) |
| 250 | po->nboff = off, |
| 251 | bits <<= (16 - off), |
| 252 | buf[0] = (buf[0] & omsk) | (bits >> 8), |
| 253 | buf[1] = bits; |
| 254 | else if(off <= 24) |
| 255 | po->nboff = off, |
| 256 | bits <<= (24 - off), |
| 257 | buf[0] = (buf[0] & omsk) | (bits >> 16), |
| 258 | buf[1] = bits >> 8, |
| 259 | buf[2] = bits; |
| 260 | else if(off <= 31) |
| 261 | po->nboff = off, |
| 262 | bits <<= (32 - off), |
| 263 | buf[0] = (buf[0] & omsk) | (bits >> 24), |
| 264 | buf[1] = bits >> 16, |
| 265 | buf[2] = bits >> 8, |
| 266 | buf[3] = bits; |
| 267 | else { |
| 268 | if(asn_put_few_bits(po, bits >> (obits - 24), 24)) return -1; |
| 269 | if(asn_put_few_bits(po, bits, obits - 24)) return -1; |
| 270 | } |
| 271 | |
| 272 | ASN_DEBUG("[PER out %u/%x => %02x buf+%ld]", |
| 273 | (int)bits, (int)bits, buf[0], |
| 274 | (long)(po->buffer - po->tmpspace)); |
| 275 | |
| 276 | return 0; |
| 277 | } |
| 278 | |
| 279 | |
| 280 | /* |
| 281 | * Output a large number of bits. |
| 282 | */ |
| 283 | int |
| 284 | asn_put_many_bits(asn_bit_outp_t *po, const uint8_t *src, int nbits) { |
| 285 | |
| 286 | while(nbits) { |
| 287 | uint32_t value; |
| 288 | |
| 289 | if(nbits >= 24) { |
| 290 | value = (src[0] << 16) | (src[1] << 8) | src[2]; |
| 291 | src += 3; |
| 292 | nbits -= 24; |
| 293 | if(asn_put_few_bits(po, value, 24)) |
| 294 | return -1; |
| 295 | } else { |
| 296 | value = src[0]; |
| 297 | if(nbits > 8) |
| 298 | value = (value << 8) | src[1]; |
| 299 | if(nbits > 16) |
| 300 | value = (value << 8) | src[2]; |
| 301 | if(nbits & 0x07) |
| 302 | value >>= (8 - (nbits & 0x07)); |
| 303 | if(asn_put_few_bits(po, value, nbits)) |
| 304 | return -1; |
| 305 | break; |
| 306 | } |
| 307 | } |
| 308 | |
| 309 | return 0; |
| 310 | } |
| 311 | |
| 312 | |
| 313 | int |
| 314 | asn_put_aligned_flush(asn_bit_outp_t *po) { |
| 315 | uint32_t unused_bits = (0x7 & (8 - (po->nboff & 0x07))); |
| 316 | size_t complete_bytes = |
| 317 | (po->buffer ? po->buffer - po->tmpspace : 0) + ((po->nboff + 7) >> 3); |
| 318 | |
| 319 | if(unused_bits) { |
| 320 | po->buffer[po->nboff >> 3] &= ~0u << unused_bits; |
| 321 | } |
| 322 | |
| 323 | if(po->output(po->tmpspace, complete_bytes, po->op_key) < 0) { |
| 324 | return -1; |
| 325 | } else { |
| 326 | po->buffer = po->tmpspace; |
| 327 | po->nboff = 0; |
| 328 | po->nbits = 8 * sizeof(po->tmpspace); |
| 329 | po->flushed_bytes += complete_bytes; |
| 330 | return 0; |
| 331 | } |
| 332 | } |
| 333 | |