summaryrefslogtreecommitdiff
path: root/lzss_extract.c
blob: b8f9d93a71487ce26b3dfb51f77d12ab3a29fe50 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/*
 * Copyright 2009      Luc Verhaegen <libv@skynet.be>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; see the file COPYING.  If not, write to
 * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <string.h>

#include "lzss_extract.h"

static inline int
LZSSBufferWrite(int fd, unsigned char *Buffer, int *BufferCount, unsigned char value)
{
    Buffer[*BufferCount] = value;
    *BufferCount += 1;

    if (*BufferCount == 0x1000) {
	if (write(fd, Buffer, 0x1000) != 0x1000) {
	    fprintf(stderr, "Error writing to output file: %s",
		    strerror(errno));
	    return 1;
	}
	*BufferCount = 0;
    }

    return 0;
}

int
LZSSExtract(unsigned char *Input, int InputSize, int fd)
{
    unsigned char Buffer[0x1000];
    unsigned short BitBuffer = 0;
    int i = 0, k, BitCount = 8, BufferCount = 0;

    while (i < InputSize) {

	if (BitCount == 8) {
	    BitBuffer = Input[i];
	    BitCount = -1;
	} else if ((BitBuffer >> BitCount) & 0x01) {
	    if (LZSSBufferWrite(fd, Buffer, &BufferCount, Input[i]))
		return 1;
	} else if ((i + 1) < InputSize) {
	    int offset = ((Input[i] | ((Input[i + 1] & 0xF0) << 4)) - 0xFEE) & 0xFFF;
	    int length = (Input[i + 1] & 0x0F) + 3;

	    for (k = 0; k < length; k++) {
		if (LZSSBufferWrite(fd, Buffer, &BufferCount, Buffer[(offset + k) & 0xFFF]))
		    return 1;
	    }
	    i++;
	} else {
	    fprintf(stderr, "Error: requesting data beyond end of input file.\n");
	    return 1;
	}

	i++;
	BitCount++;
    }

    if (BufferCount) {
	if (write(fd, Buffer, BufferCount) != BufferCount) {
	    fprintf(stderr, "Error writing to output file: %s",
		    strerror(errno));
	    return 1;
	}
    }

    return 0;
}