static void save_object_as(PDU_t *st, enum enctype how) { asn_enc_rval_t rval; /* Return value */ buf_offset = 0; /* * Save object using specified method. */ switch(how) { case AS_PER: rval = uper_encode(&asn_DEF_PDU, st, _buf_writer, 0); assert(rval.encoded > 0); fprintf(stderr, "SAVED OBJECT IN SIZE %d\n", buf_offset); return; case AS_DER: rval = der_encode(&asn_DEF_PDU, st, _buf_writer, 0); break; case AS_XER: rval = xer_encode(&asn_DEF_PDU, st, XER_F_BASIC, _buf_writer, 0); break; } if (rval.encoded == -1) { fprintf(stderr, "Cannot encode %s: %s\n", rval.failed_type->name, strerror(errno)); assert(rval.encoded != -1); return; } fprintf(stderr, "SAVED OBJECT IN SIZE %d\n", buf_offset); }
static struct _el_buffer * SET_OF__encode_sorted(const asn_TYPE_member_t *elm, const asn_anonymous_set_ *list, enum SET_OF__encode_method method) { static struct _el_buffer *encoded_els; int edx; encoded_els = (struct _el_buffer *)CALLOC(list->count, sizeof(encoded_els[0])); if(encoded_els == NULL) { return NULL; } /* * Encode all members. */ for(edx = 0; edx < list->count; edx++) { const void *memb_ptr = list->array[edx]; struct _el_buffer *encoding_el = &encoded_els[edx]; asn_enc_rval_t erval; if(!memb_ptr) break; /* * Encode the member into the prepared space. */ switch(method) { case SOES_DER: erval = elm->type->op->der_encoder(elm->type, memb_ptr, 0, elm->tag, _el_addbytes, encoding_el); break; case SOES_CUPER: erval = uper_encode(elm->type, elm->encoding_constraints.per_constraints, memb_ptr, _el_addbytes, encoding_el); if(erval.encoded != -1) { size_t extra_bits = erval.encoded % 8; assert(encoding_el->length == (size_t)(erval.encoded + 7) / 8); encoding_el->bits_unused = (8 - extra_bits) & 0x7; } break; default: assert(!"Unreachable"); break; } if(erval.encoded < 0) break; } if(edx == list->count) { /* * Sort the encoded elements according to their encoding. */ qsort(encoded_els, list->count, sizeof(encoded_els[0]), _el_buf_cmp); return encoded_els; } else { SET_OF__encode_sorted_free(encoded_els, edx); return NULL; } }
int main(int ac, char **av) { /* Declare a pointer to a T1 type */ T1_t *t1; /* Encoder return value */ asn_enc_rval_t ec; /* Allocate an instance of T1 */ t1 = calloc(1, sizeof *t1); assert(t1); /* Assume infinite memory */ /* Prepare a BIT STRING */ (*t1).first.buf = calloc(2, 1); assert((*t1).first.buf); (*t1).first.size = 2; /* 2 bytes */ (*t1).first.buf[0] = 0xc0; /* Set BIT STRING value */ (*t1).first.buf[1] = 0xc0; /* Set BIT STRING value */ (*t1).first.bits_unused = 4; /* Trim unused bits */ /* * Output the resulting structure as PER */ if(ac < 2) { fprintf(stderr,"Specify filename for PER output\n"); } else { const char *filename = av[1]; FILE *fp = fopen(filename,"wb"); /* for PER output */ if(!fp) { perror(filename); exit(71); /* better, EX_OSERR */ } /* Encode T1 as PER */ ec = uper_encode(&asn_DEF_T1,t1,write_out,fp); fclose(fp); if(ec.encoded == -1) { fprintf(stderr,"Could not encode T1 (at %s)\n", ec.failed_type ? ec.failed_type->name : "unknown"); exit(65); /* better, EX_DATAERR */ } else { fprintf(stderr,"Created %s with PER encoded T1\n",filename); } } /* * And print it as XER (XML) */ xer_fprint(stdout, &asn_DEF_T1, t1); return 0; }
int main(int ac, char *av[]) { static asn_TYPE_descriptor_t *pduType = &PDU_Type; ssize_t suggested_bufsize = 8192; /* close or equal to stdio buffer */ int number_of_iterations = 1; int num; int ch; pduInit(); /* Figure out if Unaligned PER needs to be default */ if(pduType->uper_decoder) iform = INP_PER; /* * Pocess the command-line argments. */ while((ch = getopt(ac, av, "i:o:1b:cdn:p:hs:" JUNKOPT)) != -1) switch(ch) { case 'i': if(optarg[0] == 'b') { iform = INP_BER; break; } if(optarg[0] == 'x') { iform = INP_XER; break; } if(pduType->uper_decoder && optarg[0] == 'p') { iform = INP_PER; break; } fprintf(stderr, "-i<format>: '%s': improper format selector\n", optarg); exit(EX_UNAVAILABLE); case 'o': if(optarg[0] == 'd') { oform = OUT_DER; break; } if(pduType->uper_encoder && optarg[0] == 'p') { oform = OUT_PER; break; } if(optarg[0] == 'x') { oform = OUT_XER; break; } if(optarg[0] == 't') { oform = OUT_TEXT; break; } if(optarg[0] == 'n') { oform = OUT_NULL; break; } fprintf(stderr, "-o<format>: '%s': improper format selector\n", optarg); exit(EX_UNAVAILABLE); case '1': opt_onepdu = 1; break; case 'b': suggested_bufsize = atoi(optarg); if(suggested_bufsize < 1 || suggested_bufsize > 16 * 1024 * 1024) { fprintf(stderr, "-b %s: Improper buffer size (1..16M)\n", optarg); exit(EX_UNAVAILABLE); } break; case 'c': opt_check = 1; break; case 'd': opt_debug++; /* Double -dd means ASN.1 debug */ break; case 'n': number_of_iterations = atoi(optarg); if(number_of_iterations < 1) { fprintf(stderr, "-n %s: Improper iterations count\n", optarg); exit(EX_UNAVAILABLE); } break; case 'p': if(strcmp(optarg, "er-nopad") == 0) { opt_nopad = 1; break; } #ifdef ASN_PDU_COLLECTION if(strcmp(optarg, "list") == 0) { asn_TYPE_descriptor_t **pdu = asn_pdu_collection; fprintf(stderr, "Available PDU types:\n"); for(; *pdu; pdu++) printf("%s\n", (*pdu)->name); exit(0); } else if(optarg[0] >= 'A' && optarg[0] <= 'Z') { asn_TYPE_descriptor_t **pdu = asn_pdu_collection; while(*pdu && strcmp((*pdu)->name, optarg)) pdu++; if(*pdu) { pduType = *pdu; break; } fprintf(stderr, "-p %s: Unrecognized PDU\n", optarg); } #endif /* ASN_PDU_COLLECTION */ fprintf(stderr, "-p %s: Unrecognized option\n", optarg); exit(EX_UNAVAILABLE); case 's': opt_stack = atoi(optarg); if(opt_stack < 0) { fprintf(stderr, "-s %s: Non-negative value expected\n", optarg); exit(EX_UNAVAILABLE); } break; #ifdef JUNKTEST case 'J': opt_jprob = strtod(optarg, 0); if(opt_jprob <= 0.0 || opt_jprob > 1.0) { fprintf(stderr, "-J %s: Probability range 0..1 expected \n", optarg); exit(EX_UNAVAILABLE); } break; #endif /* JUNKTEST */ case 'h': default: #ifdef ASN_CONVERTER_TITLE #define _AXS(x) #x #define _ASX(x) _AXS(x) fprintf(stderr, "%s\n", _ASX(ASN_CONVERTER_TITLE)); #endif fprintf(stderr, "Usage: %s [options] <data.ber> ...\n", av[0]); fprintf(stderr, "Where options are:\n"); if(pduType->uper_decoder) fprintf(stderr, " -iper Input is in Unaligned PER (Packed Encoding Rules) (DEFAULT)\n"); fprintf(stderr, " -iber Input is in BER (Basic Encoding Rules)%s\n", iform == INP_PER ? "" : " (DEFAULT)"); fprintf(stderr, " -ixer Input is in XER (XML Encoding Rules)\n"); if(pduType->uper_encoder) fprintf(stderr, " -oper Output in Unaligned PER (Packed Encoding Rules)\n"); fprintf(stderr, " -oder Output in DER (Distinguished Encoding Rules)\n" " -oxer Output in XER (XML Encoding Rules) (DEFAULT)\n" " -otext Output in plain semi-structured text (dump)\n" " -onull Verify (decode) input, but do not output\n"); if(pduType->uper_decoder) fprintf(stderr, " -per-nopad Assume PER PDUs are not padded (-iper)\n"); #ifdef ASN_PDU_COLLECTION fprintf(stderr, " -p <PDU> Specify PDU type to decode\n" " -p list List available PDUs\n"); #endif /* ASN_PDU_COLLECTION */ fprintf(stderr, " -1 Decode only the first PDU in file\n" " -b <size> Set the i/o buffer size (default is %ld)\n" " -c Check ASN.1 constraints after decoding\n" " -d Enable debugging (-dd is even better)\n" " -n <num> Process files <num> times\n" " -s <size> Set the stack usage limit (default is %d)\n" #ifdef JUNKTEST " -J <prob> Set random junk test bit garbaging probability\n" #endif , (long)suggested_bufsize, _ASN_DEFAULT_STACK_MAX); exit(EX_USAGE); } ac -= optind; av += optind; if(ac < 1) { fprintf(stderr, "%s: No input files specified. " "Try '-h' for more information\n", av[-optind]); exit(EX_USAGE); } setvbuf(stdout, 0, _IOLBF, 0); for(num = 0; num < number_of_iterations; num++) { int ac_i; /* * Process all files in turn. */ for(ac_i = 0; ac_i < ac; ac_i++) { asn_enc_rval_t erv; void *structure; /* Decoded structure */ FILE *file = argument_to_file(av, ac_i); char *name = argument_to_name(av, ac_i); int first_pdu; for(first_pdu = 1; first_pdu || !opt_onepdu; first_pdu = 0) { /* * Decode the encoded structure from file. */ structure = data_decode_from_file(pduType, file, name, suggested_bufsize, first_pdu); if(!structure) { if(errno) { /* Error message is already printed */ exit(EX_DATAERR); } else { /* EOF */ break; } } /* Check ASN.1 constraints */ if(opt_check) { char errbuf[128]; size_t errlen = sizeof(errbuf); if(asn_check_constraints(pduType, structure, errbuf, &errlen)) { fprintf(stderr, "%s: ASN.1 constraint " "check failed: %s\n", name, errbuf); exit(EX_DATAERR); } } switch(oform) { case OUT_NULL: #ifdef JUNKTEST if(opt_jprob == 0.0) #endif fprintf(stderr, "%s: decoded successfully\n", name); break; case OUT_TEXT: /* -otext */ asn_fprint(stdout, pduType, structure); break; case OUT_XER: /* -oxer */ if(xer_fprint(stdout, pduType, structure)) { fprintf(stderr, "%s: Cannot convert %s into XML\n", name, pduType->name); exit(EX_UNAVAILABLE); } break; case OUT_DER: erv = der_encode(pduType, structure, write_out, stdout); if(erv.encoded < 0) { fprintf(stderr, "%s: Cannot convert %s into DER\n", name, pduType->name); exit(EX_UNAVAILABLE); } DEBUG("Encoded in %ld bytes of DER", (long)erv.encoded); break; case OUT_PER: erv = uper_encode(pduType, structure, write_out, stdout); if(erv.encoded < 0) { fprintf(stderr, "%s: Cannot convert %s into Unaligned PER\n", name, pduType->name); exit(EX_UNAVAILABLE); } DEBUG("Encoded in %ld bits of UPER", (long)erv.encoded); break; } ASN_STRUCT_FREE(*pduType, structure); structure = NULL; } if(file && file != stdin) fclose(file); } } #ifdef JUNKTEST if(opt_jprob > 0.0) { fprintf(stderr, "Junked %f OK (%d/%d)\n", opt_jprob, junk_failures, number_of_iterations); } #endif /* JUNKTEST */ return 0; }
int main(int ac, char **av) { /* Declare a pointer to a T4 type */ T4_t *t4; /* Encoder return value */ asn_enc_rval_t ec; /* Allocate an instance of T4 */ t4 = calloc(1, sizeof(T4_t)); /* not malloc! */ assert(t4); /* Assume infinite memory */ /* Prepare a BIT STRING */ (*t4).first.buf = calloc(2, 1); assert((*t4).first.buf); (*t4).first.size = 2; /* 2 bytes */ (*t4).first.buf[0] = 0xc0; /* Set BIT STRING value */ (*t4).first.buf[1] = 0xc0; /* Set BIT STRING value */ (*t4).first.bits_unused = 4; /* Trim unused bits */ /* And another BIT STRING */ (*t4).second.buf = calloc(3, 1); assert((*t4).second.buf); (*t4).second.size = 3; /* 3 bytes */ (*t4).second.buf[0] = 0xc0; /* Set BIT STRING value */ (*t4).second.buf[1] = 0xc0; /* Set BIT STRING value */ (*t4).second.buf[2] = 0xc0; /* Set BIT STRING value */ (*t4).second.bits_unused = 4; /* Trim unused bits */ /* And another BIT STRING */ (*t4).nest1.third.buf = calloc(5, 1); assert((*t4).nest1.third.buf); (*t4).nest1.third.size = 5; /* 5 bytes */ (*t4).nest1.third.buf[0] = 0xc0; /* Set BIT STRING value */ (*t4).nest1.third.buf[1] = 0xc0; /* Set BIT STRING value */ (*t4).nest1.third.buf[2] = 0xc0; /* Set BIT STRING value */ (*t4).nest1.third.buf[3] = 0xc0; /* Set BIT STRING value */ (*t4).nest1.third.buf[4] = 0xc0; /* Set BIT STRING value */ (*t4).nest1.third.bits_unused = 4; /* Trim unused bits */ /* And another BIT STRING */ (*t4).nest1.fourth.buf = calloc(7, 1); assert((*t4).nest1.fourth.buf); (*t4).nest1.fourth.size = 7; /* 7 bytes */ (*t4).nest1.fourth.buf[0] = 0xc0; /* Set BIT STRING value */ (*t4).nest1.fourth.buf[1] = 0xc0; /* Set BIT STRING value */ (*t4).nest1.fourth.buf[2] = 0xc0; /* Set BIT STRING value */ (*t4).nest1.fourth.buf[3] = 0xc0; /* Set BIT STRING value */ (*t4).nest1.fourth.buf[4] = 0xc0; /* Set BIT STRING value */ (*t4).nest1.fourth.buf[5] = 0xc0; /* Set BIT STRING value */ (*t4).nest1.fourth.buf[6] = 0xc0; /* Set BIT STRING value */ (*t4).nest1.fourth.bits_unused = 4; /* Trim unused bits */ fprintf(stdout, "%d\n", sizeof(T4_t)); if(ac < 2) { fprintf(stderr,"Specify filename for PER output\n"); } else { const char *filename = av[1]; FILE *fp = fopen(filename,"wb"); /* for PER output */ if(!fp) { perror(filename); exit(71); /* better, EX_OSERR */ } /* Encode T4 as PER */ ec = uper_encode(&asn_DEF_T4,t4,write_out,fp); fclose(fp); if(ec.encoded == -1) { fprintf(stderr,"Could not encode T4 (at %s)\n", ec.failed_type ? ec.failed_type->name : "unknown"); exit(65); /* better, EX_DATAERR */ } else { fprintf(stderr,"Created %s with PER encoded T4\n",filename); } } /* Also print the constructed T4 XER encoded (XML) */ xer_fprint(stdout,&asn_DEF_T4,t4); return 0; /* Encoding finished successfully */ }
static asn_enc_rval_t asn_encode_internal(const asn_codec_ctx_t *opt_codec_ctx, enum asn_transfer_syntax syntax, const asn_TYPE_descriptor_t *td, const void *sptr, asn_app_consume_bytes_f *callback, void *callback_key) { asn_enc_rval_t er; enum xer_encoder_flags_e xer_flags = XER_F_CANONICAL; (void)opt_codec_ctx; /* Parameters are not checked on encode yet. */ if(!td || !sptr) { errno = EINVAL; ASN__ENCODE_FAILED; } switch(syntax) { case ATS_NONSTANDARD_PLAINTEXT: if(td->op->print_struct) { struct callback_count_bytes_key cb_key; cb_key.callback = callback; cb_key.callback_key = callback_key; cb_key.computed_size = 0; if(td->op->print_struct(td, sptr, 1, callback_count_bytes_cb, &cb_key) < 0 || callback_count_bytes_cb("\n", 1, &cb_key) < 0) { errno = EBADF; /* Structure has incorrect form. */ er.encoded = -1; er.failed_type = td; er.structure_ptr = sptr; } else { er.encoded = cb_key.computed_size; er.failed_type = 0; er.structure_ptr = 0; } } else { errno = ENOENT; /* Transfer syntax is not defined for this type. */ ASN__ENCODE_FAILED; } break; case ATS_RANDOM: errno = ENOENT; /* Randomization doesn't make sense on output. */ ASN__ENCODE_FAILED; case ATS_BER: /* BER is a superset of DER. */ /* Fall through. */ case ATS_DER: if(td->op->der_encoder) { er = der_encode(td, sptr, callback, callback_key); if(er.encoded == -1) { if(er.failed_type && er.failed_type->op->der_encoder) { errno = EBADF; /* Structure has incorrect form. */ } else { errno = ENOENT; /* DER is not defined for this type. */ } } } else { errno = ENOENT; /* Transfer syntax is not defined for this type. */ ASN__ENCODE_FAILED; } break; case ATS_CER: errno = ENOENT; /* Transfer syntax is not defined for any type. */ ASN__ENCODE_FAILED; #ifdef ASN_DISABLE_OER_SUPPORT case ATS_BASIC_OER: case ATS_CANONICAL_OER: errno = ENOENT; /* PER is not defined. */ ASN__ENCODE_FAILED; break; #else /* ASN_DISABLE_OER_SUPPORT */ case ATS_BASIC_OER: /* CANONICAL-OER is a superset of BASIC-OER. */ /* Fall through. */ case ATS_CANONICAL_OER: if(td->op->oer_encoder) { er = oer_encode(td, sptr, callback, callback_key); if(er.encoded == -1) { if(er.failed_type && er.failed_type->op->oer_encoder) { errno = EBADF; /* Structure has incorrect form. */ } else { errno = ENOENT; /* OER is not defined for this type. */ } } } else { errno = ENOENT; /* Transfer syntax is not defined for this type. */ ASN__ENCODE_FAILED; } break; #endif /* ASN_DISABLE_OER_SUPPORT */ #ifdef ASN_DISABLE_PER_SUPPORT case ATS_UNALIGNED_BASIC_PER: case ATS_UNALIGNED_CANONICAL_PER: errno = ENOENT; /* PER is not defined. */ ASN__ENCODE_FAILED; break; #else /* ASN_DISABLE_PER_SUPPORT */ case ATS_UNALIGNED_BASIC_PER: /* CANONICAL-UPER is a superset of BASIC-UPER. */ /* Fall through. */ case ATS_UNALIGNED_CANONICAL_PER: if(td->op->uper_encoder) { er = uper_encode(td, 0, sptr, callback, callback_key); if(er.encoded == -1) { if(er.failed_type && er.failed_type->op->uper_encoder) { errno = EBADF; /* Structure has incorrect form. */ } else { errno = ENOENT; /* UPER is not defined for this type. */ } } else { ASN_DEBUG("Complete encoded in %ld bits", (long)er.encoded); if(er.encoded == 0) { /* Enforce "Complete Encoding" of X.691 #11.1 */ if(callback("\0", 1, callback_key) < 0) { errno = EBADF; ASN__ENCODE_FAILED; } er.encoded = 8; /* Exactly 8 zero bits is added. */ } /* Convert bits into bytes */ er.encoded = (er.encoded + 7) >> 3; } } else {