EncodeExecutable.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /*============================================================================
  2. KWSys - Kitware System Library
  3. Copyright 2000-2009 Kitware, Inc., Insight Software Consortium
  4. Distributed under the OSI-approved BSD License (the "License");
  5. see accompanying file Copyright.txt for details.
  6. This software is distributed WITHOUT ANY WARRANTY; without even the
  7. implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  8. See the License 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. fprintf(ofp, "#if defined(_WIN32)\n");
  49. fprintf(ofp, "# include <io.h>\n");
  50. fprintf(ofp, "#else\n");
  51. fprintf(ofp, "# include <unistd.h>\n");
  52. fprintf(ofp, "#endif\n");
  53. fprintf(ofp, "\n");
  54. fprintf(ofp, "static void kwsys_unlink(const char* fname)\n");
  55. fprintf(ofp, "{\n");
  56. fprintf(ofp, "#if defined(__WATCOMC__)\n");
  57. fprintf(ofp, " unlink(fname);\n");
  58. fprintf(ofp, "#else\n");
  59. fprintf(ofp, " _unlink(fname);\n");
  60. fprintf(ofp, "#endif\n");
  61. fprintf(ofp, "}\n");
  62. fprintf(ofp, "\n");
  63. /* Split file up in 1024-byte chunks. */
  64. while((n = (int)fread(buffer, 1, 1024, ifp)) > 0)
  65. {
  66. fprintf(ofp, "static unsigned char kwsysEncodedArray%s_%d[%d] = {\n",
  67. argv[4], count++, n);
  68. for(i=0; i < n-1; ++i)
  69. {
  70. fprintf(ofp, "0x%02X", buffer[i]);
  71. if(i%10 == 9)
  72. {
  73. fprintf(ofp, ",\n");
  74. }
  75. else
  76. {
  77. fprintf(ofp, ", ");
  78. }
  79. }
  80. fprintf(ofp, "0x%02X};\n\n", buffer[n-1]);
  81. }
  82. fclose(ifp);
  83. /* Provide a function to write the data to a file. */
  84. fprintf(ofp, "extern %s_EXPORT int %sEncodedWriteArray%s(const char* fname)\n",
  85. argv[3], argv[3], argv[4]);
  86. fprintf(ofp, "{\n");
  87. fprintf(ofp, " FILE* ofp = fopen(fname, \"wb\");\n");
  88. fprintf(ofp, " if(!ofp) { return 0; }\n");
  89. for(i=0; i < count; ++i)
  90. {
  91. fprintf(ofp, " if(fwrite(kwsysEncodedArray%s_%d, 1,\n"
  92. " sizeof(kwsysEncodedArray%s_%d), ofp) !=\n"
  93. " sizeof(kwsysEncodedArray%s_%d))\n",
  94. argv[4], i, argv[4], i, argv[4], i);
  95. fprintf(ofp, " {\n");
  96. fprintf(ofp, " fclose(ofp);\n");
  97. fprintf(ofp, " kwsys_unlink(fname);\n");
  98. fprintf(ofp, " return 0;\n");
  99. fprintf(ofp, " }\n");
  100. }
  101. fprintf(ofp, " fclose(ofp);\n");
  102. fprintf(ofp, " return 1;\n");
  103. fprintf(ofp, "}\n");
  104. fclose(ofp);
  105. return 0;
  106. }