EncodeExecutable.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /*=========================================================================
  2. Program: KWSys - Kitware System Library
  3. Module: $RCSfile$
  4. Copyright (c) Kitware, Inc., Insight Consortium. All rights reserved.
  5. See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
  6. This software is distributed WITHOUT ANY WARRANTY; without even
  7. the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  8. PURPOSE. See the above copyright notices for more information.
  9. =========================================================================*/
  10. #include <stdio.h>
  11. #ifdef __WATCOMC__
  12. #define _unlink unlink
  13. #endif
  14. int main(int argc, char* argv[])
  15. {
  16. FILE* ifp;
  17. FILE* ofp;
  18. int i;
  19. int n;
  20. int count = 0;
  21. unsigned char buffer[1024];
  22. /* Check arguments. */
  23. if(argc != 5)
  24. {
  25. fprintf(stderr, "Usage: %s <input> <output> <kwsys-name> <array>\n",
  26. argv[0]);
  27. return 1;
  28. }
  29. /* Open the input file. */
  30. ifp = fopen(argv[1], "rb");
  31. if(!ifp)
  32. {
  33. fprintf(stderr, "Cannot open input file: \"%s\"\n", argv[1]);
  34. return 2;
  35. }
  36. ofp = fopen(argv[2], "w");
  37. if(!ofp)
  38. {
  39. fprintf(stderr, "Cannot open output file: \"%s\"\n", argv[2]);
  40. return 2;
  41. }
  42. /* Prepend header comment. */
  43. fprintf(ofp, "/*\n * DO NOT EDIT\n * This file is generated by:\n");
  44. fprintf(ofp, " * %s\n */\n\n", argv[0]);
  45. fprintf(ofp, "#include \"kwsysPrivate.h\"\n");
  46. fprintf(ofp, "#include KWSYS_HEADER(Configure.h)\n\n");
  47. fprintf(ofp, "#include <stdio.h>\n\n");
  48. /* Split file up in 1024-byte chunks. */
  49. while((n = (int)fread(buffer, 1, 1024, ifp)) > 0)
  50. {
  51. fprintf(ofp, "static unsigned char kwsysEncodedArray%s_%d[%d] = {\n",
  52. argv[4], count++, n);
  53. for(i=0; i < n-1; ++i)
  54. {
  55. fprintf(ofp, "0x%02X", buffer[i]);
  56. if(i%10 == 9)
  57. {
  58. fprintf(ofp, ",\n");
  59. }
  60. else
  61. {
  62. fprintf(ofp, ", ");
  63. }
  64. }
  65. fprintf(ofp, "0x%02X};\n\n", buffer[n-1]);
  66. }
  67. fclose(ifp);
  68. /* Provide a function to write the data to a file. */
  69. fprintf(ofp, "extern %s_EXPORT int %sEncodedWriteArray%s(const char* fname)\n",
  70. argv[3], argv[3], argv[4]);
  71. fprintf(ofp, "{\n");
  72. fprintf(ofp, "#ifdef __WATCOMC__\n");
  73. fprintf(ofp, "#define _unlink unlink\n");
  74. fprintf(ofp, "#endif\n");
  75. fprintf(ofp, " FILE* ofp = fopen(fname, \"wb\");\n");
  76. fprintf(ofp, " if(!ofp) { return 0; }\n");
  77. for(i=0; i < count; ++i)
  78. {
  79. fprintf(ofp, " if(fwrite(kwsysEncodedArray%s_%d, 1,\n"
  80. " sizeof(kwsysEncodedArray%s_%d), ofp) !=\n"
  81. " sizeof(kwsysEncodedArray%s_%d))\n",
  82. argv[4], i, argv[4], i, argv[4], i);
  83. fprintf(ofp, " {\n");
  84. fprintf(ofp, " fclose(ofp);\n");
  85. fprintf(ofp, " _unlink(fname);\n");
  86. fprintf(ofp, " return 0;\n");
  87. fprintf(ofp, " }\n");
  88. }
  89. fprintf(ofp, " fclose(ofp);\n");
  90. fprintf(ofp, " return 1;\n");
  91. fprintf(ofp, "}\n");
  92. fclose(ofp);
  93. return 0;
  94. }