summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLuc Verhaegen <libv@skynet.be>2009-06-17 14:45:24 +0200
committerLuc Verhaegen <libv@skynet.be>2009-06-17 14:45:24 +0200
commita29806510c509d82d4740ffa21116aa5ccb636b0 (patch)
tree5e1eb198b0b2f6a33b597c45c3e6f4d9d717caa6
parent838fe24972f69f97cf36b2141761f3cc1a975a97 (diff)
BCPVPD base algorithm is LZSS, so split this code off.
-rw-r--r--Makefile5
-rw-r--r--bcpvpd.c64
-rw-r--r--lzss_extract.c87
-rw-r--r--lzss_extract.h24
4 files changed, 116 insertions, 64 deletions
diff --git a/Makefile b/Makefile
index 8fb6b48..a94a4a4 100644
--- a/Makefile
+++ b/Makefile
@@ -8,8 +8,9 @@ BIOS_EXTRACT_OBJS = lh5_extract.o ami.o bios_extract.o
bios_extract: $(BIOS_EXTRACT_OBJS)
$(CC) $(CFLAGS) $(BIOS_EXTRACT_OBJS) -o bios_extract
-bcpvpd: bcpvpd.c
- $(CC) $(CFLAGS) bcpvpd.c -o bcpvpd
+BCPVPD_OBJS = lzss_extract.o bcpvpd.o
+bcpvpd: $(BCPVPD_OBJS)
+ $(CC) $(CFLAGS) $(BCPVPD_OBJS) -o bcpvpd
# just here to easily verify the functionality of the lh5 routine
LH5_TEST_OBJS = lh5_extract.o lh5_test.o
diff --git a/bcpvpd.c b/bcpvpd.c
index e9ead46..3d8c341 100644
--- a/bcpvpd.c
+++ b/bcpvpd.c
@@ -36,67 +36,7 @@
#include <string.h>
#include <sys/mman.h>
-static inline int
-BufferWrite(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;
-}
-
-static int
-BCPVPDExtract(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 (BufferWrite(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 (BufferWrite(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;
-}
+#include "lzss_extract.h"
int
main(int argc, char *argv[])
@@ -150,5 +90,5 @@ main(int argc, char *argv[])
return 1;
}
- return BCPVPDExtract(InputBuffer + 0x52, InputBufferSize - 0x52, outfd);
+ return LZSSExtract(InputBuffer + 0x52, InputBufferSize - 0x52, outfd);
}
diff --git a/lzss_extract.c b/lzss_extract.c
new file mode 100644
index 0000000..b8f9d93
--- /dev/null
+++ b/lzss_extract.c
@@ -0,0 +1,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;
+}
diff --git a/lzss_extract.h b/lzss_extract.h
new file mode 100644
index 0000000..b091b7d
--- /dev/null
+++ b/lzss_extract.h
@@ -0,0 +1,24 @@
+/*
+ * 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.
+ */
+
+#ifndef LZSS_EXTRACT_H
+#define LZSS_EXTRACT_H
+
+int LZSSExtract(unsigned char *Input, int InputSize, int fd);
+
+#endif /* LZSS_EXTRACT_H */