Lev Walkin | 22b5455 | 2004-10-28 13:22:54 +0000 | [diff] [blame] | 1 | /* |
Lev Walkin | d062568 | 2006-08-25 02:35:08 +0000 | [diff] [blame] | 2 | * Generic converter template for a selected ASN.1 type. |
| 3 | * Copyright (c) 2005, 2006 Lev Walkin <vlm@lionet.info>. All rights reserved. |
Lev Walkin | 22b5455 | 2004-10-28 13:22:54 +0000 | [diff] [blame] | 4 | * |
Lev Walkin | d062568 | 2006-08-25 02:35:08 +0000 | [diff] [blame] | 5 | * To compile with your own ASN.1 type, please redefine the PDU as shown: |
Lev Walkin | 22b5455 | 2004-10-28 13:22:54 +0000 | [diff] [blame] | 6 | * |
Lev Walkin | d062568 | 2006-08-25 02:35:08 +0000 | [diff] [blame] | 7 | * cc -DPDU=MyCustomType -o myDecoder.o -c converter-sample.c |
Lev Walkin | 22b5455 | 2004-10-28 13:22:54 +0000 | [diff] [blame] | 8 | */ |
| 9 | #ifdef HAVE_CONFIG_H |
| 10 | #include <config.h> |
| 11 | #endif |
| 12 | #include <stdio.h> |
| 13 | #include <sys/types.h> |
Lev Walkin | 4696c74 | 2005-08-22 12:23:54 +0000 | [diff] [blame] | 14 | #include <stdlib.h> /* for atoi(3) */ |
| 15 | #include <unistd.h> /* for getopt(3) */ |
Lev Walkin | 22b5455 | 2004-10-28 13:22:54 +0000 | [diff] [blame] | 16 | #include <string.h> /* for strerror(3) */ |
Lev Walkin | 22b5455 | 2004-10-28 13:22:54 +0000 | [diff] [blame] | 17 | #include <sysexits.h> /* for EX_* exit codes */ |
Lev Walkin | fc77643 | 2005-03-29 17:21:14 +0000 | [diff] [blame] | 18 | #include <assert.h> /* for assert(3) */ |
| 19 | #include <errno.h> /* for errno */ |
Lev Walkin | 22b5455 | 2004-10-28 13:22:54 +0000 | [diff] [blame] | 20 | |
Lev Walkin | 0094956 | 2005-03-10 13:39:03 +0000 | [diff] [blame] | 21 | #include <asn_application.h> |
Lev Walkin | 1d9e8dd | 2005-12-07 05:46:03 +0000 | [diff] [blame] | 22 | #include <asn_internal.h> /* for _ASN_DEFAULT_STACK_MAX */ |
Lev Walkin | 22b5455 | 2004-10-28 13:22:54 +0000 | [diff] [blame] | 23 | |
Lev Walkin | d062568 | 2006-08-25 02:35:08 +0000 | [diff] [blame] | 24 | /* Convert "Type" defined by -DPDU into "asn_DEF_Type" */ |
| 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; /* ASN.1 type to be decoded */ |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 30 | #ifdef ASN_PDU_COLLECTION /* Generated by asn1c: -pdu=... */ |
| 31 | extern asn_TYPE_descriptor_t *asn_pdu_collection[]; |
| 32 | #endif |
Lev Walkin | 22b5455 | 2004-10-28 13:22:54 +0000 | [diff] [blame] | 33 | |
| 34 | /* |
Lev Walkin | 0094956 | 2005-03-10 13:39:03 +0000 | [diff] [blame] | 35 | * Open file and parse its contens. |
Lev Walkin | 22b5455 | 2004-10-28 13:22:54 +0000 | [diff] [blame] | 36 | */ |
Lev Walkin | 0094956 | 2005-03-10 13:39:03 +0000 | [diff] [blame] | 37 | static void *data_decode_from_file(asn_TYPE_descriptor_t *asnTypeOfPDU, |
Lev Walkin | bc69177 | 2006-09-17 11:02:53 +0000 | [diff] [blame] | 38 | FILE *file, const char *name, ssize_t suggested_bufsize, int first_pdu); |
Lev Walkin | d1bfea6 | 2005-11-08 03:06:16 +0000 | [diff] [blame] | 39 | static int write_out(const void *buffer, size_t size, void *key); |
Lev Walkin | c744a02 | 2006-09-15 18:33:25 +0000 | [diff] [blame] | 40 | static FILE *argument_to_file(char *av[], int idx); |
| 41 | static char *argument_to_name(char *av[], int idx); |
Lev Walkin | 22b5455 | 2004-10-28 13:22:54 +0000 | [diff] [blame] | 42 | |
Lev Walkin | 5a621d6 | 2006-09-18 20:05:34 +0000 | [diff] [blame] | 43 | int opt_debug; /* -d (or -dd) */ |
| 44 | static int opt_check; /* -c (constraints checking) */ |
| 45 | static int opt_stack; /* -s (maximum stack size) */ |
| 46 | static int opt_ippad; /* -per-padded (PER input is byte-padded) */ |
| 47 | static int opt_onepdu; /* -1 (decode single PDU) */ |
Lev Walkin | d1bfea6 | 2005-11-08 03:06:16 +0000 | [diff] [blame] | 48 | |
| 49 | /* Input data format selector */ |
| 50 | static enum input_format { |
| 51 | INP_BER, /* -iber: BER input */ |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 52 | INP_XER, /* -ixer: XER input */ |
| 53 | INP_PER /* -iper: Unaligned PER input */ |
Lev Walkin | d1bfea6 | 2005-11-08 03:06:16 +0000 | [diff] [blame] | 54 | } iform; /* -i<format> */ |
| 55 | |
| 56 | /* Output data format selector */ |
| 57 | static enum output_format { |
| 58 | OUT_XER, /* -oxer: XER (XML) output */ |
Lev Walkin | be3a0c5 | 2006-08-25 04:56:21 +0000 | [diff] [blame] | 59 | OUT_DER, /* -oder: DER (BER) output */ |
| 60 | OUT_PER, /* -oper: Unaligned PER output */ |
Lev Walkin | d1bfea6 | 2005-11-08 03:06:16 +0000 | [diff] [blame] | 61 | OUT_TEXT, /* -otext: semi-structured text */ |
| 62 | OUT_NULL /* -onull: No pretty-printing */ |
| 63 | } oform; /* -o<format> */ |
Lev Walkin | 22b5455 | 2004-10-28 13:22:54 +0000 | [diff] [blame] | 64 | |
Lev Walkin | 1d9e8dd | 2005-12-07 05:46:03 +0000 | [diff] [blame] | 65 | /* Debug output function */ |
| 66 | static inline void |
| 67 | DEBUG(const char *fmt, ...) { |
| 68 | va_list ap; |
| 69 | if(!opt_debug) return; |
| 70 | fprintf(stderr, "AD: "); |
| 71 | va_start(ap, fmt); |
| 72 | vfprintf(stderr, fmt, ap); |
| 73 | va_end(ap); |
| 74 | fprintf(stderr, "\n"); |
| 75 | } |
Lev Walkin | 22b5455 | 2004-10-28 13:22:54 +0000 | [diff] [blame] | 76 | |
| 77 | int |
Lev Walkin | c744a02 | 2006-09-15 18:33:25 +0000 | [diff] [blame] | 78 | main(int ac, char *av[]) { |
Lev Walkin | d062568 | 2006-08-25 02:35:08 +0000 | [diff] [blame] | 79 | static asn_TYPE_descriptor_t *pduType = &PDU_Type; |
Lev Walkin | 22b5455 | 2004-10-28 13:22:54 +0000 | [diff] [blame] | 80 | ssize_t suggested_bufsize = 8192; /* close or equal to stdio buffer */ |
| 81 | int number_of_iterations = 1; |
| 82 | int num; |
| 83 | int ch; |
| 84 | |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 85 | /* Figure out if Unaligned PER needs to be default */ |
| 86 | if(pduType->uper_decoder) |
| 87 | iform = INP_PER; |
| 88 | |
Lev Walkin | 22b5455 | 2004-10-28 13:22:54 +0000 | [diff] [blame] | 89 | /* |
| 90 | * Pocess the command-line argments. |
| 91 | */ |
Lev Walkin | 5a621d6 | 2006-09-18 20:05:34 +0000 | [diff] [blame] | 92 | while((ch = getopt(ac, av, "i:o:1b:cdn:p:hs:")) != -1) |
Lev Walkin | 22b5455 | 2004-10-28 13:22:54 +0000 | [diff] [blame] | 93 | switch(ch) { |
Lev Walkin | d1bfea6 | 2005-11-08 03:06:16 +0000 | [diff] [blame] | 94 | case 'i': |
| 95 | if(optarg[0] == 'b') { iform = INP_BER; break; } |
| 96 | if(optarg[0] == 'x') { iform = INP_XER; break; } |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 97 | if(pduType->uper_decoder |
| 98 | && optarg[0] == 'p') { iform = INP_PER; break; } |
Lev Walkin | be3a0c5 | 2006-08-25 04:56:21 +0000 | [diff] [blame] | 99 | fprintf(stderr, "-i<format>: '%s': improper format selector\n", |
Lev Walkin | d1bfea6 | 2005-11-08 03:06:16 +0000 | [diff] [blame] | 100 | optarg); |
| 101 | exit(EX_UNAVAILABLE); |
| 102 | case 'o': |
| 103 | if(optarg[0] == 'd') { oform = OUT_DER; break; } |
Lev Walkin | be3a0c5 | 2006-08-25 04:56:21 +0000 | [diff] [blame] | 104 | if(pduType->uper_encoder |
| 105 | && optarg[0] == 'p') { oform = OUT_PER; break; } |
Lev Walkin | d1bfea6 | 2005-11-08 03:06:16 +0000 | [diff] [blame] | 106 | if(optarg[0] == 'x') { oform = OUT_XER; break; } |
| 107 | if(optarg[0] == 't') { oform = OUT_TEXT; break; } |
| 108 | if(optarg[0] == 'n') { oform = OUT_NULL; break; } |
Lev Walkin | be3a0c5 | 2006-08-25 04:56:21 +0000 | [diff] [blame] | 109 | fprintf(stderr, "-o<format>: '%s': improper format selector\n", |
Lev Walkin | d1bfea6 | 2005-11-08 03:06:16 +0000 | [diff] [blame] | 110 | optarg); |
| 111 | exit(EX_UNAVAILABLE); |
Lev Walkin | bc69177 | 2006-09-17 11:02:53 +0000 | [diff] [blame] | 112 | case '1': |
| 113 | opt_onepdu = 1; |
| 114 | break; |
Lev Walkin | 22b5455 | 2004-10-28 13:22:54 +0000 | [diff] [blame] | 115 | case 'b': |
| 116 | suggested_bufsize = atoi(optarg); |
| 117 | if(suggested_bufsize < 1 |
| 118 | || suggested_bufsize > 16 * 1024 * 1024) { |
| 119 | fprintf(stderr, |
| 120 | "-b %s: Improper buffer size (1..16M)\n", |
| 121 | optarg); |
| 122 | exit(EX_UNAVAILABLE); |
| 123 | } |
| 124 | break; |
| 125 | case 'c': |
| 126 | opt_check = 1; |
| 127 | break; |
| 128 | case 'd': |
| 129 | opt_debug++; /* Double -dd means ASN.1 debug */ |
| 130 | break; |
| 131 | case 'n': |
| 132 | number_of_iterations = atoi(optarg); |
| 133 | if(number_of_iterations < 1) { |
| 134 | fprintf(stderr, |
| 135 | "-n %s: Improper iterations count\n", optarg); |
| 136 | exit(EX_UNAVAILABLE); |
| 137 | } |
| 138 | break; |
Lev Walkin | bc69177 | 2006-09-17 11:02:53 +0000 | [diff] [blame] | 139 | case 'p': |
Lev Walkin | 5a621d6 | 2006-09-18 20:05:34 +0000 | [diff] [blame] | 140 | if(strcmp(optarg, "er-padded") == 0) { |
| 141 | opt_ippad = 1; |
| 142 | break; |
| 143 | } |
Lev Walkin | bc69177 | 2006-09-17 11:02:53 +0000 | [diff] [blame] | 144 | #ifdef ASN_PDU_COLLECTION |
Lev Walkin | 5a621d6 | 2006-09-18 20:05:34 +0000 | [diff] [blame] | 145 | if(strcmp(optarg, "list") == 0) { |
Lev Walkin | bc69177 | 2006-09-17 11:02:53 +0000 | [diff] [blame] | 146 | asn_TYPE_descriptor_t **pdu = asn_pdu_collection; |
Lev Walkin | 5a621d6 | 2006-09-18 20:05:34 +0000 | [diff] [blame] | 147 | fprintf(stderr, "Available PDU types:\n"); |
| 148 | for(; *pdu; pdu++) printf("%s\n", (*pdu)->name); |
| 149 | exit(0); |
| 150 | } else if(optarg[0] >= 'A' && optarg[0] <= 'Z') { |
| 151 | asn_TYPE_descriptor_t **pdu = asn_pdu_collection; |
Lev Walkin | bc69177 | 2006-09-17 11:02:53 +0000 | [diff] [blame] | 152 | while(*pdu && strcmp((*pdu)->name, optarg)) pdu++; |
| 153 | if(*pdu) { pduType = *pdu; break; } |
Lev Walkin | 5a621d6 | 2006-09-18 20:05:34 +0000 | [diff] [blame] | 154 | fprintf(stderr, "-p %s: Unrecognized PDU\n", optarg); |
Lev Walkin | bc69177 | 2006-09-17 11:02:53 +0000 | [diff] [blame] | 155 | } |
| 156 | #endif /* ASN_PDU_COLLECTION */ |
Lev Walkin | 5a621d6 | 2006-09-18 20:05:34 +0000 | [diff] [blame] | 157 | fprintf(stderr, "-p %s: Unrecognized option\n", optarg); |
Lev Walkin | bc69177 | 2006-09-17 11:02:53 +0000 | [diff] [blame] | 158 | exit(EX_UNAVAILABLE); |
Lev Walkin | 22b5455 | 2004-10-28 13:22:54 +0000 | [diff] [blame] | 159 | case 's': |
| 160 | opt_stack = atoi(optarg); |
Lev Walkin | 1d9e8dd | 2005-12-07 05:46:03 +0000 | [diff] [blame] | 161 | if(opt_stack < 0) { |
Lev Walkin | 22b5455 | 2004-10-28 13:22:54 +0000 | [diff] [blame] | 162 | fprintf(stderr, |
Lev Walkin | 1d9e8dd | 2005-12-07 05:46:03 +0000 | [diff] [blame] | 163 | "-s %s: Non-negative value expected\n", |
Lev Walkin | 22b5455 | 2004-10-28 13:22:54 +0000 | [diff] [blame] | 164 | optarg); |
| 165 | exit(EX_UNAVAILABLE); |
| 166 | } |
| 167 | break; |
Lev Walkin | 22b5455 | 2004-10-28 13:22:54 +0000 | [diff] [blame] | 168 | case 'h': |
| 169 | default: |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 170 | fprintf(stderr, "Usage: %s [options] <data.ber> ...\n", av[0]); |
| 171 | fprintf(stderr, "Where options are:\n"); |
| 172 | if(pduType->uper_decoder) |
Lev Walkin | 22b5455 | 2004-10-28 13:22:54 +0000 | [diff] [blame] | 173 | fprintf(stderr, |
Lev Walkin | 857d11b | 2006-02-22 08:54:49 +0000 | [diff] [blame] | 174 | " -iper Input is in Unaligned PER (Packed Encoding Rules) (DEFAULT)\n"); |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 175 | fprintf(stderr, |
Lev Walkin | 857d11b | 2006-02-22 08:54:49 +0000 | [diff] [blame] | 176 | " -iber Input is in BER (Basic Encoding Rules)%s\n", |
| 177 | iform == INP_PER ? "" : " (DEFAULT)"); |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 178 | fprintf(stderr, |
Lev Walkin | be3a0c5 | 2006-08-25 04:56:21 +0000 | [diff] [blame] | 179 | " -ixer Input is in XER (XML Encoding Rules)\n"); |
| 180 | if(pduType->uper_encoder) |
| 181 | fprintf(stderr, |
| 182 | " -oper Output in Unaligned PER (Packed Encoding Rules)\n"); |
| 183 | fprintf(stderr, |
Lev Walkin | d1bfea6 | 2005-11-08 03:06:16 +0000 | [diff] [blame] | 184 | " -oder Output in DER (Distinguished Encoding Rules)\n" |
Lev Walkin | 857d11b | 2006-02-22 08:54:49 +0000 | [diff] [blame] | 185 | " -oxer Output in XER (XML Encoding Rules) (DEFAULT)\n" |
Lev Walkin | d1bfea6 | 2005-11-08 03:06:16 +0000 | [diff] [blame] | 186 | " -otext Output in plain semi-structured text (dump)\n" |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 187 | " -onull Verify (decode) input, but do not output\n"); |
Lev Walkin | 5a621d6 | 2006-09-18 20:05:34 +0000 | [diff] [blame] | 188 | if(pduType->uper_decoder) |
| 189 | fprintf(stderr, |
| 190 | " -per-padded Assume PER PDUs are byte-padded (-iper)\n"); |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 191 | #ifdef ASN_PDU_COLLECTION |
| 192 | fprintf(stderr, |
| 193 | " -p <PDU> Specify PDU type to decode\n" |
| 194 | " -p list List available PDUs\n"); |
| 195 | #endif /* ASN_PDU_COLLECTION */ |
| 196 | fprintf(stderr, |
Lev Walkin | c8edcb8 | 2006-09-18 22:30:55 +0000 | [diff] [blame] | 197 | " -1 Decode only the first PDU in file\n" |
Lev Walkin | 22b5455 | 2004-10-28 13:22:54 +0000 | [diff] [blame] | 198 | " -b <size> Set the i/o buffer size (default is %ld)\n" |
| 199 | " -c Check ASN.1 constraints after decoding\n" |
| 200 | " -d Enable debugging (-dd is even better)\n" |
| 201 | " -n <num> Process files <num> times\n" |
Lev Walkin | 1d9e8dd | 2005-12-07 05:46:03 +0000 | [diff] [blame] | 202 | " -s <size> Set the stack usage limit (default is %d)\n" |
| 203 | , (long)suggested_bufsize, _ASN_DEFAULT_STACK_MAX); |
Lev Walkin | 22b5455 | 2004-10-28 13:22:54 +0000 | [diff] [blame] | 204 | exit(EX_USAGE); |
| 205 | } |
| 206 | |
| 207 | ac -= optind; |
| 208 | av += optind; |
| 209 | |
| 210 | if(ac < 1) { |
Lev Walkin | d1bfea6 | 2005-11-08 03:06:16 +0000 | [diff] [blame] | 211 | fprintf(stderr, "%s: No input files specified. " |
| 212 | "Try '-h' for more information\n", |
| 213 | av[-optind]); |
Lev Walkin | 22b5455 | 2004-10-28 13:22:54 +0000 | [diff] [blame] | 214 | exit(EX_USAGE); |
| 215 | } |
| 216 | |
| 217 | setvbuf(stdout, 0, _IOLBF, 0); |
| 218 | |
| 219 | for(num = 0; num < number_of_iterations; num++) { |
| 220 | int ac_i; |
| 221 | /* |
| 222 | * Process all files in turn. |
| 223 | */ |
| 224 | for(ac_i = 0; ac_i < ac; ac_i++) { |
Lev Walkin | bc69177 | 2006-09-17 11:02:53 +0000 | [diff] [blame] | 225 | asn_enc_rval_t erv; |
| 226 | void *structure; /* Decoded structure */ |
| 227 | FILE *file = argument_to_file(av, ac_i); |
| 228 | char *name = argument_to_name(av, ac_i); |
| 229 | int first_pdu; |
Lev Walkin | 22b5455 | 2004-10-28 13:22:54 +0000 | [diff] [blame] | 230 | |
Lev Walkin | bc69177 | 2006-09-17 11:02:53 +0000 | [diff] [blame] | 231 | for(first_pdu = 1; first_pdu || !opt_onepdu; first_pdu = 0) { |
Lev Walkin | 22b5455 | 2004-10-28 13:22:54 +0000 | [diff] [blame] | 232 | /* |
| 233 | * Decode the encoded structure from file. |
| 234 | */ |
Lev Walkin | 0094956 | 2005-03-10 13:39:03 +0000 | [diff] [blame] | 235 | structure = data_decode_from_file(pduType, |
Lev Walkin | bc69177 | 2006-09-17 11:02:53 +0000 | [diff] [blame] | 236 | file, name, suggested_bufsize, first_pdu); |
Lev Walkin | 22b5455 | 2004-10-28 13:22:54 +0000 | [diff] [blame] | 237 | if(!structure) { |
Lev Walkin | bc69177 | 2006-09-17 11:02:53 +0000 | [diff] [blame] | 238 | if(errno) { |
| 239 | /* Error message is already printed */ |
| 240 | exit(EX_DATAERR); |
| 241 | } else { |
| 242 | /* EOF */ |
| 243 | break; |
| 244 | } |
Lev Walkin | 22b5455 | 2004-10-28 13:22:54 +0000 | [diff] [blame] | 245 | } |
| 246 | |
Lev Walkin | 22b5455 | 2004-10-28 13:22:54 +0000 | [diff] [blame] | 247 | /* Check ASN.1 constraints */ |
| 248 | if(opt_check) { |
| 249 | char errbuf[128]; |
| 250 | size_t errlen = sizeof(errbuf); |
Lev Walkin | 0094956 | 2005-03-10 13:39:03 +0000 | [diff] [blame] | 251 | if(asn_check_constraints(pduType, structure, |
Lev Walkin | 22b5455 | 2004-10-28 13:22:54 +0000 | [diff] [blame] | 252 | errbuf, &errlen)) { |
| 253 | fprintf(stderr, "%s: ASN.1 constraint " |
Lev Walkin | c744a02 | 2006-09-15 18:33:25 +0000 | [diff] [blame] | 254 | "check failed: %s\n", name, errbuf); |
Lev Walkin | 22b5455 | 2004-10-28 13:22:54 +0000 | [diff] [blame] | 255 | exit(EX_DATAERR); |
| 256 | } |
| 257 | } |
| 258 | |
Lev Walkin | d1bfea6 | 2005-11-08 03:06:16 +0000 | [diff] [blame] | 259 | switch(oform) { |
| 260 | case OUT_NULL: |
Lev Walkin | c744a02 | 2006-09-15 18:33:25 +0000 | [diff] [blame] | 261 | fprintf(stderr, "%s: decoded successfully\n", name); |
Lev Walkin | d1bfea6 | 2005-11-08 03:06:16 +0000 | [diff] [blame] | 262 | break; |
| 263 | case OUT_TEXT: /* -otext */ |
| 264 | asn_fprint(stdout, pduType, structure); |
| 265 | break; |
| 266 | case OUT_XER: /* -oxer */ |
| 267 | if(xer_fprint(stdout, pduType, structure)) { |
Lev Walkin | efbba4a | 2006-09-18 20:26:24 +0000 | [diff] [blame] | 268 | fprintf(stderr, |
| 269 | "%s: Cannot convert %s into XML\n", |
| 270 | name, pduType->name); |
Lev Walkin | d1bfea6 | 2005-11-08 03:06:16 +0000 | [diff] [blame] | 271 | exit(EX_UNAVAILABLE); |
| 272 | } |
| 273 | break; |
| 274 | case OUT_DER: |
| 275 | erv = der_encode(pduType, structure, write_out, stdout); |
| 276 | if(erv.encoded < 0) { |
Lev Walkin | efbba4a | 2006-09-18 20:26:24 +0000 | [diff] [blame] | 277 | fprintf(stderr, |
| 278 | "%s: Cannot convert %s into DER\n", |
| 279 | name, pduType->name); |
Lev Walkin | d1bfea6 | 2005-11-08 03:06:16 +0000 | [diff] [blame] | 280 | exit(EX_UNAVAILABLE); |
| 281 | } |
Lev Walkin | bc69177 | 2006-09-17 11:02:53 +0000 | [diff] [blame] | 282 | DEBUG("Encoded in %ld bytes of DER", (long)erv.encoded); |
Lev Walkin | d1bfea6 | 2005-11-08 03:06:16 +0000 | [diff] [blame] | 283 | break; |
Lev Walkin | be3a0c5 | 2006-08-25 04:56:21 +0000 | [diff] [blame] | 284 | case OUT_PER: |
| 285 | erv = uper_encode(pduType, structure, write_out, stdout); |
| 286 | if(erv.encoded < 0) { |
Lev Walkin | efbba4a | 2006-09-18 20:26:24 +0000 | [diff] [blame] | 287 | fprintf(stderr, |
| 288 | "%s: Cannot convert %s into Unaligned PER\n", |
| 289 | name, pduType->name); |
Lev Walkin | be3a0c5 | 2006-08-25 04:56:21 +0000 | [diff] [blame] | 290 | exit(EX_UNAVAILABLE); |
| 291 | } |
Lev Walkin | bc69177 | 2006-09-17 11:02:53 +0000 | [diff] [blame] | 292 | DEBUG("Encoded in %ld bits of UPER", (long)erv.encoded); |
Lev Walkin | be3a0c5 | 2006-08-25 04:56:21 +0000 | [diff] [blame] | 293 | break; |
Lev Walkin | d1bfea6 | 2005-11-08 03:06:16 +0000 | [diff] [blame] | 294 | } |
| 295 | |
Lev Walkin | adcb586 | 2006-03-17 02:11:12 +0000 | [diff] [blame] | 296 | ASN_STRUCT_FREE(*pduType, structure); |
Lev Walkin | bc69177 | 2006-09-17 11:02:53 +0000 | [diff] [blame] | 297 | } |
| 298 | |
| 299 | if(file && file != stdin) |
| 300 | fclose(file); |
Lev Walkin | 22b5455 | 2004-10-28 13:22:54 +0000 | [diff] [blame] | 301 | } |
| 302 | } |
| 303 | |
| 304 | return 0; |
| 305 | } |
| 306 | |
Lev Walkin | 419f675 | 2006-09-13 04:02:00 +0000 | [diff] [blame] | 307 | static struct dynamic_buffer { |
Lev Walkin | 5a621d6 | 2006-09-18 20:05:34 +0000 | [diff] [blame] | 308 | uint8_t *data; /* Pointer to the data bytes */ |
Lev Walkin | 419f675 | 2006-09-13 04:02:00 +0000 | [diff] [blame] | 309 | size_t offset; /* Offset from the start */ |
| 310 | size_t length; /* Length of meaningful contents */ |
Lev Walkin | 5a621d6 | 2006-09-18 20:05:34 +0000 | [diff] [blame] | 311 | size_t unbits; /* Unused bits in the last byte */ |
Lev Walkin | 419f675 | 2006-09-13 04:02:00 +0000 | [diff] [blame] | 312 | size_t allocated; /* Allocated memory for data */ |
| 313 | int nreallocs; /* Number of data reallocations */ |
| 314 | off_t bytes_shifted; /* Number of bytes ever shifted */ |
| 315 | } DynamicBuffer; |
Lev Walkin | 22b5455 | 2004-10-28 13:22:54 +0000 | [diff] [blame] | 316 | |
Lev Walkin | 5a621d6 | 2006-09-18 20:05:34 +0000 | [diff] [blame] | 317 | static void |
| 318 | buffer_dump() { |
| 319 | uint8_t *p = DynamicBuffer.data + DynamicBuffer.offset; |
| 320 | uint8_t *e = p + DynamicBuffer.length - (DynamicBuffer.unbits ? 1 : 0); |
| 321 | if(!opt_debug) return; |
| 322 | DEBUG("Buffer: { d=%p, o=%ld, l=%ld, u=%ld, a=%ld, s=%ld }", |
| 323 | DynamicBuffer.data, |
| 324 | (long)DynamicBuffer.offset, |
| 325 | (long)DynamicBuffer.length, |
| 326 | (long)DynamicBuffer.unbits, |
| 327 | (long)DynamicBuffer.allocated, |
| 328 | (long)DynamicBuffer.bytes_shifted); |
| 329 | for(; p < e; p++) { |
| 330 | fprintf(stderr, " %c%c%c%c%c%c%c%c", |
| 331 | ((*p >> 7) & 1) ? '1' : '0', |
| 332 | ((*p >> 6) & 1) ? '1' : '0', |
| 333 | ((*p >> 5) & 1) ? '1' : '0', |
| 334 | ((*p >> 4) & 1) ? '1' : '0', |
| 335 | ((*p >> 3) & 1) ? '1' : '0', |
| 336 | ((*p >> 2) & 1) ? '1' : '0', |
| 337 | ((*p >> 1) & 1) ? '1' : '0', |
| 338 | ((*p >> 0) & 1) ? '1' : '0'); |
| 339 | } |
| 340 | if(DynamicBuffer.unbits) { |
| 341 | int shift; |
| 342 | fprintf(stderr, " "); |
| 343 | for(shift = 7; shift >= DynamicBuffer.unbits; shift--) |
| 344 | fprintf(stderr, "%c", ((*p >> shift) & 1) ? '1' : '0'); |
| 345 | fprintf(stderr, " %d:%d\n", |
| 346 | DynamicBuffer.length - 1, 8 - DynamicBuffer.unbits); |
| 347 | } else { |
| 348 | fprintf(stderr, " %d\n", DynamicBuffer.length); |
| 349 | } |
| 350 | } |
| 351 | |
| 352 | /* |
| 353 | * Move the buffer content left N bits, possibly joining it with |
| 354 | * preceeding content. |
| 355 | */ |
| 356 | static void |
| 357 | buffer_shift_left(size_t offset, int bits) { |
| 358 | uint8_t *ptr = DynamicBuffer.data + DynamicBuffer.offset + offset; |
| 359 | uint8_t *end = DynamicBuffer.data + DynamicBuffer.offset |
| 360 | + DynamicBuffer.length - 1; |
| 361 | |
| 362 | if(!bits) return; |
| 363 | |
| 364 | DEBUG("Shifting left %d bits off %ld (o=%ld, u=%ld, l=%ld)", |
| 365 | bits, (long)offset, |
| 366 | (long)DynamicBuffer.offset, |
| 367 | (long)DynamicBuffer.unbits, |
| 368 | (long)DynamicBuffer.length); |
| 369 | |
| 370 | if(offset) { |
| 371 | int right; |
| 372 | right = ptr[0] >> (8 - bits); |
| 373 | |
| 374 | DEBUG("oleft: %c%c%c%c%c%c%c%c", |
| 375 | ((ptr[-1] >> 7) & 1) ? '1' : '0', |
| 376 | ((ptr[-1] >> 6) & 1) ? '1' : '0', |
| 377 | ((ptr[-1] >> 5) & 1) ? '1' : '0', |
| 378 | ((ptr[-1] >> 4) & 1) ? '1' : '0', |
| 379 | ((ptr[-1] >> 3) & 1) ? '1' : '0', |
| 380 | ((ptr[-1] >> 2) & 1) ? '1' : '0', |
| 381 | ((ptr[-1] >> 1) & 1) ? '1' : '0', |
| 382 | ((ptr[-1] >> 0) & 1) ? '1' : '0'); |
| 383 | |
| 384 | DEBUG("oriht: %c%c%c%c%c%c%c%c", |
| 385 | ((ptr[0] >> 7) & 1) ? '1' : '0', |
| 386 | ((ptr[0] >> 6) & 1) ? '1' : '0', |
| 387 | ((ptr[0] >> 5) & 1) ? '1' : '0', |
| 388 | ((ptr[0] >> 4) & 1) ? '1' : '0', |
| 389 | ((ptr[0] >> 3) & 1) ? '1' : '0', |
| 390 | ((ptr[0] >> 2) & 1) ? '1' : '0', |
| 391 | ((ptr[0] >> 1) & 1) ? '1' : '0', |
| 392 | ((ptr[0] >> 0) & 1) ? '1' : '0'); |
| 393 | |
| 394 | DEBUG("mriht: %c%c%c%c%c%c%c%c", |
| 395 | ((right >> 7) & 1) ? '1' : '0', |
| 396 | ((right >> 6) & 1) ? '1' : '0', |
| 397 | ((right >> 5) & 1) ? '1' : '0', |
| 398 | ((right >> 4) & 1) ? '1' : '0', |
| 399 | ((right >> 3) & 1) ? '1' : '0', |
| 400 | ((right >> 2) & 1) ? '1' : '0', |
| 401 | ((right >> 1) & 1) ? '1' : '0', |
| 402 | ((right >> 0) & 1) ? '1' : '0'); |
| 403 | |
| 404 | ptr[-1] = (ptr[-1] & (0xff << bits)) | right; |
| 405 | |
| 406 | DEBUG("after: %c%c%c%c%c%c%c%c", |
| 407 | ((ptr[-1] >> 7) & 1) ? '1' : '0', |
| 408 | ((ptr[-1] >> 6) & 1) ? '1' : '0', |
| 409 | ((ptr[-1] >> 5) & 1) ? '1' : '0', |
| 410 | ((ptr[-1] >> 4) & 1) ? '1' : '0', |
| 411 | ((ptr[-1] >> 3) & 1) ? '1' : '0', |
| 412 | ((ptr[-1] >> 2) & 1) ? '1' : '0', |
| 413 | ((ptr[-1] >> 1) & 1) ? '1' : '0', |
| 414 | ((ptr[-1] >> 0) & 1) ? '1' : '0'); |
| 415 | } |
| 416 | |
| 417 | buffer_dump(); |
| 418 | |
| 419 | for(; ptr < end; ptr++) { |
| 420 | int right = ptr[1] >> (8 - bits); |
| 421 | *ptr = (*ptr << bits) | right; |
| 422 | } |
| 423 | *ptr <<= bits; |
| 424 | |
| 425 | DEBUG("Unbits [%d=>", (int)DynamicBuffer.unbits); |
| 426 | if(DynamicBuffer.unbits == 0) { |
| 427 | DynamicBuffer.unbits += bits; |
| 428 | } else { |
| 429 | DynamicBuffer.unbits += bits; |
| 430 | if(DynamicBuffer.unbits > 7) { |
| 431 | DynamicBuffer.unbits -= 8; |
| 432 | DynamicBuffer.length--; |
| 433 | DynamicBuffer.bytes_shifted++; |
| 434 | } |
| 435 | } |
| 436 | DEBUG("Unbits =>%d]", (int)DynamicBuffer.unbits); |
| 437 | |
| 438 | buffer_dump(); |
| 439 | |
| 440 | DEBUG("Shifted. Now (o=%ld, u=%ld l=%ld)", |
| 441 | (long)DynamicBuffer.offset, |
| 442 | (long)DynamicBuffer.unbits, |
| 443 | (long)DynamicBuffer.length); |
| 444 | |
| 445 | |
| 446 | } |
| 447 | |
Lev Walkin | 22b5455 | 2004-10-28 13:22:54 +0000 | [diff] [blame] | 448 | /* |
Lev Walkin | d1bfea6 | 2005-11-08 03:06:16 +0000 | [diff] [blame] | 449 | * Ensure that the buffer contains at least this amount of free space. |
Lev Walkin | 22b5455 | 2004-10-28 13:22:54 +0000 | [diff] [blame] | 450 | */ |
Lev Walkin | 5a621d6 | 2006-09-18 20:05:34 +0000 | [diff] [blame] | 451 | static void add_bytes_to_buffer(const void *data2add, size_t bytes) { |
Lev Walkin | 22b5455 | 2004-10-28 13:22:54 +0000 | [diff] [blame] | 452 | |
Lev Walkin | 5a621d6 | 2006-09-18 20:05:34 +0000 | [diff] [blame] | 453 | if(bytes == 0) return; |
| 454 | |
| 455 | DEBUG("=> add_bytes(%ld) { o=%ld l=%ld u=%ld, s=%ld }", |
| 456 | (long)bytes, |
Lev Walkin | 419f675 | 2006-09-13 04:02:00 +0000 | [diff] [blame] | 457 | (long)DynamicBuffer.offset, |
| 458 | (long)DynamicBuffer.length, |
Lev Walkin | 5a621d6 | 2006-09-18 20:05:34 +0000 | [diff] [blame] | 459 | (long)DynamicBuffer.unbits, |
Lev Walkin | 419f675 | 2006-09-13 04:02:00 +0000 | [diff] [blame] | 460 | (long)DynamicBuffer.allocated); |
Lev Walkin | 22b5455 | 2004-10-28 13:22:54 +0000 | [diff] [blame] | 461 | |
Lev Walkin | 419f675 | 2006-09-13 04:02:00 +0000 | [diff] [blame] | 462 | if(DynamicBuffer.allocated |
Lev Walkin | 5a621d6 | 2006-09-18 20:05:34 +0000 | [diff] [blame] | 463 | >= (DynamicBuffer.offset + DynamicBuffer.length + bytes)) { |
Lev Walkin | f9492a9 | 2006-09-13 04:14:17 +0000 | [diff] [blame] | 464 | DEBUG("\tNo buffer reallocation is necessary"); |
Lev Walkin | 5a621d6 | 2006-09-18 20:05:34 +0000 | [diff] [blame] | 465 | } else if(bytes <= DynamicBuffer.offset) { |
Lev Walkin | 419f675 | 2006-09-13 04:02:00 +0000 | [diff] [blame] | 466 | DEBUG("\tContents shifted by %ld", DynamicBuffer.offset); |
Lev Walkin | 22b5455 | 2004-10-28 13:22:54 +0000 | [diff] [blame] | 467 | |
| 468 | /* Shift the buffer contents */ |
Lev Walkin | 419f675 | 2006-09-13 04:02:00 +0000 | [diff] [blame] | 469 | memmove(DynamicBuffer.data, |
| 470 | DynamicBuffer.data + DynamicBuffer.offset, |
| 471 | DynamicBuffer.length); |
| 472 | DynamicBuffer.bytes_shifted += DynamicBuffer.offset; |
| 473 | DynamicBuffer.offset = 0; |
Lev Walkin | 22b5455 | 2004-10-28 13:22:54 +0000 | [diff] [blame] | 474 | } else { |
Lev Walkin | 5a621d6 | 2006-09-18 20:05:34 +0000 | [diff] [blame] | 475 | size_t newsize = (DynamicBuffer.allocated << 2) + bytes; |
Lev Walkin | 419f675 | 2006-09-13 04:02:00 +0000 | [diff] [blame] | 476 | void *p = MALLOC(newsize); |
Lev Walkin | 1d9e8dd | 2005-12-07 05:46:03 +0000 | [diff] [blame] | 477 | if(!p) { |
Lev Walkin | 419f675 | 2006-09-13 04:02:00 +0000 | [diff] [blame] | 478 | perror("malloc()"); |
Lev Walkin | 22b5455 | 2004-10-28 13:22:54 +0000 | [diff] [blame] | 479 | exit(EX_OSERR); |
| 480 | } |
Lev Walkin | e1baa4d | 2006-09-17 08:23:43 +0000 | [diff] [blame] | 481 | memcpy(p, |
| 482 | DynamicBuffer.data + DynamicBuffer.offset, |
| 483 | DynamicBuffer.length); |
Lev Walkin | 419f675 | 2006-09-13 04:02:00 +0000 | [diff] [blame] | 484 | FREEMEM(DynamicBuffer.data); |
| 485 | DynamicBuffer.data = (char *)p; |
| 486 | DynamicBuffer.offset = 0; |
| 487 | DynamicBuffer.allocated = newsize; |
| 488 | DynamicBuffer.nreallocs++; |
Lev Walkin | f9492a9 | 2006-09-13 04:14:17 +0000 | [diff] [blame] | 489 | DEBUG("\tBuffer reallocated to %ld (%d time)", |
Lev Walkin | 419f675 | 2006-09-13 04:02:00 +0000 | [diff] [blame] | 490 | newsize, DynamicBuffer.nreallocs); |
Lev Walkin | 22b5455 | 2004-10-28 13:22:54 +0000 | [diff] [blame] | 491 | } |
Lev Walkin | 1d9e8dd | 2005-12-07 05:46:03 +0000 | [diff] [blame] | 492 | |
Lev Walkin | 5a621d6 | 2006-09-18 20:05:34 +0000 | [diff] [blame] | 493 | memcpy(DynamicBuffer.data |
| 494 | + DynamicBuffer.offset + DynamicBuffer.length, |
| 495 | data2add, bytes); |
| 496 | DynamicBuffer.length += bytes; |
| 497 | if(DynamicBuffer.unbits) { |
| 498 | int bits = DynamicBuffer.unbits; |
| 499 | DynamicBuffer.unbits = 0; |
| 500 | buffer_shift_left(DynamicBuffer.length - bytes, bits); |
| 501 | } |
| 502 | |
| 503 | DEBUG("<= add_bytes(%ld) { o=%ld l=%ld u=%ld, s=%ld }", |
| 504 | (long)bytes, |
| 505 | (long)DynamicBuffer.offset, |
| 506 | (long)DynamicBuffer.length, |
| 507 | (long)DynamicBuffer.unbits, |
| 508 | (long)DynamicBuffer.allocated); |
Lev Walkin | 22b5455 | 2004-10-28 13:22:54 +0000 | [diff] [blame] | 509 | } |
| 510 | |
Lev Walkin | c744a02 | 2006-09-15 18:33:25 +0000 | [diff] [blame] | 511 | static void * |
Lev Walkin | bc69177 | 2006-09-17 11:02:53 +0000 | [diff] [blame] | 512 | data_decode_from_file(asn_TYPE_descriptor_t *pduType, FILE *file, const char *name, ssize_t suggested_bufsize, int on_first_pdu) { |
Lev Walkin | 5a621d6 | 2006-09-18 20:05:34 +0000 | [diff] [blame] | 513 | static uint8_t *fbuf; |
Lev Walkin | 22b5455 | 2004-10-28 13:22:54 +0000 | [diff] [blame] | 514 | static ssize_t fbuf_size; |
| 515 | static asn_codec_ctx_t s_codec_ctx; |
| 516 | asn_codec_ctx_t *opt_codec_ctx = 0; |
| 517 | void *structure = 0; |
Lev Walkin | d1bfea6 | 2005-11-08 03:06:16 +0000 | [diff] [blame] | 518 | asn_dec_rval_t rval; |
Lev Walkin | bc69177 | 2006-09-17 11:02:53 +0000 | [diff] [blame] | 519 | size_t old_offset; |
| 520 | size_t new_offset; |
Lev Walkin | 5a621d6 | 2006-09-18 20:05:34 +0000 | [diff] [blame] | 521 | int tolerate_eof; |
Lev Walkin | 22b5455 | 2004-10-28 13:22:54 +0000 | [diff] [blame] | 522 | size_t rd; |
Lev Walkin | c744a02 | 2006-09-15 18:33:25 +0000 | [diff] [blame] | 523 | |
| 524 | if(!file) { |
Lev Walkin | bc69177 | 2006-09-17 11:02:53 +0000 | [diff] [blame] | 525 | fprintf(stderr, "%s: %s\n", name, strerror(errno)); |
| 526 | errno = EINVAL; |
Lev Walkin | c744a02 | 2006-09-15 18:33:25 +0000 | [diff] [blame] | 527 | return 0; |
| 528 | } |
Lev Walkin | 22b5455 | 2004-10-28 13:22:54 +0000 | [diff] [blame] | 529 | |
| 530 | if(opt_stack) { |
| 531 | s_codec_ctx.max_stack_size = opt_stack; |
| 532 | opt_codec_ctx = &s_codec_ctx; |
| 533 | } |
| 534 | |
Lev Walkin | bc69177 | 2006-09-17 11:02:53 +0000 | [diff] [blame] | 535 | DEBUG("Processing %s", name); |
Lev Walkin | 22b5455 | 2004-10-28 13:22:54 +0000 | [diff] [blame] | 536 | |
| 537 | /* prepare the file buffer */ |
| 538 | if(fbuf_size != suggested_bufsize) { |
Lev Walkin | 419f675 | 2006-09-13 04:02:00 +0000 | [diff] [blame] | 539 | fbuf = (char *)REALLOC(fbuf, suggested_bufsize); |
Lev Walkin | 22b5455 | 2004-10-28 13:22:54 +0000 | [diff] [blame] | 540 | if(!fbuf) { |
| 541 | perror("realloc()"); |
| 542 | exit(EX_OSERR); |
| 543 | } |
| 544 | fbuf_size = suggested_bufsize; |
| 545 | } |
| 546 | |
Lev Walkin | bc69177 | 2006-09-17 11:02:53 +0000 | [diff] [blame] | 547 | if(on_first_pdu) { |
| 548 | DynamicBuffer.offset = 0; |
| 549 | DynamicBuffer.length = 0; |
Lev Walkin | 5a621d6 | 2006-09-18 20:05:34 +0000 | [diff] [blame] | 550 | DynamicBuffer.unbits = 0; |
Lev Walkin | bc69177 | 2006-09-17 11:02:53 +0000 | [diff] [blame] | 551 | DynamicBuffer.allocated = 0; |
| 552 | DynamicBuffer.bytes_shifted = 0; |
| 553 | DynamicBuffer.nreallocs = 0; |
| 554 | } |
| 555 | |
| 556 | old_offset = DynamicBuffer.bytes_shifted + DynamicBuffer.offset; |
Lev Walkin | 22b5455 | 2004-10-28 13:22:54 +0000 | [diff] [blame] | 557 | |
Lev Walkin | d1bfea6 | 2005-11-08 03:06:16 +0000 | [diff] [blame] | 558 | /* Pretend immediate EOF */ |
| 559 | rval.code = RC_WMORE; |
| 560 | rval.consumed = 0; |
| 561 | |
Lev Walkin | 5a621d6 | 2006-09-18 20:05:34 +0000 | [diff] [blame] | 562 | for(tolerate_eof = 1; /* Allow EOF first time buffer is non-empty */ |
| 563 | (rd = fread(fbuf, 1, fbuf_size, file)) |
| 564 | || feof(file) == 0 |
| 565 | || (tolerate_eof && DynamicBuffer.length) |
| 566 | ;) { |
Lev Walkin | bc69177 | 2006-09-17 11:02:53 +0000 | [diff] [blame] | 567 | int ecbits = 0; /* Extra consumed bits in case of PER */ |
Lev Walkin | d1bfea6 | 2005-11-08 03:06:16 +0000 | [diff] [blame] | 568 | char *i_bptr; |
| 569 | size_t i_size; |
Lev Walkin | 22b5455 | 2004-10-28 13:22:54 +0000 | [diff] [blame] | 570 | |
| 571 | /* |
| 572 | * Copy the data over, or use the original buffer. |
| 573 | */ |
Lev Walkin | 419f675 | 2006-09-13 04:02:00 +0000 | [diff] [blame] | 574 | if(DynamicBuffer.allocated) { |
Lev Walkin | 5a621d6 | 2006-09-18 20:05:34 +0000 | [diff] [blame] | 575 | /* Append new data into the existing dynamic buffer */ |
Lev Walkin | 419f675 | 2006-09-13 04:02:00 +0000 | [diff] [blame] | 576 | add_bytes_to_buffer(fbuf, rd); |
| 577 | i_bptr = DynamicBuffer.data + DynamicBuffer.offset; |
Lev Walkin | f9492a9 | 2006-09-13 04:14:17 +0000 | [diff] [blame] | 578 | i_size = DynamicBuffer.length; |
Lev Walkin | 22b5455 | 2004-10-28 13:22:54 +0000 | [diff] [blame] | 579 | } else { |
Lev Walkin | d1bfea6 | 2005-11-08 03:06:16 +0000 | [diff] [blame] | 580 | i_bptr = fbuf; |
| 581 | i_size = rd; |
| 582 | } |
Lev Walkin | 22b5455 | 2004-10-28 13:22:54 +0000 | [diff] [blame] | 583 | |
Lev Walkin | bc69177 | 2006-09-17 11:02:53 +0000 | [diff] [blame] | 584 | DEBUG("Decoding %ld bytes", (long)i_size); |
| 585 | |
Lev Walkin | d1bfea6 | 2005-11-08 03:06:16 +0000 | [diff] [blame] | 586 | switch(iform) { |
| 587 | case INP_BER: |
Lev Walkin | 0094956 | 2005-03-10 13:39:03 +0000 | [diff] [blame] | 588 | rval = ber_decode(opt_codec_ctx, pduType, |
Lev Walkin | d1bfea6 | 2005-11-08 03:06:16 +0000 | [diff] [blame] | 589 | (void **)&structure, i_bptr, i_size); |
| 590 | break; |
| 591 | case INP_XER: |
| 592 | rval = xer_decode(opt_codec_ctx, pduType, |
| 593 | (void **)&structure, i_bptr, i_size); |
| 594 | break; |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 595 | case INP_PER: |
Lev Walkin | 1d9e8dd | 2005-12-07 05:46:03 +0000 | [diff] [blame] | 596 | rval = uper_decode(opt_codec_ctx, pduType, |
Lev Walkin | 5a621d6 | 2006-09-18 20:05:34 +0000 | [diff] [blame] | 597 | (void **)&structure, i_bptr, i_size, 0, |
| 598 | DynamicBuffer.unbits); |
Lev Walkin | bc69177 | 2006-09-17 11:02:53 +0000 | [diff] [blame] | 599 | ecbits = rval.consumed % 8; /* Extra bits */ |
Lev Walkin | bc69177 | 2006-09-17 11:02:53 +0000 | [diff] [blame] | 600 | rval.consumed /= 8; /* Convert to value in bytes! */ |
Lev Walkin | 5a621d6 | 2006-09-18 20:05:34 +0000 | [diff] [blame] | 601 | /* Check if input is byte-padded at the end */ |
| 602 | if(opt_ippad && ecbits && rval.code == RC_OK) { |
| 603 | rval.consumed++; |
| 604 | ecbits = 0; |
| 605 | } |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 606 | break; |
Lev Walkin | d1bfea6 | 2005-11-08 03:06:16 +0000 | [diff] [blame] | 607 | } |
Lev Walkin | 5a621d6 | 2006-09-18 20:05:34 +0000 | [diff] [blame] | 608 | DEBUG("decode(%ld) consumed %ld+%db (%ld), code %d", |
Lev Walkin | 419f675 | 2006-09-13 04:02:00 +0000 | [diff] [blame] | 609 | (long)DynamicBuffer.length, |
Lev Walkin | 4a858bb | 2006-09-17 11:22:00 +0000 | [diff] [blame] | 610 | (long)rval.consumed, ecbits, (long)i_size, |
Lev Walkin | 1d9e8dd | 2005-12-07 05:46:03 +0000 | [diff] [blame] | 611 | rval.code); |
Lev Walkin | 22b5455 | 2004-10-28 13:22:54 +0000 | [diff] [blame] | 612 | |
Lev Walkin | 419f675 | 2006-09-13 04:02:00 +0000 | [diff] [blame] | 613 | if(DynamicBuffer.allocated == 0) { |
Lev Walkin | 22b5455 | 2004-10-28 13:22:54 +0000 | [diff] [blame] | 614 | /* |
Lev Walkin | bc69177 | 2006-09-17 11:02:53 +0000 | [diff] [blame] | 615 | * Flush remainder into the intermediate buffer. |
Lev Walkin | 22b5455 | 2004-10-28 13:22:54 +0000 | [diff] [blame] | 616 | */ |
| 617 | if(rval.code != RC_FAIL && rval.consumed < rd) { |
Lev Walkin | 419f675 | 2006-09-13 04:02:00 +0000 | [diff] [blame] | 618 | add_bytes_to_buffer(fbuf + rval.consumed, |
Lev Walkin | 5a621d6 | 2006-09-18 20:05:34 +0000 | [diff] [blame] | 619 | rd - rval.consumed); |
| 620 | buffer_shift_left(0, ecbits); |
| 621 | DynamicBuffer.bytes_shifted = rval.consumed; |
Lev Walkin | 1d9e8dd | 2005-12-07 05:46:03 +0000 | [diff] [blame] | 622 | rval.consumed = 0; |
Lev Walkin | 5a621d6 | 2006-09-18 20:05:34 +0000 | [diff] [blame] | 623 | ecbits = 0; |
Lev Walkin | 22b5455 | 2004-10-28 13:22:54 +0000 | [diff] [blame] | 624 | } |
| 625 | } |
| 626 | |
Lev Walkin | bc69177 | 2006-09-17 11:02:53 +0000 | [diff] [blame] | 627 | /* |
| 628 | * Adjust position inside the source buffer. |
| 629 | */ |
| 630 | if(DynamicBuffer.allocated) { |
| 631 | DynamicBuffer.offset += rval.consumed; |
| 632 | DynamicBuffer.length -= rval.consumed; |
| 633 | } else { |
| 634 | DynamicBuffer.bytes_shifted += rval.consumed; |
| 635 | } |
| 636 | |
Lev Walkin | 22b5455 | 2004-10-28 13:22:54 +0000 | [diff] [blame] | 637 | switch(rval.code) { |
| 638 | case RC_OK: |
Lev Walkin | 5a621d6 | 2006-09-18 20:05:34 +0000 | [diff] [blame] | 639 | if(ecbits) buffer_shift_left(0, ecbits); |
| 640 | DEBUG("RC_OK, finishing up with %ld+%d", |
| 641 | (long)rval.consumed, ecbits); |
Lev Walkin | 22b5455 | 2004-10-28 13:22:54 +0000 | [diff] [blame] | 642 | return structure; |
| 643 | case RC_WMORE: |
Lev Walkin | 5a621d6 | 2006-09-18 20:05:34 +0000 | [diff] [blame] | 644 | DEBUG("RC_WMORE, continuing read=%ld, cons=%ld " |
| 645 | " with %ld..%ld-%ld..%ld", |
| 646 | (long)rd, |
Lev Walkin | f9492a9 | 2006-09-13 04:14:17 +0000 | [diff] [blame] | 647 | (long)rval.consumed, |
| 648 | (long)DynamicBuffer.offset, |
| 649 | (long)DynamicBuffer.length, |
Lev Walkin | 5a621d6 | 2006-09-18 20:05:34 +0000 | [diff] [blame] | 650 | (long)DynamicBuffer.unbits, |
Lev Walkin | f9492a9 | 2006-09-13 04:14:17 +0000 | [diff] [blame] | 651 | (long)DynamicBuffer.allocated); |
Lev Walkin | 5a621d6 | 2006-09-18 20:05:34 +0000 | [diff] [blame] | 652 | if(!rd) tolerate_eof--; |
Lev Walkin | 22b5455 | 2004-10-28 13:22:54 +0000 | [diff] [blame] | 653 | continue; |
| 654 | case RC_FAIL: |
| 655 | break; |
| 656 | } |
| 657 | break; |
| 658 | } |
| 659 | |
Lev Walkin | 22b5455 | 2004-10-28 13:22:54 +0000 | [diff] [blame] | 660 | /* Clean up partially decoded structure */ |
Lev Walkin | adcb586 | 2006-03-17 02:11:12 +0000 | [diff] [blame] | 661 | ASN_STRUCT_FREE(*pduType, structure); |
Lev Walkin | 22b5455 | 2004-10-28 13:22:54 +0000 | [diff] [blame] | 662 | |
Lev Walkin | bc69177 | 2006-09-17 11:02:53 +0000 | [diff] [blame] | 663 | new_offset = DynamicBuffer.bytes_shifted + DynamicBuffer.offset; |
| 664 | |
| 665 | /* |
| 666 | * Print a message and return failure only if not EOF, |
| 667 | * unless this is our first PDU (empty file). |
| 668 | */ |
Lev Walkin | 0091881 | 2006-09-18 21:19:32 +0000 | [diff] [blame] | 669 | if(on_first_pdu |
| 670 | || DynamicBuffer.length |
| 671 | || new_offset - old_offset > ((iform == INP_XER)?sizeof("\r\n")-1:0) |
| 672 | ) { |
Lev Walkin | 5a621d6 | 2006-09-18 20:05:34 +0000 | [diff] [blame] | 673 | DEBUG("ofp %d, no=%ld, oo=%ld, dbl=%ld", |
| 674 | on_first_pdu, (long)new_offset, (long)old_offset, |
| 675 | (long)DynamicBuffer.length); |
Lev Walkin | bc69177 | 2006-09-17 11:02:53 +0000 | [diff] [blame] | 676 | fprintf(stderr, "%s: " |
| 677 | "Decode failed past byte %ld: %s\n", |
| 678 | name, (long)new_offset, |
| 679 | (rval.code == RC_WMORE) |
| 680 | ? "Unexpected end of input" |
| 681 | : "Input processing error"); |
Lev Walkin | 4bf96b9 | 2006-09-18 21:46:50 +0000 | [diff] [blame] | 682 | #ifndef ENOMSG |
| 683 | #define ENOMSG EINVAL |
| 684 | #endif |
| 685 | #ifndef EBADMSG |
| 686 | #define EBADMSG EINVAL |
| 687 | #endif |
Lev Walkin | bc69177 | 2006-09-17 11:02:53 +0000 | [diff] [blame] | 688 | errno = (rval.code == RC_WMORE) ? ENOMSG : EBADMSG; |
| 689 | } else { |
Lev Walkin | 5a621d6 | 2006-09-18 20:05:34 +0000 | [diff] [blame] | 690 | /* Got EOF after a few successful PDUs */ |
Lev Walkin | bc69177 | 2006-09-17 11:02:53 +0000 | [diff] [blame] | 691 | errno = 0; |
| 692 | } |
Lev Walkin | 22b5455 | 2004-10-28 13:22:54 +0000 | [diff] [blame] | 693 | |
| 694 | return 0; |
| 695 | } |
| 696 | |
Lev Walkin | 419f675 | 2006-09-13 04:02:00 +0000 | [diff] [blame] | 697 | /* Dump the buffer out to the specified FILE */ |
| 698 | static int write_out(const void *buffer, size_t size, void *key) { |
| 699 | FILE *fp = (FILE *)key; |
| 700 | return (fwrite(buffer, 1, size, fp) == size) ? 0 : -1; |
| 701 | } |
Lev Walkin | c744a02 | 2006-09-15 18:33:25 +0000 | [diff] [blame] | 702 | |
| 703 | static int argument_is_stdin(char *av[], int idx) { |
| 704 | if(strcmp(av[idx], "-")) { |
| 705 | return 0; /* Certainly not <stdin> */ |
| 706 | } else { |
| 707 | /* This might be <stdin>, unless `./program -- -` */ |
| 708 | if(strcmp(av[-1], "--")) |
| 709 | return 1; |
| 710 | else |
| 711 | return 0; |
| 712 | } |
| 713 | } |
| 714 | |
| 715 | static FILE *argument_to_file(char *av[], int idx) { |
| 716 | return argument_is_stdin(av, idx) |
| 717 | ? stdin |
| 718 | : fopen(av[idx], "r"); |
| 719 | } |
| 720 | |
| 721 | static char *argument_to_name(char *av[], int idx) { |
| 722 | return argument_is_stdin(av, idx) |
| 723 | ? "standard input" |
| 724 | : av[idx]; |
| 725 | } |