ttstrip.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. /******************************************************************************
  2. *
  3. * project name: TI-68k Developer Utilities
  4. * file name: ttstrip.c
  5. * initial date: 13/08/2000
  6. * author: thomas.nussbaumer@gmx.net
  7. * description: strips PC header and trailing checksum from an TI executable
  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 ttstrip 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. #ifndef __TTSTRIP__
  30. #define __TTSTRIP__
  31. // if EMBEDDED_USE is defined, than we use this sourcefile from within another
  32. // sourcefile
  33. #ifndef EMBEDDED_USE
  34. #include <stdio.h>
  35. #include <string.h>
  36. #include <stdlib.h>
  37. #include "ttversion.h"
  38. #include "revtools.h"
  39. #include "tt.h"
  40. #ifdef FILE_REVISION
  41. #undef FILE_REVISION
  42. #endif
  43. #define FILE_REVISION "1.8"
  44. //=============================================================================
  45. // outputs usage information of this tool
  46. //=============================================================================
  47. void PrintUsage() {
  48. #ifndef EMBEDDED_USE
  49. PRINT_ID("TTStrip");
  50. #endif
  51. fprintf(USAGE_OUT, "Usage: ttstrip [-quiet] <infile> <outfile>\n\n" \
  52. " -quiet ... don't output standard messages\n\n" \
  53. " extracts the raw binary content of a TI file by stripping\n" \
  54. " the header and trailing checksum which are only necessary\n" \
  55. " for the link software to send the data to the calc.\n" \
  56. " NOTE: after stripping these informations it is no longer\n" \
  57. " possible to distinguish between a TI89 and a TI92p file.\n" \
  58. " So be careful!\n\n");
  59. }
  60. //=============================================================================
  61. // its a main ...
  62. //=============================================================================
  63. int main(int argc,char *argv[]) {
  64. #else
  65. int TTStrip(int argc,char *argv[]) {
  66. #endif
  67. char* infile = NULL;
  68. char* outfile = NULL;
  69. FILE* ifp;
  70. FILE* ofp;
  71. char sig[9];
  72. int length;
  73. int striplen;
  74. int n;
  75. int quiet = 0;
  76. // check for too less arguments
  77. if (argc < 3) {
  78. #ifndef EMBEDDED_USE
  79. PrintUsage();
  80. #endif
  81. return 1;
  82. }
  83. // parse arguments
  84. for (n=1; n<argc; n++) {
  85. if (!strcmp(argv[n], "-quiet")) quiet = 1;
  86. else if (argv[n][0] == '-') {
  87. fprintf(stderr,"ERROR: invalid option %s",argv[n]);
  88. return 1;
  89. }
  90. else if (!infile) infile = argv[n];
  91. else if (!outfile) outfile = argv[n];
  92. else {
  93. #ifndef EMBEDDED_USE
  94. PrintUsage();
  95. #endif
  96. return 1;
  97. }
  98. }
  99. // check if all necessary arguments are supplied
  100. if (!infile || !outfile) {
  101. #ifndef EMBEDDED_USE
  102. PrintUsage();
  103. #endif
  104. return 1;
  105. }
  106. #ifndef EMBEDDED_USE
  107. if (!quiet) PRINT_ID("TTStrip");
  108. #endif
  109. if (!(ifp = fopen(infile,"rb"))) {
  110. fprintf(stderr,"ERROR: cannot open inputfile %s\n",infile);
  111. return 1;
  112. }
  113. memset(sig,0,9);
  114. fread(sig,1,8,ifp);
  115. fseek(ifp,0,SEEK_END);
  116. length = ftell(ifp);
  117. length -= 88; // 86 bytes for header + 2 bytes of trailing checksum
  118. rewind(ifp);
  119. fseek(ifp,86,SEEK_SET);
  120. if (strcmp(sig,SIGNATURE_TI89) && strcmp(sig,SIGNATURE_TI92P)) {
  121. fprintf(stderr,"WARNING: neither TI89 nor TI92 inputfile\n");
  122. }
  123. if (!(ofp = fopen(outfile,"wb"))) {
  124. fprintf(stderr,"ERROR: cannot open outputfile %s\n",outfile);
  125. fclose(ifp);
  126. return 1;
  127. }
  128. striplen = length;
  129. while (length--) fputc(fgetc(ifp),ofp);
  130. fclose(ifp);
  131. fclose(ofp);
  132. if (!quiet) fprintf(stdout,"%d bytes written to %s\n",striplen,outfile);
  133. return 0;
  134. }
  135. #endif
  136. //#############################################################################
  137. //###################### NO MORE FAKES BEYOND THIS LINE #######################
  138. //#############################################################################
  139. //
  140. //=============================================================================
  141. // Revision History
  142. //=============================================================================
  143. //
  144. // Revision 1.8 2009/01/25 Lionel Debroux
  145. // Changes by Romain Liévin and/or me for 64-bit compatibility.
  146. // Adapt to new version display (revtools.h).
  147. //
  148. // Revision 1.7 2002/03/14 08:59:47 tnussb
  149. // (1) new flag "-quiet" added (suppress standard messages)
  150. // (2) usage text completely rewritten
  151. //
  152. // Revision 1.6 2002/03/04 14:32:42 tnussb
  153. // now tool can be used as embedded version from within other tools
  154. // by defining EMBEDDED_VERSION before including the sourcefile
  155. //
  156. // Revision 1.5 2002/02/07 09:49:38 tnussb
  157. // all local includes changed, because header files are now located in pctools folder
  158. //
  159. // Revision 1.4 2000/11/28 00:04:21 Thomas Nussbaumer
  160. // using now USAGE_OUT stream for usage info
  161. //
  162. // Revision 1.3 2000/08/23 20:21:29 Thomas Nussbaumer
  163. // adapted to automatic version display (revtools.h)
  164. //
  165. // Revision 1.2 2000/08/13 20:23:15 Thomas Nussbaumer
  166. // usage text modified
  167. //
  168. // Revision 1.1 2000/08/13 16:26:02 Thomas Nussbaumer
  169. // initial version
  170. //
  171. //
  172. //