bin2oth.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. /******************************************************************************
  2. *
  3. * project name: TI-68k Developer Utilities
  4. * file name: bin2oth.c
  5. * initial date: 22/08/2000
  6. * author: thomas.nussbaumer@gmx.net
  7. * description: converts a data buffer into an oth buffer and checks
  8. * for maximal length, too
  9. *
  10. * NOTE: this routine is used in many tools, therefore it is implemented in a
  11. * separately C file and included in the tools
  12. *
  13. * NOTE2: !!!! THIS SOURCECODE DEPENDS ON MINIMUM 32BIT INTEGERS !!!!
  14. *
  15. ******************************************************************************/
  16. /*
  17. This file is part of ttbin2oth, one of the TI-68k Developer Utilities.
  18. This file is free software; you can redistribute it and/or
  19. modify it under the terms of the GNU Lesser General Public
  20. License as published by the Free Software Foundation; either
  21. version 2.1 of the License, or (at your option) any later version.
  22. As a special exception, UNMODIFIED copies of ttbin2oth may also be
  23. redistributed or sold without source code, for any purpose. (The Lesser
  24. General Public License restrictions do apply in other respects; for example,
  25. they cover modification of the program.) This exception notice must be
  26. removed on modified copies of this file.
  27. This program is distributed in the hope that it will be useful,
  28. but WITHOUT ANY WARRANTY; without even the implied warranty of
  29. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  30. Lesser General Public License for more details.
  31. You should have received a copy of the GNU Lesser General Public
  32. License along with this library; if not, write to the Free Software
  33. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  34. */
  35. #ifndef __BIN2OTH__
  36. #define __BIN2OTH__
  37. #include <stdio.h>
  38. #include <string.h>
  39. #include <stdlib.h>
  40. #include "tt.h"
  41. #include "strhead.h"
  42. #define TT_MAX_MEMBLOCK 65520
  43. #define TT_MAX_OTHDATA (TT_MAX_MEMBLOCK - 2 - 7)
  44. //=============================================================================
  45. // returns a pointer to a newly allocated memory block which holds the
  46. // OTH file contents ready to flush into a file
  47. //
  48. // the length of the newly allocated memory block will be stored in outlength
  49. //
  50. // NOTE: the length of the extension may vary between 1 and 4 characters
  51. // (no checks are performed on the type of the character)
  52. //=============================================================================
  53. unsigned char* DataBuffer2OTHBuffer(int calctype,
  54. char* folder,
  55. char* varname,
  56. char* extension,
  57. unsigned int inlength,
  58. unsigned char* indata,
  59. unsigned int* outlength)
  60. {
  61. StrHeader* psh;
  62. unsigned int checksum;
  63. unsigned char* ptr;
  64. unsigned int size;
  65. unsigned char* output;
  66. int ext_len;
  67. if (!extension) {
  68. fprintf(stderr,"ERROR: no extension given\n");
  69. return 0;
  70. }
  71. ext_len = strlen(extension);
  72. if (ext_len < 1 || ext_len > 4) {
  73. fprintf(stderr,"ERROR: invalid OTH extension [%s]\n",extension);
  74. return 0;
  75. }
  76. if (inlength > TT_MAX_OTHDATA) {
  77. fprintf(stderr,"ERROR: length (%u) exceeds max. user data size (%u)\n",inlength,TT_MAX_OTHDATA);
  78. return 0;
  79. }
  80. size = sizeof(StrHeader)+inlength+2+3+ext_len; // 6 == OTH bytes , 2 == checksum
  81. if (!(output = (unsigned char*)malloc(size))) {
  82. fprintf(stderr,"ERROR: cannot allocate %u bytes of memory\n",size);
  83. return 0;
  84. }
  85. *outlength = size;
  86. psh = (StrHeader*)output;
  87. memset(psh,0,sizeof(StrHeader));
  88. //-------------------------------------------------------------------
  89. // fill up all the static fields
  90. //-------------------------------------------------------------------
  91. psh->fill1[0]=1;psh->fill1[1]=0;
  92. psh->fill2[0]=0x01;psh->fill2[1]=0x00;psh->fill2[2]=0x52;
  93. psh->fill2[3]=0x00;psh->fill2[4]=0x00;psh->fill2[5]=0x00;
  94. psh->fill3[0]=0xA5;psh->fill3[1]=0x5A;psh->fill3[2]=0x00;
  95. psh->fill3[3]=0x00;psh->fill3[4]=0x00;psh->fill3[5]=0x00;
  96. psh->type[0]=0x1c;psh->type[1]=0x00;psh->type[2]=0x00;psh->type[3]=0x00;
  97. //-------------------------------------------------------------------
  98. // fill in the magic marker string depending on given calc type
  99. //-------------------------------------------------------------------
  100. if (calctype == CALC_TI89) strncpy(psh->signature,SIGNATURE_TI89,8);
  101. else strncpy(psh->signature,SIGNATURE_TI92P,8);
  102. //-------------------------------------------------------------------
  103. // fill in folder and variable name
  104. // if folder name pointer is NULL, use DEFAULT_FOLDER ("main")
  105. //-------------------------------------------------------------------
  106. if (!folder) strncpy(psh->folder,DEFAULT_FOLDER,8);
  107. else strncpy(psh->folder,folder,8);
  108. strncpy(psh->name,varname,8);
  109. //-------------------------------------------------------------------
  110. // size holds the complete output size == filelength including header
  111. //-------------------------------------------------------------------
  112. psh->size[0] = (unsigned char)(size & 0xff);
  113. psh->size[1] = (unsigned char)((size >> 8) & 0xff);
  114. psh->size[2] = (unsigned char)((size >> 16) & 0xff);
  115. psh->size[3] = (unsigned char)((size >> 24) & 0xff);
  116. //-------------------------------------------------------------------
  117. // data size will hold user data size + OTH tag related bytes
  118. //-------------------------------------------------------------------
  119. size -= sizeof(StrHeader);
  120. size -= 2;
  121. psh->datasize[0] = (unsigned char)((size >> 8) & 0xff);
  122. psh->datasize[1] = (unsigned char)(size & 0xff);
  123. //-------------------------------------------------------------------
  124. // copy complete indata
  125. //-------------------------------------------------------------------
  126. memcpy(output + sizeof(StrHeader),indata,inlength);
  127. //-------------------------------------------------------------------
  128. // append OTH bytes
  129. //-------------------------------------------------------------------
  130. ptr = output + sizeof(StrHeader) + inlength;
  131. *ptr++ = 0;
  132. switch (ext_len) {
  133. case 1:
  134. *ptr++ = *extension;
  135. *ptr++ = 0;
  136. break;
  137. case 2:
  138. *ptr++ = *extension++;
  139. *ptr++ = *extension;
  140. *ptr++ = 0;
  141. break;
  142. case 3:
  143. *ptr++ = *extension++;
  144. *ptr++ = *extension++;
  145. *ptr++ = *extension;
  146. *ptr++ = 0;
  147. break;
  148. case 4:
  149. *ptr++ = *extension++;
  150. *ptr++ = *extension++;
  151. *ptr++ = *extension++;
  152. *ptr++ = *extension;
  153. *ptr++ = 0;
  154. break;
  155. }
  156. *ptr++ = 0xF8;
  157. size = *outlength - sizeof(StrHeader);
  158. ptr = psh->datasize;
  159. checksum = 0;
  160. while (size--) checksum += *ptr++;
  161. output[*outlength-2] = (unsigned char)(checksum & 0xff);
  162. output[*outlength-1] = (unsigned char)((checksum >> 8) & 0xff);
  163. return output;
  164. }
  165. #endif
  166. //#############################################################################
  167. //###################### NO MORE FAKES BEYOND THIS LINE #######################
  168. //#############################################################################
  169. //
  170. //=============================================================================
  171. // Revision History
  172. //=============================================================================
  173. //
  174. // Revision 1.8 2009/01/25 Lionel Debroux
  175. // Changes by Romain Liévin and/or me for 64-bit compatibility.
  176. // Adapt to new version display (revtools.h).
  177. //
  178. // Revision 1.7 2002/05/13 15:17:43 tnussb
  179. // static header information fixed (thanx to Sebastian again)
  180. //
  181. // Revision 1.6 2002/05/13 14:09:56 tnussb
  182. // TT_MAX_OTHDATA ... corrected (thanx to Sebastian Reichelt)
  183. //
  184. // Revision 1.5 2002/03/14 09:00:56 tnussb
  185. // checking for define __BIN2OTH__ added at begin of file
  186. //
  187. // Revision 1.4 2002/03/13 22:05:41 tnussb
  188. // handles now between 1 and 4 characters for the extension
  189. //
  190. // Revision 1.3 2002/02/07 09:49:36 tnussb
  191. // all local includes changed, because header files are now located in pctools folder
  192. //
  193. // Revision 1.2 2000/08/23 19:37:14 Thomas Nussbaumer
  194. // changes due to headerfile and define renaming
  195. //
  196. // Revision 1.1 2000/08/23 01:09:44 Thomas Nussbaumer
  197. // initial version (extracted and clearified from ttbin2oth.c)
  198. //
  199. //
  200. //