ttunebk.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. /******************************************************************************
  2. *
  3. * project name: TI-68k Developer Utilities
  4. * file name: ttunebk.c
  5. * initial date: 11/04/2001
  6. * authors: albert@cs.tut.fi
  7. * thomas.nussbaumer@gmx.net
  8. * description: unpacks ebooks
  9. *
  10. * -----------------------------------------------------------------------------
  11. *
  12. * based on code from Pasi 'Albert' Ojala, albert@cs.tut.fi
  13. *
  14. * heavily reduced to fit to the needs by thomas.nussbaumer@gmx.net
  15. *
  16. ******************************************************************************/
  17. /*
  18. This file is part of TI-68k Developer Utilities.
  19. This file is free software; you can redistribute it and/or
  20. modify it under the terms of the GNU Lesser General Public
  21. License as published by the Free Software Foundation; either
  22. version 2.1 of the License, or (at your option) any later version.
  23. As a special exception, UNMODIFIED copies of ttunebk may also be
  24. redistributed or sold without source code, for any purpose. (The Lesser
  25. General Public License restrictions do apply in other respects; for example,
  26. they cover modification of the program.) This exception notice must be
  27. removed on modified copies of this file.
  28. This program is distributed in the hope that it will be useful,
  29. but WITHOUT ANY WARRANTY; without even the implied warranty of
  30. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  31. Lesser General Public License for more details.
  32. You should have received a copy of the GNU Lesser General Public
  33. License along with this library; if not, write to the Free Software
  34. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  35. */
  36. #include <stdio.h>
  37. #include <stdlib.h>
  38. #include <string.h>
  39. #include <ctype.h>
  40. #include "unpack.c"
  41. #include "tt.h" // generic defines
  42. #include "ttversion.h" // TI-68k Developer Utilities version info
  43. #include "revtools.h"
  44. #include "ttunpack.h" // errorcodes definition
  45. #include "packhead.h" // compressed header definition
  46. #include "ttarchive.h" // ttarchive definitions
  47. #ifdef FILE_REVISION
  48. #undef FILE_REVISION
  49. #endif
  50. #define FILE_REVISION "1.3"
  51. //=============================================================================
  52. // outputs usage information of this tool
  53. //=============================================================================
  54. void PrintUsage() {
  55. fprintf(USAGE_OUT, "Usage: ttunebk <infile> <outfile>\n\n"\
  56. " exports the complete text of an ebook to a plain textfile\n\n");
  57. }
  58. //=============================================================================
  59. // our main reason why we are here ...
  60. //=============================================================================
  61. int main(int argc, char* argv[]) {
  62. unsigned char* src;
  63. FILE* fp;
  64. char* infilename = NULL;
  65. char* outfilename = NULL;
  66. unsigned long insize;
  67. int nr_entries;
  68. int i;
  69. PRINT_ID("TTUnEbk");
  70. for (i=1;i<argc;i++) {
  71. if (!infilename) infilename = argv[i];
  72. else if (!outfilename) outfilename = argv[i];
  73. else {
  74. PrintUsage();
  75. return 1;
  76. }
  77. }
  78. //-------------------------------------------
  79. // check if infile and out file are specified
  80. //-------------------------------------------
  81. if (!infilename || !outfilename) {
  82. PrintUsage();
  83. return 1;
  84. }
  85. if (!(fp = fopen(infilename, "rb"))) {
  86. fprintf(stderr, "ERROR: cannot open input file %s\n", infilename);
  87. return 1;
  88. }
  89. fseek(fp,0,SEEK_END);
  90. insize = ftell(fp);
  91. rewind(fp);
  92. // treatment of PC header ...
  93. insize -= 88;
  94. fseek(fp,86,SEEK_SET);
  95. if (!(src = (unsigned char*)malloc(insize))) {
  96. fprintf(stderr, "ERROR: cannot allocate buffer\n");
  97. fclose(fp);
  98. return 1;
  99. }
  100. if (fread(src, 1, insize, fp) != insize) {
  101. fprintf(stderr, "ERROR: cannot read %ld bytes from %s\n",insize,infilename);
  102. fclose(fp);
  103. free(src);
  104. return 1;
  105. }
  106. fclose(fp);
  107. if (!IsTTArchive(src+2)) {
  108. fprintf(stderr, "ERROR: %s is not a valid eBook\n",infilename);
  109. free(src);
  110. return 1;
  111. }
  112. nr_entries = GetNrEntries(src+2);
  113. //printf("%s contains %d entries ...\n",infilename,nr_entries);
  114. if (!(fp = fopen(outfilename, "wb"))) {
  115. fprintf(stderr, "ERROR: cannot open output file %s\n", outfilename);
  116. return 1;
  117. }
  118. for (i=1;i<nr_entries;i++) {
  119. TTAEntry* entry = GetEntryInfo(src+2,i);
  120. unsigned char* data = GetEntryStart(src+2,entry);
  121. PackedHeader* ch = (PackedHeader*)data;
  122. unsigned int origsize;
  123. unsigned char* dest;
  124. int result;
  125. printf("processing entry %d ...\n",i);
  126. if (ch->magic1 != MAGIC_CHAR1 || ch->magic2 != MAGIC_CHAR2) {
  127. fprintf(stderr, "ERROR: format mismatch of part %d (skipping)\n",i);
  128. continue;
  129. }
  130. origsize = ch->origsize_lo | (ch->origsize_hi << 8);
  131. if (!(dest = (unsigned char*)malloc(origsize))) {
  132. fprintf(stderr,"ERROR: cannot allocate %d bytes for output buffer\n",origsize);
  133. free(src);
  134. fclose(fp);
  135. return 1;
  136. }
  137. result=UnPack(data,dest);
  138. if (result) {
  139. fprintf(stderr,"ERROR: cannot decompress (code %03d)\n",result);
  140. free(src);
  141. free(dest);
  142. fclose(fp);
  143. return 1;
  144. }
  145. if (fwrite(dest,1,origsize,fp) != origsize) {
  146. fprintf(stderr,"ERROR: cannot decompress (code %03d)\n",result);
  147. free(src);
  148. free(dest);
  149. fclose(fp);
  150. return 1;
  151. }
  152. printf("%d bytes written to %s\n",origsize,outfilename);
  153. free(dest);
  154. }
  155. free(src);
  156. fclose(fp);
  157. return 0;
  158. }
  159. //#############################################################################
  160. //###################### NO MORE FAKES BEYOND THIS LINE #######################
  161. //#############################################################################
  162. //
  163. //=============================================================================
  164. // Revision History
  165. //=============================================================================
  166. //
  167. // Revision 1.3 2009/01/25 Lionel Debroux
  168. // Changes by Romain Liévin and/or me for 64-bit compatibility.
  169. // Adapt to new version display (revtools.h).
  170. //
  171. // Revision 1.2 2002/02/07 09:49:38 tnussb
  172. // all local includes changed, because header files are now located in pctools folder
  173. //
  174. // Revision 1.1 2001/04/11 22:59:40 Thomas Nussbaumer
  175. // initial version
  176. //
  177. //