diff options
Diffstat (limited to 'extra')
-rwxr-xr-x | extra/7z2lzma/7z2lzma.bash | 115 | ||||
-rw-r--r-- | extra/scanlzma/scanlzma.c | 88 |
2 files changed, 203 insertions, 0 deletions
diff --git a/extra/7z2lzma/7z2lzma.bash b/extra/7z2lzma/7z2lzma.bash new file mode 100755 index 0000000..1777c78 --- /dev/null +++ b/extra/7z2lzma/7z2lzma.bash @@ -0,0 +1,115 @@ +#!/bin/bash +# +############################################################################# +# +# 7z2lzma.bash is very primitive .7z to .lzma converter. The input file must +# have exactly one LZMA compressed stream, which has been created with the +# default lc, lp, and pb values. The CRC32 in the .7z archive is not checked, +# and the script may seem to succeed while it actually created a corrupt .lzma +# file. You should always try uncompressing both the original .7z and the +# created .lzma and compare that the output is identical. +# +# This script requires basic GNU tools and 7z or 7za tool from p7zip. +# +# Last modified: 2009-01-15 14:25+0200 +# +############################################################################# +# +# Author: Lasse Collin <lasse.collin@tukaani.org> +# +# This file has been put into the public domain. +# You can do whatever you want with this file. +# +############################################################################# + +# You can use 7z or 7za, both will work. +SEVENZIP=7za + +if [ $# != 2 -o -z "$1" -o -z "$2" ]; then + echo "Usage: $0 input.7z output.lzma" + exit 1 +fi + +# Converts an integer variable to little endian binary integer. +int2bin() +{ + local LEN=$1 + local NUM=$2 + local HEX=(0 1 2 3 4 5 6 7 8 9 A B C D E F) + local I + for ((I=0; I < "$LEN"; ++I)); do + printf "\\x${HEX[(NUM >> 4) & 0x0F]}${HEX[NUM & 0x0F]}" + NUM=$((NUM >> 8)) + done +} + +# Make sure we get possible errors from pipes. +set -o pipefail + +# Get information about the input file. At least older 7z and 7za versions +# may return with zero exit status even when an error occurred, so check +# if the output has any lines beginning with "Error". +INFO=$("$SEVENZIP" l -slt "$1") +if [ $? != 0 ] || printf '%s\n' "$INFO" | grep -q ^Error; then + printf '%s\n' "$INFO" + exit 1 +fi + +# Check if the input file has more than one compressed block. +if printf '%s\n' "$INFO" | grep -q '^Block = 1'; then + echo "Cannot convert, because the input file has more than" + echo "one compressed block." + exit 1 +fi + +# Get compressed, uncompressed, and dictionary size. +CSIZE=$(printf '%s\n' "$INFO" | sed -rn 's|^Packed Size = ([0-9]+$)|\1|p') +USIZE=$(printf '%s\n' "$INFO" | sed -rn 's|^Size = ([0-9]+$)|\1|p') +DICT=$(printf '%s\n' "$INFO" | sed -rn 's|^Method = LZMA:([0-9]+[bkm]?)$|\1|p') + +if [ -z "$CSIZE" -o -z "$USIZE" -o -z "$DICT" ]; then + echo "Parsing output of $SEVENZIP failed. Maybe the file uses some" + echo "other compression method than plain LZMA." + exit 1 +fi + +# The following assumes that the default lc, lp, and pb settings were used. +# Otherwise the output will be corrupt. +printf '\x5D' > "$2" + +# Dictionary size can be either was power of two, bytes, kibibytes, or +# mebibytes. We need to convert it to bytes. +case $DICT in + *b) + DICT=${DICT%b} + ;; + *k) + DICT=${DICT%k} + DICT=$((DICT << 10)) + ;; + *m) + DICT=${DICT%m} + DICT=$((DICT << 20)) + ;; + *) + DICT=$((1 << DICT)) + ;; +esac +int2bin 4 "$DICT" >> "$2" + +# Uncompressed size +int2bin 8 "$USIZE" >> "$2" + +# Copy the actual compressed data. Using multiple dd commands to avoid +# copying large amount of data with one-byte block size, which would be +# annoyingly slow. +BS=8192 +BIGSIZE=$((CSIZE / BS)) +CSIZE=$((CSIZE % BS)) +{ + dd of=/dev/null bs=32 count=1 \ + && dd bs="$BS" count="$BIGSIZE" \ + && dd bs=1 count="$CSIZE" +} < "$1" >> "$2" + +exit $? diff --git a/extra/scanlzma/scanlzma.c b/extra/scanlzma/scanlzma.c new file mode 100644 index 0000000..110f822 --- /dev/null +++ b/extra/scanlzma/scanlzma.c @@ -0,0 +1,88 @@ +/* + scanlzma, scan for lzma compressed data in stdin and echo it to stdout. + Copyright (C) 2006 Timo Lindfors + + 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 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. +*/ + +/* Usage example: + + $ wget http://www.wifi-shop.cz/Files/produkty/wa2204/wa2204av1.4.1.zip + $ unzip wa2204av1.4.1.zip + $ gcc scanlzma.c -o scanlzma -Wall + $ ./scanlzma 0 < WA2204-FW1.4.1/linux-1.4.bin | lzma -c -d | strings | grep -i "copyright" + UpdateDD version 2.5, Copyright (C) 2005 Philipp Benner. + Copyright (C) 2005 Philipp Benner. + Copyright (C) 2005 Philipp Benner. + mawk 1.3%s%s %s, Copyright (C) Michael D. Brennan + # Copyright (C) 1998, 1999, 2001 Henry Spencer. + ... + +*/ + + +/* LZMA compressed file format */ +/* --------------------------- */ +/* Offset Size Description */ +/* 0 1 Special LZMA properties for compressed data */ +/* 1 4 Dictionary size (little endian) */ +/* 5 8 Uncompressed size (little endian). -1 means unknown size */ +/* 13 Compressed data */ + +#include <stdlib.h> +#include <stdio.h> +#include <string.h> + +#define BUFSIZE 4096 + +int find_lzma_header(unsigned char *buf) { + return (buf[0] < 0xE1 + && buf[0] == 0x5d + && buf[4] < 0x20 + && (memcmp (buf + 10 , "\x00\x00\x00", 3) == 0 + || (memcmp (buf + 5, "\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF", 8) == 0))); +} + +int main(int argc, char *argv[]) { + unsigned char buf[BUFSIZE]; + int ret, i, numlzma, blocks=0; + + if (argc != 2) { + printf("usage: %s numlzma < infile | lzma -c -d > outfile\n" + "where numlzma is index of lzma file to extract, starting from zero.\n", + argv[0]); + exit(1); + } + numlzma = atoi(argv[1]); + + for (;;) { + /* Read data. */ + ret = fread(buf, BUFSIZE, 1, stdin); + if (ret != 1) + break; + + /* Scan for signature. */ + for (i = 0; i<BUFSIZE-23; i++) { + if (find_lzma_header(buf+i) && numlzma-- <= 0) { + fwrite(buf+i, (BUFSIZE-i), 1, stdout); + for (;;) { + int ch; + ch = getchar(); + if (ch == EOF) + exit(0); + putchar(ch); + } + } + } + blocks++; + } + return 1; +} |