Changeset 19345 in vbox
- Timestamp:
- May 4, 2009 11:25:10 PM (16 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/bldprogs/bin2c.c
r17692 r19345 59 59 " -max <n> check if <binaryfile> is not bigger than <n>KB\n" 60 60 " -mask <n> check if size of binaryfile is <n>-aligned\n" 61 " -width <n> number of bytes per line (default: 16)\n" 62 " -break <n> break every <n> lines (default: -1)\n" 61 63 " -ascii show ASCII representation of binary as comment\n", 62 64 argv0); … … 75 77 int fAscii = 0; 76 78 int fExport = 0; 77 unsigned char abLine[16]; 79 long iBreakEvery = -1; 80 unsigned char abLine[32]; 81 size_t cbLine = 16; 78 82 size_t off; 79 83 size_t cbRead; … … 84 88 return usage(argv[0]); 85 89 86 for (i =1; i<argc; i++)90 for (i = 1; i < argc; i++) 87 91 { 88 92 if (!strcmp(argv[i], "-min")) … … 111 115 { 112 116 fExport = 1; 117 } 118 else if (!strcmp(argv[i], "-width")) 119 { 120 if (++i >= argc) 121 return usage(argv[0]); 122 cbLine = strtoul(argv[i], NULL, 0); 123 if (cbLine == 0 || cbLine > sizeof(abLine)) 124 { 125 fprintf(stderr, "%s: '%s' is too wide, max %u\n", 126 argv[0], argv[i], (unsigned)sizeof(abLine)); 127 return 1; 128 } 129 } 130 else if (!strcmp(argv[i], "-break")) 131 { 132 if (++i >= argc) 133 return usage(argv[0]); 134 iBreakEvery = strtol(argv[i], NULL, 0); 135 if (iBreakEvery <= 0 && iBreakEvery != -1) 136 { 137 fprintf(stderr, "%s: -break value '%s' is not >= 1 or -1.\n", 138 argv[0], argv[i], (unsigned)sizeof(abLine)); 139 return 1; 140 } 113 141 } 114 142 else if (i == argc - 3) … … 116 144 else 117 145 { 118 fprintf(stderr, "%s: syntax error: Unknown argument '%s'\n", 146 fprintf(stderr, "%s: syntax error: Unknown argument '%s'\n", 119 147 argv[0], argv[i]); 120 148 return usage(argv[0]); … … 162 190 /* the binary data */ 163 191 off = 0; 164 while ((cbRead = fread(&abLine[0], 1, sizeof(abLine), pFileIn)) > 0)192 while ((cbRead = fread(&abLine[0], 1, cbLine, pFileIn)) > 0) 165 193 { 166 194 size_t i; 195 196 if ( iBreakEvery > 0 197 && off 198 && (off / cbLine) % iBreakEvery == 0) 199 fprintf(pFileOut, "\n"); 200 167 201 fprintf(pFileOut, " "); 168 202 for (i = 0; i < cbRead; i++) 169 203 fprintf(pFileOut, " 0x%02x,", abLine[i]); 170 for (; i < sizeof(abLine); i++)204 for (; i < cbLine; i++) 171 205 fprintf(pFileOut, " "); 172 206 if (fAscii) … … 175 209 for (i = 0; i < cbRead; i++) 176 210 /* be careful with '/' prefixed/followed by a '*'! */ 177 fprintf(pFileOut, "%c", 211 fprintf(pFileOut, "%c", 178 212 isprint(abLine[i]) && abLine[i] != '/' ? abLine[i] : '.'); 179 for (; i < sizeof(abLine); i++)213 for (; i < cbLine; i++) 180 214 fprintf(pFileOut, " "); 181 215 fprintf(pFileOut, " */"); 182 216 } 183 217 fprintf(pFileOut, "\n"); 184 218 185 219 off += cbRead; 186 220 } 187 221 188 222 /* check for errors */ 189 223 if (ferror(pFileIn) && !feof(pFileIn)) … … 200 234 "/* end of file */\n", 201 235 fExport ? "DECLEXPORT(" : "", fExport ? ")" : "", argv[i], argv[i]); 202 236 203 237 /* flush output and check for error. */ 204 238 fflush(pFileOut);
Note:
See TracChangeset
for help on using the changeset viewer.