Merge Unix and RISC OS makefiles
[debian-nspark.git] / pack.c
blob087494d85acde3eff16232fcefb7d2246fb3f53d
2 /*
3 * pack/unpack archive files
5 * $Header: pack.c 1.5 95/08/01 $
6 * $Log: pack.c,v $
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
11 * reformatted source.
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
20 * Initial revision
24 #include <stdio.h>
25 #include "spark.h"
26 #include "main.h"
27 #include "crc.h"
28 #include "garble.h"
30 /* BB changed next line because of conflict with Borland's io.h */
32 /* #include "io.h" */
33 #include "nsparkio.h"
35 #include "pack.h"
37 static short running;
38 static Word complen;
41 void
42 putc_init()
44 running = 0;
48 * write run-length encoding to output file
51 void
52 putc_ncr(FILE *ofp, Byte byte)
54 static Byte prevbyte;
56 if (running)
58 if (!byte)
59 { /* means write RUNMARK to output */
60 calccrc(RUNMARK);
61 if (!testing)
62 write_byte(ofp, RUNMARK);
64 else
66 /* BB changed next line */
67 /* Borland C++ is `a bit fuzzy' about next line */
68 /* while (--byte) { */
69 #ifdef __MSDOS__
70 while (--byte != 0)
72 #else
73 while (--byte)
75 #endif
76 calccrc(prevbyte);
77 if (!testing)
78 write_byte(ofp, prevbyte);
81 running = 0;
83 else if (byte == RUNMARK)
85 running++;
87 else
89 prevbyte = byte; /* save in case next byte is RUNMARK */
90 calccrc(byte);
91 if (!testing)
92 write_byte(ofp, byte);
96 Status
97 unpack(Header *header, FILE *ifp, FILE *ofp)
99 register Word len = header->complen;
101 init_garble();
103 crc = 0;
104 putc_init();
105 while (len--)
107 if (check_stream(ifp) != FNOERR)
108 break;
109 putc_ncr(ofp, ungarble(read_byte(ifp)));
112 if (check_stream(ifp) == FRWERR)
113 return (RERR);
114 if (!testing && check_stream(ofp) == FRWERR)
115 return (WERR);
116 if ((Halfword) crc != header->crc)
117 return (CRCERR);
118 if (testing)
119 printf("OK (packed)");
120 else
121 printf("unpacked");
122 return (NOERR);
125 void
126 write_ncr(FILE *ofp, Byte byte, int bytecount)
128 int i;
130 if (bytecount > 1)
132 fputc((int)garble(byte), ofp);
133 fputc((int)garble(RUNMARK), ofp);
134 fputc((int)garble(bytecount), ofp);
135 complen += 3;
136 for (i = 0; i < bytecount; i++)
138 calccrc(byte);
141 else
143 if (byte == RUNMARK)
145 calccrc(RUNMARK);
146 fputc((int)garble(RUNMARK), ofp);
147 fputc(garble(0), ofp);
148 complen += 2;
150 else
152 calccrc(byte);
153 fputc((int)garble(byte), ofp);
154 complen += 1;
159 Status
160 pack(Header *header, FILE *ifp, FILE *ofp)
162 register Word len = header->origlen;
163 Byte prevbyte = '\0', byte;
164 int bytecount = 0;
166 init_garble();
168 complen = 0;
169 crc = 0;
170 prevbyte = read_byte(ifp);
171 len--;
172 bytecount = 1;
173 while (len--)
175 byte = read_byte(ifp);
176 if(prevbyte == RUNMARK)
178 write_ncr(ofp, prevbyte, 1);
179 bytecount = 1;
181 else if (byte == prevbyte && bytecount < 254)
183 bytecount++;
185 else
187 write_ncr(ofp, prevbyte, bytecount);
188 bytecount = 1;
190 prevbyte = byte;
191 if (check_stream(ifp) != FNOERR)
192 break;
194 write_ncr(ofp, prevbyte, bytecount);
196 if (check_stream(ifp) == FRWERR)
197 return (RERR);
198 if (!testing && check_stream(ofp) == FRWERR)
199 return (WERR);
200 if (testing)
201 printf("OK (packed)");
202 else
203 printf("packed");
205 header->crc = (Halfword) crc;
206 header->complen = complen;
208 return (NOERR);