Logo Search packages:      
Sourcecode: cbmconvert version File versions  Download package

static int read_sector ( void   )  [static]

Decode a sector

Returns:
0 on success; 1 on failure

Definition at line 143 of file zip2disk.c.

References infile, inname, max_sect, track, trackbuf, and trackbuf_decoded.

Referenced by read_track().

{
  int trk, sec, ch;

  trk = fgetc (infile);
  sec = fgetc (infile);

  if ((trk & 0x3f) != track || sec < 0 || sec >= max_sect ||
      trackbuf_decoded[sec]) {
  Error:
    fprintf (stderr, "zip2disk: Input file %s is corrupted.\n", inname);
    return 1;
  }

  trackbuf_decoded[sec] = 1;

  /* RLE compressed sector */
  if (trk & 0x80) {
    /* number of decompressed bytes */
    int count = 0;
    /* length of the compressed stream, and the escape character */
    int len = fgetc (infile), esc = fgetc (infile);

    while (len--) {
      ch = fgetc (infile);
      if (ch != esc) {
      trackbuf[sec][count++] = ch;
      if (count > 256)
        goto Error;
      }
      else if (len >= 2) {
      /* escape character: get the number of repetitions */
      int repnum = fgetc (infile);
      /* get the repeated character */
      ch = fgetc (infile);
      if (repnum < 0 || repnum + count > 256 || ch == EOF)
        goto Error;
      memset (trackbuf[sec] + count, ch, repnum);
      count += repnum;
      len -= 2;
      }
      else
      goto Error;
    }

    if (count != 256)
      goto Error;
  }
  /* whole sector filled with a single character */
  else if (trk & 0x40) {
    if ((ch = fgetc (infile)) == EOF)
      goto Error;
    memset (trackbuf[sec], ch, 256);
  }
  /* no compression */
  else if (256 != fread (trackbuf[sec], 1, 256, infile))
    goto Error;

  return 0;
}


Generated by  Doxygen 1.6.0   Back to index