[Scummvm-git-logs] scummvm-tools master -> a0620b0cf35f6e51a6194c117e724c7dcc5c51c1

sev- noreply at scummvm.org
Fri Nov 18 00:13:04 UTC 2022


This automated email contains information about 2 new commits which have been
pushed to the 'scummvm-tools' repo located at https://github.com/scummvm/scummvm-tools .

Summary:
1902776ee4 MADS: import libblast for rex nebular installer decompression
a0620b0cf3 MADS: A new tool to extract Rex nebular installer


Commit: 1902776ee4d9851d7f49469136a3e469610ce3fa
    https://github.com/scummvm/scummvm-tools/commit/1902776ee4d9851d7f49469136a3e469610ce3fa
Author: Vladimir Serbinenko (phcoder at gmail.com)
Date: 2022-11-18T01:13:01+01:00

Commit Message:
MADS: import libblast for rex nebular installer decompression

Changed paths:
  A engines/mads/libblast/blast.c
  A engines/mads/libblast/blast.h


diff --git a/engines/mads/libblast/blast.c b/engines/mads/libblast/blast.c
new file mode 100644
index 00000000..6aa3fb1b
--- /dev/null
+++ b/engines/mads/libblast/blast.c
@@ -0,0 +1,423 @@
+/* blast.c
+ * Copyright (C) 2003, 2012, 2013 Mark Adler
+ * For conditions of distribution and use, see copyright notice in blast.h
+ * version 1.3, 24 Aug 2013
+ *
+ * blast.c decompresses data compressed by the PKWare Compression Library.
+ * This function provides functionality similar to the explode() function of
+ * the PKWare library, hence the name "blast".
+ *
+ * This decompressor is based on the excellent format description provided by
+ * Ben Rudiak-Gould in comp.compression on August 13, 2001.  Interestingly, the
+ * example Ben provided in the post is incorrect.  The distance 110001 should
+ * instead be 111000.  When corrected, the example byte stream becomes:
+ *
+ *    00 04 82 24 25 8f 80 7f
+ *
+ * which decompresses to "AIAIAIAIAIAIA" (without the quotes).
+ */
+
+/*
+ * Change history:
+ *
+ * 1.0  12 Feb 2003     - First version
+ * 1.1  16 Feb 2003     - Fixed distance check for > 4 GB uncompressed data
+ * 1.2  24 Oct 2012     - Add note about using binary mode in stdio
+ *                      - Fix comparisons of differently signed integers
+ * 1.3  24 Aug 2013     - Return unused input from blast()
+ *                      - Fix test code to correctly report unused input
+ *                      - Enable the provision of initial input to blast()
+ */
+
+#include <stddef.h>             /* for NULL */
+#include <setjmp.h>             /* for setjmp(), longjmp(), and jmp_buf */
+#include "blast.h"              /* prototype for blast() */
+
+#define MAXBITS 13              /* maximum code length */
+#define MAXWIN 4096             /* maximum window size */
+
+/* input and output state */
+struct state {
+    /* input state */
+    blast_in infun;             /* input function provided by user */
+    void *inhow;                /* opaque information passed to infun() */
+    unsigned char *in;          /* next input location */
+    unsigned left;              /* available input at in */
+    int bitbuf;                 /* bit buffer */
+    int bitcnt;                 /* number of bits in bit buffer */
+
+    /* input limit error return state for bits() and decode() */
+    jmp_buf env;
+
+    /* output state */
+    blast_out outfun;           /* output function provided by user */
+    void *outhow;               /* opaque information passed to outfun() */
+    unsigned next;              /* index of next write location in out[] */
+    int first;                  /* true to check distances (for first 4K) */
+    unsigned char out[MAXWIN];  /* output buffer and sliding window */
+};
+
+/*
+ * Return need bits from the input stream.  This always leaves less than
+ * eight bits in the buffer.  bits() works properly for need == 0.
+ *
+ * Format notes:
+ *
+ * - Bits are stored in bytes from the least significant bit to the most
+ *   significant bit.  Therefore bits are dropped from the bottom of the bit
+ *   buffer, using shift right, and new bytes are appended to the top of the
+ *   bit buffer, using shift left.
+ */
+static int bits(struct state *s, int need)
+{
+    int val;            /* bit accumulator */
+
+    /* load at least need bits into val */
+    val = s->bitbuf;
+    while (s->bitcnt < need) {
+        if (s->left == 0) {
+            s->left = s->infun(s->inhow, &(s->in));
+            if (s->left == 0) longjmp(s->env, 1);       /* out of input */
+        }
+        val |= (int)(*(s->in)++) << s->bitcnt;          /* load eight bits */
+        s->left--;
+        s->bitcnt += 8;
+    }
+
+    /* drop need bits and update buffer, always zero to seven bits left */
+    s->bitbuf = val >> need;
+    s->bitcnt -= need;
+
+    /* return need bits, zeroing the bits above that */
+    return val & ((1 << need) - 1);
+}
+
+/*
+ * Huffman code decoding tables.  count[1..MAXBITS] is the number of symbols of
+ * each length, which for a canonical code are stepped through in order.
+ * symbol[] are the symbol values in canonical order, where the number of
+ * entries is the sum of the counts in count[].  The decoding process can be
+ * seen in the function decode() below.
+ */
+struct huffman {
+    short *count;       /* number of symbols of each length */
+    short *symbol;      /* canonically ordered symbols */
+};
+
+/*
+ * Decode a code from the stream s using huffman table h.  Return the symbol or
+ * a negative value if there is an error.  If all of the lengths are zero, i.e.
+ * an empty code, or if the code is incomplete and an invalid code is received,
+ * then -9 is returned after reading MAXBITS bits.
+ *
+ * Format notes:
+ *
+ * - The codes as stored in the compressed data are bit-reversed relative to
+ *   a simple integer ordering of codes of the same lengths.  Hence below the
+ *   bits are pulled from the compressed data one at a time and used to
+ *   build the code value reversed from what is in the stream in order to
+ *   permit simple integer comparisons for decoding.
+ *
+ * - The first code for the shortest length is all ones.  Subsequent codes of
+ *   the same length are simply integer decrements of the previous code.  When
+ *   moving up a length, a one bit is appended to the code.  For a complete
+ *   code, the last code of the longest length will be all zeros.  To support
+ *   this ordering, the bits pulled during decoding are inverted to apply the
+ *   more "natural" ordering starting with all zeros and incrementing.
+ */
+static int decode(struct state *s, struct huffman *h)
+{
+    int len;            /* current number of bits in code */
+    int code;           /* len bits being decoded */
+    int first;          /* first code of length len */
+    int count;          /* number of codes of length len */
+    int index;          /* index of first code of length len in symbol table */
+    int bitbuf;         /* bits from stream */
+    int left;           /* bits left in next or left to process */
+    short *next;        /* next number of codes */
+
+    bitbuf = s->bitbuf;
+    left = s->bitcnt;
+    code = first = index = 0;
+    len = 1;
+    next = h->count + 1;
+    while (1) {
+        while (left--) {
+            code |= (bitbuf & 1) ^ 1;   /* invert code */
+            bitbuf >>= 1;
+            count = *next++;
+            if (code < first + count) { /* if length len, return symbol */
+                s->bitbuf = bitbuf;
+                s->bitcnt = (s->bitcnt - len) & 7;
+                return h->symbol[index + (code - first)];
+            }
+            index += count;             /* else update for next length */
+            first += count;
+            first <<= 1;
+            code <<= 1;
+            len++;
+        }
+        left = (MAXBITS+1) - len;
+        if (left == 0) break;
+        if (s->left == 0) {
+            s->left = s->infun(s->inhow, &(s->in));
+            if (s->left == 0) longjmp(s->env, 1);       /* out of input */
+        }
+        bitbuf = *(s->in)++;
+        s->left--;
+        if (left > 8) left = 8;
+    }
+    return -9;                          /* ran out of codes */
+}
+
+/*
+ * Given a list of repeated code lengths rep[0..n-1], where each byte is a
+ * count (high four bits + 1) and a code length (low four bits), generate the
+ * list of code lengths.  This compaction reduces the size of the object code.
+ * Then given the list of code lengths length[0..n-1] representing a canonical
+ * Huffman code for n symbols, construct the tables required to decode those
+ * codes.  Those tables are the number of codes of each length, and the symbols
+ * sorted by length, retaining their original order within each length.  The
+ * return value is zero for a complete code set, negative for an over-
+ * subscribed code set, and positive for an incomplete code set.  The tables
+ * can be used if the return value is zero or positive, but they cannot be used
+ * if the return value is negative.  If the return value is zero, it is not
+ * possible for decode() using that table to return an error--any stream of
+ * enough bits will resolve to a symbol.  If the return value is positive, then
+ * it is possible for decode() using that table to return an error for received
+ * codes past the end of the incomplete lengths.
+ */
+static int construct(struct huffman *h, const unsigned char *rep, int n)
+{
+    int symbol;         /* current symbol when stepping through length[] */
+    int len;            /* current length when stepping through h->count[] */
+    int left;           /* number of possible codes left of current length */
+    short offs[MAXBITS+1];      /* offsets in symbol table for each length */
+    short length[256];  /* code lengths */
+
+    /* convert compact repeat counts into symbol bit length list */
+    symbol = 0;
+    do {
+        len = *rep++;
+        left = (len >> 4) + 1;
+        len &= 15;
+        do {
+            length[symbol++] = len;
+        } while (--left);
+    } while (--n);
+    n = symbol;
+
+    /* count number of codes of each length */
+    for (len = 0; len <= MAXBITS; len++)
+        h->count[len] = 0;
+    for (symbol = 0; symbol < n; symbol++)
+        (h->count[length[symbol]])++;   /* assumes lengths are within bounds */
+    if (h->count[0] == n)               /* no codes! */
+        return 0;                       /* complete, but decode() will fail */
+
+    /* check for an over-subscribed or incomplete set of lengths */
+    left = 1;                           /* one possible code of zero length */
+    for (len = 1; len <= MAXBITS; len++) {
+        left <<= 1;                     /* one more bit, double codes left */
+        left -= h->count[len];          /* deduct count from possible codes */
+        if (left < 0) return left;      /* over-subscribed--return negative */
+    }                                   /* left > 0 means incomplete */
+
+    /* generate offsets into symbol table for each length for sorting */
+    offs[1] = 0;
+    for (len = 1; len < MAXBITS; len++)
+        offs[len + 1] = offs[len] + h->count[len];
+
+    /*
+     * put symbols in table sorted by length, by symbol order within each
+     * length
+     */
+    for (symbol = 0; symbol < n; symbol++)
+        if (length[symbol] != 0)
+            h->symbol[offs[length[symbol]]++] = symbol;
+
+    /* return zero for complete set, positive for incomplete set */
+    return left;
+}
+
+/*
+ * Decode PKWare Compression Library stream.
+ *
+ * Format notes:
+ *
+ * - First byte is 0 if literals are uncoded or 1 if they are coded.  Second
+ *   byte is 4, 5, or 6 for the number of extra bits in the distance code.
+ *   This is the base-2 logarithm of the dictionary size minus six.
+ *
+ * - Compressed data is a combination of literals and length/distance pairs
+ *   terminated by an end code.  Literals are either Huffman coded or
+ *   uncoded bytes.  A length/distance pair is a coded length followed by a
+ *   coded distance to represent a string that occurs earlier in the
+ *   uncompressed data that occurs again at the current location.
+ *
+ * - A bit preceding a literal or length/distance pair indicates which comes
+ *   next, 0 for literals, 1 for length/distance.
+ *
+ * - If literals are uncoded, then the next eight bits are the literal, in the
+ *   normal bit order in the stream, i.e. no bit-reversal is needed. Similarly,
+ *   no bit reversal is needed for either the length extra bits or the distance
+ *   extra bits.
+ *
+ * - Literal bytes are simply written to the output.  A length/distance pair is
+ *   an instruction to copy previously uncompressed bytes to the output.  The
+ *   copy is from distance bytes back in the output stream, copying for length
+ *   bytes.
+ *
+ * - Distances pointing before the beginning of the output data are not
+ *   permitted.
+ *
+ * - Overlapped copies, where the length is greater than the distance, are
+ *   allowed and common.  For example, a distance of one and a length of 518
+ *   simply copies the last byte 518 times.  A distance of four and a length of
+ *   twelve copies the last four bytes three times.  A simple forward copy
+ *   ignoring whether the length is greater than the distance or not implements
+ *   this correctly.
+ */
+static enum BlastError decomp(struct state *s)
+{
+    int lit;            /* true if literals are coded */
+    int dict;           /* log2(dictionary size) - 6 */
+    int symbol;         /* decoded symbol, extra bits for distance */
+    int len;            /* length for copy */
+    unsigned dist;      /* distance for copy */
+    int copy;           /* copy counter */
+    unsigned char *from, *to;   /* copy pointers */
+    static int virgin = 1;                              /* build tables once */
+    static short litcnt[MAXBITS+1], litsym[256];        /* litcode memory */
+    static short lencnt[MAXBITS+1], lensym[16];         /* lencode memory */
+    static short distcnt[MAXBITS+1], distsym[64];       /* distcode memory */
+    static struct huffman litcode = {litcnt, litsym};   /* length code */
+    static struct huffman lencode = {lencnt, lensym};   /* length code */
+    static struct huffman distcode = {distcnt, distsym};/* distance code */
+        /* bit lengths of literal codes */
+    static const unsigned char litlen[] = {
+        11, 124, 8, 7, 28, 7, 188, 13, 76, 4, 10, 8, 12, 10, 12, 10, 8, 23, 8,
+        9, 7, 6, 7, 8, 7, 6, 55, 8, 23, 24, 12, 11, 7, 9, 11, 12, 6, 7, 22, 5,
+        7, 24, 6, 11, 9, 6, 7, 22, 7, 11, 38, 7, 9, 8, 25, 11, 8, 11, 9, 12,
+        8, 12, 5, 38, 5, 38, 5, 11, 7, 5, 6, 21, 6, 10, 53, 8, 7, 24, 10, 27,
+        44, 253, 253, 253, 252, 252, 252, 13, 12, 45, 12, 45, 12, 61, 12, 45,
+        44, 173};
+        /* bit lengths of length codes 0..15 */
+    static const unsigned char lenlen[] = {2, 35, 36, 53, 38, 23};
+        /* bit lengths of distance codes 0..63 */
+    static const unsigned char distlen[] = {2, 20, 53, 230, 247, 151, 248};
+    static const short base[16] = {     /* base for length codes */
+        3, 2, 4, 5, 6, 7, 8, 9, 10, 12, 16, 24, 40, 72, 136, 264};
+    static const char extra[16] = {     /* extra bits for length codes */
+        0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 5, 6, 7, 8};
+
+    /* set up decoding tables (once--might not be thread-safe) */
+    if (virgin) {
+        construct(&litcode, litlen, sizeof(litlen));
+        construct(&lencode, lenlen, sizeof(lenlen));
+        construct(&distcode, distlen, sizeof(distlen));
+        virgin = 0;
+    }
+
+    /* read header */
+    lit = bits(s, 8);
+    if (lit > 1) return BLAST_WRONG_LITERAL_FLAG;
+    dict = bits(s, 8);
+    if (dict < 4 || dict > 6) return BLAST_WRONG_DICTIONARY;
+
+    /* decode literals and length/distance pairs */
+    do {
+        if (bits(s, 1)) {
+            /* get length */
+            symbol = decode(s, &lencode);
+            len = base[symbol] + bits(s, extra[symbol]);
+            if (len == 519) break;              /* end code */
+
+            /* get distance */
+            symbol = len == 2 ? 2 : dict;
+            dist = decode(s, &distcode) << symbol;
+            dist += bits(s, symbol);
+            dist++;
+            if (s->first && dist > s->next)
+                return BLAST_DISTANCE_TOO_BIG;              /* distance too far back */
+
+            /* copy length bytes from distance bytes back */
+            do {
+                to = s->out + s->next;
+                from = to - dist;
+                copy = MAXWIN;
+                if (s->next < dist) {
+                    from += copy;
+                    copy = dist;
+                }
+                copy -= s->next;
+                if (copy > len) copy = len;
+                len -= copy;
+                s->next += copy;
+                do {
+                    *to++ = *from++;
+                } while (--copy);
+                if (s->next == MAXWIN) {
+                    if (s->outfun(s->outhow, s->out, s->next)) return BLAST_OUTPUT_ERROR;
+                    s->next = 0;
+                    s->first = 0;
+                }
+            } while (len != 0);
+        }
+        else {
+            /* get literal and write it */
+            symbol = lit ? decode(s, &litcode) : bits(s, 8);
+            s->out[s->next++] = symbol;
+            if (s->next == MAXWIN) {
+                if (s->outfun(s->outhow, s->out, s->next)) return BLAST_OUTPUT_ERROR;
+                s->next = 0;
+                s->first = 0;
+            }
+        }
+    } while (1);
+    return BLAST_SUCCESS;
+}
+
+
+/* See comments in blast.h */
+enum BlastError blast(blast_in infun, void *inhow, blast_out outfun, void *outhow,
+          unsigned *left, unsigned char **in)
+{
+    struct state s;             /* input/output state */
+    enum BlastError err;        /* return value */
+
+    /* initialize input state */
+    s.infun = infun;
+    s.inhow = inhow;
+    if (left != NULL && *left) {
+        s.left = *left;
+        s.in = *in;
+    }
+    else
+        s.left = 0;
+    s.bitbuf = 0;
+    s.bitcnt = 0;
+
+    /* initialize output state */
+    s.outfun = outfun;
+    s.outhow = outhow;
+    s.next = 0;
+    s.first = 1;
+
+    /* return if bits() or decode() tries to read past available input */
+    if (setjmp(s.env) != 0)             /* if came back here via longjmp(), */
+        err = BLAST_INPUT_EXHAUSTED;    /*  then skip decomp(), return error */
+    else
+        err = decomp(&s);               /* decompress */
+
+    /* return unused input */
+    if (left != NULL)
+        *left = s.left;
+    if (in != NULL)
+        *in = s.left ? s.in : NULL;
+
+    /* write any leftover output and update the error code if needed */
+    if (err != BLAST_OUTPUT_ERROR && s.next && s.outfun(s.outhow, s.out, s.next) && err == BLAST_SUCCESS)
+        err = BLAST_OUTPUT_ERROR;
+    return err;
+}
diff --git a/engines/mads/libblast/blast.h b/engines/mads/libblast/blast.h
new file mode 100644
index 00000000..3c7324f7
--- /dev/null
+++ b/engines/mads/libblast/blast.h
@@ -0,0 +1,107 @@
+#pragma once
+/* blast.h -- interface for blast.c
+  Copyright (C) 2003, 2012, 2013 Mark Adler
+  version 1.3, 24 Aug 2013
+
+  This software is provided 'as-is', without any express or implied
+  warranty.  In no event will the author be held liable for any damages
+  arising from the use of this software.
+
+  Permission is granted to anyone to use this software for any purpose,
+  including commercial applications, and to alter it and redistribute it
+  freely, subject to the following restrictions:
+
+  1. The origin of this software must not be misrepresented; you must not
+     claim that you wrote the original software. If you use this software
+     in a product, an acknowledgment in the product documentation would be
+     appreciated but is not required.
+  2. Altered source versions must be plainly marked as such, and must not be
+     misrepresented as being the original software.
+  3. This notice may not be removed or altered from any source distribution.
+
+  Mark Adler    madler at alumni.caltech.edu
+ */
+
+
+enum BlastError {
+	BLAST_SUCCESS = 0, //< successful decompression
+
+	// If there is not enough input available or there is not enough output space, then a positive error is returned.
+	BLAST_OUTPUT_ERROR = 1,  //< output error before completing decompression
+	BLAST_INPUT_EXHAUSTED = 2,  //< ran out of input before completing decompression
+
+	// Errors in the source data
+	BLAST_WRONG_LITERAL_FLAG = -1,  //< literal flag not zero or one
+	BLAST_WRONG_DICTIONARY = -2,  //< dictionary size not in 4..6
+	BLAST_DISTANCE_TOO_BIG = -3,  //< distance is too far back
+};
+
+
+/*
+ * blast() decompresses the PKWare Data Compression Library (DCL) compressed
+ * format.  It provides the same functionality as the explode() function in
+ * that library.  (Note: PKWare overused the "implode" verb, and the format
+ * used by their library implode() function is completely different and
+ * incompatible with the implode compression method supported by PKZIP.)
+ *
+ * The binary mode for stdio functions should be used to assure that the
+ * compressed data is not corrupted when read or written.  For example:
+ * fopen(..., "rb") and fopen(..., "wb").
+ */
+
+
+typedef unsigned (*blast_in)(void *how, unsigned char **buf);
+typedef int (*blast_out)(void *how, unsigned char *buf, unsigned len);
+/* Definitions for input/output functions passed to blast().  See below for
+ * what the provided functions need to do.
+ */
+
+#ifdef _WIN32
+    #ifdef LIBBLAST_EXPORTS
+        #define LIBBLAST_API __declspec(dllexport)
+    #else
+        #define LIBBLAST_API __declspec(dllimport)
+    #endif
+#else
+    #ifdef libblast_EXPORTS
+        #define LIBBLAST_API __attribute__((visibility("default")))
+    #else
+        #define LIBBLAST_API
+    #endif
+#endif
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+LIBBLAST_API enum BlastError blast(blast_in infun, void *inhow, blast_out outfun, void *outhow, unsigned *left, unsigned char **in);
+/* Decompress input to output using the provided infun() and outfun() calls.
+ * On success, the return value of blast() is zero.  If there is an error in
+ * the source data, i.e. it is not in the proper format, then a negative value
+ * is returned.  If there is not enough input available or there is not enough
+ * output space, then a positive error is returned.
+ *
+ * The input function is invoked: len = infun(how, &buf), where buf is set by
+ * infun() to point to the input buffer, and infun() returns the number of
+ * available bytes there.  If infun() returns zero, then blast() returns with
+ * an input error.  (blast() only asks for input if it needs it.)  inhow is for
+ * use by the application to pass an input descriptor to infun(), if desired.
+ *
+ * If left and in are not NULL and *left is not zero when blast() is called,
+ * then the *left bytes are *in are consumed for input before infun() is used.
+ *
+ * The output function is invoked: err = outfun(how, buf, len), where the bytes
+ * to be written are buf[0..len-1].  If err is not zero, then blast() returns
+ * with an output error.  outfun() is always called with len <= 4096.  outhow
+ * is for use by the application to pass an output descriptor to outfun(), if
+ * desired.
+ *
+ * If there is any unused input, *left is set to the number of bytes that were
+ * read and *in points to them.  Otherwise *left is set to zero and *in is set
+ * to NULL.  If left or in are NULL, then they are not set.
+ *
+ * At the bottom of blast.c is an example program that uses blast() that can be
+ * compiled to produce a command-line decompression filter by defining TEST.
+ */
+#ifdef __cplusplus
+}
+#endif


Commit: a0620b0cf35f6e51a6194c117e724c7dcc5c51c1
    https://github.com/scummvm/scummvm-tools/commit/a0620b0cf35f6e51a6194c117e724c7dcc5c51c1
Author: Vladimir Serbinenko (phcoder at gmail.com)
Date: 2022-11-18T01:13:01+01:00

Commit Message:
MADS: A new tool to extract Rex nebular installer

Changed paths:
  A engines/mads/extract_mps.cpp
    Makefile.common


diff --git a/Makefile.common b/Makefile.common
index 9687b867..c97a0723 100644
--- a/Makefile.common
+++ b/Makefile.common
@@ -39,6 +39,7 @@ PROGRAMS = \
 	extract_hadesch \
 	extract_hadesch_img \
 	extract_lokalizator \
+	extract_mps \
 	grim_animb2txt \
 	grim_bm2bmp \
 	grim_cosb2cos \
@@ -158,6 +159,11 @@ extract_hadesch_img_LIBS := $(LIBS)
 extract_lokalizator_OBJS := \
 	engines/scumm/extract_lokalizator.o
 
+extract_mps_OBJS := \
+	engines/mads/extract_mps.o \
+	engines/mads/libblast/blast.o \
+	$(UTILS)
+
 deprince_OBJS := \
 	engines/prince/deprince.o \
 	engines/prince/flags.o \
diff --git a/engines/mads/extract_mps.cpp b/engines/mads/extract_mps.cpp
new file mode 100644
index 00000000..b54d73df
--- /dev/null
+++ b/engines/mads/extract_mps.cpp
@@ -0,0 +1,169 @@
+/* ScummVM Tools
+ *
+ * ScummVM Tools is the legal property of its developers, whose names
+ * are too numerous to list here. Please refer to the COPYRIGHT
+ * file distributed with this source distribution.
+ *
+ * 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 3 of the License, 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.  If not, see <http://www.gnu.org/licenses/>.
+ *
+ */
+
+#include "common/endian.h"
+#include "common/str.h"
+#include "common/util.h"
+#include "libblast/blast.h"
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <errno.h>
+
+struct FileDescriptorBin {
+	char name[0x52]; // zero-terminated, rest is filled with what looks like garbage
+	uint16 compression;
+	uint16 volume;
+	uint32 offset_in_volume;
+	uint32 compressedSize;
+	uint32 uncompressedSize;
+} __attribute__ ((packed));
+
+struct blastMemInputWrapperStruct {
+	uint32 _offset, _len;
+	byte *_data;
+	blastMemInputWrapperStruct() : _len(0), _offset(0), _data(nullptr) {}
+	blastMemInputWrapperStruct(byte *data, uint32 len) : _len(len), _offset(0), _data(data) {}
+};
+
+uint blastMemInputWrapper(void *how, byte **buf) {
+	struct blastMemInputWrapperStruct *ctx = (struct blastMemInputWrapperStruct *) how;
+	*buf = ctx->_data + ctx->_offset;
+	uint avail = ctx->_len - ctx->_offset;
+	ctx->_offset += avail;
+	return avail;
+}
+
+struct blastMemOutputWrapperStruct {
+	uint32 _written, _bufLen;
+	byte *_data;
+	blastMemOutputWrapperStruct() : _bufLen(0), _written(0), _data(nullptr) {}
+	blastMemOutputWrapperStruct(byte *data, uint32 len) : _bufLen(len), _written(0), _data(data) {}
+};
+
+int blastMemOutputWrapper(void *how, byte *buf, uint len) {
+	struct blastMemOutputWrapperStruct *ctx = (struct blastMemOutputWrapperStruct *) how;
+	if (len + ctx->_written > ctx->_bufLen)
+		return 1;
+	memcpy(ctx->_data + ctx->_written, buf, len);
+	ctx->_written += len;
+	return 0;
+}
+
+int main (int argc, char **argv) {
+	unsigned char * buf;
+	size_t indexSize;
+	FILE *fin;
+
+	if (argc < 3) {
+		fprintf (stderr, "USAGE: %s BASENAME OUTDIR\n", argv[0]);
+		return -1;
+	}
+
+	Common::String base = argv[1];
+
+	fin = fopen ((base + ".IDX").c_str(), "rb");
+	if (fin == NULL) {
+		fprintf (stderr, "Unable to open %s: %s\n", argv[1], strerror(errno));
+		return -2;
+	}
+	fseek (fin, 0, SEEK_END);
+	indexSize = ftell (fin);
+	fseek (fin, 0, SEEK_SET);
+
+	buf = (byte *) malloc (indexSize);
+
+	fread (buf, 1, indexSize, fin);
+	fclose (fin);
+
+	int filecnt = READ_LE_UINT16(buf);
+
+	byte *ptr;
+	int i;
+
+	hexdump(buf + 2, 10);
+
+	if (filecnt > (indexSize - 12) / sizeof(FileDescriptorBin))
+		filecnt = (indexSize - 12) / sizeof(FileDescriptorBin);
+
+	for (ptr = buf + 12, i = 0; i < filecnt; ptr += sizeof(FileDescriptorBin), i++) {
+		FileDescriptorBin *descBin = (FileDescriptorBin *) ptr;
+		uint32 compressedSize = FROM_LE_32(descBin->compressedSize);
+		uint32 uncompressedSize = FROM_LE_32(descBin->uncompressedSize);
+		uint16 compressionAlgo = FROM_LE_16(descBin->compression);
+		printf("name: %s, compressed=%d, uncompressed=%d, compression=%d, offset=%x, volume = %03d\n",
+		       descBin->name, compressedSize,
+		       uncompressedSize, compressionAlgo, FROM_LE_32(descBin->offset_in_volume), FROM_LE_16(descBin->volume));
+		if (compressionAlgo != 0 && compressionAlgo != 1) {
+			fprintf (stderr, "Unsupported compression alorithm for %s, skipping\n", descBin->name);
+			continue;
+		}
+		Common::String fn = Common::String::format("%s/%s", argv[2], descBin->name);
+		uint32 off = FROM_LE_32(descBin->offset_in_volume);
+		int vol = FROM_LE_16(descBin->volume);
+		byte *compressedBuf = new byte[compressedSize];
+		byte *outptr = compressedBuf;
+		uint32 rem = compressedSize;
+		while (rem > 0) {
+			FILE *fvol = fopen((base + Common::String::format(".%03d", vol)).c_str(), "rb");
+			fseek(fvol, off, SEEK_SET);
+			int actual = fread(outptr, 1, rem, fvol);
+			fclose(fvol);
+			rem -= actual;
+			outptr += actual;
+			if (actual == 0)
+				break;
+			vol++;
+			off = 0;
+		}
+		byte *uncompressedBuf;
+		switch (compressionAlgo) {
+		case 0:
+			uncompressedBuf = compressedBuf;
+			uncompressedSize = compressedSize;
+			break;
+		case 1:
+			blastMemInputWrapperStruct blastInput(compressedBuf, compressedSize);
+			uncompressedBuf = new byte[uncompressedSize];
+			blastMemOutputWrapperStruct blastOutput(uncompressedBuf, uncompressedSize);
+			BlastError blastErr = blast(blastMemInputWrapper, &blastInput, blastMemOutputWrapper, &blastOutput, nullptr, nullptr);
+			if (blastErr) {
+				fprintf (stderr, "Unable to decompress %s: %d\n", descBin->name, blastErr);
+				continue;
+			}
+				
+			break;
+		}
+		FILE *fout;
+		fout = fopen(fn.c_str(), "wb");
+		if (fout == NULL) {
+			fprintf (stderr, "Unable to open %s: %s\n", fn.c_str(), strerror(errno));
+			return -3;
+		}
+		fwrite(uncompressedBuf, 1, uncompressedSize, fout);
+		fclose(fout);
+		if (uncompressedBuf != compressedBuf)
+			delete[] uncompressedBuf;
+		delete[]compressedBuf;
+	}
+
+	return 0;
+}




More information about the Scummvm-git-logs mailing list