ttbin2str.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. /******************************************************************************
  2. *
  3. * project name: TI-68k Developer Utilities
  4. * file name: ttbin2str.c
  5. * initial date: 13/08/2000
  6. * author: thomas.nussbaumer@gmx.net
  7. * description: enwraps inputfile into TI89 and TI92 strings
  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 ttbin2str 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. #include "strhead.h"
  36. #ifdef FILE_REVISION
  37. #undef FILE_REVISION
  38. #endif
  39. #define FILE_REVISION "1.8"
  40. //=============================================================================
  41. // outputs usage information of this tool
  42. //=============================================================================
  43. void PrintUsage() {
  44. fprintf(USAGE_OUT, "Usage: ttbin2str <-s89 | -s92> <infile> <name> [folder]\n\n" \
  45. " -s89 .... generate TI89 string\n" \
  46. " -s92 .... generate TI92 string\n" \
  47. " -f .... don't print a warning if the string contains at least one 0x00\n" \
  48. " (strings containing 0x00 may prove problematic to transfer using TI-Connect)\n\n" \
  49. " enwraps any file into a TI89 or TI92 string variable\n");
  50. }
  51. //=============================================================================
  52. // this routine is very similar to the Data2OTH routine, but it will a string
  53. // variable (now-a-days OTH types are preferred)
  54. //=============================================================================
  55. int Data2Str(FILE* fp,
  56. int calctype,
  57. char* folder,
  58. char* name,
  59. unsigned int length,
  60. unsigned char* data,
  61. int force)
  62. {
  63. StrHeader header;
  64. unsigned int i;
  65. unsigned int checksum;
  66. unsigned char* ptr;
  67. int size = sizeof(StrHeader)+length+5;
  68. int retval = size;
  69. unsigned char c;
  70. static int contains0x00 = 0;
  71. memset(&header,0,sizeof(StrHeader));
  72. if (calctype == CALC_TI89) strncpy(header.signature,SIGNATURE_TI89,8);
  73. else strncpy(header.signature,SIGNATURE_TI92P,8);
  74. header.fill1[0] = 1;
  75. header.fill1[1] = 0;
  76. if (!folder) strcpy(header.folder,DEFAULT_FOLDER);
  77. else strncpy(header.folder,folder,8);
  78. header.fill2[0] = 0x01;
  79. header.fill2[1] = 0x00;
  80. header.fill2[2] = 0x52;
  81. header.fill2[3] = 0x00;
  82. header.fill2[4] = 0x00;
  83. header.fill2[5] = 0x00;
  84. strncpy(header.name,name,8);
  85. header.type[0] = 0x0c; // 0c
  86. header.type[1] = 0x00;
  87. header.type[2] = 0x03;
  88. header.type[3] = 0x00;
  89. header.size[0] = size & 0xff;
  90. header.size[1] = (size >> 8) & 0xff;
  91. header.size[2] = (size >> 16) & 0xff;
  92. header.size[3] = (size >> 24) & 0xff;
  93. header.fill3[0] = 0xA5;
  94. header.fill3[1] = 0x5A;
  95. header.fill3[2] = 0x00;
  96. header.fill3[3] = 0x00;
  97. header.fill3[4] = 0x00;
  98. header.fill3[5] = 0x00;
  99. size -= sizeof(StrHeader);
  100. size -= 2;
  101. header.datasize[0] = (size >> 8) & 0xff;
  102. header.datasize[1] = size & 0xff;
  103. checksum = header.datasize[0];
  104. checksum += header.datasize[1];
  105. ptr = (unsigned char*)&header;
  106. for (i=0;i<sizeof(StrHeader);i++) {
  107. fputc(*ptr,fp);
  108. ptr++;
  109. }
  110. fputc(0,fp);
  111. for (i=0;i<length;i++) {
  112. c = *data++;
  113. if ((c == 0x00) && (!force) && (!contains0x00)) {
  114. fprintf(stderr,"WARNING: string contains 0x00: TI-Connect may truncate the string\n");
  115. contains0x00 = 1;
  116. }
  117. fputc(c,fp);
  118. checksum+=c;
  119. }
  120. fputc(0,fp);
  121. fputc(0x2D,fp);
  122. checksum+=0x2d;
  123. fputc(checksum%256,fp);
  124. fputc(checksum/256,fp);
  125. return retval;
  126. }
  127. //=============================================================================
  128. // an old friend ...
  129. //=============================================================================
  130. int main(int argc,char *argv[]) {
  131. char* infile = NULL;
  132. char* folder = NULL;
  133. char* name = NULL;
  134. int calctype = -1;
  135. FILE* ifp;
  136. FILE* ofp;
  137. char outfile[1024];
  138. unsigned char* buffer;
  139. int length;
  140. int n;
  141. int force = 0;
  142. PRINT_ID("TTBin2Str");
  143. // check for too less arguments
  144. if (argc < 4) {
  145. PrintUsage();
  146. return 1;
  147. }
  148. // parse arguments
  149. for (n=1; n<argc; n++) {
  150. if (!strcmp(argv[n], "-s89")) calctype = CALC_TI89;
  151. else if (!strcmp(argv[n], "-s92")) calctype = CALC_TI92P;
  152. else if (!strcmp(argv[n], "-f")) force = 1;
  153. else if (argv[n][0] == '-') {
  154. fprintf(stderr,"ERROR: invalid option %s",argv[n]);
  155. return 1;
  156. }
  157. else if (!infile) infile = argv[n];
  158. else if (!name) name = argv[n];
  159. else if (!folder) folder = argv[n];
  160. else {
  161. PrintUsage();
  162. return 1;
  163. }
  164. }
  165. // check if all necessary arguments are supplied
  166. if (!infile || !name || calctype == -1) {
  167. PrintUsage();
  168. return 1;
  169. }
  170. if (!(ifp = fopen(infile,"rb"))) {
  171. fprintf(stderr,"ERROR: cannot open inputfile %s\n",infile);
  172. return 1;
  173. }
  174. if (calctype == CALC_TI89) sprintf(outfile,"%s.89s",name);
  175. else sprintf(outfile,"%s.9xs",name);
  176. if (!(ofp = fopen(outfile,"wb"))) {
  177. fprintf(stderr,"ERROR: cannot open outputfile %s\n",outfile);
  178. fclose(ifp);
  179. return 1;
  180. }
  181. // read infile into buffer
  182. fseek(ifp,0,SEEK_END);
  183. length = ftell(ifp);
  184. buffer = (unsigned char*)malloc(length);
  185. if (!buffer) {
  186. fprintf(stderr,"ERROR: cannot allocate memory (%d bytes)\n",length);
  187. fclose(ifp);
  188. fclose(ofp);
  189. return 1;
  190. }
  191. rewind(ifp);
  192. fread(buffer,1,length,ifp);
  193. fclose(ifp);
  194. length = Data2Str(ofp,calctype,folder,name,(unsigned int)length,buffer,force);
  195. free(buffer);
  196. fclose(ofp);
  197. fprintf(stderr,"%d bytes written to %s\n",length,outfile);
  198. return 0;
  199. }
  200. //#############################################################################
  201. //###################### NO MORE FAKES BEYOND THIS LINE #######################
  202. //#############################################################################
  203. //
  204. //=============================================================================
  205. // Revision History
  206. //=============================================================================
  207. //
  208. // Revision 1.8 2009/01/25 Lionel Debroux
  209. // Changes by Romain Liévin and/or me for 64-bit compatibility.
  210. // Changes by Kevin Kofler and me to generate a warning (unless explicitly told
  211. // not to warn) if the string contains at least one 0x00.
  212. // Adapt to new version display (revtools.h).
  213. //
  214. // Revision 1.7 2002/02/07 09:49:37 tnussb
  215. // all local includes changed, because header files are now located in pctools folder
  216. //
  217. // Revision 1.6 2001/02/05 20:39:55 Thomas Nussbaumer
  218. // filling now PC file size bytes correctly
  219. //
  220. // Revision 1.5 2000/11/28 00:03:31 Thomas Nussbaumer
  221. // using now USAGE_OUT stream for usage info
  222. //
  223. // Revision 1.4 2000/08/23 19:45:45 Thomas Nussbaumer
  224. // adapted to automatic version display (revtools.h)
  225. //
  226. // Revision 1.3 2000/08/13 20:22:42 Thomas Nussbaumer
  227. // (1) help text slightly modified
  228. // (2) exit if unknown flag is detected
  229. //
  230. // Revision 1.2 2000/08/13 16:27:10 Thomas Nussbaumer
  231. // unneccessary code removed
  232. //
  233. // Revision 1.1 2000/08/13 16:01:08 Thomas Nussbaumer
  234. // initial version
  235. //
  236. //
  237. //