Lev Walkin | 1e609cf | 2004-09-27 20:50:48 +0000 | [diff] [blame] | 1 | #undef NDEBUG |
| 2 | #include <stdio.h> |
| 3 | #include <stdlib.h> |
| 4 | #include <sys/types.h> |
Lev Walkin | e0d321a | 2014-09-11 01:28:57 -0700 | [diff] [blame] | 5 | #include <unistd.h> /* for chdir(2), getcwd(3) */ |
Lev Walkin | 1e609cf | 2004-09-27 20:50:48 +0000 | [diff] [blame] | 6 | #include <string.h> |
| 7 | #include <dirent.h> |
| 8 | #include <assert.h> |
| 9 | #include <errno.h> |
| 10 | |
| 11 | #include <T.h> |
| 12 | |
Lev Walkin | e0d321a | 2014-09-11 01:28:57 -0700 | [diff] [blame] | 13 | #ifndef SRCDIR |
| 14 | #define SRCDIR_S ".." |
| 15 | #else |
| 16 | #define STRINGIFY_MACRO2(x) #x |
| 17 | #define STRINGIFY_MACRO(x) STRINGIFY_MACRO2(x) |
| 18 | #define SRCDIR_S STRINGIFY_MACRO(SRCDIR) |
| 19 | #endif |
| 20 | |
Lev Walkin | 1e609cf | 2004-09-27 20:50:48 +0000 | [diff] [blame] | 21 | enum expectation { |
| 22 | EXP_OK, /* Encoding/decoding must succeed */ |
| 23 | EXP_BROKEN, /* Decoding must fail */ |
| 24 | EXP_RECLESS, /* Reconstruction is allowed to yield less data */ |
Lev Walkin | c536460 | 2004-10-05 06:38:38 +0000 | [diff] [blame] | 25 | EXP_DIFFERENT, /* Reconstruction will yield different encoding */ |
Lev Walkin | 1e609cf | 2004-09-27 20:50:48 +0000 | [diff] [blame] | 26 | }; |
| 27 | |
| 28 | static unsigned char buf[4096]; |
| 29 | static int buf_offset; |
| 30 | |
| 31 | static int |
| 32 | _buf_writer(const void *buffer, size_t size, void *app_key) { |
| 33 | unsigned char *b, *bend; |
| 34 | (void)app_key; |
| 35 | assert(buf_offset + size < sizeof(buf)); |
| 36 | memcpy(buf + buf_offset, buffer, size); |
| 37 | b = buf + buf_offset; |
| 38 | bend = b + size; |
| 39 | printf("=> ["); |
| 40 | for(; b < bend; b++) |
| 41 | printf(" %02X", *b); |
Lev Walkin | 1715b32 | 2013-03-28 04:02:13 -0700 | [diff] [blame] | 42 | printf("]:%zd\n", size); |
Lev Walkin | 1e609cf | 2004-09-27 20:50:48 +0000 | [diff] [blame] | 43 | buf_offset += size; |
| 44 | return 0; |
| 45 | } |
| 46 | |
| 47 | static int |
| 48 | save_object(T_t *st) { |
| 49 | asn_enc_rval_t rval; /* Return value */ |
| 50 | |
| 51 | buf_offset = 0; |
| 52 | |
Lev Walkin | c7400c5 | 2004-09-29 13:14:36 +0000 | [diff] [blame] | 53 | rval = der_encode(&asn_DEF_T, st, _buf_writer, 0); |
Lev Walkin | 1e609cf | 2004-09-27 20:50:48 +0000 | [diff] [blame] | 54 | if (rval.encoded == -1) { |
| 55 | fprintf(stderr, |
| 56 | "Cannot encode %s: %s\n", |
| 57 | rval.failed_type->name, strerror(errno)); |
| 58 | assert(rval.encoded != -1); |
| 59 | return -1; /* JIC */ |
| 60 | } |
| 61 | |
| 62 | fprintf(stderr, "SAVED OBJECT IN SIZE %d\n", buf_offset); |
| 63 | |
| 64 | return 0; |
| 65 | } |
| 66 | |
| 67 | static T_t * |
Lev Walkin | 5b63acf | 2014-01-14 01:48:37 -0800 | [diff] [blame] | 68 | load_object(enum expectation expectation, unsigned char *fbuf, size_t size) { |
Lev Walkin | 90bf7ae | 2004-10-20 15:46:56 +0000 | [diff] [blame] | 69 | asn_dec_rval_t rval; |
Lev Walkin | 1e609cf | 2004-09-27 20:50:48 +0000 | [diff] [blame] | 70 | T_t *st = 0; |
| 71 | int csize; |
| 72 | |
Lev Walkin | 5b63acf | 2014-01-14 01:48:37 -0800 | [diff] [blame] | 73 | fprintf(stderr, "LOADING OBJECT OF SIZE %d\n", (int)size); |
Lev Walkin | 1e609cf | 2004-09-27 20:50:48 +0000 | [diff] [blame] | 74 | |
| 75 | /* Perform multiple iterations with multiple chunks sizes */ |
| 76 | for(csize = 1; csize < 20; csize += 1) { |
| 77 | int fbuf_offset = 0; |
| 78 | int fbuf_left = size; |
| 79 | int fbuf_chunk = csize; |
| 80 | |
Bi-Ruei, Chiu | 1f87ac0 | 2017-08-20 01:25:45 +0800 | [diff] [blame] | 81 | if(st) ASN_STRUCT_FREE(asn_DEF_T, st); |
Lev Walkin | 1e609cf | 2004-09-27 20:50:48 +0000 | [diff] [blame] | 82 | st = 0; |
| 83 | |
| 84 | do { |
| 85 | fprintf(stderr, "Decoding from %d with %d (left %d)\n", |
| 86 | fbuf_offset, fbuf_chunk, fbuf_left); |
Lev Walkin | c7400c5 | 2004-09-29 13:14:36 +0000 | [diff] [blame] | 87 | rval = ber_decode(0, &asn_DEF_T, (void **)&st, |
Lev Walkin | 1e609cf | 2004-09-27 20:50:48 +0000 | [diff] [blame] | 88 | fbuf + fbuf_offset, |
| 89 | fbuf_chunk < fbuf_left |
| 90 | ? fbuf_chunk : fbuf_left); |
| 91 | fbuf_offset += rval.consumed; |
| 92 | fbuf_left -= rval.consumed; |
| 93 | if(rval.code == RC_WMORE) |
| 94 | fbuf_chunk += 1; /* Give little more */ |
| 95 | else |
| 96 | fbuf_chunk = csize; /* Back off */ |
| 97 | } while(fbuf_left && rval.code == RC_WMORE); |
| 98 | |
| 99 | if(expectation != EXP_BROKEN) { |
| 100 | assert(rval.code == RC_OK); |
Lev Walkin | 5b63acf | 2014-01-14 01:48:37 -0800 | [diff] [blame] | 101 | assert(fbuf_offset == (ssize_t)size); |
Lev Walkin | 1e609cf | 2004-09-27 20:50:48 +0000 | [diff] [blame] | 102 | } else { |
| 103 | assert(rval.code != RC_OK); |
| 104 | fprintf(stderr, "Failed, but this was expected\n"); |
Bi-Ruei, Chiu | 1f87ac0 | 2017-08-20 01:25:45 +0800 | [diff] [blame] | 105 | ASN_STRUCT_FREE(asn_DEF_T, st); |
Lev Walkin | 1e609cf | 2004-09-27 20:50:48 +0000 | [diff] [blame] | 106 | st = 0; /* ignore leak for now */ |
| 107 | } |
| 108 | } |
| 109 | |
Lev Walkin | c7400c5 | 2004-09-29 13:14:36 +0000 | [diff] [blame] | 110 | if(st) asn_fprint(stderr, &asn_DEF_T, st); |
Lev Walkin | 1e609cf | 2004-09-27 20:50:48 +0000 | [diff] [blame] | 111 | return st; |
| 112 | } |
| 113 | |
| 114 | |
| 115 | static void |
Lev Walkin | 5b63acf | 2014-01-14 01:48:37 -0800 | [diff] [blame] | 116 | process_data(enum expectation expectation, unsigned char *fbuf, ssize_t size) { |
Lev Walkin | 1e609cf | 2004-09-27 20:50:48 +0000 | [diff] [blame] | 117 | T_t *st; |
| 118 | int ret; |
| 119 | |
| 120 | st = load_object(expectation, fbuf, size); |
| 121 | if(!st) return; |
| 122 | |
| 123 | ret = save_object(st); |
Lev Walkin | 5b63acf | 2014-01-14 01:48:37 -0800 | [diff] [blame] | 124 | assert(buf_offset < (ssize_t)sizeof(buf)); |
Lev Walkin | 1e609cf | 2004-09-27 20:50:48 +0000 | [diff] [blame] | 125 | assert(ret == 0); |
| 126 | |
Lev Walkin | c536460 | 2004-10-05 06:38:38 +0000 | [diff] [blame] | 127 | switch(expectation) { |
| 128 | case EXP_RECLESS: |
Lev Walkin | 1e609cf | 2004-09-27 20:50:48 +0000 | [diff] [blame] | 129 | assert(buf_offset > 0 && buf_offset < size); |
| 130 | assert(memcmp(buf + 2, fbuf + 2, buf_offset - 2) == 0); |
Lev Walkin | c536460 | 2004-10-05 06:38:38 +0000 | [diff] [blame] | 131 | break; |
| 132 | case EXP_DIFFERENT: |
| 133 | assert(buf_offset > 0 && buf_offset < size); |
| 134 | break; |
| 135 | case EXP_BROKEN: |
Lev Walkin | b5ce57f | 2005-02-18 09:41:13 +0000 | [diff] [blame] | 136 | assert(buf_offset != size |
| 137 | || memcmp(buf, fbuf, buf_offset)); |
Lev Walkin | c536460 | 2004-10-05 06:38:38 +0000 | [diff] [blame] | 138 | break; |
Lev Walkin | 1c83003 | 2004-10-28 13:24:27 +0000 | [diff] [blame] | 139 | case EXP_OK: |
Lev Walkin | 5b63acf | 2014-01-14 01:48:37 -0800 | [diff] [blame] | 140 | assert(buf_offset == (ssize_t)size); |
Lev Walkin | b5ce57f | 2005-02-18 09:41:13 +0000 | [diff] [blame] | 141 | assert(memcmp(buf, fbuf, buf_offset) == 0); |
Lev Walkin | 1c83003 | 2004-10-28 13:24:27 +0000 | [diff] [blame] | 142 | break; |
Lev Walkin | 1e609cf | 2004-09-27 20:50:48 +0000 | [diff] [blame] | 143 | } |
| 144 | |
Bi-Ruei, Chiu | 1f87ac0 | 2017-08-20 01:25:45 +0800 | [diff] [blame] | 145 | ASN_STRUCT_FREE(asn_DEF_T, st); |
Lev Walkin | 1e609cf | 2004-09-27 20:50:48 +0000 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | /* |
| 149 | * Decode the .der files and try to regenerate them. |
| 150 | */ |
| 151 | static int |
| 152 | process(const char *fname) { |
Lev Walkin | e0d321a | 2014-09-11 01:28:57 -0700 | [diff] [blame] | 153 | char prevdir[256]; |
Lev Walkin | 5b63acf | 2014-01-14 01:48:37 -0800 | [diff] [blame] | 154 | unsigned char fbuf[4096]; |
Lev Walkin | 1e609cf | 2004-09-27 20:50:48 +0000 | [diff] [blame] | 155 | char *ext = strrchr(fname, '.'); |
| 156 | enum expectation expectation; |
| 157 | int ret; |
| 158 | int rd; |
| 159 | FILE *fp; |
| 160 | |
| 161 | if(ext == 0 || strcmp(ext, ".ber")) |
| 162 | return 0; |
| 163 | |
| 164 | switch(ext[-1]) { |
| 165 | case 'B': /* The file is intentionally broken */ |
| 166 | expectation = EXP_BROKEN; break; |
Lev Walkin | c536460 | 2004-10-05 06:38:38 +0000 | [diff] [blame] | 167 | case 'D': /* Reconstructing should yield different data */ |
| 168 | expectation = EXP_DIFFERENT; break; |
Lev Walkin | 1e609cf | 2004-09-27 20:50:48 +0000 | [diff] [blame] | 169 | case 'L': /* Extensions are present */ |
| 170 | expectation = EXP_RECLESS; break; |
| 171 | default: |
| 172 | expectation = EXP_OK; break; |
| 173 | } |
| 174 | |
| 175 | fprintf(stderr, "\nProcessing file [../%s]\n", fname); |
| 176 | |
Lev Walkin | e0d321a | 2014-09-11 01:28:57 -0700 | [diff] [blame] | 177 | getcwd(prevdir, sizeof(prevdir)); |
| 178 | ret = chdir(SRCDIR_S "/data-62"); |
Lev Walkin | 1e609cf | 2004-09-27 20:50:48 +0000 | [diff] [blame] | 179 | assert(ret == 0); |
| 180 | fp = fopen(fname, "r"); |
Lev Walkin | e0d321a | 2014-09-11 01:28:57 -0700 | [diff] [blame] | 181 | ret = chdir(prevdir); |
Lev Walkin | 1e609cf | 2004-09-27 20:50:48 +0000 | [diff] [blame] | 182 | assert(ret == 0); |
| 183 | assert(fp); |
| 184 | |
| 185 | rd = fread(fbuf, 1, sizeof(fbuf), fp); |
| 186 | fclose(fp); |
| 187 | |
Lev Walkin | 5b63acf | 2014-01-14 01:48:37 -0800 | [diff] [blame] | 188 | assert(rd < (ssize_t)sizeof(fbuf)); /* expect small files */ |
Lev Walkin | 1e609cf | 2004-09-27 20:50:48 +0000 | [diff] [blame] | 189 | |
| 190 | process_data(expectation, fbuf, rd); |
| 191 | |
| 192 | return 1; |
| 193 | } |
| 194 | |
| 195 | int |
| 196 | main() { |
| 197 | DIR *dir; |
| 198 | struct dirent *dent; |
| 199 | int processed_files = 0; |
Lev Walkin | c536460 | 2004-10-05 06:38:38 +0000 | [diff] [blame] | 200 | char *str; |
Lev Walkin | 1e609cf | 2004-09-27 20:50:48 +0000 | [diff] [blame] | 201 | |
Lev Walkin | e0d321a | 2014-09-11 01:28:57 -0700 | [diff] [blame] | 202 | dir = opendir(SRCDIR_S "/data-62"); |
Lev Walkin | 1e609cf | 2004-09-27 20:50:48 +0000 | [diff] [blame] | 203 | assert(dir); |
| 204 | |
Lev Walkin | c536460 | 2004-10-05 06:38:38 +0000 | [diff] [blame] | 205 | str = getenv("DATA_62_FILE"); |
| 206 | if(str && strncmp(str, "data-62-", 8) == 0) |
| 207 | process(str); |
Lev Walkin | 1e609cf | 2004-09-27 20:50:48 +0000 | [diff] [blame] | 208 | |
Lev Walkin | c536460 | 2004-10-05 06:38:38 +0000 | [diff] [blame] | 209 | while((dent = readdir(dir))) { |
| 210 | if(strncmp(dent->d_name, "data-62-", 8) == 0) |
| 211 | if(process(dent->d_name)) |
| 212 | processed_files++; |
Lev Walkin | 1e609cf | 2004-09-27 20:50:48 +0000 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | assert(processed_files); |
| 216 | closedir(dir); |
| 217 | |
| 218 | return 0; |
| 219 | } |
| 220 | |