home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 9 Archive / 09-Archive.zip / zip21.zip / zipup.c < prev   
C/C++ Source or Header  |  1996-04-23  |  19KB  |  683 lines

  1. /*
  2.  
  3.  Copyright (C) 1990-1996 Mark Adler, Richard B. Wales, Jean-loup Gailly,
  4.  Kai Uwe Rommel, Onno van der Linden and Igor Mandrichenko.
  5.  Permission is granted to any individual or institution to use, copy, or
  6.  redistribute this software so long as all of the original files are included,
  7.  that it is not sold for profit, and that this copyright notice is retained.
  8.  
  9. */
  10.  
  11. /*
  12.  *  zipup.c by Mark Adler and Jean-loup Gailly.
  13.  */
  14.  
  15. #define NOCPYRT         /* this is not a main module */
  16. #include <ctype.h>
  17. #include "zip.h"
  18.  
  19. #ifndef UTIL            /* This module contains no code for Zip Utilities */
  20.  
  21. #include "revision.h"
  22. #include "crypt.h"
  23. #ifdef OS2
  24. #  include "os2/os2zip.h"
  25. #endif
  26.  
  27. #if defined(MMAP)
  28. #  include <sys/mman.h>
  29. #  ifndef PAGESIZE   /* used to be SYSV, what about pagesize on SVR3 ? */
  30. #    define PAGESIZE getpagesize()
  31. #  endif
  32. #endif
  33.  
  34.  
  35. /* Use the raw functions for MSDOS and Unix to save on buffer space.
  36.    They're not used for VMS since it doesn't work (raw is weird on VMS).
  37.    (This sort of stuff belongs in fileio.c, but oh well.) */
  38.  
  39. #ifdef AMIGA
  40. #  include "amiga/zipup.h"
  41. #endif /* AMIGA */
  42.  
  43. #ifdef AOSVS
  44. #  include "aosvs/zipup.h"
  45. #endif /* AOSVS */
  46.  
  47. #ifdef ATARI
  48. #  include "atari/zipup.h"
  49. #endif
  50.  
  51. #ifdef __human68k__
  52. #  include "human68k/zipup.h"
  53. #endif /* __human68k__ */
  54.  
  55. #ifdef DOS
  56. #  include "msdos/zipup.h"
  57. #endif /* DOS */
  58.  
  59. #ifdef OS2
  60. #  include "os2/zipup.h"
  61. #endif /* OS2 */
  62.  
  63. #ifdef RISCOS
  64. #  include "acorn/zipup.h"
  65. #endif
  66.  
  67. #ifdef TOPS20
  68. #  include "tops20/zipup.h"
  69. #endif
  70.  
  71. #ifdef UNIX
  72. #  include "unix/zipup.h"
  73. #endif
  74.  
  75. #ifdef CMS_MVS
  76. #  include "vmcms/zipup.h"
  77. #endif /* CMS_MVS */
  78.  
  79. #ifdef VMS
  80. #  include "vms/zipup.h"
  81. #endif /* VMS */
  82.  
  83. #ifdef WIN32
  84. #ifdef WIZZIPDLL
  85. #  include "win32\zipup.h"
  86. #else
  87. #  include "win32/zipup.h"
  88. #endif
  89. #endif
  90.  
  91.  
  92. /* Deflate "internal" global data (currently not in zip.h) */
  93. #if defined(MMAP) || defined(BIG_MEM)
  94.   extern uch * window;          /* Used to read all input file at once */
  95. #endif
  96. extern ulg window_size;         /* size of said window */
  97.  
  98. /* Local data */
  99.  
  100.   local ulg crc;       /* crc on uncompressed file data */
  101.   local ftype ifile;   /* file to compress */
  102. #if defined(MMAP) || defined(BIG_MEM)
  103.   local long remain;
  104.   /* window bytes not yet processed.
  105.    *  >= 0 only for BIG_MEM or MMAP, -1 for normal reads.
  106.    */
  107. #endif /* MMAP || BIG_MEM */
  108. #ifdef DEBUG
  109.   ulg isize;           /* input file size. global only for debugging */
  110. #else /* !DEBUG */
  111.   local ulg isize;     /* input file size. */
  112. #endif /* ?DEBUG */
  113.  
  114. /* Local functions */
  115. #ifndef RISCOS
  116.    local int suffixes OF((char *, char *));
  117. #else
  118.    local int filetypes OF((char *, char *));
  119. #endif
  120.  
  121.  
  122. int percent(n, m)
  123. ulg n;
  124. ulg m;               /* n is the original size, m is the new size */
  125. /* Return the percentage compression from n to m using only integer
  126.    operations */
  127. {
  128.   if (n > 0xffffffL)            /* If n >= 16M */
  129.   {                             /*  then divide n and m by 256 */
  130.     n += 0x80;  n >>= 8;
  131.     m += 0x80;  m >>= 8;
  132.   }
  133.   return n > m ? (int)(1 + (200 * (n - m)/n)) / 2 : 0;
  134. }
  135.  
  136. #ifndef RISCOS
  137.  
  138. local int suffixes(a, s)
  139. char *a;                /* name to check suffix of */
  140. char *s;                /* list of suffixes separated by : or ; */
  141. /* Return true if a ends in any of the suffixes in the list s. */
  142. {
  143.   int m;                /* true if suffix matches so far */
  144.   char *p;              /* pointer into special */
  145.   char *q;              /* pointer into name a */
  146.  
  147.   m = 1;
  148. #ifdef VMS
  149.   if( (q = strrchr(a,';')) != NULL )    /* Cut out VMS file version */
  150.     --q;
  151.   else
  152.     q = a + strlen(a) - 1;
  153. #else /* !VMS */
  154.   q = a + strlen(a) - 1;
  155. #endif /* ?VMS */
  156.   for (p = s + strlen(s) - 1; p >= s; p--)
  157.     if (*p == ':' || *p == ';')
  158.       if (m)
  159.         return 1;
  160.       else
  161.       {
  162.         m = 1;
  163. #ifdef VMS
  164.         if( (q = strrchr(a,';')) != NULL )      /* Cut out VMS file version */
  165.           --q;
  166.         else
  167.           q = a + strlen(a) - 1;
  168. #else /* !VMS */
  169.         q = a + strlen(a) - 1;
  170. #endif /* ?VMS */
  171.       }
  172.     else
  173.     {
  174.       m = m && q >= a && case_map(*p) == case_map(*q);
  175.       q--;
  176.     }
  177.   return m;
  178. }
  179.  
  180. #else /* RISCOS */
  181.  
  182. local int filetypes(a, s)
  183. char *a;                /* extra field of file to check filetype of */
  184. char *s;                /* list of filetypes separated by : or ; */
  185. /* Return true if a is any of the filetypes in the list s. */
  186. {
  187.  char *p;              /* pointer into special */
  188.  char typestr[4];     /* filetype hex string taken from a */
  189.  
  190.  if (((unsigned*)a)[2] & 0xFFF00000 != 0xFFF00000) {
  191.  /* The file is not filestamped, always try to compress it */
  192.    return 0;
  193.  }
  194.  
  195.  sprintf(typestr,"%.3X",(((unsigned*)a)[2] & 0x000FFF00) >> 8);
  196.  
  197.  for (p=s;p<=s+strlen(s)-3;p+=3) { /* p+=3 to skip 3 hex type */
  198.    while (*p==':' || *p==';')
  199.      p++;
  200.  
  201.    if (typestr[0]==toupper(p[0]) && typestr[1]==toupper(p[1]) && typestr[2]==toupper(p[2]))
  202.      return 1;
  203.  }
  204.  return 0;
  205. }
  206. #endif /* ?RISCOS */
  207.  
  208. /* Note: a zip "entry" includes a local header (which includes the file
  209.    name), an encryption header if encrypting, the compressed data
  210.    and possibly an extended local header. */
  211.  
  212. int zipup(z, y)
  213. struct zlist far *z;    /* zip entry to compress */
  214. FILE *y;                /* output file */
  215. /* Compress the file z->name into the zip entry described by *z and write
  216.    it to the file *y. Encrypt if requested.  Return an error code in the
  217.    ZE_ class.  Also, update tempzn by the number of bytes written. */
  218. {
  219.   ztimbuf f_utim;       /* UNIX GMT timestamps, filled by filetime() */
  220.   ulg tim;              /* time returned by filetime() */
  221.   ulg a = 0L;           /* attributes returned by filetime() */
  222.   char *b;              /* malloc'ed file buffer */
  223.   extent k = 0;         /* result of zread */
  224.   int l = 0;            /* true if this file is a symbolic link */
  225.   int m;                /* method for this entry */
  226.   ulg o, p;             /* offsets in zip file */
  227.   long q = -3L;         /* size returned by filetime */
  228.   int r;                /* temporary variable */
  229.   ulg s = 0L;           /* size of compressed data */
  230.   int isdir;            /* set for a directory name */
  231.   int set_type = 0;     /* set if file type (ascii/binary) unknown */
  232.  
  233.   z->nam = strlen(z->zname);
  234.   isdir = z->zname[z->nam-1] == '/';
  235.  
  236.   if ((tim = filetime(z->name, &a, &q, &f_utim)) == 0 || q < -2L)
  237.     return ZE_OPEN;
  238.  
  239.   /* q is set to -1 if the input file is a device, -2 for a volume label */
  240.   if (q == -2L) {
  241.      isdir = 1;
  242.      q = 0;
  243.   } else if (isdir != ((a & MSDOS_DIR_ATTR) != 0)) {
  244.      /* don't overwrite a directory with a file and vice-versa */
  245.      return ZE_MISS;
  246.   }
  247.   z->att = (ush)UNKNOWN; /* will be changed later */
  248.   z->atx = 0; /* may be changed by set_extra_field() */
  249.  
  250.   /* Free the old extra fields which are probably obsolete */
  251.   if (z->ext) {
  252.     free((zvoid *)(z->extra));
  253.   }
  254.   if (z->cext && z->extra != z->cextra) {
  255.     free((zvoid *)(z->cextra));
  256.   }
  257.   z->ext = z->cext = 0;
  258.  
  259. #if defined(MMAP) || defined(BIG_MEM)
  260.   remain = -1L; /* changed only for MMAP or BIG_MEM */
  261. #endif /* MMAP || BIG_MEM */
  262.   window_size = 0L;
  263.  
  264.   /* Select method based on the suffix and the global method */
  265. #ifndef RISCOS
  266.   m = special != NULL && suffixes(z->name, special) ? STORE : method;
  267. #else /* RISCOS  must set m after setting extra field */
  268.   m = method;
  269. #endif /* ?RISCOS */
  270.  
  271.   /* Open file to zip up unless it is stdin */
  272.   if (strcmp(z->name, "-") == 0)
  273.   {
  274.     ifile = (ftype)zstdin;
  275. #if defined(MSDOS) || defined(__human68k__)
  276.     setmode(zstdin, O_BINARY);
  277. #endif
  278.     z->tim = tim;
  279.   }
  280.   else
  281.   {
  282. #if !(defined(VMS) && defined(VMS_PK_EXTRA))
  283.     if (extra_fields) {
  284.       /* create extra field and change z->att and z->atx if desired */
  285.       set_extra_field(z, &f_utim);
  286. #ifdef RISCOS
  287.       m = special != NULL && filetypes(z->extra, special) ? STORE : method;
  288. #endif /* RISCOS */
  289.     }
  290. #endif /* !(VMS && VMS_PK_EXTRA) */
  291.     l = issymlnk(a);
  292.     if (l)
  293.       ifile = fbad;
  294.     else if (isdir) { /* directory */
  295.       ifile = fbad;
  296.       m = STORE;
  297.       q = 0;
  298.     }
  299.     else {
  300. #ifdef CMS_MVS
  301.       if (bflag) {
  302.         if ((ifile = zopen(z->name, fhowb)) == fbad)
  303.            return ZE_OPEN;
  304.       }
  305.       else
  306. #endif /* CMS_MVS */
  307.       if ((ifile = zopen(z->name, fhow)) == fbad)
  308.          return ZE_OPEN;
  309.     }
  310.  
  311.     z->tim = tim;
  312.  
  313. #if defined(VMS) && defined(VMS_PK_EXTRA)
  314.     /* vms_get_attributes must be called after vms_open() */
  315.     if (extra_fields) {
  316.       /* create extra field and change z->att and z->atx if desired */
  317.       vms_get_attributes(ifile, z, &f_utim);
  318.     }
  319. #endif /* VMS && VMS_PK_EXTRA */
  320.  
  321. #ifdef MMAP
  322.     /* Map ordinary files but not devices. This code should go in fileio.c */
  323.     if (q > 0 && !translate_eol) {
  324.       if (window != NULL)
  325.         free(window);  /* window can't be a mapped file here */
  326.       window_size = q + MIN_LOOKAHEAD;
  327.       remain = window_size & (PAGESIZE-1);
  328.       /* If we can't touch the page beyond the end of file, we must
  329.        * allocate an extra page.
  330.        */
  331.       if (remain > MIN_LOOKAHEAD) {
  332.         window = (uch*)mmap(0, window_size, PROT_READ, MAP_PRIVATE, ifile, 0);
  333.       } else {
  334.         window = (uch*)valloc(window_size - remain + PAGESIZE);
  335.         if (window != NULL) {
  336.           window = (uch*)mmap((char*)window, window_size - remain, PROT_READ,
  337.                         MAP_PRIVATE | MAP_FIXED, ifile, 0);
  338.         } else {
  339.           window = (uch*)(-1);
  340.         }
  341.       }
  342.       if (window == (uch*)(-1)) {
  343.         Trace((mesg, " mmap failure on %s\n", z->name));
  344.         window = NULL;
  345.         window_size = 0L;
  346.         remain = -1L;
  347.       } else {
  348.         remain = q;
  349.       }
  350.     }
  351. #else /* !MMAP */
  352. # ifdef BIG_MEM
  353.     /* Read the whole input file at once */
  354.     if (q > 0 && !translate_eol) {
  355.       window_size = q + MIN_LOOKAHEAD;
  356.       window = window ? (uch*) realloc(window, (unsigned)window_size)
  357.                       : (uch*) malloc((unsigned)window_size);
  358.       /* Just use normal code if big malloc or realloc fails: */
  359.       if (window != NULL) {
  360.         remain = zread(ifile, (char*)window, q+1);
  361.         if (remain != q) {
  362.           fprintf(mesg, " q=%ld, remain=%ld ", q, remain);
  363.           error("can't read whole file at once");
  364.         }
  365.       } else {
  366.         window_size = 0L;
  367.       }
  368.     }
  369. # endif /* BIG_MEM */
  370. #endif /* ?MMAP */
  371.  
  372.   } /* strcmp(z->name, "-") == 0 */
  373.  
  374.   if (l || q == 0)
  375.     m = STORE;
  376.   if (m == BEST)
  377.     m = DEFLATE;
  378.  
  379.   /* Do not create STORED files with extended local headers if the
  380.    * input size is not known, because such files could not be extracted.
  381.    * So if the zip file is not seekable and the input file is not
  382.    * on disk, obey the -0 option by forcing deflation with stored block.
  383.    * Note however that using "zip -0" as filter is not very useful...
  384.    * ??? to be done.
  385.    */
  386.  
  387.   /* Fill in header information and write local header to zip file.
  388.    * This header will later be re-written since compressed length and
  389.    * crc are not yet known.
  390.    */
  391.  
  392.   /* (Assume ext, cext, com, and zname already filled in.) */
  393. #if defined(OS2) || defined(WIN32)
  394.   z->vem = z->dosflag ? 20       /* Made under MSDOS by PKZIP 2.0 */
  395.          : OS_CODE + REVISION;
  396.   /* For a FAT file system, we cheat and pretend that the file
  397.    * was not made on OS2 but under DOS. unzip is confused otherwise.
  398.    */
  399. #else /* !(OS2 || WIN32) */
  400.   z->vem = dosify ? 20 : OS_CODE + REVISION;
  401. #endif /* ?(OS2 || WIN32) */
  402.  
  403.   z->ver = m == STORE ? 10 : 20;     /* Need PKUNZIP 2.0 except for store */
  404.   z->crc = 0;  /* to be updated later */
  405.   /* Assume first that we will need an extended local header: */
  406.   z->flg = 8;  /* to be updated later */
  407. #ifdef CRYPT
  408.   if (key != NULL) {
  409.     z->flg |= 1;
  410.     /* Since we do not yet know the crc here, we pretend that the crc
  411.      * is the modification time:
  412.      */
  413.     z->crc = z->tim << 16;
  414.   }
  415. #endif /* CRYPT */
  416.   z->lflg = z->flg;
  417.   z->how = m;                             /* may be changed later  */
  418.   z->siz = m == STORE && q >= 0 ? q : 0;  /* will be changed later  */
  419.   z->len = q >= 0 ? q : 0;                /* may be changed later  */
  420.   z->dsk = 0;
  421.   if (z->att == (ush)UNKNOWN) {
  422.       z->att = BINARY;                    /* set sensible value in header */
  423.       set_type = 1;
  424.   }
  425.   /* Attributes from filetime(), flag bits from set_extra_field(): */
  426.   z->atx = z->dosflag ? a & 0xff : a | (z->atx & 0x0000ff00);
  427.   z->off = tempzn;
  428.   if ((r = putlocal(z, y)) != ZE_OK)
  429.     return r;
  430.   tempzn += 4 + LOCHEAD + z->nam + z->ext;
  431.  
  432. #ifdef CRYPT
  433.   if (key != NULL) {
  434.     crypthead(key, z->crc, y);
  435.     z->siz += RAND_HEAD_LEN;  /* to be updated later */
  436.     tempzn += RAND_HEAD_LEN;
  437.   }
  438. #endif /* CRYPT */
  439.   if (ferror(y))
  440.     ziperr(ZE_WRITE, "unexpected error on zip file");
  441.   o = ftell(y); /* for debugging only, ftell can fail on pipes */
  442.   if (ferror(y))
  443.     clearerr(y);
  444.  
  445.   /* Write stored or deflated file to zip file */
  446.   isize = 0L;
  447.   crc = crc32(0L, (uch *)NULL, 0);
  448.  
  449.   if (m == DEFLATE) {
  450.      bi_init(y);
  451.      if (set_type) z->att = (ush)UNKNOWN; /* will be changed in deflate() */
  452.      ct_init(&z->att, &m);
  453.      lm_init(level, &z->flg);
  454.      s = deflate();
  455.   }
  456.   else if (!isdir)
  457.   {
  458.     if ((b = malloc(CBSZ)) == NULL)
  459.        return ZE_MEM;
  460.  
  461.     if (l) {
  462.       k = rdsymlnk(z->name, b, CBSZ);
  463. /*
  464.  * compute crc first because zfwrite will alter the buffer b points to !!
  465.  */
  466.       crc = crc32(crc, (uch *) b, k);
  467.       if (zfwrite(b, 1, k, y) != k)
  468.       {
  469.         free((zvoid *)b);
  470.         return ZE_TEMP;
  471.       }
  472.       isize = k;
  473. #ifdef MINIX
  474.       q = k;
  475. #endif /* MINIX */
  476.     }
  477.     else
  478.       while ((k = file_read(b, CBSZ)) > 0 && k != (extent) EOF)
  479.       {
  480.         if (zfwrite(b, 1, k, y) != k)
  481.         {
  482.           free((zvoid *)b);
  483.           return ZE_TEMP;
  484.         }
  485.         if (verbose) putc('.', stderr);
  486.       }
  487.     free((zvoid *)b);
  488.     s = isize;
  489.   }
  490.   if (ifile != fbad && zerr(ifile)) {
  491.     perror("\nzip warning");
  492.     zipwarn("could not read input file: ", z->zname);
  493.   }
  494.   if (ifile != fbad)
  495.     zclose(ifile);
  496. #ifdef MMAP
  497.   if (remain >= 0L) {
  498.     munmap(window, window_size);
  499.     window = NULL;
  500.   }
  501. #endif /*MMAP */
  502.  
  503.   tempzn += s;
  504.   p = tempzn; /* save for future fseek() */
  505.  
  506. #if (!defined(MSDOS) || defined(OS2))
  507. #if !defined(VMS) && !defined(CMS_MVS)
  508.   /* Check input size (but not in VMS -- variable record lengths mess it up)
  509.    * and not on MSDOS -- diet in TSR mode reports an incorrect file size)
  510.    */
  511.   if (q >= 0 && isize != (ulg)q && !translate_eol)
  512.   {
  513.     Trace((mesg, " i=%ld, q=%ld ", isize, q));
  514.     zipwarn(" file size changed while zipping ", z->name);
  515.   }
  516. #endif /* !VMS && !CMS_MVS */
  517. #endif /* (!MSDOS || OS2) */
  518.  
  519.   /* Try to rewrite the local header with correct information */
  520.   z->crc = crc;
  521.   z->siz = s;
  522. #ifdef CRYPT
  523.   if (key != NULL)
  524.     z->siz += RAND_HEAD_LEN;
  525. #endif /* CRYPT */
  526.   z->len = isize;
  527.   if (fseek(y, z->off, SEEK_SET)) {
  528.     if (z->how != (ush) m)
  529.        error("can't rewrite method");
  530.     if (m == STORE && q < 0)
  531.        ziperr(ZE_PARMS, "zip -0 not supported for I/O on pipes or devices");
  532.     if ((r = putextended(z, y)) != ZE_OK)
  533.       return r;
  534.     tempzn += 16L;
  535.     z->flg = z->lflg; /* if flg modified by inflate */
  536.   } else {
  537.      /* seek ok, ftell() should work, check compressed size */
  538. #if !defined(VMS) && !defined(CMS_MVS)
  539.     if (p - o != s) {
  540.       fprintf(mesg, " s=%ld, actual=%ld ", s, p-o);
  541.       error("incorrect compressed size");
  542.     }
  543. #endif /* !VMS && !CMS_MVS */
  544.     z->how = m;
  545.     z->ver = m == STORE ? 10 : 20;     /* Need PKUNZIP 2.0 except for store */
  546.     if ((z->flg & 1) == 0)
  547.       z->flg &= ~8; /* clear the extended local header flag */
  548.     z->lflg = z->flg;
  549.     /* rewrite the local header: */
  550.     if ((r = putlocal(z, y)) != ZE_OK)
  551.       return r;
  552.     if (fseek(y, p, SEEK_SET))
  553.       return ZE_READ;
  554.     if ((z->flg & 1) != 0) {
  555.       /* encrypted file, extended header still required */
  556.       if ((r = putextended(z, y)) != ZE_OK)
  557.         return r;
  558.       tempzn += 16L;
  559.     }
  560.   }
  561.   /* Free the local extra field which is no longer needed */
  562.   if (z->ext) {
  563.     if (z->extra != z->cextra)
  564.       free((zvoid *)(z->extra));
  565.     z->ext = 0;
  566.   }
  567.  
  568.   /* Display statistics */
  569.   if (noisy)
  570.   {
  571.     if (verbose)
  572.       fprintf(mesg, "\t(in=%lu) (out=%lu)", isize, s);
  573.     if (m == DEFLATE)
  574.       fprintf(mesg, " (deflated %d%%)\n", percent(isize, s));
  575.     else
  576.       fprintf(mesg, " (stored 0%%)\n");
  577.     fflush(mesg);
  578.   }
  579.   return ZE_OK;
  580. }
  581.  
  582.  
  583. int file_read(buf, size)
  584.   char *buf;
  585.   unsigned size;
  586. /* Read a new buffer from the current input file, perform end-of-line
  587.  * translation, and update the crc and input file size.
  588.  * IN assertion: size >= 2 (for end-of-line translation)
  589.  */
  590. {
  591.   unsigned len;
  592.   char *b;
  593.  
  594. #if defined(MMAP) || defined(BIG_MEM)
  595.   if (remain == 0L) {
  596.     return 0;
  597.   } else if (remain > 0L) {
  598.     /* The window data is already in place. We still compute the crc
  599.      * by 32K blocks instead of once on whole file to keep a certain
  600.      * locality of reference.
  601.      */
  602.     Assert (buf == (char*)window + isize, "are you lost?");
  603.     if (size > remain) size = remain;
  604.     if (size > WSIZE) size = WSIZE; /* don't touch all pages at once */
  605.     remain -= (long) size;
  606.     len = size;
  607.   } else
  608. #endif /* MMAP || BIG_MEM */
  609.   if (translate_eol == 0) {
  610.     len = zread(ifile, buf, size);
  611.     if (len == (unsigned)EOF || len == 0) return (int)len;
  612.   } else if (translate_eol == 1) {
  613.     /* Transform LF to CR LF */
  614.     size >>= 1;
  615.     b = buf+size;
  616.     size = len = zread(ifile, b, size);
  617.     if (len == (unsigned)EOF || len == 0) return (int)len;
  618. #ifdef EBCDIC
  619.     if (aflag == ASCII)
  620.     {
  621.        do {
  622.           char c;
  623.  
  624.           if ((c = *b++) == '\n') {
  625.              *buf++ = CR; *buf++ = LF; len++;
  626.           } else {
  627.             *buf++ = (char)ascii[(uch)c];
  628.           }
  629.        } while (--size != 0);
  630.     }
  631.     else
  632. #endif /* EBCDIC */
  633.     {
  634.        do {
  635.           if ((*buf++ = *b++) == '\n') *(buf-1) = '\r', *buf++ = '\n', len++;
  636.        } while (--size != 0);
  637.     }
  638.     buf -= len;
  639.  
  640.   } else {
  641.     /* Transform CR LF to LF and suppress final ^Z */
  642.     b = buf;
  643.     size = len = zread(ifile, buf, size-1);
  644.     if (len == (unsigned)EOF || len == 0) return (int)len;
  645.     buf[len] = '\n'; /* I should check if next char is really a \n */
  646. #ifdef EBCDIC
  647.     if (aflag == ASCII)
  648.     {
  649.        do {
  650.           char c;
  651.  
  652.           if ((c = *b++) == '\r' && *b == '\n') {
  653.              len--;
  654.           } else {
  655.              *buf++ = (char)(c == '\n' ? LF : ascii[(uch)c]);
  656.           }
  657.        } while (--size != 0);
  658.     }
  659.     else
  660. #endif /* EBCDIC */
  661.     {
  662.        do {
  663.           if (( *buf++ = *b++) == '\r' && *b == '\n') buf--, len--;
  664.        } while (--size != 0);
  665.     }
  666.     if (len == 0) {
  667.        zread(ifile, buf, 1); len = 1; /* keep single \r if EOF */
  668. #ifdef EBCDIC
  669.        if (aflag == ASCII) {
  670.           *buf = (char)(*buf == '\n' ? LF : ascii[(uch)(*buf)]);
  671.        }
  672. #endif
  673.     } else {
  674.        buf -= len;
  675.        if (buf[len-1] == CTRLZ) len--; /* suppress final ^Z */
  676.     }
  677.   }
  678.   crc = crc32(crc, (uch *) buf, len);
  679.   isize += (ulg)len;
  680.   return (int)len;
  681. }
  682. #endif /* !UTIL */
  683.