-#include "config.h"
+/* vi: set sw=4 ts=4: */
#include "libbb.h"
/* uncompress for busybox -- (c) 2002 Robert Griebl
#include <string.h>
#include <unistd.h>
-/* Defailt input buffer size */
+/* Default input buffer size */
#define IBUFSIZ 2048
/* Default output buffer size */
#undef MAXSEG_64K
#define MAXCODE(n) (1L << (n))
-/* Block compress mode -C compatible with 2.0 */
-int block_mode = BLOCK_MODE;
-
-/* user settable max # bits/code */
-int maxbits = BITS;
-
-/* Exitcode of compress (-1 no file compressed) */
-int exit_code = -1;
-
-/* Input buffer */
-unsigned char inbuf[IBUFSIZ + 64];
-
-/* Output buffer */
-unsigned char outbuf[OBUFSIZ + 2048];
-
-
-long int htab[HSIZE];
-unsigned short codetab[HSIZE];
-
#define htabof(i) htab[i]
#define codetabof(i) codetab[i]
#define tab_prefixof(i) codetabof(i)
#define tab_suffixof(i) ((unsigned char *)(htab))[i]
#define de_stack ((unsigned char *)&(htab[HSIZE-1]))
-#define clear_htab() memset(htab, -1, sizeof(htab))
+#define clear_htab() memset(htab, -1, HSIZE)
#define clear_tab_prefixof() memset(codetab, 0, 256);
* with those of the compress() routine. See the definitions above.
*/
-extern int uncompress(int fd_in, int fd_out)
+USE_DESKTOP(long long) int
+uncompress(int fd_in, int fd_out)
{
+ USE_DESKTOP(long long total_written = 0;)
unsigned char *stackp;
long int code;
int finchar;
long int maxmaxcode;
int n_bits;
int rsize = 0;
+ RESERVE_CONFIG_UBUFFER(inbuf, IBUFSIZ + 64);
+ RESERVE_CONFIG_UBUFFER(outbuf, OBUFSIZ + 2048);
+ unsigned char htab[HSIZE];
+ unsigned short codetab[HSIZE];
+
+ /* Hmm, these were statics - why?! */
+ /* user settable max # bits/code */
+ int maxbits; /* = BITS; */
+ /* block compress mode -C compatible with 2.0 */
+ int block_mode; /* = BLOCK_MODE; */
+
+ memset(inbuf, 0, IBUFSIZ + 64);
+ memset(outbuf, 0, OBUFSIZ + 2048);
insize = 0;
- inbuf[0] = bb_xread_char(fd_in);
+ /* xread isn't good here, we have to return - caller may want
+ * to do some cleanup (e.g. delete incomplete unpacked file etc) */
+ if (full_read(fd_in, inbuf, 1) != 1) {
+ bb_error_msg("short read");
+ return -1;
+ }
maxbits = inbuf[0] & BIT_MASK;
block_mode = inbuf[0] & BLOCK_MODE;
maxmaxcode = MAXCODE(maxbits);
if (maxbits > BITS) {
- bb_error_msg("compressed with %d bits, can only handle %d bits", maxbits,
- BITS);
+ bb_error_msg("compressed with %d bits, can only handle "
+ "%d bits", maxbits, BITS);
return -1;
}
- maxcode = MAXCODE(n_bits = INIT_BITS) - 1;
- bitmask = (1 << n_bits) - 1;
+ n_bits = INIT_BITS;
+ maxcode = MAXCODE(INIT_BITS) - 1;
+ bitmask = (1 << INIT_BITS) - 1;
oldcode = -1;
finchar = 0;
outpos = 0;
}
do {
- resetbuf:;
+ resetbuf:
{
int i;
int e;
int o;
- e = insize - (o = (posbits >> 3));
+ o = posbits >> 3;
+ e = insize - o;
for (i = 0; i < e; ++i)
inbuf[i] = inbuf[i + o];
posbits = 0;
}
- if (insize < (int) sizeof(inbuf) - IBUFSIZ) {
+ if (insize < (int) (IBUFSIZ + 64) - IBUFSIZ) {
rsize = safe_read(fd_in, inbuf + insize, IBUFSIZ);
+//error check??
insize += rsize;
}
{
unsigned char *p = &inbuf[posbits >> 3];
- code =
- ((((long) (p[0])) | ((long) (p[1]) << 8) |
- ((long) (p[2]) << 16)) >> (posbits & 0x7)) & bitmask;
+ code = ((((long) (p[0])) | ((long) (p[1]) << 8) |
+ ((long) (p[2]) << 16)) >> (posbits & 0x7)) & bitmask;
}
posbits += n_bits;
if (oldcode == -1) {
- outbuf[outpos++] = (unsigned char) (finchar =
- (int) (oldcode = code));
+ oldcode = code;
+ finchar = (int) oldcode;
+ outbuf[outpos++] = (unsigned char) finchar;
continue;
}
((posbits - 1) +
((n_bits << 3) -
(posbits - 1 + (n_bits << 3)) % (n_bits << 3)));
- maxcode = MAXCODE(n_bits = INIT_BITS) - 1;
- bitmask = (1 << n_bits) - 1;
+ n_bits = INIT_BITS;
+ maxcode = MAXCODE(INIT_BITS) - 1;
+ bitmask = (1 << INIT_BITS) - 1;
goto resetbuf;
}
code = tab_prefixof(code);
}
- *--stackp = (unsigned char) (finchar = tab_suffixof(code));
+ finchar = tab_suffixof(code);
+ *--stackp = (unsigned char) finchar;
/* And put them out in forward order */
{
int i;
- if (outpos + (i = (de_stack - stackp)) >= OBUFSIZ) {
+ i = de_stack - stackp;
+ if (outpos + i >= OBUFSIZ) {
do {
if (i > OBUFSIZ - outpos) {
i = OBUFSIZ - outpos;
}
if (outpos >= OBUFSIZ) {
- write(fd_out, outbuf, outpos);
+ full_write(fd_out, outbuf, outpos);
+//error check??
+ USE_DESKTOP(total_written += outpos;)
outpos = 0;
}
stackp += i;
- } while ((i = (de_stack - stackp)) > 0);
+ i = de_stack - stackp;
+ } while (i > 0);
} else {
memcpy(outbuf + outpos, stackp, i);
outpos += i;
}
/* Generate the new entry. */
- if ((code = free_ent) < maxmaxcode) {
+ code = free_ent;
+ if (code < maxmaxcode) {
tab_prefixof(code) = (unsigned short) oldcode;
tab_suffixof(code) = (unsigned char) finchar;
free_ent = code + 1;
} while (rsize > 0);
if (outpos > 0) {
- write(fd_out, outbuf, outpos);
+ full_write(fd_out, outbuf, outpos);
+//error check??
+ USE_DESKTOP(total_written += outpos;)
}
- return 0;
+ RELEASE_CONFIG_BUFFER(inbuf);
+ RELEASE_CONFIG_BUFFER(outbuf);
+ return USE_DESKTOP(total_written) + 0;
}