| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
#ifdef HAVE_CONFIG_H |
| 11 |
#include <config.h> |
| 12 |
#endif |
| 13 |
#include <stdio.h> |
| 14 |
#include <sys/types.h> |
| 15 |
#include <stdlib.h> |
| 16 |
#include <unistd.h> |
| 17 |
#include <string.h> |
| 18 |
#include <sysexits.h> |
| 19 |
#include <errno.h> |
| 20 |
|
| 21 |
#include <asn_application.h> |
| 22 |
#include <asn_internal.h> |
| 23 |
|
| 24 |
|
| 25 |
#define ASN_DEF_PDU(t) asn_DEF_ ## t |
| 26 |
#define DEF_PDU_Type(t) ASN_DEF_PDU(t) |
| 27 |
#define PDU_Type DEF_PDU_Type(PDU) |
| 28 |
|
| 29 |
extern asn_TYPE_descriptor_t PDU_Type; |
| 30 |
#ifdef ASN_PDU_COLLECTION |
| 31 |
extern asn_TYPE_descriptor_t *asn_pdu_collection[]; |
| 32 |
#endif |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
static void *data_decode_from_file(asn_TYPE_descriptor_t *asnTypeOfPDU, |
| 38 |
FILE *file, const char *name, ssize_t suggested_bufsize, int first_pdu); |
| 39 |
static int write_out(const void *buffer, size_t size, void *key); |
| 40 |
static FILE *argument_to_file(char *av[], int idx); |
| 41 |
static char *argument_to_name(char *av[], int idx); |
| 42 |
|
| 43 |
int opt_debug; |
| 44 |
static int opt_check; |
| 45 |
static int opt_stack; |
| 46 |
static int opt_nopad; |
| 47 |
static int opt_onepdu; |
| 48 |
|
| 49 |
|
| 50 |
static enum input_format { |
| 51 |
INP_BER, |
| 52 |
INP_XER, |
| 53 |
INP_PER |
| 54 |
} iform; |
| 55 |
|
| 56 |
|
| 57 |
static enum output_format { |
| 58 |
OUT_XER, |
| 59 |
OUT_DER, |
| 60 |
OUT_PER, |
| 61 |
OUT_TEXT, |
| 62 |
OUT_NULL |
| 63 |
} oform; |
| 64 |
|
| 65 |
#ifdef JUNKTEST |
| 66 |
#define JUNKOPT "J:" |
| 67 |
static double opt_jprob; |
| 68 |
static int junk_failures; |
| 69 |
static void junk_bytes_with_probability(uint8_t *, size_t, double prob); |
| 70 |
#else |
| 71 |
#define JUNKOPT |
| 72 |
#endif |
| 73 |
|
| 74 |
|
| 75 |
static inline void |
| 76 |
DEBUG(const char *fmt, ...) { |
| 77 |
va_list ap; |
| 78 |
if(!opt_debug) return; |
| 79 |
fprintf(stderr, "AD: "); |
| 80 |
va_start(ap, fmt); |
| 81 |
vfprintf(stderr, fmt, ap); |
| 82 |
va_end(ap); |
| 83 |
fprintf(stderr, "\n"); |
| 84 |
} |
| 85 |
|
| 86 |
int |
| 87 |
main(int ac, char *av[]) { |
| 88 |
static asn_TYPE_descriptor_t *pduType = &PDU_Type; |
| 89 |
ssize_t suggested_bufsize = 8192; |
| 90 |
int number_of_iterations = 1; |
| 91 |
int num; |
| 92 |
int ch; |
| 93 |
|
| 94 |
|
| 95 |
if(pduType->uper_decoder) |
| 96 |
iform = INP_PER; |
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
while((ch = getopt(ac, av, "i:o:1b:cdn:p:hs:" JUNKOPT)) != -1) |
| 102 |
switch(ch) { |
| 103 |
case 'i': |
| 104 |
if(optarg[0] == 'b') { iform = INP_BER; break; } |
| 105 |
if(optarg[0] == 'x') { iform = INP_XER; break; } |
| 106 |
if(pduType->uper_decoder |
| 107 |
&& optarg[0] == 'p') { iform = INP_PER; break; } |
| 108 |
fprintf(stderr, "-i<format>: '%s': improper format selector\n", |
| 109 |
optarg); |
| 110 |
exit(EX_UNAVAILABLE); |
| 111 |
case 'o': |
| 112 |
if(optarg[0] == 'd') { oform = OUT_DER; break; } |
| 113 |
if(pduType->uper_encoder |
| 114 |
&& optarg[0] == 'p') { oform = OUT_PER; break; } |
| 115 |
if(optarg[0] == 'x') { oform = OUT_XER; break; } |
| 116 |
if(optarg[0] == 't') { oform = OUT_TEXT; break; } |
| 117 |
if(optarg[0] == 'n') { oform = OUT_NULL; break; } |
| 118 |
fprintf(stderr, "-o<format>: '%s': improper format selector\n", |
| 119 |
optarg); |
| 120 |
exit(EX_UNAVAILABLE); |
| 121 |
case '1': |
| 122 |
opt_onepdu = 1; |
| 123 |
break; |
| 124 |
case 'b': |
| 125 |
suggested_bufsize = atoi(optarg); |
| 126 |
if(suggested_bufsize < 1 |
| 127 |
|| suggested_bufsize > 16 * 1024 * 1024) { |
| 128 |
fprintf(stderr, |
| 129 |
"-b %s: Improper buffer size (1..16M)\n", |
| 130 |
optarg); |
| 131 |
exit(EX_UNAVAILABLE); |
| 132 |
} |
| 133 |
break; |
| 134 |
case 'c': |
| 135 |
opt_check = 1; |
| 136 |
break; |
| 137 |
case 'd': |
| 138 |
opt_debug++; |
| 139 |
break; |
| 140 |
case 'n': |
| 141 |
number_of_iterations = atoi(optarg); |
| 142 |
if(number_of_iterations < 1) { |
| 143 |
fprintf(stderr, |
| 144 |
"-n %s: Improper iterations count\n", optarg); |
| 145 |
exit(EX_UNAVAILABLE); |
| 146 |
} |
| 147 |
break; |
| 148 |
case 'p': |
| 149 |
if(strcmp(optarg, "er-nopad") == 0) { |
| 150 |
opt_nopad = 1; |
| 151 |
break; |
| 152 |
} |
| 153 |
#ifdef ASN_PDU_COLLECTION |
| 154 |
if(strcmp(optarg, "list") == 0) { |
| 155 |
asn_TYPE_descriptor_t **pdu = asn_pdu_collection; |
| 156 |
fprintf(stderr, "Available PDU types:\n"); |
| 157 |
for(; *pdu; pdu++) printf("%s\n", (*pdu)->name); |
| 158 |
exit(0); |
| 159 |
} else if(optarg[0] >= 'A' && optarg[0] <= 'Z') { |
| 160 |
asn_TYPE_descriptor_t **pdu = asn_pdu_collection; |
| 161 |
while(*pdu && strcmp((*pdu)->name, optarg)) pdu++; |
| 162 |
if(*pdu) { pduType = *pdu; break; } |
| 163 |
fprintf(stderr, "-p %s: Unrecognized PDU\n", optarg); |
| 164 |
} |
| 165 |
#endif |
| 166 |
fprintf(stderr, "-p %s: Unrecognized option\n", optarg); |
| 167 |
exit(EX_UNAVAILABLE); |
| 168 |
case 's': |
| 169 |
opt_stack = atoi(optarg); |
| 170 |
if(opt_stack < 0) { |
| 171 |
fprintf(stderr, |
| 172 |
"-s %s: Non-negative value expected\n", |
| 173 |
optarg); |
| 174 |
exit(EX_UNAVAILABLE); |
| 175 |
} |
| 176 |
break; |
| 177 |
#ifdef JUNKTEST |
| 178 |
case 'J': |
| 179 |
opt_jprob = strtod(optarg, 0); |
| 180 |
if(opt_jprob <= 0.0 || opt_jprob > 1.0) { |
| 181 |
fprintf(stderr, |
| 182 |
"-J %s: Probability range 0..1 expected \n", |
| 183 |
optarg); |
| 184 |
exit(EX_UNAVAILABLE); |
| 185 |
} |
| 186 |
break; |
| 187 |
#endif |
| 188 |
case 'h': |
| 189 |
default: |
| 190 |
#ifdef ASN_CONVERTER_TITLE |
| 191 |
#define _AXS(x) #x |
| 192 |
#define _ASX(x) _AXS(x) |
| 193 |
fprintf(stderr, "%s\n", _ASX(ASN_CONVERTER_TITLE)); |
| 194 |
#endif |
| 195 |
fprintf(stderr, "Usage: %s [options] <data.ber> ...\n", av[0]); |
| 196 |
fprintf(stderr, "Where options are:\n"); |
| 197 |
if(pduType->uper_decoder) |
| 198 |
fprintf(stderr, |
| 199 |
" -iper Input is in Unaligned PER (Packed Encoding Rules) (DEFAULT)\n"); |
| 200 |
fprintf(stderr, |
| 201 |
" -iber Input is in BER (Basic Encoding Rules)%s\n", |
| 202 |
iform == INP_PER ? "" : " (DEFAULT)"); |
| 203 |
fprintf(stderr, |
| 204 |
" -ixer Input is in XER (XML Encoding Rules)\n"); |
| 205 |
if(pduType->uper_encoder) |
| 206 |
fprintf(stderr, |
| 207 |
" -oper Output in Unaligned PER (Packed Encoding Rules)\n"); |
| 208 |
fprintf(stderr, |
| 209 |
" -oder Output in DER (Distinguished Encoding Rules)\n" |
| 210 |
" -oxer Output in XER (XML Encoding Rules) (DEFAULT)\n" |
| 211 |
" -otext Output in plain semi-structured text (dump)\n" |
| 212 |
" -onull Verify (decode) input, but do not output\n"); |
| 213 |
if(pduType->uper_decoder) |
| 214 |
fprintf(stderr, |
| 215 |
" -per-nopad Assume PER PDUs are not padded (-iper)\n"); |
| 216 |
#ifdef ASN_PDU_COLLECTION |
| 217 |
fprintf(stderr, |
| 218 |
" -p <PDU> Specify PDU type to decode\n" |
| 219 |
" -p list List available PDUs\n"); |
| 220 |
#endif |
| 221 |
fprintf(stderr, |
| 222 |
" -1 Decode only the first PDU in file\n" |
| 223 |
" -b <size> Set the i/o buffer size (default is %ld)\n" |
| 224 |
" -c Check ASN.1 constraints after decoding\n" |
| 225 |
" -d Enable debugging (-dd is even better)\n" |
| 226 |
" -n <num> Process files <num> times\n" |
| 227 |
" -s <size> Set the stack usage limit (default is %d)\n" |
| 228 |
#ifdef JUNKTEST |
| 229 |
" -J <prob> Set random junk test bit garbaging probability\n" |
| 230 |
#endif |
| 231 |
, (long)suggested_bufsize, ASN__DEFAULT_STACK_MAX); |
| 232 |
exit(EX_USAGE); |
| 233 |
} |
| 234 |
|
| 235 |
ac -= optind; |
| 236 |
av += optind; |
| 237 |
|
| 238 |
if(ac < 1) { |
| 239 |
fprintf(stderr, "%s: No input files specified. " |
| 240 |
"Try '-h' for more information\n", |
| 241 |
av[-optind]); |
| 242 |
exit(EX_USAGE); |
| 243 |
} |
| 244 |
|
| 245 |
setvbuf(stdout, 0, _IOLBF, 0); |
| 246 |
|
| 247 |
for(num = 0; num < number_of_iterations; num++) { |
| 248 |
int ac_i; |
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
for(ac_i = 0; ac_i < ac; ac_i++) { |
| 253 |
asn_enc_rval_t erv; |
| 254 |
void *structure; |
| 255 |
FILE *file = argument_to_file(av, ac_i); |
| 256 |
char *name = argument_to_name(av, ac_i); |
| 257 |
int first_pdu; |
| 258 |
|
| 259 |
for(first_pdu = 1; first_pdu || !opt_onepdu; first_pdu = 0) { |
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
structure = data_decode_from_file(pduType, |
| 264 |
file, name, suggested_bufsize, first_pdu); |
| 265 |
if(!structure) { |
| 266 |
if(errno) { |
| 267 |
|
| 268 |
exit(EX_DATAERR); |
| 269 |
} else { |
| 270 |
|
| 271 |
break; |
| 272 |
} |
| 273 |
} |
| 274 |
|
| 275 |
|
| 276 |
if(opt_check) { |
| 277 |
char errbuf[128]; |
| 278 |
size_t errlen = sizeof(errbuf); |
| 279 |
if(asn_check_constraints(pduType, structure, |
| 280 |
errbuf, &errlen)) { |
| 281 |
fprintf(stderr, "%s: ASN.1 constraint " |
| 282 |
"check failed: %s\n", name, errbuf); |
| 283 |
exit(EX_DATAERR); |
| 284 |
} |
| 285 |
} |
| 286 |
|
| 287 |
switch(oform) { |
| 288 |
case OUT_NULL: |
| 289 |
#ifdef JUNKTEST |
| 290 |
if(opt_jprob == 0.0) |
| 291 |
#endif |
| 292 |
fprintf(stderr, "%s: decoded successfully\n", name); |
| 293 |
break; |
| 294 |
case OUT_TEXT: |
| 295 |
asn_fprint(stdout, pduType, structure); |
| 296 |
break; |
| 297 |
case OUT_XER: |
| 298 |
if(xer_fprint(stdout, pduType, structure)) { |
| 299 |
fprintf(stderr, |
| 300 |
"%s: Cannot convert %s into XML\n", |
| 301 |
name, pduType->name); |
| 302 |
exit(EX_UNAVAILABLE); |
| 303 |
} |
| 304 |
break; |
| 305 |
case OUT_DER: |
| 306 |
erv = der_encode(pduType, structure, write_out, stdout); |
| 307 |
if(erv.encoded < 0) { |
| 308 |
fprintf(stderr, |
| 309 |
"%s: Cannot convert %s into DER\n", |
| 310 |
name, pduType->name); |
| 311 |
exit(EX_UNAVAILABLE); |
| 312 |
} |
| 313 |
DEBUG("Encoded in %ld bytes of DER", (long)erv.encoded); |
| 314 |
break; |
| 315 |
case OUT_PER: |
| 316 |
erv = uper_encode(pduType, structure, write_out, stdout); |
| 317 |
if(erv.encoded < 0) { |
| 318 |
fprintf(stderr, |
| 319 |
"%s: Cannot convert %s into Unaligned PER\n", |
| 320 |
name, pduType->name); |
| 321 |
exit(EX_UNAVAILABLE); |
| 322 |
} |
| 323 |
DEBUG("Encoded in %ld bits of UPER", (long)erv.encoded); |
| 324 |
break; |
| 325 |
} |
| 326 |
|
| 327 |
ASN_STRUCT_FREE(*pduType, structure); |
| 328 |
} |
| 329 |
|
| 330 |
if(file && file != stdin) |
| 331 |
fclose(file); |
| 332 |
} |
| 333 |
} |
| 334 |
|
| 335 |
#ifdef JUNKTEST |
| 336 |
if(opt_jprob > 0.0) { |
| 337 |
fprintf(stderr, "Junked %f OK (%d/%d)\n", |
| 338 |
opt_jprob, junk_failures, number_of_iterations); |
| 339 |
} |
| 340 |
#endif |
| 341 |
|
| 342 |
return 0; |
| 343 |
} |
| 344 |
|
| 345 |
static struct dynamic_buffer { |
| 346 |
uint8_t *data; |
| 347 |
size_t offset; |
| 348 |
size_t length; |
| 349 |
size_t unbits; |
| 350 |
size_t allocated; |
| 351 |
int nreallocs; |
| 352 |
off_t bytes_shifted; |
| 353 |
} DynamicBuffer; |
| 354 |
|
| 355 |
static void |
| 356 |
buffer_dump() { |
| 357 |
uint8_t *p = DynamicBuffer.data + DynamicBuffer.offset; |
| 358 |
uint8_t *e = p + DynamicBuffer.length - (DynamicBuffer.unbits ? 1 : 0); |
| 359 |
if(!opt_debug) return; |
| 360 |
DEBUG("Buffer: { d=%p, o=%ld, l=%ld, u=%ld, a=%ld, s=%ld }", |
| 361 |
DynamicBuffer.data, |
| 362 |
(long)DynamicBuffer.offset, |
| 363 |
(long)DynamicBuffer.length, |
| 364 |
(long)DynamicBuffer.unbits, |
| 365 |
(long)DynamicBuffer.allocated, |
| 366 |
(long)DynamicBuffer.bytes_shifted); |
| 367 |
for(; p < e; p++) { |
| 368 |
fprintf(stderr, " %c%c%c%c%c%c%c%c", |
| 369 |
((*p >> 7) & 1) ? '1' : '0', |
| 370 |
((*p >> 6) & 1) ? '1' : '0', |
| 371 |
((*p >> 5) & 1) ? '1' : '0', |
| 372 |
((*p >> 4) & 1) ? '1' : '0', |
| 373 |
((*p >> 3) & 1) ? '1' : '0', |
| 374 |
((*p >> 2) & 1) ? '1' : '0', |
| 375 |
((*p >> 1) & 1) ? '1' : '0', |
| 376 |
((*p >> 0) & 1) ? '1' : '0'); |
| 377 |
} |
| 378 |
if(DynamicBuffer.unbits) { |
| 379 |
unsigned int shift; |
| 380 |
fprintf(stderr, " "); |
| 381 |
for(shift = 7; shift >= DynamicBuffer.unbits; shift--) |
| 382 |
fprintf(stderr, "%c", ((*p >> shift) & 1) ? '1' : '0'); |
| 383 |
fprintf(stderr, " %ld:%ld\n", |
| 384 |
(long)DynamicBuffer.length - 1, |
| 385 |
(long)8 - DynamicBuffer.unbits); |
| 386 |
} else { |
| 387 |
fprintf(stderr, " %ld\n", (long)DynamicBuffer.length); |
| 388 |
} |
| 389 |
} |
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
static void |
| 396 |
buffer_shift_left(size_t offset, int bits) { |
| 397 |
uint8_t *ptr = DynamicBuffer.data + DynamicBuffer.offset + offset; |
| 398 |
uint8_t *end = DynamicBuffer.data + DynamicBuffer.offset |
| 399 |
+ DynamicBuffer.length - 1; |
| 400 |
|
| 401 |
if(!bits) return; |
| 402 |
|
| 403 |
DEBUG("Shifting left %d bits off %ld (o=%ld, u=%ld, l=%ld)", |
| 404 |
bits, (long)offset, |
| 405 |
(long)DynamicBuffer.offset, |
| 406 |
(long)DynamicBuffer.unbits, |
| 407 |
(long)DynamicBuffer.length); |
| 408 |
|
| 409 |
if(offset) { |
| 410 |
int right; |
| 411 |
right = ptr[0] >> (8 - bits); |
| 412 |
|
| 413 |
DEBUG("oleft: %c%c%c%c%c%c%c%c", |
| 414 |
((ptr[-1] >> 7) & 1) ? '1' : '0', |
| 415 |
((ptr[-1] >> 6) & 1) ? '1' : '0', |
| 416 |
((ptr[-1] >> 5) & 1) ? '1' : '0', |
| 417 |
((ptr[-1] >> 4) & 1) ? '1' : '0', |
| 418 |
((ptr[-1] >> 3) & 1) ? '1' : '0', |
| 419 |
((ptr[-1] >> 2) & 1) ? '1' : '0', |
| 420 |
((ptr[-1] >> 1) & 1) ? '1' : '0', |
| 421 |
((ptr[-1] >> 0) & 1) ? '1' : '0'); |
| 422 |
|
| 423 |
DEBUG("oriht: %c%c%c%c%c%c%c%c", |
| 424 |
((ptr[0] >> 7) & 1) ? '1' : '0', |
| 425 |
((ptr[0] >> 6) & 1) ? '1' : '0', |
| 426 |
((ptr[0] >> 5) & 1) ? '1' : '0', |
| 427 |
((ptr[0] >> 4) & 1) ? '1' : '0', |
| 428 |
((ptr[0] >> 3) & 1) ? '1' : '0', |
| 429 |
((ptr[0] >> 2) & 1) ? '1' : '0', |
| 430 |
((ptr[0] >> 1) & 1) ? '1' : '0', |
| 431 |
((ptr[0] >> 0) & 1) ? '1' : '0'); |
| 432 |
|
| 433 |
DEBUG("mriht: %c%c%c%c%c%c%c%c", |
| 434 |
((right >> 7) & 1) ? '1' : '0', |
| 435 |
((right >> 6) & 1) ? '1' : '0', |
| 436 |
((right >> 5) & 1) ? '1' : '0', |
| 437 |
((right >> 4) & 1) ? '1' : '0', |
| 438 |
((right >> 3) & 1) ? '1' : '0', |
| 439 |
((right >> 2) & 1) ? '1' : '0', |
| 440 |
((right >> 1) & 1) ? '1' : '0', |
| 441 |
((right >> 0) & 1) ? '1' : '0'); |
| 442 |
|
| 443 |
ptr[-1] = (ptr[-1] & (0xff << bits)) | right; |
| 444 |
|
| 445 |
DEBUG("after: %c%c%c%c%c%c%c%c", |
| 446 |
((ptr[-1] >> 7) & 1) ? '1' : '0', |
| 447 |
((ptr[-1] >> 6) & 1) ? '1' : '0', |
| 448 |
((ptr[-1] >> 5) & 1) ? '1' : '0', |
| 449 |
((ptr[-1] >> 4) & 1) ? '1' : '0', |
| 450 |
((ptr[-1] >> 3) & 1) ? '1' : '0', |
| 451 |
((ptr[-1] >> 2) & 1) ? '1' : '0', |
| 452 |
((ptr[-1] >> 1) & 1) ? '1' : '0', |
| 453 |
((ptr[-1] >> 0) & 1) ? '1' : '0'); |
| 454 |
} |
| 455 |
|
| 456 |
buffer_dump(); |
| 457 |
|
| 458 |
for(; ptr < end; ptr++) { |
| 459 |
int right = ptr[1] >> (8 - bits); |
| 460 |
*ptr = (*ptr << bits) | right; |
| 461 |
} |
| 462 |
*ptr <<= bits; |
| 463 |
|
| 464 |
DEBUG("Unbits [%d=>", (int)DynamicBuffer.unbits); |
| 465 |
if(DynamicBuffer.unbits == 0) { |
| 466 |
DynamicBuffer.unbits += bits; |
| 467 |
} else { |
| 468 |
DynamicBuffer.unbits += bits; |
| 469 |
if(DynamicBuffer.unbits > 7) { |
| 470 |
DynamicBuffer.unbits -= 8; |
| 471 |
DynamicBuffer.length--; |
| 472 |
DynamicBuffer.bytes_shifted++; |
| 473 |
} |
| 474 |
} |
| 475 |
DEBUG("Unbits =>%d]", (int)DynamicBuffer.unbits); |
| 476 |
|
| 477 |
buffer_dump(); |
| 478 |
|
| 479 |
DEBUG("Shifted. Now (o=%ld, u=%ld l=%ld)", |
| 480 |
(long)DynamicBuffer.offset, |
| 481 |
(long)DynamicBuffer.unbits, |
| 482 |
(long)DynamicBuffer.length); |
| 483 |
|
| 484 |
|
| 485 |
} |
| 486 |
|
| 487 |
|
| 488 |
|
| 489 |
|
| 490 |
static void add_bytes_to_buffer(const void *data2add, size_t bytes) { |
| 491 |
|
| 492 |
if(bytes == 0) return; |
| 493 |
|
| 494 |
DEBUG("=> add_bytes(%ld) { o=%ld l=%ld u=%ld, s=%ld }", |
| 495 |
(long)bytes, |
| 496 |
(long)DynamicBuffer.offset, |
| 497 |
(long)DynamicBuffer.length, |
| 498 |
(long)DynamicBuffer.unbits, |
| 499 |
(long)DynamicBuffer.allocated); |
| 500 |
|
| 501 |
if(DynamicBuffer.allocated |
| 502 |
>= (DynamicBuffer.offset + DynamicBuffer.length + bytes)) { |
| 503 |
DEBUG("\tNo buffer reallocation is necessary"); |
| 504 |
} else if(bytes <= DynamicBuffer.offset) { |
| 505 |
DEBUG("\tContents shifted by %ld", DynamicBuffer.offset); |
| 506 |
|
| 507 |
|
| 508 |
memmove(DynamicBuffer.data, |
| 509 |
DynamicBuffer.data + DynamicBuffer.offset, |
| 510 |
DynamicBuffer.length); |
| 511 |
DynamicBuffer.bytes_shifted += DynamicBuffer.offset; |
| 512 |
DynamicBuffer.offset = 0; |
| 513 |
} else { |
| 514 |
size_t newsize = (DynamicBuffer.allocated << 2) + bytes; |
| 515 |
void *p = MALLOC(newsize); |
| 516 |
if(!p) { |
| 517 |
perror("malloc()"); |
| 518 |
exit(EX_OSERR); |
| 519 |
} |
| 520 |
memcpy(p, |
| 521 |
DynamicBuffer.data + DynamicBuffer.offset, |
| 522 |
DynamicBuffer.length); |
| 523 |
FREEMEM(DynamicBuffer.data); |
| 524 |
DynamicBuffer.data = (uint8_t *)p; |
| 525 |
DynamicBuffer.offset = 0; |
| 526 |
DynamicBuffer.allocated = newsize; |
| 527 |
DynamicBuffer.nreallocs++; |
| 528 |
DEBUG("\tBuffer reallocated to %ld (%d time)", |
| 529 |
newsize, DynamicBuffer.nreallocs); |
| 530 |
} |
| 531 |
|
| 532 |
memcpy(DynamicBuffer.data |
| 533 |
+ DynamicBuffer.offset + DynamicBuffer.length, |
| 534 |
data2add, bytes); |
| 535 |
DynamicBuffer.length += bytes; |
| 536 |
if(DynamicBuffer.unbits) { |
| 537 |
int bits = DynamicBuffer.unbits; |
| 538 |
DynamicBuffer.unbits = 0; |
| 539 |
buffer_shift_left(DynamicBuffer.length - bytes, bits); |
| 540 |
} |
| 541 |
|
| 542 |
DEBUG("<= add_bytes(%ld) { o=%ld l=%ld u=%ld, s=%ld }", |
| 543 |
(long)bytes, |
| 544 |
(long)DynamicBuffer.offset, |
| 545 |
(long)DynamicBuffer.length, |
| 546 |
(long)DynamicBuffer.unbits, |
| 547 |
(long)DynamicBuffer.allocated); |
| 548 |
} |
| 549 |
|
| 550 |
static void * |
| 551 |
data_decode_from_file(asn_TYPE_descriptor_t *pduType, FILE *file, const char *name, ssize_t suggested_bufsize, int on_first_pdu) { |
| 552 |
static uint8_t *fbuf; |
| 553 |
static ssize_t fbuf_size; |
| 554 |
static asn_codec_ctx_t s_codec_ctx; |
| 555 |
asn_codec_ctx_t *opt_codec_ctx = 0; |
| 556 |
void *structure = 0; |
| 557 |
asn_dec_rval_t rval; |
| 558 |
size_t old_offset; |
| 559 |
size_t new_offset; |
| 560 |
int tolerate_eof; |
| 561 |
size_t rd; |
| 562 |
|
| 563 |
if(!file) { |
| 564 |
fprintf(stderr, "%s: %s\n", name, strerror(errno)); |
| 565 |
errno = EINVAL; |
| 566 |
return 0; |
| 567 |
} |
| 568 |
|
| 569 |
if(opt_stack) { |
| 570 |
s_codec_ctx.max_stack_size = opt_stack; |
| 571 |
opt_codec_ctx = &s_codec_ctx; |
| 572 |
} |
| 573 |
|
| 574 |
DEBUG("Processing %s", name); |
| 575 |
|
| 576 |
|
| 577 |
if(fbuf_size != suggested_bufsize) { |
| 578 |
fbuf = (uint8_t *)REALLOC(fbuf, suggested_bufsize); |
| 579 |
if(!fbuf) { |
| 580 |
perror("realloc()"); |
| 581 |
exit(EX_OSERR); |
| 582 |
} |
| 583 |
fbuf_size = suggested_bufsize; |
| 584 |
} |
| 585 |
|
| 586 |
if(on_first_pdu) { |
| 587 |
DynamicBuffer.offset = 0; |
| 588 |
DynamicBuffer.length = 0; |
| 589 |
DynamicBuffer.unbits = 0; |
| 590 |
DynamicBuffer.allocated = 0; |
| 591 |
DynamicBuffer.bytes_shifted = 0; |
| 592 |
DynamicBuffer.nreallocs = 0; |
| 593 |
} |
| 594 |
|
| 595 |
old_offset = DynamicBuffer.bytes_shifted + DynamicBuffer.offset; |
| 596 |
|
| 597 |
|
| 598 |
rval.code = RC_WMORE; |
| 599 |
rval.consumed = 0; |
| 600 |
|
| 601 |
for(tolerate_eof = 1; |
| 602 |
(rd = fread(fbuf, 1, fbuf_size, file)) |
| 603 |
|| feof(file) == 0 |
| 604 |
|| (tolerate_eof && DynamicBuffer.length) |
| 605 |
;) { |
| 606 |
int ecbits = 0; |
| 607 |
uint8_t *i_bptr; |
| 608 |
size_t i_size; |
| 609 |
|
| 610 |
|
| 611 |
|
| 612 |
|
| 613 |
if(DynamicBuffer.allocated) { |
| 614 |
|
| 615 |
add_bytes_to_buffer(fbuf, rd); |
| 616 |
i_bptr = DynamicBuffer.data + DynamicBuffer.offset; |
| 617 |
i_size = DynamicBuffer.length; |
| 618 |
} else { |
| 619 |
i_bptr = fbuf; |
| 620 |
i_size = rd; |
| 621 |
} |
| 622 |
|
| 623 |
DEBUG("Decoding %ld bytes", (long)i_size); |
| 624 |
|
| 625 |
#ifdef JUNKTEST |
| 626 |
junk_bytes_with_probability(i_bptr, i_size, opt_jprob); |
| 627 |
#endif |
| 628 |
|
| 629 |
switch(iform) { |
| 630 |
case INP_BER: |
| 631 |
rval = ber_decode(opt_codec_ctx, pduType, |
| 632 |
(void **)&structure, i_bptr, i_size); |
| 633 |
break; |
| 634 |
case INP_XER: |
| 635 |
rval = xer_decode(opt_codec_ctx, pduType, |
| 636 |
(void **)&structure, i_bptr, i_size); |
| 637 |
break; |
| 638 |
case INP_PER: |
| 639 |
if(opt_nopad) |
| 640 |
rval = uper_decode(opt_codec_ctx, pduType, |
| 641 |
(void **)&structure, i_bptr, i_size, 0, |
| 642 |
DynamicBuffer.unbits); |
| 643 |
else |
| 644 |
rval = uper_decode_complete(opt_codec_ctx, pduType, |
| 645 |
(void **)&structure, i_bptr, i_size); |
| 646 |
switch(rval.code) { |
| 647 |
case RC_OK: |
| 648 |
|
| 649 |
case RC_FAIL: |
| 650 |
if(opt_nopad) { |
| 651 |
|
| 652 |
|
| 653 |
ecbits = rval.consumed % 8; |
| 654 |
|
| 655 |
rval.consumed /= 8; |
| 656 |
} |
| 657 |
break; |
| 658 |
case RC_WMORE: |
| 659 |
|
| 660 |
ASN_STRUCT_FREE(*pduType, structure); |
| 661 |
structure = 0; |
| 662 |
rval.consumed = 0; |
| 663 |
|
| 664 |
break; |
| 665 |
} |
| 666 |
break; |
| 667 |
} |
| 668 |
DEBUG("decode(%ld) consumed %ld+%db (%ld), code %d", |
| 669 |
(long)DynamicBuffer.length, |
| 670 |
(long)rval.consumed, ecbits, (long)i_size, |
| 671 |
rval.code); |
| 672 |
|
| 673 |
if(DynamicBuffer.allocated == 0) { |
| 674 |
|
| 675 |
|
| 676 |
|
| 677 |
if(rval.code != RC_FAIL && rval.consumed < rd) { |
| 678 |
add_bytes_to_buffer(fbuf + rval.consumed, |
| 679 |
rd - rval.consumed); |
| 680 |
buffer_shift_left(0, ecbits); |
| 681 |
DynamicBuffer.bytes_shifted = rval.consumed; |
| 682 |
rval.consumed = 0; |
| 683 |
ecbits = 0; |
| 684 |
} |
| 685 |
} |
| 686 |
|
| 687 |
|
| 688 |
|
| 689 |
|
| 690 |
if(DynamicBuffer.allocated) { |
| 691 |
DynamicBuffer.offset += rval.consumed; |
| 692 |
DynamicBuffer.length -= rval.consumed; |
| 693 |
} else { |
| 694 |
DynamicBuffer.bytes_shifted += rval.consumed; |
| 695 |
} |
| 696 |
|
| 697 |
switch(rval.code) { |
| 698 |
case RC_OK: |
| 699 |
if(ecbits) buffer_shift_left(0, ecbits); |
| 700 |
DEBUG("RC_OK, finishing up with %ld+%d", |
| 701 |
(long)rval.consumed, ecbits); |
| 702 |
return structure; |
| 703 |
case RC_WMORE: |
| 704 |
DEBUG("RC_WMORE, continuing read=%ld, cons=%ld " |
| 705 |
" with %ld..%ld-%ld..%ld", |
| 706 |
(long)rd, |
| 707 |
(long)rval.consumed, |
| 708 |
(long)DynamicBuffer.offset, |
| 709 |
(long)DynamicBuffer.length, |
| 710 |
(long)DynamicBuffer.unbits, |
| 711 |
(long)DynamicBuffer.allocated); |
| 712 |
if(!rd) tolerate_eof--; |
| 713 |
continue; |
| 714 |
case RC_FAIL: |
| 715 |
break; |
| 716 |
} |
| 717 |
break; |
| 718 |
} |
| 719 |
|
| 720 |
DEBUG("Clean up partially decoded structure"); |
| 721 |
ASN_STRUCT_FREE(*pduType, structure); |
| 722 |
|
| 723 |
new_offset = DynamicBuffer.bytes_shifted + DynamicBuffer.offset; |
| 724 |
|
| 725 |
|
| 726 |
|
| 727 |
|
| 728 |
|
| 729 |
if(on_first_pdu |
| 730 |
|| DynamicBuffer.length |
| 731 |
|| new_offset - old_offset > ((iform == INP_XER)?sizeof("\r\n")-1:0) |
| 732 |
) { |
| 733 |
|
| 734 |
#ifdef JUNKTEST |
| 735 |
|
| 736 |
|
| 737 |
|
| 738 |
|
| 739 |
if(opt_jprob != 0.0) { |
| 740 |
junk_failures++; |
| 741 |
errno = 0; |
| 742 |
return 0; |
| 743 |
} |
| 744 |
#endif |
| 745 |
|
| 746 |
DEBUG("ofp %d, no=%ld, oo=%ld, dbl=%ld", |
| 747 |
on_first_pdu, (long)new_offset, (long)old_offset, |
| 748 |
(long)DynamicBuffer.length); |
| 749 |
fprintf(stderr, "%s: " |
| 750 |
"Decode failed past byte %ld: %s\n", |
| 751 |
name, (long)new_offset, |
| 752 |
(rval.code == RC_WMORE) |
| 753 |
? "Unexpected end of input" |
| 754 |
: "Input processing error"); |
| 755 |
#ifndef ENOMSG |
| 756 |
#define ENOMSG EINVAL |
| 757 |
#endif |
| 758 |
#ifndef EBADMSG |
| 759 |
#define EBADMSG EINVAL |
| 760 |
#endif |
| 761 |
errno = (rval.code == RC_WMORE) ? ENOMSG : EBADMSG; |
| 762 |
} else { |
| 763 |
|
| 764 |
errno = 0; |
| 765 |
} |
| 766 |
|
| 767 |
return 0; |
| 768 |
} |
| 769 |
|
| 770 |
|
| 771 |
static int write_out(const void *buffer, size_t size, void *key) { |
| 772 |
FILE *fp = (FILE *)key; |
| 773 |
return (fwrite(buffer, 1, size, fp) == size) ? 0 : -1; |
| 774 |
} |
| 775 |
|
| 776 |
static int argument_is_stdin(char *av[], int idx) { |
| 777 |
if(strcmp(av[idx], "-")) { |
| 778 |
return 0; |
| 779 |
} else { |
| 780 |
|
| 781 |
if(strcmp(av[-1], "--")) |
| 782 |
return 1; |
| 783 |
else |
| 784 |
return 0; |
| 785 |
} |
| 786 |
} |
| 787 |
|
| 788 |
static FILE *argument_to_file(char *av[], int idx) { |
| 789 |
return argument_is_stdin(av, idx) |
| 790 |
? stdin |
| 791 |
: fopen(av[idx], "rb"); |
| 792 |
} |
| 793 |
|
| 794 |
static char *argument_to_name(char *av[], int idx) { |
| 795 |
return argument_is_stdin(av, idx) |
| 796 |
? "standard input" |
| 797 |
: av[idx]; |
| 798 |
} |
| 799 |
|
| 800 |
#ifdef JUNKTEST |
| 801 |
|
| 802 |
|
| 803 |
|
| 804 |
static void |
| 805 |
junk_bytes_with_probability(uint8_t *buf, size_t size, double prob) { |
| 806 |
static int junkmode; |
| 807 |
uint8_t *ptr; |
| 808 |
uint8_t *end; |
| 809 |
if(opt_jprob <= 0.0) return; |
| 810 |
for(ptr = buf, end = ptr + size; ptr < end; ptr++) { |
| 811 |
int byte = *ptr; |
| 812 |
if(junkmode++ & 1) { |
| 813 |
if((((double)random() / RAND_MAX) < prob)) |
| 814 |
byte = random() & 0xff; |
| 815 |
} else { |
| 816 |
#define BPROB(b) ((((double)random() / RAND_MAX) < prob) ? b : 0) |
| 817 |
byte ^= BPROB(0x80); |
| 818 |
byte ^= BPROB(0x40); |
| 819 |
byte ^= BPROB(0x20); |
| 820 |
byte ^= BPROB(0x10); |
| 821 |
byte ^= BPROB(0x08); |
| 822 |
byte ^= BPROB(0x04); |
| 823 |
byte ^= BPROB(0x02); |
| 824 |
byte ^= BPROB(0x01); |
| 825 |
} |
| 826 |
if(byte != *ptr) { |
| 827 |
DEBUG("Junk buf[%d] %02x -> %02x", |
| 828 |
ptr - buf, *ptr, byte); |
| 829 |
*ptr = byte; |
| 830 |
} |
| 831 |
} |
| 832 |
} |
| 833 |
#endif |
| 834 |
|