Changeset 12192 in vbox for trunk/src/bldprogs
- Timestamp:
- Sep 8, 2008 1:54:10 AM (17 years ago)
- svn:sync-xref-src-repo-rev:
- 36166
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/bldprogs/bin2c.c
r8155 r12192 1 1 /* $Id$ */ 2 2 /** @file 3 * PC-BIOS- Binary 2 C Structure Converter.3 * bin2c - Binary 2 C Structure Converter. 4 4 */ 5 5 … … 52 52 } 53 53 54 static int usage(const char *argv0) 55 { 56 fprintf(stderr, 57 "Syntax: %s [options] <arrayname> <binaryfile> <outname>\n" 58 " -min <n> check if <binaryfile> is not smaller than <n>KB\n" 59 " -max <n> check if <binaryfile> is not bigger than <n>KB\n" 60 " -mask <n> check if size of binaryfile is <n>-aligned\n" 61 " -ascii show ASCII representation of binary as comment\n", 62 argv0); 63 64 return 1; 65 } 54 66 55 67 int main(int argc, char *argv[]) … … 67 79 size_t cbRead; 68 80 size_t cbBin; 81 int rc = 1; /* assume the worst... */ 69 82 70 83 if (argc < 2) 71 goto syntax_error;84 return usage(argv[0]); 72 85 73 86 for (i=1; i<argc; i++) … … 75 88 if (!strcmp(argv[i], "-min")) 76 89 { 77 if (++i >=argc)78 goto syntax_error;90 if (++i >= argc) 91 return usage(argv[0]); 79 92 cbMin = 1024 * strtoul(argv[i], NULL, 0); 80 continue;81 93 } 82 94 else if (!strcmp(argv[i], "-max")) 83 95 { 84 if (++i >=argc)85 goto syntax_error;96 if (++i >= argc) 97 return usage(argv[0]); 86 98 cbMax = 1024 * strtoul(argv[i], NULL, 0); 87 continue;88 99 } 89 100 else if (!strcmp(argv[i], "-mask")) 90 101 { 91 if (++i >=argc)92 goto syntax_error;102 if (++i >= argc) 103 return usage(argv[0]); 93 104 uMask = strtoul(argv[i], NULL, 0); 94 continue;95 105 } 96 106 else if (!strcmp(argv[i], "-ascii")) 97 107 { 98 108 fAscii = 1; 99 continue;100 109 } 101 110 else if (!strcmp(argv[i], "-export")) 102 111 { 103 112 fExport = 1; 104 continue; 105 } 106 else if (i==argc-3) 113 } 114 else if (i == argc - 3) 107 115 break; 108 109 syntax_error: 110 fprintf(stderr, 111 "Syntax: %s [options] <arrayname> <binaryfile> <outname>\n" 112 " -min <n> check if <binaryfile> is not smaller than <n>KB\n" 113 " -max <n> check if <binaryfile> is not bigger than <n>KB\n" 114 " -mask <n> check if size of binaryfile is <n>-aligned\n" 115 " -ascii show ASCII representation of binary as comment\n", 116 argv[0]); 117 return 1; 116 else 117 { 118 fprintf(stderr, "%s: syntax error: Unknown argument '%s'\n", 119 argv[0], argv[i]); 120 return usage(argv[0]); 121 } 118 122 } 119 123 … … 150 154 /* check size restrictions */ 151 155 if (uMask && (cbBin & uMask)) 152 {153 156 fprintf(stderr, "%s: size=%ld - Not aligned!\n", argv[0], (long)cbBin); 154 return 1; 155 } 156 if (cbBin < cbMin || cbBin > cbMax) 157 { 157 else if (cbBin < cbMin || cbBin > cbMax) 158 158 fprintf(stderr, "%s: size=%ld - Not %ld-%ldb in size!\n", 159 159 argv[0], (long)cbBin, (long)cbMin, (long)cbMax); 160 return 1; 161 } 162 163 /* the binary data */ 164 off = 0; 165 while ((cbRead = fread(&abLine[0], 1, sizeof(abLine), pFileIn)) > 0) 166 { 167 size_t i; 168 fprintf(pFileOut, " "); 169 for (i = 0; i < cbRead; i++) 170 fprintf(pFileOut, " 0x%02x,", abLine[i]); 171 for (; i < sizeof(abLine); i++) 172 fprintf(pFileOut, " "); 173 if (fAscii) 174 { 175 fprintf(pFileOut, " /* 0x%08lx: ", (long)off); 160 else 161 { 162 /* the binary data */ 163 off = 0; 164 while ((cbRead = fread(&abLine[0], 1, sizeof(abLine), pFileIn)) > 0) 165 { 166 size_t i; 167 fprintf(pFileOut, " "); 176 168 for (i = 0; i < cbRead; i++) 177 /* be careful with '/' prefixed/followed by a '*'! */ 178 fprintf(pFileOut, "%c", 179 isprint(abLine[i]) && abLine[i] != '/' ? abLine[i] : '.'); 169 fprintf(pFileOut, " 0x%02x,", abLine[i]); 180 170 for (; i < sizeof(abLine); i++) 181 fprintf(pFileOut, " "); 182 fprintf(pFileOut, " */"); 183 } 184 fprintf(pFileOut, "\n"); 185 186 off += cbRead; 187 } 188 189 /* check for errors */ 190 if (ferror(pFileIn) && !feof(pFileIn)) 191 { 192 fprintf(stderr, "%s: read error\n", argv[0]); 193 goto error; 194 } 195 if (off != cbBin) 196 { 197 fprintf(stderr, "%s: read error off=%ld cbBin=%ld\n", argv[0], (long)off, (long)cbBin); 198 goto error; 199 } 200 201 /* finish the structure. */ 202 fprintf(pFileOut, 203 "};\n" 204 "\n" 205 "%sconst unsigned%s g_cb%s = sizeof(g_ab%s);\n" 206 "/* end of file */\n", 207 fExport ? "DECLEXPORT(" : "", fExport ? ")" : "", argv[i], argv[i]); 171 fprintf(pFileOut, " "); 172 if (fAscii) 173 { 174 fprintf(pFileOut, " /* 0x%08lx: ", (long)off); 175 for (i = 0; i < cbRead; i++) 176 /* be careful with '/' prefixed/followed by a '*'! */ 177 fprintf(pFileOut, "%c", 178 isprint(abLine[i]) && abLine[i] != '/' ? abLine[i] : '.'); 179 for (; i < sizeof(abLine); i++) 180 fprintf(pFileOut, " "); 181 fprintf(pFileOut, " */"); 182 } 183 fprintf(pFileOut, "\n"); 184 185 off += cbRead; 186 } 187 188 /* check for errors */ 189 if (ferror(pFileIn) && !feof(pFileIn)) 190 fprintf(stderr, "%s: read error\n", argv[0]); 191 else if (off != cbBin) 192 fprintf(stderr, "%s: read error off=%ld cbBin=%ld\n", argv[0], (long)off, (long)cbBin); 193 else 194 { 195 /* no errors, finish the structure. */ 196 fprintf(pFileOut, 197 "};\n" 198 "\n" 199 "%sconst unsigned%s g_cb%s = sizeof(g_ab%s);\n" 200 "/* end of file */\n", 201 fExport ? "DECLEXPORT(" : "", fExport ? ")" : "", argv[i], argv[i]); 202 203 /* flush output and check for error. */ 204 fflush(pFileOut); 205 if (ferror(pFileOut)) 206 fprintf(stderr, "%s: write error\n", argv[0]); 207 else 208 rc = 0; /* success! */ 209 } 210 } 211 212 /* cleanup, delete the output file on failure. */ 213 fclose(pFileOut); 208 214 fclose(pFileIn); 209 210 /* flush output and check for error. */ 211 fflush(pFileOut); 212 if (ferror(pFileOut)) 213 { 214 fprintf(stderr, "%s: write error\n", argv[0]); 215 goto error; 216 } 217 fclose(pFileOut); 218 219 return 0; 220 221 error: 222 fclose(pFileOut); 223 remove(argv[i+2]); 224 return 1; 215 if (rc) 216 remove(argv[i+2]); 217 218 return rc; 225 219 }
Note:
See TracChangeset
for help on using the changeset viewer.