crurom.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. const char crurom_rcsid[] = "@(#) $Id: crurom.c 5351 2013-09-24 10:16:53Z haraldkipp $";
  2. #ifdef HAVE_CONFIG_H
  3. #include <config.h>
  4. #endif
  5. #include <fcntl.h>
  6. #include <sys/types.h>
  7. #include <sys/stat.h>
  8. #include <ctype.h>
  9. #include <stdlib.h>
  10. #include <stdio.h>
  11. #include <string.h>
  12. #include <errno.h>
  13. #ifdef _WIN32
  14. #include <io.h>
  15. #include "dirent.h"
  16. #else
  17. #include <unistd.h>
  18. #include <dirent.h>
  19. #define stricmp strcasecmp
  20. #define strnicmp strncasecmp
  21. #endif
  22. #include "getopt.h"
  23. #ifndef O_BINARY
  24. #define O_BINARY 0
  25. #endif
  26. #define IDENT "crurom"
  27. #undef VERSION
  28. #define VERSION "2.0.0"
  29. static int entryno = 0;
  30. static int verbose = 0;
  31. static int recursive = 0;
  32. static int prog_types_compat = 0;
  33. static int enc_hex = 0;
  34. static int enc_dec = 0;
  35. static int enc_strings = 0;
  36. static int enc_ugly = 0;
  37. static int max_char_per_line = 16;
  38. static char rootdir[256];
  39. static int rootlen = 0;
  40. static char outname[256];
  41. static FILE *fpout;
  42. int dofile(char *name)
  43. {
  44. static char *esc_seq = "\aa\bb\ff\nn\rr\tt\vv";
  45. int rc = 0;
  46. int fd;
  47. unsigned char buf[512];
  48. int i;
  49. int nl_flag = 1;
  50. int cpl_cnt = 0;
  51. int cnt;
  52. long total = 0;
  53. char *fsname = name;
  54. if(strnicmp(fsname, rootdir, rootlen) == 0)
  55. fsname += rootlen;
  56. if((fd = open(name, O_RDONLY | O_BINARY)) == -1) {
  57. perror(name);
  58. return -1;
  59. }
  60. if(verbose)
  61. fprintf(stderr, IDENT ": Reading %s\n", name);
  62. for(;;) {
  63. if((cnt = read(fd, buf, sizeof(buf))) < 0) {
  64. perror(name);
  65. rc = -1;
  66. total = 0;
  67. break;
  68. }
  69. if(total == 0) {
  70. entryno++;
  71. if (!enc_ugly) {
  72. fprintf(fpout, "/*\n * File entry %d: %s\n */\n", entryno, fsname);
  73. }
  74. if (prog_types_compat) {
  75. fprintf(fpout, "prog_char file%ddata[]", entryno);
  76. } else {
  77. fprintf(fpout, "const char file%ddata[] PROGMEM", entryno);
  78. }
  79. if (enc_strings) {
  80. fputs(" = ", fpout);
  81. } else {
  82. fputs(" = {", fpout);
  83. }
  84. }
  85. if(cnt == 0)
  86. break;
  87. if (enc_strings) {
  88. /*
  89. * Encode buffer to string.
  90. *
  91. * When using this format, then at least text files will
  92. * become user-editable. However, due to the string terminator
  93. * each file occupies an additional byte in memory.
  94. */
  95. for(i = 0; i < cnt; i++) {
  96. if (nl_flag) {
  97. fputs("\n\"", fpout);
  98. nl_flag = 0;
  99. cpl_cnt = 0;
  100. }
  101. /* Handle non-printable values. */
  102. if (buf[i] < 32 || buf[i] > 126) {
  103. /* Handle a few escape sequences. */
  104. char *cp = strchr(esc_seq, buf[i]);
  105. if (cp && *cp) {
  106. cp++;
  107. fputc('\\', fpout);
  108. fputc(*cp, fpout);
  109. cpl_cnt += 2;
  110. /* Terminate line at line-feeds. */
  111. if (!enc_ugly) {
  112. nl_flag = *cp == 'n';
  113. }
  114. }
  115. else {
  116. /* Write all other values as hex. */
  117. cpl_cnt += fprintf(fpout, enc_ugly ? "\\x%x" : "\\x%02X", buf[i]);
  118. /* ANSI-C doesn't limit hex to 2 characters.
  119. Terminate the line if the next character
  120. is a hex digit. */
  121. if (i + 1 < cnt && isxdigit(buf[i + 1])) {
  122. fputc('"', fpout);
  123. nl_flag = 1;
  124. }
  125. }
  126. }
  127. /* Handle non-printable values. */
  128. else {
  129. if (buf[i] == '"' || buf[i] == '\\') {
  130. fputc('\\', fpout);
  131. cpl_cnt++;
  132. }
  133. fputc((int)buf[i], fpout);
  134. cpl_cnt++;
  135. }
  136. /* Terminate line if... */
  137. if(nl_flag == 0 && /* ...not already flagged. */
  138. cpl_cnt >= max_char_per_line && /* ...maximum length reached. */
  139. i + 1 < cnt) { /* ...more to come. */
  140. fputc('"', fpout);
  141. nl_flag = 1;
  142. }
  143. }
  144. } else {
  145. /*
  146. * Encode buffer to characters.
  147. *
  148. * This is the original default encoding, where file contents
  149. * represented in character arrays.
  150. */
  151. for(i = 0; i < cnt; i++) {
  152. /* Limit characters per line. */
  153. if((i % max_char_per_line) == 0) {
  154. if(total != 0 || i != 0) {
  155. fputc(',', fpout);
  156. }
  157. fputs("\n ", fpout);
  158. } else {
  159. fputc(',', fpout);
  160. }
  161. /* Write all characters as hex values. */
  162. if (enc_hex) {
  163. fprintf(fpout, enc_ugly ? "0x%x" : "0x%02X", buf[i]);
  164. }
  165. /* Write all characters as decimal values. */
  166. else if (enc_dec) {
  167. fprintf(fpout, enc_ugly ? "%u" : "%3u", buf[i]);
  168. }
  169. /* Write characters or decimal values. */
  170. else {
  171. /* Write non-printable values as decimal. */
  172. if (buf[i] < 32 || buf[i] > 126 || buf[i] == '\'' || buf[i] == '\\') {
  173. fprintf(fpout, enc_ugly ? "%u" : "%3u", buf[i]);
  174. }
  175. else
  176. fprintf(fpout, "'%c'", buf[i]);
  177. }
  178. }
  179. }
  180. total += cnt;
  181. }
  182. close(fd);
  183. if (enc_strings) {
  184. if (nl_flag == 0) {
  185. fputc('"', fpout);
  186. }
  187. } else {
  188. fputs("\n}", fpout);
  189. }
  190. fputs(enc_ugly ? ";\n" : ";\n\n", fpout);
  191. if (!enc_ugly) {
  192. fputc('\n', fpout);
  193. }
  194. if (prog_types_compat) {
  195. fprintf(fpout, "prog_char file%dname[] = \"%s\";\n", entryno, fsname);
  196. } else {
  197. fprintf(fpout, "const char file%dname[] PROGMEM = \"%s\";\n", entryno, fsname);
  198. }
  199. if (!enc_ugly) {
  200. fputc('\n', fpout);
  201. }
  202. fprintf(fpout, "static ROMENTRY file%dentry = { ", entryno);
  203. if(entryno > 1)
  204. fprintf(fpout, "&file%dentry, ", entryno - 1);
  205. else
  206. fprintf(fpout, "0, ");
  207. if (prog_types_compat) {
  208. fprintf(fpout, "(prog_char *)file%dname, %ld, (prog_char *)file%ddata };\n", entryno, total, entryno);
  209. } else {
  210. fprintf(fpout, "file%dname, %ld, file%ddata };\n", entryno, total, entryno);
  211. }
  212. return rc;
  213. }
  214. int dodir(char *dirpath)
  215. {
  216. int rc = 0;
  217. char path[256];
  218. DIR *dir;
  219. struct dirent *dire;
  220. struct stat statbuf;
  221. if((dir = opendir(dirpath)) == NULL) {
  222. fprintf(stderr, "Failed to scan directory %s\n", dirpath);
  223. return -1;
  224. }
  225. if(verbose)
  226. fprintf(stderr, "Scan %s\n", dirpath);
  227. while((dire = readdir(dir)) != NULL && rc == 0) {
  228. if((dire->d_name[0] == '.') || (stricmp(dire->d_name, "cvs") == 0) || (stricmp(dire->d_name, "svn") == 0))
  229. continue;
  230. strcpy(path, dirpath);
  231. strcat(path, "/");
  232. strcat(path, dire->d_name);
  233. stat(path, &statbuf);
  234. if(statbuf.st_mode & S_IFDIR)
  235. rc = dodir(path);
  236. else if(statbuf.st_mode & S_IFREG)
  237. rc = dofile(path);
  238. }
  239. closedir(dir);
  240. return rc;
  241. }
  242. void usage(void)
  243. {
  244. fputs("Usage: crurom OPTIONS DIRECTORY\n"
  245. "OPTIONS:\n"
  246. "-cp create __PROG_TYPES_COMPAT__\n"
  247. "-ed encode to decimal values\n"
  248. "-eh encode to hex values\n"
  249. "-es encode to strings\n"
  250. "-eu ugly but compact encoding\n"
  251. "-l <num> characters per line\n"
  252. "-o <file> output file\n"
  253. "-r recursive\n"
  254. "-v verbose\n"
  255. , stderr);
  256. }
  257. int main(int argc, char **argv)
  258. {
  259. int option;
  260. int i;
  261. int rc = 0;
  262. char *ocp;
  263. while((option = getopt(argc, argv, "c:e:l:o:rv?")) != EOF) {
  264. switch(option) {
  265. case 'c':
  266. if (strchr(optarg, 'p')) {
  267. prog_types_compat++;
  268. }
  269. break;
  270. case 'e':
  271. for (ocp = optarg; *ocp; ocp++) {
  272. if (*ocp == 'd') {
  273. enc_dec++;
  274. }
  275. else if (*ocp == 'h') {
  276. enc_hex++;
  277. }
  278. else if (*ocp == 's') {
  279. enc_strings++;
  280. max_char_per_line = 128;
  281. }
  282. else if (*ocp == 'u') {
  283. enc_ugly++;
  284. }
  285. else {
  286. usage();
  287. return 1;
  288. }
  289. }
  290. break;
  291. case 'l':
  292. max_char_per_line = atoi(optarg);
  293. break;
  294. case 'o':
  295. strcpy(outname, optarg);
  296. break;
  297. case 'r':
  298. recursive++;
  299. break;
  300. case 'v':
  301. verbose++;
  302. break;
  303. default:
  304. usage();
  305. return 1;
  306. }
  307. }
  308. argc -= optind;
  309. argv += optind;
  310. if(outname[0]) {
  311. if((fpout = fopen(outname, "w")) == NULL) {
  312. perror(outname);
  313. return 3;
  314. }
  315. }
  316. else
  317. fpout = stdout;
  318. fprintf(fpout, "/*\n");
  319. fprintf(fpout, " * This file is automatically created by " IDENT " " VERSION "\n");
  320. fprintf(fpout, " */\n");
  321. fprintf(fpout, "#include <fs/uromfs.h>\n\n");
  322. if(argc) {
  323. for(i = 0; i < argc && rc == 0; i++) {
  324. strcpy(rootdir, argv[i]);
  325. strcat(rootdir, "/");
  326. rootlen = strlen(rootdir);
  327. rc = dodir(argv[i]);
  328. }
  329. }
  330. else {
  331. strcpy(rootdir, "./");
  332. rootlen = 2;
  333. rc = dodir(".");
  334. }
  335. fprintf(fpout, "\nROMENTRY *romEntryList = &file%dentry;\n", entryno);
  336. if(fpout != stdout)
  337. fclose(fpout);
  338. return rc;
  339. }