ttbin2hex.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. /******************************************************************************
  2. *
  3. * project name: TI-68k Developer Utilities
  4. * file name: ttbin2hex.c
  5. * initial date: 13/08/2000
  6. * author: thomas.nussbaumer@gmx.net
  7. * description: converts binary file to C hex dump
  8. *
  9. ******************************************************************************/
  10. /*
  11. This file is part of TI-68k Developer Utilities.
  12. This file is free software; you can redistribute it and/or
  13. modify it under the terms of the GNU Lesser General Public
  14. License as published by the Free Software Foundation; either
  15. version 2.1 of the License, or (at your option) any later version.
  16. As a special exception, UNMODIFIED copies of ttbin2hex may also be
  17. redistributed or sold without source code, for any purpose. (The Lesser
  18. General Public License restrictions do apply in other respects; for example,
  19. they cover modification of the program.) This exception notice must be
  20. removed on modified copies of this file.
  21. This program is distributed in the hope that it will be useful,
  22. but WITHOUT ANY WARRANTY; without even the implied warranty of
  23. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  24. Lesser General Public License for more details.
  25. You should have received a copy of the GNU Lesser General Public
  26. License along with this library; if not, write to the Free Software
  27. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  28. */
  29. #include <stdio.h>
  30. #include <string.h>
  31. #include <stdlib.h>
  32. #include "ttversion.h"
  33. #include "revtools.h"
  34. #include "tt.h"
  35. #ifdef FILE_REVISION
  36. #undef FILE_REVISION
  37. #endif
  38. #define FILE_REVISION "1.10"
  39. //=============================================================================
  40. // outputs usage information of this tool
  41. //=============================================================================
  42. void PrintUsage() {
  43. PRINT_ID("TTBin2Hex");
  44. fprintf(USAGE_OUT, "Usage: ttbin2hex [flags] <infile> <outfile>\n\n" \
  45. " -quiet .... don't output standard messages\n" \
  46. " -slash .... append backslash at end of line\n" \
  47. " -c <count> .... number of items per line\n" \
  48. " -b1 .... generate unsigned char array\n" \
  49. " -b2 .... generate unsigned int array\n" \
  50. " -b4 .... generate unsigned long array\n" \
  51. " -a <name> .... generate an array with given name\n" \
  52. " -ss <nr> .... skip number of bytes at begin of inputfile\n"\
  53. " -se <nr> .... skip number of bytes at end of inputfile\n"\
  54. " -lb <nr> .... insert an additionally linebreak every nr items\n"\
  55. " infile .... name of infile\n" \
  56. " outfile .... name of outfile (use \"-\" for stdout)\n\n" \
  57. " converts binary file to C array data\n\n");
  58. }
  59. //=============================================================================
  60. // ... another "just a main routine" tool ...
  61. //=============================================================================
  62. int main(int argc,char *argv[]) {
  63. char* infile = NULL;
  64. char* outfile = NULL;
  65. FILE* ifp;
  66. FILE* ofp;
  67. int bytes_per_item = 1;
  68. int items_per_line = DEFAULT_ITEMS_PER_LINE;
  69. int n;
  70. int data[4];
  71. int appendslash = 0;
  72. long insize;
  73. long bytes_processed;
  74. long loop;
  75. long itemcount;
  76. char* arrayname = NULL;
  77. int quiet = 0;
  78. int skip_start = 0;
  79. int skip_end = 0;
  80. int empty_line = 0;
  81. // check for too less arguments
  82. if (argc < 3) {
  83. PrintUsage();
  84. return 1;
  85. }
  86. // parse arguments
  87. for (n=1; n<argc; n++) {
  88. if (!strcmp(argv[n], "-slash")) appendslash = 1;
  89. else if (!strcmp(argv[n], "-quiet")) quiet = 1;
  90. else if (!strcmp(argv[n], "-b1")) bytes_per_item = 1;
  91. else if (!strcmp(argv[n], "-b2")) bytes_per_item = 2;
  92. else if (!strcmp(argv[n], "-b4")) bytes_per_item = 4;
  93. else if (!strcmp(argv[n], "-c")) {
  94. if (n == argc -1) {
  95. PrintUsage();
  96. return 1;
  97. }
  98. else {
  99. if (sscanf(argv[n+1],"%d",&items_per_line) != 1) {
  100. PrintUsage();
  101. return 1;
  102. }
  103. if (items_per_line < 1 || items_per_line > 200) {
  104. fprintf(stderr,"ERROR: invalid size for items per line (must be >=1 and <=200)\n");
  105. return 1;
  106. }
  107. n++;
  108. }
  109. }
  110. else if (!strcmp(argv[n], "-ss")) {
  111. if (n == argc -1) {
  112. PrintUsage();
  113. return 1;
  114. }
  115. else {
  116. if (sscanf(argv[n+1],"%d",&skip_start) != 1) {
  117. PrintUsage();
  118. return 1;
  119. }
  120. if (skip_start < 0) {
  121. fprintf(stderr,"ERROR: invalid count for option -ss\n");
  122. return 1;
  123. }
  124. n++;
  125. }
  126. }
  127. else if (!strcmp(argv[n], "-se")) {
  128. if (n == argc -1) {
  129. PrintUsage();
  130. return 1;
  131. }
  132. else {
  133. if (sscanf(argv[n+1],"%d",&skip_end) != 1) {
  134. PrintUsage();
  135. return 1;
  136. }
  137. if (skip_end < 0) {
  138. fprintf(stderr,"ERROR: invalid count for option -se\n");
  139. return 1;
  140. }
  141. n++;
  142. }
  143. }
  144. else if (!strcmp(argv[n], "-a")) {
  145. if (n == argc -1) {
  146. PrintUsage();
  147. return 1;
  148. }
  149. else {
  150. arrayname = argv[n+1];
  151. n++;
  152. }
  153. }
  154. else if (!strcmp(argv[n], "-lb")) {
  155. if (n == argc -1) {
  156. PrintUsage();
  157. return 1;
  158. }
  159. else {
  160. if (sscanf(argv[n+1],"%d",&empty_line) != 1) {
  161. PrintUsage();
  162. return 1;
  163. }
  164. n++;
  165. }
  166. }
  167. else if (!infile) infile = argv[n];
  168. else if (!outfile) outfile = argv[n];
  169. else {
  170. PrintUsage();
  171. return 1;
  172. }
  173. }
  174. if (!infile || !outfile) {
  175. PrintUsage();
  176. return 1;
  177. }
  178. if (!(ifp = fopen(infile,"rb"))) {
  179. fprintf(stderr,"ERROR: cannot open inputfile %s\n",infile);
  180. return 1;
  181. }
  182. if (!strcmp(outfile,"-")) {
  183. ofp = stdout;
  184. }
  185. else {
  186. if (!quiet) PRINT_ID("TTBin2Hex");
  187. if (!(ofp = fopen(outfile,"w"))) {
  188. fprintf(stderr,"ERROR: cannot open outputfile %s\n",outfile);
  189. fclose(ifp);
  190. return 1;
  191. }
  192. }
  193. fseek(ifp,0,SEEK_END);
  194. insize = ftell(ifp);
  195. rewind(ifp);
  196. bytes_processed = 0;
  197. itemcount = 0;
  198. insize-=skip_start;
  199. insize-=skip_end;
  200. if (insize<0) {
  201. fprintf(stderr,"ERROR: no bytes left to process\n");
  202. fclose(ifp);
  203. fclose(ofp);
  204. return 1;
  205. }
  206. if (skip_start > 0) fseek(ifp,skip_start,SEEK_SET);
  207. if (arrayname) {
  208. int correct = (insize%bytes_per_item) ? 1 : 0;
  209. switch(bytes_per_item) {
  210. case 1:
  211. fprintf(ofp,"unsigned char %s[%ld] = {%c\n",arrayname,insize/bytes_per_item+correct,(appendslash)?'\\':' ');
  212. break;
  213. case 2:
  214. fprintf(ofp,"unsigned short %s[%ld] = {%c\n",arrayname,insize/bytes_per_item+correct,(appendslash)?'\\':' ');
  215. break;
  216. case 4:
  217. fprintf(ofp,"unsigned long %s[%ld] = {%c\n",arrayname,insize/bytes_per_item+correct,(appendslash)?'\\':' ');
  218. break;
  219. }
  220. }
  221. for (loop = 0;loop<insize/bytes_per_item;loop++) {
  222. switch(bytes_per_item) {
  223. case 1:
  224. data[0] = fgetc(ifp);
  225. fprintf(ofp,"0x%02x", data[0]);
  226. break;
  227. case 2:
  228. data[0] = fgetc(ifp);
  229. data[1] = fgetc(ifp);
  230. fprintf(ofp,"0x%02x%02x",data[0],data[1]);
  231. break;
  232. case 4:
  233. data[0] = fgetc(ifp);
  234. data[1] = fgetc(ifp);
  235. data[2] = fgetc(ifp);
  236. data[3] = fgetc(ifp);
  237. fprintf(ofp,"0x%02x%02x%02x%02x",data[0],data[1],data[2],data[3]);
  238. break;
  239. }
  240. bytes_processed+=bytes_per_item;
  241. itemcount++;
  242. if (bytes_processed < insize) fputc(',',ofp);
  243. if (itemcount == items_per_line) {
  244. if (appendslash) fputc('\\',ofp);
  245. fputc('\n',ofp);
  246. itemcount=0;
  247. }
  248. if (empty_line) {
  249. if (!((loop+1) % empty_line)) {
  250. if (appendslash) fputc('\\',ofp);
  251. fputc('\n',ofp);
  252. itemcount=0;
  253. }
  254. }
  255. }
  256. if (insize%bytes_per_item) {
  257. for (n=insize%bytes_per_item;n<bytes_per_item;n++) data[n]=0;
  258. switch(bytes_per_item) {
  259. case 1: fprintf(ofp,"0x%02x", data[0]); break;
  260. case 2: fprintf(ofp,"0x%02x%02x", data[0],data[1]); break;
  261. case 4: fprintf(ofp,"0x%02x%02x%02x%02x",data[0],data[1],data[2],data[3]); break;
  262. }
  263. if (appendslash) fputc('\\',ofp);
  264. fputc('\n',ofp);
  265. }
  266. if (arrayname) {
  267. fputc('}',ofp);
  268. fputc(';',ofp);
  269. fputc('\n',ofp);
  270. }
  271. fclose(ifp);
  272. if (ofp != stdout) {
  273. fclose(ofp);
  274. loop = insize/bytes_per_item;
  275. if (insize%bytes_per_item) loop++;
  276. if (!quiet) fprintf(stdout,"%ld items with %d byte(s) written to %s\n",loop,bytes_per_item,outfile);
  277. }
  278. return 0;
  279. }
  280. //#############################################################################
  281. //###################### NO MORE FAKES BEYOND THIS LINE #######################
  282. //#############################################################################
  283. //
  284. //=============================================================================
  285. // Revision History
  286. //=============================================================================
  287. //
  288. // Revision 1.10 2009/01/25 Lionel Debroux
  289. // Changes by Romain Liévin and/or me for 64-bit compatibility.
  290. // Adapt to new version display (revtools.h).
  291. //
  292. // Revision 1.9 2002/05/07 15:53:38 tnussb
  293. // (1) option -a fixed. previously the array length was off by one when the
  294. // input length was not a multiple of the itemsize
  295. // (2) option -lb added. Now additionally line breaks can be added after every
  296. // n items.
  297. //
  298. // Revision 1.8 2002/04/17 10:46:45 tnussb
  299. // commandline options -ss and -se added
  300. //
  301. // Revision 1.7 2002/03/15 13:17:30 tnussb
  302. // (1) treatment of -quiet (suppress standard messages) added
  303. //
  304. // Revision 1.6 2002/02/07 09:49:36 tnussb
  305. // all local includes changed, because header files are now located in pctools folder
  306. //
  307. // Revision 1.5 2001/06/20 13:08:01 Thomas Nussbaumer
  308. // (1) handling of output redirection to stdout (outfilename == "-")
  309. // (2) handling of "-a arrayname" to generate a complete C array of name arrayname
  310. //
  311. // Revision 1.4 2001/02/05 20:42:49 Thomas Nussbaumer
  312. // (1) adding no comma after last item anymore
  313. // (2) -slash parameter implemented (adding a backslash at end of line)
  314. //
  315. // Revision 1.3 2000/11/28 00:03:04 Thomas Nussbaumer
  316. // using now USAGE_OUT stream for usage info
  317. //
  318. // Revision 1.2 2000/08/23 19:35:47 Thomas Nussbaumer
  319. // adapted to automatic version display (revtools.h)
  320. //
  321. // Revision 1.1 2000/08/13 20:20:55 Thomas Nussbaumer
  322. // initial version
  323. //
  324. //
  325. //
  326. //