3 * pack/unpack archive files
5 * $Header: pack.c 1.5 95/08/01 $
7 * Revision 1.5 95/08/01 xx:xx:xx BB
8 * Fixed for Borland C/C++
10 * Revision 1.4 92/12/07 17:19:29 duplain
13 * Revision 1.3 92/11/09 14:48:36 duplain
14 * Added putc_init() to re-initialise variables.
16 * Revision 1.2 92/10/01 11:22:35 duplain
17 * Added check for EOF.
19 * Revision 1.1 92/09/29 18:02:25 duplain
31 /* BB changed next line because of conflict with Borland's io.h */
49 * write run-length encoding to output file
53 putc_ncr(FILE *ofp
, Byte byte
)
60 { /* means write RUNMARK to output */
63 write_byte(ofp
, RUNMARK
);
67 /* BB changed next line */
68 /* Borland C++ is `a bit fuzzy' about next line */
69 /* while (--byte) { */
79 write_byte(ofp
, prevbyte
);
84 else if (byte
== RUNMARK
)
90 prevbyte
= byte
; /* save in case next byte is RUNMARK */
93 write_byte(ofp
, byte
);
98 unpack(Header
*header
, FILE *ifp
, FILE *ofp
)
100 register Word len
= header
->complen
;
108 if (check_stream(ifp
) != FNOERR
)
110 putc_ncr(ofp
, ungarble(read_byte(ifp
)));
113 if (check_stream(ifp
) == FRWERR
)
115 if (!testing
&& check_stream(ofp
) == FRWERR
)
117 if ((Halfword
) crc
!= header
->crc
)
127 write_ncr(FILE *ofp
, Byte byte
, int bytecount
)
133 fputc((int)garble(byte
), ofp
);
134 fputc((int)garble(RUNMARK
), ofp
);
135 fputc((int)garble(bytecount
), ofp
);
137 for (i
= 0; i
< bytecount
; i
++)
147 fputc((int)garble(RUNMARK
), ofp
);
148 fputc(garble(0), ofp
);
154 fputc((int)garble(byte
), ofp
);
161 pack(Header
*header
, FILE *ifp
, FILE *ofp
)
163 register Word len
= header
->origlen
;
164 Byte prevbyte
= '\0', byte
;
171 prevbyte
= read_byte(ifp
);
176 byte
= read_byte(ifp
);
177 if(prevbyte
== RUNMARK
)
179 write_ncr(ofp
, prevbyte
, 1);
182 else if (byte
== prevbyte
&& bytecount
< 254)
188 write_ncr(ofp
, prevbyte
, bytecount
);
192 if (check_stream(ifp
) != FNOERR
)
195 write_ncr(ofp
, prevbyte
, bytecount
);
197 if (check_stream(ifp
) == FRWERR
)
199 if (!testing
&& check_stream(ofp
) == FRWERR
)
206 header
->crc
= (Halfword
) crc
;
207 header
->complen
= complen
;