home *** CD-ROM | disk | FTP | other *** search
/ Media Share 9 / MEDIASHARE_09.ISO / hamradio / 7plus202.zip / 7PL2SRC.LZH / 7PLUS.C < prev    next >
C/C++ Source or Header  |  1992-06-11  |  15KB  |  460 lines

  1. /* Version */
  2. #define _7P_VMAJOR 2
  3. #define _7P_VMINOR 00
  4. #define VERSION "2.00"
  5. #define DATE "920611"
  6.  
  7. /*********************************************************************
  8. *** 7PLUS ASCII- Encoder/Decoder, Axel Bauda, DG1BBQ @DB0CL.DEU.EU  ***
  9. **********************************************************************
  10. ***
  11. *** Compile:
  12. *** --------
  13. ***
  14. *** -------
  15. *** | IBM |  : SMALL memory model
  16. *** -------
  17. ***            Turbo C++ and Turbo C 2.0:
  18. ***            MAKE -FTC_MAKE
  19. ***
  20. ***      also: Turbo C++:
  21. ***            Compile in IDE using 7PLUS.PRJ
  22. ***
  23. ***      or  : Turbo C 2.0:
  24. ***            Rename TC2.PRJ to 7PLUS.PRJ and compile in IDE
  25. ***
  26. *** ---------
  27. *** | ATARI |  Turbo C 2.0 : Use 7PLST.PRJ
  28. *** ---------
  29. *** --------
  30. *** | UNIX |   support by Torsten H. Bischoff, DF6NL @ DB0BOX
  31. *** --------
  32. *** Supported systems are:
  33. ***   Interactive UNIX 386
  34. ***   SCO XENIX 386
  35. ***   VAX BSD 4.3/ Ultrix 4.1
  36. ***
  37. *** Compile: make -fu_make
  38. ***
  39. *** -------
  40. *** |AMIGA|    Manx Aztec C 5.1b/5.2:  make -fa_make
  41. *** -------
  42. ***
  43. *** All systems:
  44. *** Signed or unsigned char per default is: don't care.
  45. *** No floating point lib required.
  46. ***
  47. *** TABSIZE when editing: 2; don't insert real TABs (^I), use spaces instead.
  48. ***
  49. *** When porting or modifying this source, make SURE it can still be compiled
  50. *** on all systems! Do this by using #ifdef directives! Please let me know
  51. *** about the modifications or portations, so I can include them in the origi-
  52. *** nal 7PLUS.
  53. ***
  54. **********************************************************************
  55. ***  7PLUS ASCII-Encoder/Decoder, Axel Bauda, DG1BBQ @DB0CL.DEU.EU  ***
  56. **********************************************************************
  57. ***
  58. *** File converter for transfer of arbitrary binary data
  59. *** via store & forward.
  60. ***
  61. *** 7PLUS is HAMWARE. No commercial use. Pass on only in it's entirety!
  62. *** There is no warranty for the proper functioning. Use at own risk.
  63. ***
  64. *** Tnx to:
  65. *** DB7YAH, DG3YFU and DL6YAV for testing and tips regarding ATARI ST.
  66. *** DB5ZP for his efforts concerning the AMIGA implementation.
  67. *** DF6NL for adding UNIX-compatibility.
  68. ***
  69.  */
  70.  
  71. #include "7plus.h"
  72.  
  73. /** globals **/
  74. uint    crctab[256];
  75. byte    decode[256];
  76. byte    code  [216];
  77. byte    extended = INDICATE;
  78. size_t  buflen;
  79. char    _drive[MAXDRIVE], _dir[MAXDIR], _file[MAXFILE], _ext[MAXEXT];
  80. char    spaces[] = "                                                   ";
  81. char    *endstr;
  82. char    delimit[] = "\n";
  83. char    cant[] = "\007\n'%s': Can't open. Break.\n";
  84. char    notsame[] = "\007Filesize in %s differs from the original file!\nBreak.\n";
  85. int     noquery = 0;
  86. int     force   = 0;
  87. int     fls     = 0;
  88. int     autokill= 0;
  89.  
  90. #ifdef __TOS__
  91.   int   nowait  = 0;
  92. #endif
  93.  
  94. char logon_ctrl[] =
  95. #ifdef __MSDOS__
  96. "\n╔════════════════════════════════════════════════╗\n║%s║\n║%s║\n╚══════════\
  97. ══════════════════════════════════════╝\n";
  98. #else
  99.  #ifdef __TOS__
  100. "\033p\033v\n%s\n%s\n\033q\n";
  101.  #else
  102.   #ifdef _AMIGA_
  103. "\n\033[3m%s\n%s\033[0m\n\n";
  104.   #else    /* neither __MSDOS__ or __TOS__ or _AMIGA_ */
  105. "\n[]----------------------------------------------[]\n|%s|\n|%s|\n[]---------\
  106. -------------------------------------[]\n";
  107.   #endif   /* ifdef _AMIGA_   */
  108.  #endif   /* ifdef __TOS__   */
  109. #endif   /* ifdef __MSDOS__ */
  110.  
  111. char *logon[] = { "   7PLUS - file converter for store & forward   ",
  112.                   " version "VERSION" ("DATE"), (C) DG1BBQ@DB0CL.DEU.EU " };
  113.  
  114. char *help[] = {
  115. #ifdef _AMIGA_
  116. "\n\033[1mExamples:\n",
  117. "7plus <FILE.7pl>      Decode <FILE.7pl>. create <FILE.err>, if errors detected\n",
  118. "7plus <FILE.p01>      Recombine & decode.                    \"\n",
  119. "7plus <FILE>          Automatically correct and decode.\n",
  120. "                      If a file named <FILE> exists, encode it.\n",
  121. "7plus <FILE>          Encode (automatically split into 10K chunks).\n",
  122. "             -s 30    30 lines/file (max 512 lines/file).\n",
  123. "             -sp 3    3 parts of roughly equal size (max 255 parts).\n",
  124. "             -sb 3000 Parts of roughly 3000 bytes (max 36000).\n",
  125. "7plus <FILE.err> dh0:/pr/ Create correction file. look for <FILE> in dh0:/pr/.\n",
  126. "7plus <FILE.cor>      Use <FILE.cor> to correct metafile <FILE.7mf>.\n",
  127. "7plus <FILE.7pl> -c   Use 7PLUS-file as a correction file.\n",
  128. "7plus <LOGFILE> -x <TEXT> Extract 7plus-files from <LOGFILE>. Only extract a\n",
  129. "                      file, if its name contains <TEXT>. Omit <TEXT> to\n",
  130. "                      extract files in <LOGFILE>.\n",
  131. "7plus <FILE.err> <FILE2.err> -j  Add contents of error report <FILE2err> to\n",
  132. "                      error report <FILE.err>.\n",
  133. "7plus <FILE.err> -j   Add contents of error reports <FILE.eXX> to error\n",
  134. "                      report <FILE.err> (multiple join).\n",
  135. #ifdef _CHSIZE_OK
  136. "7plus <FILE.7mf>      Create new error report from metafile.\n\n",
  137. #else
  138. "7plus <FILE.7ix>      Create new error report from indexfile.\n\n",
  139. #endif
  140. "Other Options:\n",
  141. "-k           Automatically kill all obsolete files.\n",
  142. "-p           Use Packet line separator CR for encoded files. Should be\n",
  143. "             used, when uploading files to the BBS in binary mode!\n",
  144. "-r 5         When encoding, only create part 5. Be sure to split the same\n",
  145. "             way as for the first upload!\n",
  146. "-t /ex       Append string '/ex' to encoded files (BBS file termination).\n",
  147. "-tb <FILE>   Get head and footlines from <FILE> when encoding. See manual.\n",
  148. "-y           Assume YES on all queries.\n",
  149. "-#           Create 7PLUS.FLS when encoding. See Manual.\n",
  150. "\033[0m\n\n",
  151. #else
  152. "\nExamples:\n",
  153. "7plus file.7pl          Decode file.7pl. create file.err, if errors detected.\n",
  154. "7plus file.p01          Recombine & decode.              \"\n",
  155. "7plus file              Automatically correct and decode.\n",
  156. "                        If a file named 'file' exists, encode it.\n",
  157. "7plus file.txt          Encode (automatically split into 10K chunks).\n",
  158. "               -s 30    30 lines/file (max 512 lines/file).\n",
  159. "               -sp 3    3 parts of roughly equal size (max 255 parts).\n",
  160. "               -sb 3000 Parts of roughly 3000 bytes (max 36000).\n",
  161. "7plus file.err c:\\pr\\   Create correction file. look for file.txt in c:\\pr\\.\n",
  162. "7plus file.cor          Use file.cor to correct metafile file.7mf.\n",
  163. "7plus file.7pl -c       Use 7PLUS-file as a correction file.\n",
  164. "7plus logfile -x text   Extract 7plus-files from logfile. Only extract a file\n",
  165. "                        if its name contains 'text'. Omit 'text' to extract\n",
  166. "                        all files in logfile.\n",
  167. "7plus file.err file2.err -j  Add contents of error report file2.err to\n",
  168. "                        error report file.err.\n",
  169. "7plus file.err -j       Add contents of error reports file.eXX to error\n",
  170. "                        report file.err (multiple join).\n",
  171. #ifdef _CHSIZE_OK
  172. "7plus file.7mf          Create new error report from metafile.\n\n",
  173. #else
  174. "7plus file.7ix          Create new error report from indexfile.\n\n",
  175. #endif
  176. "Other Options:\n",
  177. "-k           Automatically kill all obsolete files.\n",
  178. #ifdef __TOS__
  179. "-n           Don't wait for a keystroke at termination.\n",
  180. #endif
  181. "-p           Use Packet line separator CR for encoded files. Should be\n",
  182. "             used, when uploading files to the BBS in binary mode!\n",
  183. "-r 5         When encoding, only create part 5. Be sure to split the same\n",
  184. "             way as for the first upload!\n",
  185. "-t /ex       Append string '/ex' to encoded files (BBS file termination).\n",
  186. "-tb file     Get head and footlines from 'file' when encoding. See manual.\n",
  187. "-y           Assume YES on all queries.\n",
  188. "-#           Create 7PLUS.FLS when encoding. See manual.\n",
  189. #endif
  190. NULLCP
  191. };
  192.  
  193.  
  194. /* Depending on the system, it may be nessesary to prompt the user for a
  195.    keystroke, before terminating, because user wouldn't be able to read
  196.    the outputs to the screen, when the window closes at termination.
  197.    However, the '-n' option overrides this. */
  198. int main (int argc, char **argv)
  199. {
  200. #ifdef __TOS__
  201.   int ret;
  202.   
  203.   ret = go_at_it (argc, argv);
  204.   if ((!nowait || noquery) && !system(NULLCP))
  205.   {
  206.     printf("\n\033p Hit any key \033q");
  207.     getch();
  208.   }
  209.   return (ret);
  210. #else
  211.   return (go_at_it (argc, argv));
  212. #endif
  213. }
  214.  
  215. /* This is the real main() */
  216. int go_at_it (int argc, char **argv)
  217. {
  218.   char *p, *q, *r, *s, *t;
  219.   int  i, extract, join, part, cor;
  220.   long blocksize;
  221.  
  222.   i = extract = join = part = cor = 0;
  223.   p = q = r = s = t = endstr = NULLCP;
  224.  
  225.  
  226.   /* Default blocksize (abt 10000 bytes) */
  227.   blocksize = 138 * 62;
  228.  
  229.   printf (logon_ctrl, logon[0], logon[1]);
  230.  
  231.   while (++i<argc)
  232.   {
  233.     if (*argv[i] != '-')
  234.     {
  235.       if (!p)
  236.       {
  237.         p = argv[i];  /* Name of file to de/encode */
  238.         continue;
  239.       }
  240.       if (!r)
  241.       {
  242.         r = argv[i];  /* Searchpath for non-coded file. Needed for */
  243.         continue;     /* generating correction file */
  244.       }
  245.     }
  246.  
  247.     if (!stricmp (argv[i], "-S")) /* Split option */
  248.     {
  249.       i++;
  250.       if (i == argc)
  251.       {
  252.         blocksize = 512 * 62;  /* No parameter, set max blocksize */
  253.         i--;
  254.       }
  255.       else
  256.         blocksize = atol (argv[i]) * 62; /* Set blocksize to parameter */
  257.     }
  258.  
  259.     if (!stricmp (argv[i], "-SP")) /* Split in equal parts */
  260.     {
  261.       i++;
  262.       if (i == argc)
  263.       {
  264.         blocksize = 0; /* No parameter, no user defined split */
  265.         i--;
  266.       }
  267.       else
  268.         blocksize = 50000L + atol (argv[i]); /* Number of parts to encode */
  269.     }
  270.  
  271.     if (!stricmp (argv[i], "-SB")) /* Split in parts of n bytes */
  272.     {
  273.       i++;
  274.       if (i == argc)
  275.         i--;
  276.       else
  277.         blocksize = (atol (argv[i]) /71 -2) *62;
  278.     }
  279.  
  280.     if (!stricmp (argv[i], "-R")) /* Only reencode part n */
  281.     {
  282.       i++;
  283.       if (i == argc)
  284.         i--;
  285.       else
  286.         part = atoi (argv[i]);
  287.     }
  288.  
  289.     if (!stricmp (argv[i], "-TB")) /* File to get head and foot lines from */
  290.     {
  291.       i++;
  292.       if (i == argc)
  293.         i--;
  294.       else
  295.         t = argv[i];
  296.     }
  297.  
  298.     if (!stricmp (argv[i], "-T")) /* Define BBSs termination string */
  299.     {
  300.       i++;
  301.       if (i == argc)
  302.         i--;
  303.       else
  304.       {
  305.         endstr = malloc ((int) strlen (argv[i]) +1);
  306.         strcpy (endstr, argv[i]);
  307.       }
  308.     }
  309.  
  310.     if (!stricmp (argv[i], "-#")) /* Create 7PLUS.FLS. Contents e.g.:     */
  311.       fls = 1;                    /* 10 TEST */
  312.                                   /* for TEST.EXE encoded into 10 parts   */
  313.  
  314.     if (!stricmp (argv[i], "-C")) /* Use 7PLUS-file as a correction file  */
  315.       cor = 1;
  316.  
  317.     if (!stricmp (argv[i], "-K")) /* Kill obsolete files */
  318.       autokill = 1;
  319.  
  320.     if (!stricmp (argv[i], "-F")) /* Force usage of correction file */
  321.       force = 1;
  322.  
  323.     if (!stricmp (argv[i], "-J")) /* Join two error reports */
  324.       join = 1;
  325.  
  326.     #ifdef __TOS__
  327.     if (!stricmp (argv[i], "-N")) /* Don't wait for a key at termination  */
  328.       nowait = 1;
  329.     #endif
  330.  
  331.     if (!stricmp (argv[i], "-P")) /* Write encoded files in Packet format */
  332.       sprintf (delimit, "\r");    /* for direct binary upload. */
  333.  
  334.     if (!stricmp (argv[i], "-X")) /* Extract 7plus-files from log-file    */
  335.       extract = 1;
  336.  
  337.     if (!stricmp (argv[i], "-VMAJOR")) /* Return version number +100      */
  338.       return (_7P_VMAJOR +100);
  339.  
  340.     if (!stricmp (argv[i], "-VMINOR")) /* Return subversion number + 100  */
  341.       return (_7P_VMINOR+100);
  342.  
  343.     if (!stricmp (argv[i], "-Y")) /* Always assume YES on queries.*/
  344.       noquery = 1;
  345.  
  346.   }
  347.   if (!p ) /* No File specified, show help */
  348.   {
  349.     int n = 4;
  350.     i = 0;
  351.     while (help[i])
  352.     {
  353.       if (++n == 24 && !noquery)
  354.       {
  355.         printf ("Press any key to continue....\r");
  356.         fflush (stdout);
  357.         while (!getch ());
  358.         fflush (stdin);
  359.         n = 0;
  360.         printf ("                             \r");
  361.       }
  362.       printf (help[i++]);
  363.     }
  364.     return (0);
  365.   }
  366.  
  367.   if ((s = malloc (32768U)) == NULLCP)
  368.   {
  369.      printf ("\007Argh error: Not enough memory present! Can't continue.....\n");
  370.      exit (12);
  371.   }
  372.   free (s);
  373.  
  374.   buflen = 16384;
  375.  
  376.   init_crctab (); /* Initialize table for CRC-calculation */
  377.   init_decodetab (); /* decoding-table */
  378.   init_codetab   (); /* encoding-table */
  379.  
  380.   if (extract)
  381.     if (p)
  382.       return (extract_files (p, r));
  383.     else
  384.     {
  385.       printf ("\007File to extract from not specified. Break.\n");
  386.       return (6);
  387.     }
  388.   if (join)
  389.     return (join_control (p, r));
  390.  
  391.   /* Does the filename contain an extension? */
  392.   if ((q = strrchr (p, '.')) != NULLCP)
  393.   {
  394.     char test[3][2];
  395.  
  396.     if (cor)
  397.       return (correct_meta (p, 0));
  398.  
  399.     if (!strnicmp (".cor", q, 4) ||
  400.         sscanf (q, ".%1[cC]%1[0-9a-fA-F]%1[0-9a-fA-F]",  test[0], test[1], test[2]) == 3)
  401.       return (correct_meta (p, 1));
  402.  
  403.     /* Call decode_file() if ext ist 7PL, P01, else encode_file() */
  404.     if (!strnicmp (".7pl", q, 4) || !strnicmp (".p01", q, 4))
  405.       return (control_decode (p));
  406.  
  407.     #ifdef _CHSIZE_OK
  408.      if (!strnicmp (".7mf", q, 4))
  409.        return(make_new_err (p));
  410.     #else
  411.      if (!strnicmp (".7ix", q, 4))
  412.        return(make_new_err (p));
  413.     #endif
  414.  
  415.     return (encode_file (p, blocksize, r, part, t));
  416.   }
  417.   else
  418.   {
  419.     if (!test_exist (p)) /* no EXT, but file exists on disk, then encode */
  420.       return (encode_file (p, blocksize, r, part, t));
  421.     else
  422.       return (control_decode (p));
  423.   }
  424. }
  425.  
  426. /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*
  427.  *+ Possible return codes:                                                 +*
  428.  *++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*
  429.  
  430.   0 No errors detected.
  431.   1 Write error.
  432.   2 File not found.
  433.   3 7PLUS header not found.
  434.   4 File does not contain expected part.
  435.   5 7PLUS header corrrupted.
  436.   6 No filename for extracting defined.
  437.   7 invalid error report / correction / index file.
  438.   8 Max number of parts exceeded.
  439.   9 Bit 8 stripped.
  440.  10 User break in test_file();
  441.  11 Error report generated.
  442.  12 Only one or no error report to join
  443.  13 Error report/cor-file does not relate to the same original file
  444.  14 Couldn't write 7plus.fls
  445.  15 Filesize of original file and the size reported in err/cor-file not equal
  446.  16 Correction not successful.
  447.  17 No CRC found in err/cor-file.
  448.  18 Timestamp in metafile differs from that in the correction file.
  449.  19 Metafile already exists.
  450.  
  451.  1XX If invoked with '-VMAJOR' option, 7PLUS returns version number.
  452.      e.g.: 101  <--- v1.5
  453.          : 108  <--- v8.8
  454.  
  455.      Invoked with '-VMINOR':
  456.      e.g.: 150  <--- v1.5
  457.          : 180  <--- v8.8
  458.  
  459.  *++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
  460.