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
30 /* BB changed next line because of conflict with Borland's io.h */
48 * write run-length encoding to output file
52 putc_ncr(FILE *ofp
, Byte byte
)
59 { /* means write RUNMARK to output */
62 write_byte(ofp
, RUNMARK
);
66 /* BB changed next line */
67 /* Borland C++ is `a bit fuzzy' about next line */
68 /* while (--byte) { */
78 write_byte(ofp
, prevbyte
);
83 else if (byte
== RUNMARK
)
89 prevbyte
= byte
; /* save in case next byte is RUNMARK */
92 write_byte(ofp
, byte
);
97 unpack(Header
*header
, FILE *ifp
, FILE *ofp
)
99 register Word len
= header
->complen
;
107 if (check_stream(ifp
) != FNOERR
)
109 putc_ncr(ofp
, ungarble(read_byte(ifp
)));
112 if (check_stream(ifp
) == FRWERR
)
114 if (!testing
&& check_stream(ofp
) == FRWERR
)
116 if ((Halfword
) crc
!= header
->crc
)
119 printf("OK (packed)");
126 write_ncr(FILE *ofp
, Byte byte
, int bytecount
)
132 fputc((int)garble(byte
), ofp
);
133 fputc((int)garble(RUNMARK
), ofp
);
134 fputc((int)garble(bytecount
), ofp
);
136 for (i
= 0; i
< bytecount
; i
++)
146 fputc((int)garble(RUNMARK
), ofp
);
147 fputc(garble(0), ofp
);
153 fputc((int)garble(byte
), ofp
);
160 pack(Header
*header
, FILE *ifp
, FILE *ofp
)
162 register Word len
= header
->origlen
;
163 Byte prevbyte
= '\0', byte
;
170 prevbyte
= read_byte(ifp
);
175 byte
= read_byte(ifp
);
176 if(prevbyte
== RUNMARK
)
178 write_ncr(ofp
, prevbyte
, 1);
181 else if (byte
== prevbyte
&& bytecount
< 254)
187 write_ncr(ofp
, prevbyte
, bytecount
);
191 if (check_stream(ifp
) != FNOERR
)
194 write_ncr(ofp
, prevbyte
, bytecount
);
196 if (check_stream(ifp
) == FRWERR
)
198 if (!testing
&& check_stream(ofp
) == FRWERR
)
201 printf("OK (packed)");
205 header
->crc
= (Halfword
) crc
;
206 header
->complen
= complen
;