summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLuc Verhaegen <libv@skynet.be>2009-06-16 08:22:14 +0200
committerLuc Verhaegen <libv@skynet.be>2009-06-16 08:22:14 +0200
commit1646a418194b08452fa33be1839b933b69760455 (patch)
tree94f3f6178c1f53954bc6db2694bf4d4e385ee32b
parent2e42406f9256273d78b5a2c8f71e9d0b5b8c648b (diff)
Split lh5_extract into lh5_test, lh5_extract.o and lh5_extract.h.
This to prepare inclusion into the bios decoder.
-rw-r--r--Makefile12
-rw-r--r--lh5_extract.c116
-rw-r--r--lh5_extract.h32
-rw-r--r--lh5_test.c136
4 files changed, 180 insertions, 116 deletions
diff --git a/Makefile b/Makefile
index 617215c..de5dead 100644
--- a/Makefile
+++ b/Makefile
@@ -2,14 +2,18 @@ MAKE = make
CFLAGS = -g -fpack-struct -Wall
CC = gcc
-all: amideco lh5_extract
+all: amideco lh5_test
amideco: amideco.c
$(CC) $(CFLAGS) amideco.c -o amideco
-lh5_extract: lh5_extract.c
- $(CC) $(CFLAGS) lh5_extract.c -o lh5_extract
+lh5_extract.o: lh5_extract.c
+ $(CC) $(CFLAGS) -c lh5_extract.c -o lh5_extract.o
+
+lh5_test: lh5_extract.o lh5_test.c
+ $(CC) $(CFLAGS) lh5_extract.o lh5_test.c -o lh5_test
clean:
+ rm -f *.o
rm -f amideco
- rm -f lh5_extract
+ rm -f lh5_test
diff --git a/lh5_extract.c b/lh5_extract.c
index ba1e2b2..60ecd48 100644
--- a/lh5_extract.c
+++ b/lh5_extract.c
@@ -30,14 +30,10 @@
#define _GNU_SOURCE 1
#include <stdio.h>
-#include <errno.h>
-#include <sys/types.h>
#include <string.h>
#include <stdlib.h>
-#include <fcntl.h>
-#include <unistd.h>
-#include <sys/mman.h>
-#include <sys/stat.h>
+
+#include "lh5_extract.h"
/*
*
@@ -86,7 +82,7 @@ calc_sum(unsigned char *p, int len)
* -------------------------------------------------
*
*/
-static unsigned int
+unsigned int
LH5HeaderParse(unsigned char *Buffer, int BufferSize,
unsigned int *original_size, unsigned int *packed_size,
char **name, unsigned short *crc)
@@ -521,7 +517,7 @@ decode_p_st1(void)
return j;
}
-static void
+void
LH5Decode(unsigned char *PackedBuffer, int PackedBufferSize,
unsigned char *OutputBuffer, int OutputBufferSize)
{
@@ -556,107 +552,3 @@ LH5Decode(unsigned char *PackedBuffer, int PackedBufferSize,
}
}
}
-
-int
-main(int argc, char *argv[])
-{
- char *filename;
- unsigned short header_crc;
- unsigned int header_size, original_size, packed_size;
- int infd, outfd;
- int LHABufferSize = 0;
- unsigned char *LHABuffer, *OutBuffer;
-
- if (argc != 2) {
- fprintf(stderr, "Error: archive file not specified\n");
- return 1;
- }
-
- /* open archive file */
- infd = open(argv[1], O_RDONLY);
- if (infd == -1) {
- fprintf(stderr, "Error: Failed to open \"%s\": %s\n",
- argv[1], strerror(errno));
- return 1;
- }
-
- LHABufferSize = lseek(infd, 0, SEEK_END);
- if (LHABufferSize < 0) {
- fprintf(stderr, "Error: Failed to lseek \"%s\": %s\n",
- argv[1], strerror(errno));
- return 1;
- }
-
- LHABuffer = mmap(NULL, LHABufferSize, PROT_READ, MAP_PRIVATE, infd, 0);
- if (LHABuffer == ((void *) -1)) {
- fprintf(stderr, "Error: Failed to mmap %s: %s\n",
- argv[1], strerror(errno));
- return 1;
- }
-
- header_size = LH5HeaderParse(LHABuffer, LHABufferSize, &original_size,
- &packed_size, &filename, &header_crc);
- if (!header_size)
- return 1;
-
- if ((header_size + packed_size) < LHABufferSize) {
- fprintf(stderr, "Error: LHA archive is bigger than \"%s\".\n",
- argv[1]);
- return 1;
- }
-
- outfd = open(filename, O_RDWR | O_TRUNC | O_CREAT, S_IRWXU);
- if (outfd == -1) {
- fprintf(stderr, "Error: Failed to open \"%s\": %s\n",
- filename, strerror(errno));
- return 1;
- }
-
- /* grow file */
- if (lseek(outfd, original_size - 1, SEEK_SET) == -1) {
- fprintf(stderr, "Error: Failed to grow \"%s\": %s\n",
- filename, strerror(errno));
- return 1;
- }
-
- if (write(outfd, "", 1) != 1) {
- fprintf(stderr, "Error: Failed to write to \"%s\": %s\n",
- filename, strerror(errno));
- return 1;
- }
-
- OutBuffer = mmap(NULL, original_size, PROT_READ | PROT_WRITE, MAP_SHARED, outfd, 0);
- if (OutBuffer == ((void *) -1)) {
- fprintf(stderr, "Error: Failed to mmap %s: %s\n",
- filename, strerror(errno));
- return 1;
- }
-
- LH5Decode(LHABuffer + header_size, packed_size, OutBuffer, original_size);
-
- if (CRC16Calculate(OutBuffer, original_size) != header_crc) {
- fprintf(stderr, "Warning: invalid CRC on \"%s\"\n", filename);
- return 1;
- }
-
- if (munmap(OutBuffer, original_size))
- fprintf(stderr, "Warning: Failed to munmap \"%s\": %s\n",
- filename, strerror(errno));
-
- if (close(outfd))
- fprintf(stderr, "Warning: Failed to close \"%s\": %s\n",
- filename, strerror(errno));
-
- free(filename);
-
- /* get rid of our input file */
- if (munmap(LHABuffer, LHABufferSize))
- fprintf(stderr, "Warning: Failed to munmap \"%s\": %s\n",
- argv[1], strerror(errno));
-
- if (close(infd))
- fprintf(stderr, "Warning: Failed to close \"%s\": %s\n",
- argv[1], strerror(errno));
-
- return 0;
-}
diff --git a/lh5_extract.h b/lh5_extract.h
new file mode 100644
index 0000000..644c39a
--- /dev/null
+++ b/lh5_extract.h
@@ -0,0 +1,32 @@
+/*
+ * 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 LH5_EXTRACT_H
+#define LH5_EXTRACT_H
+
+unsigned int LH5HeaderParse(unsigned char *Buffer, int BufferSize,
+ unsigned int *original_size,
+ unsigned int *packed_size,
+ char **name, unsigned short *crc);
+
+unsigned short CRC16Calculate(unsigned char *Buffer, int BufferSize);
+
+void LH5Decode(unsigned char *PackedBuffer, int PackedBufferSize,
+ unsigned char *OutputBuffer, int OutputBufferSize);
+
+#endif /* LH5_EXTRACT_H */
diff --git a/lh5_test.c b/lh5_test.c
new file mode 100644
index 0000000..31abef0
--- /dev/null
+++ b/lh5_test.c
@@ -0,0 +1,136 @@
+/*
+ * 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.
+ */
+
+/*
+ * Test/Example code for LH5 extraction.
+ */
+
+#include <stdio.h>
+#include <errno.h>
+#include <string.h>
+#include <stdlib.h>
+#include <fcntl.h>
+#include <unistd.h>
+#include <sys/mman.h>
+#include <sys/stat.h>
+
+#include "lh5_extract.h"
+
+int
+main(int argc, char *argv[])
+{
+ char *filename;
+ unsigned short header_crc;
+ unsigned int header_size, original_size, packed_size;
+ int infd, outfd;
+ int LHABufferSize = 0;
+ unsigned char *LHABuffer, *OutBuffer;
+
+ if (argc != 2) {
+ fprintf(stderr, "Error: archive file not specified\n");
+ return 1;
+ }
+
+ /* open archive file */
+ infd = open(argv[1], O_RDONLY);
+ if (infd == -1) {
+ fprintf(stderr, "Error: Failed to open \"%s\": %s\n",
+ argv[1], strerror(errno));
+ return 1;
+ }
+
+ LHABufferSize = lseek(infd, 0, SEEK_END);
+ if (LHABufferSize < 0) {
+ fprintf(stderr, "Error: Failed to lseek \"%s\": %s\n",
+ argv[1], strerror(errno));
+ return 1;
+ }
+
+ LHABuffer = mmap(NULL, LHABufferSize, PROT_READ, MAP_PRIVATE, infd, 0);
+ if (LHABuffer == ((void *) -1)) {
+ fprintf(stderr, "Error: Failed to mmap %s: %s\n",
+ argv[1], strerror(errno));
+ return 1;
+ }
+
+ header_size = LH5HeaderParse(LHABuffer, LHABufferSize, &original_size,
+ &packed_size, &filename, &header_crc);
+ if (!header_size)
+ return 1;
+
+ if ((header_size + packed_size) < LHABufferSize) {
+ fprintf(stderr, "Error: LHA archive is bigger than \"%s\".\n",
+ argv[1]);
+ return 1;
+ }
+
+ outfd = open(filename, O_RDWR | O_TRUNC | O_CREAT, S_IRWXU);
+ if (outfd == -1) {
+ fprintf(stderr, "Error: Failed to open \"%s\": %s\n",
+ filename, strerror(errno));
+ return 1;
+ }
+
+ /* grow file */
+ if (lseek(outfd, original_size - 1, SEEK_SET) == -1) {
+ fprintf(stderr, "Error: Failed to grow \"%s\": %s\n",
+ filename, strerror(errno));
+ return 1;
+ }
+
+ if (write(outfd, "", 1) != 1) {
+ fprintf(stderr, "Error: Failed to write to \"%s\": %s\n",
+ filename, strerror(errno));
+ return 1;
+ }
+
+ OutBuffer = mmap(NULL, original_size, PROT_READ | PROT_WRITE, MAP_SHARED, outfd, 0);
+ if (OutBuffer == ((void *) -1)) {
+ fprintf(stderr, "Error: Failed to mmap %s: %s\n",
+ filename, strerror(errno));
+ return 1;
+ }
+
+ LH5Decode(LHABuffer + header_size, packed_size, OutBuffer, original_size);
+
+ if (CRC16Calculate(OutBuffer, original_size) != header_crc) {
+ fprintf(stderr, "Warning: invalid CRC on \"%s\"\n", filename);
+ return 1;
+ }
+
+ if (munmap(OutBuffer, original_size))
+ fprintf(stderr, "Warning: Failed to munmap \"%s\": %s\n",
+ filename, strerror(errno));
+
+ if (close(outfd))
+ fprintf(stderr, "Warning: Failed to close \"%s\": %s\n",
+ filename, strerror(errno));
+
+ free(filename);
+
+ /* get rid of our input file */
+ if (munmap(LHABuffer, LHABufferSize))
+ fprintf(stderr, "Warning: Failed to munmap \"%s\": %s\n",
+ argv[1], strerror(errno));
+
+ if (close(infd))
+ fprintf(stderr, "Warning: Failed to close \"%s\": %s\n",
+ argv[1], strerror(errno));
+
+ return 0;
+}