Windows: Embed an application manifest in the EXE files
[xz/debian.git] / extra / scanlzma / scanlzma.c
blob1c8fcd8f48ffea9ef9142bd47f25247ef764504f
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
3 /*
4 scanlzma, scan for lzma compressed data in stdin and echo it to stdout.
5 Copyright (C) 2006 Timo Lindfors
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
18 /* Usage example:
20 $ wget http://www.wifi-shop.cz/Files/produkty/wa2204/wa2204av1.4.1.zip
21 $ unzip wa2204av1.4.1.zip
22 $ gcc scanlzma.c -o scanlzma -Wall
23 $ ./scanlzma 0 < WA2204-FW1.4.1/linux-1.4.bin | lzma -c -d | strings | grep -i "copyright"
24 UpdateDD version 2.5, Copyright (C) 2005 Philipp Benner.
25 Copyright (C) 2005 Philipp Benner.
26 Copyright (C) 2005 Philipp Benner.
27 mawk 1.3%s%s %s, Copyright (C) Michael D. Brennan
28 # Copyright (C) 1998, 1999, 2001 Henry Spencer.
29 ...
34 /* LZMA compressed file format */
35 /* --------------------------- */
36 /* Offset Size Description */
37 /* 0 1 Special LZMA properties for compressed data */
38 /* 1 4 Dictionary size (little endian) */
39 /* 5 8 Uncompressed size (little endian). -1 means unknown size */
40 /* 13 Compressed data */
42 #include <stdlib.h>
43 #include <stdio.h>
44 #include <string.h>
46 #define BUFSIZE 4096
48 int find_lzma_header(unsigned char *buf) {
49 return (buf[0] < 0xE1
50 && buf[0] == 0x5d
51 && buf[4] < 0x20
52 && (memcmp (buf + 10 , "\x00\x00\x00", 3) == 0
53 || (memcmp (buf + 5, "\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF", 8) == 0)));
56 int main(int argc, char *argv[]) {
57 unsigned char buf[BUFSIZE];
58 int ret, i, numlzma, blocks=0;
60 if (argc != 2) {
61 printf("usage: %s numlzma < infile | lzma -c -d > outfile\n"
62 "where numlzma is index of lzma file to extract, starting from zero.\n",
63 argv[0]);
64 exit(1);
66 numlzma = atoi(argv[1]);
68 for (;;) {
69 /* Read data. */
70 ret = fread(buf, BUFSIZE, 1, stdin);
71 if (ret != 1)
72 break;
74 /* Scan for signature. */
75 for (i = 0; i<BUFSIZE-23; i++) {
76 if (find_lzma_header(buf+i) && numlzma-- <= 0) {
77 fwrite(buf+i, (BUFSIZE-i), 1, stdout);
78 for (;;) {
79 int ch;
80 ch = getchar();
81 if (ch == EOF)
82 exit(0);
83 putchar(ch);
87 blocks++;
89 return 1;