blob: b44c669e3d9dd642b4a333c468189c857f1b7696 [file] [log] [blame]
Luc Verhaegen57741672009-06-16 23:34:21 +02001/*
2 * Copyright 2009 Luc Verhaegen <libv@skynet.be>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2, or (at your option)
7 * any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; see the file COPYING. If not, write to
16 * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
17 */
18
19/*
20 * This code is heavily based on Veit Kannegiesers <veit@kannegieser.net>
21 * e_bcpvpd.pas program. This software comes with no license, but is freely
22 * downloadable at http://kannegieser.net/veit/quelle/phoedeco_src.arj
23 */
24/*
25 * It should be very straightfoward to add support for the $COMPIBM compressed
26 * bios images as well. According to Veits code, the id-string is "$COMPIBM",
27 * and the data starts straight after the (not null-terminated) id-string.
28 */
29
30#define _GNU_SOURCE 1
31
32#include <stdio.h>
33#include <unistd.h>
34#include <fcntl.h>
35#include <errno.h>
36#include <string.h>
37#include <sys/mman.h>
38
Idwer Vollering0ab44552012-09-23 01:22:20 +020039#include "compat.h"
Luc Verhaegena2980652009-06-17 14:45:24 +020040#include "lzss_extract.h"
Luc Verhaegen57741672009-06-16 23:34:21 +020041
Anton Kochkov8f9427f2013-04-07 13:20:30 +040042int main(int argc, char *argv[])
Luc Verhaegen57741672009-06-16 23:34:21 +020043{
Anton Kochkov8f9427f2013-04-07 13:20:30 +040044 int infd, outfd;
45 unsigned char *InputBuffer;
46 int InputBufferSize;
Luc Verhaegen57741672009-06-16 23:34:21 +020047
Anton Kochkov8f9427f2013-04-07 13:20:30 +040048 if (argc != 3) {
49 printf("usage: %s <input file> <output file>\n", argv[0]);
50 return 1;
51 }
Luc Verhaegen57741672009-06-16 23:34:21 +020052
Anton Kochkov8f9427f2013-04-07 13:20:30 +040053 infd = open(argv[1], O_RDONLY);
54 if (infd < 0) {
55 fprintf(stderr, "Error: Failed to open %s: %s\n", argv[1],
56 strerror(errno));
57 return 1;
58 }
Luc Verhaegen57741672009-06-16 23:34:21 +020059
Anton Kochkov8f9427f2013-04-07 13:20:30 +040060 InputBufferSize = lseek(infd, 0, SEEK_END);
61 if (InputBufferSize < 0) {
62 fprintf(stderr, "Error: Failed to lseek \"%s\": %s\n", argv[1],
63 strerror(errno));
64 return 1;
65 }
Luc Verhaegen57741672009-06-16 23:34:21 +020066
Anton Kochkov8f9427f2013-04-07 13:20:30 +040067 InputBuffer =
68 mmap(NULL, InputBufferSize, PROT_READ, MAP_PRIVATE, infd, 0);
69 if (InputBuffer < 0) {
70 fprintf(stderr, "Error: Failed to mmap %s: %s\n", argv[1],
71 strerror(errno));
72 return 1;
73 }
Luc Verhaegen57741672009-06-16 23:34:21 +020074
Anton Kochkov8f9427f2013-04-07 13:20:30 +040075 if (InputBufferSize < 0x52) {
76 fprintf(stderr,
77 "Error: \"%s\" is too small tp be a BCPVPD file.\n",
78 argv[1]);
79 return 1;
80 }
Luc Verhaegen57741672009-06-16 23:34:21 +020081
Anton Kochkov8f9427f2013-04-07 13:20:30 +040082 if (strncmp((char *)InputBuffer, "BCPVPD", 7)) {
83 fprintf(stderr,
84 "Error: unable to find BCPVPD header in \"%s\".\n",
85 argv[1]);
86 return 1;
87 }
Luc Verhaegen57741672009-06-16 23:34:21 +020088
Anton Kochkov8f9427f2013-04-07 13:20:30 +040089 outfd = open(argv[2], O_RDWR | O_TRUNC | O_CREAT, S_IRWXU);
90 if (outfd == -1) {
91 fprintf(stderr, "Error: Failed to open \"%s\": %s\n", argv[2],
92 strerror(errno));
93 return 1;
94 }
Luc Verhaegen57741672009-06-16 23:34:21 +020095
Anton Kochkov8f9427f2013-04-07 13:20:30 +040096 return LZSSExtract(InputBuffer + 0x52, InputBufferSize - 0x52, outfd);
Luc Verhaegen57741672009-06-16 23:34:21 +020097}