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

  1. #include "7plus.h"
  2. #include "globals.h"
  3.  
  4. /*
  5. ***
  6. ***
  7. ***
  8.  */
  9. int correct_meta (char *name, int itsacor)
  10. {
  11.   struct   m_index index;
  12.   FILE     *rfile, *meta, *ifile;
  13.   char     inpath[MAXFPATH], indexfile[MAXPATH], metafile[MAXPATH];
  14.   char     newname[MAXPATH], orgname[13], filename[13];
  15.   char     *p, line[81];
  16.   int      i, j, corrpart, corrline, corrline2, splitsize, length;
  17.   int      num;
  18.   uint     crc, csequence;
  19.   long     binbytes, c_line, offset, oldoffset;
  20.   ulong    ftimestamp;
  21.  
  22.   *indexfile = EOS;
  23.   ftimestamp = 0UL;
  24.   splitsize = corrpart = corrline = corrline2 = num = j = 0;
  25.   crc = csequence = 0U;
  26.   binbytes = offset = 0L;
  27.   oldoffset = -62L;
  28.   rfile = ifile = meta = NULLFP;
  29.  
  30.  
  31.   /* Isolate input-path and filename */
  32.   fnsplit (name, _drive, _dir, _file, _ext);
  33.   sprintf (inpath, "%s%s", _drive, _dir);
  34.   build_DOS_name (_file);
  35.   build_DOS_name (_ext);
  36.   _file[8] = _ext[3] = EOS;
  37.   strcpy (newname, name);
  38.  
  39.   printf ("\n-------------\nCorrecting...\n-------------\n\n");
  40.  
  41.   index.lines_left = 1;
  42.  
  43.   while (index.lines_left)
  44.   {
  45.     if (itsacor)
  46.     {
  47.       if (num)
  48.       {
  49.         if (stricmp( _ext, "cor") && num == 1)
  50.           break;
  51.  
  52.         if (num == 256 || (j > 4))
  53.           break;
  54.  
  55.         sprintf (newname, "%s%s%s.c%02x", _drive, _dir, _file, num);
  56.         if (test_exist (newname))
  57.         {
  58.           j++;
  59.           continue;
  60.         }
  61.         j = 0;
  62.       }
  63.       num++;
  64.  
  65.       if ((i = crc_file (newname, "7PLUS corr", " P00:\n", 1)) != 0)
  66.       {
  67.         if (i != 17)
  68.           return (i);
  69.         if (!force)
  70.         {
  71.           printf ("\n\007If you want to use this cor-file anyway, run 7PLUS again\n");
  72.           printf ("with the '-F' option (force usage). Bear in mind, that this can\n");
  73.           printf ("cause irreparable damage to the metafile! Use at own risk.\n");
  74.           return(7);
  75.         }
  76.       }
  77.     }
  78.     else
  79.      if (num)
  80.        break;
  81.  
  82.     /* Open COR-file */
  83.     if (!(rfile = fopen (newname, OPEN_READ_BINARY)))
  84.     {
  85.       printf (cant, newname);
  86.       return (2);
  87.     }
  88.  
  89.     while ((p = my_fgets (line, 80, rfile)) != NULL)
  90.     {
  91.       if (!strncmp (line, "7PLUS correction:", 17) && itsacor)
  92.         break;
  93.       if (!strncmp (line, " go_7+.", 7) && !itsacor)
  94.         break;
  95.     }
  96.     if (!p)
  97.     {
  98.       printf ("\007\n'%s': invalid correction file. Break.\n", newname);
  99.       fclose (rfile);
  100.       continue;
  101.     }
  102.  
  103.     if (itsacor)
  104.     {
  105.       /* Get info from COR-file */
  106.       sscanf (line, "7PLUS correction: %12s %ld %s [%X]",
  107.                                     orgname, &binbytes, filename, &ftimestamp);
  108.       splitsize = get_hex (filename);
  109.     }
  110.     else
  111.     {
  112.       /* Get info from 7PLUS header */
  113.       if(sscanf (line+8, "%d %s %d %s %ld %s %s %s %s %s",
  114.                   &corrpart, indexfile, indexfile, orgname, &binbytes,
  115.                   indexfile, metafile, indexfile, indexfile, indexfile) == 10)
  116.       {
  117.  
  118.         if (strlen (indexfile) == 5)
  119.           if (!mcrc(line, 2))
  120.             *orgname = EOS;
  121.  
  122.         splitsize = get_hex (metafile);
  123.         strlwr (orgname);
  124.       }
  125.       else
  126.        *orgname = EOS;
  127.  
  128.       if (!*orgname)
  129.       {
  130.         printf ("\007\n'%s': Header is corrupted. Break.\n", newname);
  131.         fclose (rfile);
  132.         break;
  133.       }
  134.       offset = ftell (rfile);
  135.       fseek (rfile, -72L, SEEK_END);
  136.  
  137.       while ((p = my_fgets (line, 80, rfile)) != NULL)
  138.         if (!strncmp (line, " stop_7+.", 9))
  139.           break;
  140.  
  141.       ftimestamp = 0UL;
  142.  
  143.       if (p)
  144.       {
  145.         /* Get timespamp */
  146.         if (strchr (line, '['))
  147.         {
  148.           if (!mcrc (line, 0))
  149.             rebuild (line, 2);
  150.  
  151.           if (mcrc (line, 0))
  152.             if (sscanf (line, " stop_7+. %s [%X]",
  153.                 indexfile, &ftimestamp) != 2)
  154.               ftimestamp = 0UL;
  155.         }
  156.       }
  157.  
  158.       fseek (rfile, offset, SEEK_SET);
  159.       offset = 0L;
  160.  
  161.       num = 1;
  162.     }
  163.  
  164.     if (num == 1)
  165.     {
  166.       /* Strip ext from filename */
  167.       fnsplit (orgname, NULL, NULL, filename, NULL);
  168.       strlwr (filename);
  169.  
  170.       sprintf (metafile , "%s.7mf", filename);
  171.  
  172.       #ifndef _CHSIZE_OK
  173.        sprintf (indexfile, "%s.7ix", filename);
  174.  
  175.        /* Open index file */
  176.        if (!(ifile = fopen (indexfile, OPEN_READ_BINARY)))
  177.        {
  178.          printf (cant, indexfile);
  179.          fclose (rfile);
  180.          return (2);
  181.        }
  182.  
  183.        /* read index info into struct index */
  184.        if (read_index (ifile, &index))
  185.        {
  186.          printf ("\007Invalid index info.\n");
  187.          fclose (rfile);
  188.          return (7);
  189.        }
  190.  
  191.        fclose (ifile);
  192.       #endif
  193.  
  194.       /* Open meta file */
  195.       if (!(meta = fopen (metafile, OPEN_RANDOM_BINARY)))
  196.       {
  197.         printf (cant, metafile);
  198.         fclose (rfile);
  199.         return (2);
  200.       }
  201.  
  202.       #ifdef _CHSIZE_OK
  203.        strcpy (indexfile, metafile);
  204.        ifile = meta;
  205.  
  206.        /* read index info into struct index */
  207.        if (read_index (ifile, &index))
  208.        {
  209.          printf ("\007Invalid index info.\n");
  210.          fclose (rfile);
  211.          return (7);
  212.        }
  213.       #endif
  214.     }
  215.     if (stricmp (orgname, index.filename) ||
  216.         binbytes && binbytes != index.length)
  217.     {
  218.       printf ("\007\nCorrection file '%s.%s' and metafile '%s' do not relate\n",
  219.                                                        _file, _ext, metafile);
  220.       printf (" to the same original file!\n");
  221.       fclose (rfile);
  222.       continue;
  223.     }
  224.     if ((ftimestamp && index.timestamp && (ftimestamp != index.timestamp)) &&
  225.          !force)
  226.     {
  227.       printf ("\007WARNING! The timestamps in the metafile and the correction file\n");
  228.       printf ("'%s.%s' differ!\n", _file, _ext);
  229.       printf ("If you still want to go ahead with the correction, call 7PLUS again\n");
  230.       printf ("with the addition of the '-f' option (force usage).\n");
  231.       printf ("Bear in mind, that this can cause irreparable damage to the metafile!\n");
  232.       printf ("Use at own risk.\n");
  233.       return (18);
  234.     }
  235.  
  236.     printf ("Processing '%s'. Missing lines left: %ld      \r",
  237.                                                    newname, index.lines_left);
  238.     fflush (stdout);
  239.  
  240.     if (!splitsize)
  241.       splitsize = index.splitsize;
  242.  
  243.     while (1==1)
  244.     {
  245.       if (!(p = my_fgets (line, 80, rfile)))
  246.         break;
  247.  
  248.       if (itsacor)
  249.       {
  250.         if (p[0] == ' ' && p[1] == 'P')
  251.         {
  252.           if (!(corrpart = get_hex (p+2)))
  253.             break;
  254.           my_fgets (p, 80, rfile);
  255.         }
  256.         corrline = get_hex (p+2);
  257.         my_fgets (p, 80, rfile);
  258.       }
  259.       else
  260.         crc_n_lnum (&crc, &corrline, p);
  261.  
  262.       c_line = ((long) (corrpart-1) * splitsize) + corrline;
  263.  
  264.       /* Check, if that line is needed */
  265.       if (!(index.lines_ok[(int)(c_line>>4)] & (1U << (uint)(c_line&15L))))
  266.         continue;
  267.  
  268.       /* Get crc from code line */
  269.       crc_n_lnum (&crc, &corrline2, p);
  270.  
  271.       /* Calculate CRC */
  272.       csequence = 0;
  273.       for (i=0; i<64; i++)
  274.         csequence = crctab[csequence>>8] ^ (((csequence&255)<<8) | (byte)p[i]);
  275.       csequence &= 0x3fff; /* strip calculated CRC to 14 bits */
  276.  
  277.       if (csequence != crc)
  278.         if (!rebuild (p, 0))
  279.           /* Incorrect CRC. Ignore line. */
  280.           continue;
  281.         else
  282.           crc_n_lnum (&crc, &corrline2, p);
  283.  
  284.       /* Is it really the right line? */
  285.       if (corrline2 != corrline)
  286.         continue;
  287.  
  288.       /* Calculate offset to metafile and position the read pointer there */
  289.       offset = (long) c_line * 62UL;
  290.       if (offset != (oldoffset + 62UL) || !offset)
  291.         fseek (meta, offset, SEEK_SET);
  292.       oldoffset = offset;
  293.  
  294.       /* Calculate number of valid bytes in the line */
  295.       length = 62;
  296.       if (c_line == ((index.length +61) /62)-1)
  297.       {
  298.         length = (int) index.length % 62;
  299.         if (!length)
  300.           length = 62;
  301.       }
  302.       /* Decode & insert the line into the metafile */
  303.       decode_n_write (meta, p, length);
  304.  
  305.       /* Mark line as present */
  306.       index.lines_ok[(int)(c_line>>4)] &= (0xffffU - (1U << (uint)(c_line&15)));
  307.       index.lines_left--;
  308.       printf ("Processing '%s'. Missing lines left: %ld      \r",
  309.                                                    newname, index.lines_left);
  310.       fflush (stdout);
  311.  
  312.     }
  313.     fclose (rfile);
  314.   }
  315.   printf ("\n");
  316.  
  317.   if (meta)
  318.   {
  319.     #ifdef _CHSIZE_OK
  320.      if (!index.lines_left)
  321.        chsize (fileno(meta), index.length);
  322.     #endif
  323.     fclose (meta);
  324.   }
  325.  
  326. #if (__MSDOS__ || __TOS__)
  327.   p = index.filename;
  328. #else
  329.   p = index.full_name;
  330. #endif
  331.  
  332.   if (index.lines_left)
  333.   {
  334.     if (autokill)
  335.       kill_em (_file, inpath, "cor", "c", NULL, NULL, NULL);
  336.  
  337.     w_index_err (&index, 0);
  338.     printf ("\nCorrection of %s not successful.\n", p);
  339.  
  340.     return (16);
  341.   }
  342.  
  343.   test_file (NULLFP, p, 1, MAXFNAME-1);
  344.   replace (p, metafile, index.timestamp);
  345.   #ifndef _CHSIZE_OK
  346.    unlink (indexfile);
  347.   #endif
  348.  
  349.   if (autokill)
  350.     kill_em (_file, inpath, "err", "e", "cor", "c", NULL);
  351.  
  352.   printf ("\nCorrection successful! '%s', %ld bytes.\n", p, index.length);
  353.  
  354.   return (0);
  355. }
  356.