summaryrefslogtreecommitdiff
path: root/Build/source/utils/lzma-utils/src/lzmainfo/lzmainfo.c
blob: a899f675872fe8df3b209b8d5c9f110fcc4f400e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/******************************************************************************

    Simple tool to view LZMA header information. Uses liblzmadec.

    Copyright (C) 2005 Lasse Collin <lasse.collin@tukaani.org>

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation; either
    version 2.1 of the License, or (at your option) any later version.

    This library 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
    Lesser General Public License for more details.

******************************************************************************/

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <stdio.h>
#include <string.h>
#include <inttypes.h>

#ifdef HAVE_ERRNO_H
#include <errno.h>
#else
extern int errno
#endif

#include "../lzma_version.h"

/* This can be compiled without liblzmadec's stdio support. */
#define LZMADEC_NO_STDIO
#include <lzmadec.h>


/* Extremely simple and limited logarithm function */
static uint_fast32_t
Log2 (uint_fast32_t n)
{
	uint_fast32_t e;
	for (e = 0; n > 1; e++, n /= 2);
	return (e);
}

static int
lzmainfo (FILE *file, const char *name, const char *argv0)
{
	lzmadec_info info;
	uint8_t buf[LZMADEC_MINIMUM_SIZE];
	/* Read the first bytes of the file. */
	if (LZMADEC_MINIMUM_SIZE != fread (buf, sizeof (uint8_t),
			LZMADEC_MINIMUM_SIZE, file)) {
		fprintf (stderr, "%s: %s: Too small to be an LZMA "
				"encoded file.\n", argv0, name);
		return 1; /* Error */
	} else if (LZMADEC_OK != lzmadec_buffer_info (
			&info, buf, LZMADEC_MINIMUM_SIZE)) {
		fprintf (stderr, "%s: %s: Invalid LZMA header.\n",
				argv0, name);
		return 1;
	} else {
		if (file != stdin)
			printf ("%s\n", name);
		printf ("Uncompressed size:             ");
		if (info.is_streamed)
			printf ("Unknown");
		else
			printf ("%llu MB (%llu bytes)",
					(info.uncompressed_size + 512 * 1024)
					/ (1024 * 1024),
					info.uncompressed_size);
		printf ("\nDictionary size:               "
				"%u MB (2^%u bytes)\n"
				"Literal context bits (lc):     %d\n"
				"Literal pos bits (lp):         %d\n"
				"Number of pos bits (pb):       %d\n",
				(info.dictionary_size + 512 * 1024)
				/ (1024 * 1024),
				Log2 (info.dictionary_size),
				(int)info.lc,
				(int)info.lp,
				(int)info.pb);
	}
	fclose (file);
	return 0;
}

int
main (int argc, char **argv)
{
	FILE *file;
	int i;
	int ret = 0;

	/* Check the command line arguments. */
	if (argc < 2) {
		/* No arguments, reading standard input. */
		ret = lzmainfo (stdin, "(standard input)", argv[0]);
		return ret;
	} else if (0 == strcmp (argv[1], "--help")) {
		printf (/* Breaking the indentation style */
"\nLZMAinfo - Show information stored in the LZMA file header\n\n"
"Usage: lzmainfo [--help | filename.lzma [filename2.lzma ...]]\n\n"
"If no filename is specified lzmainfo reads stdin.\n"
"The information is always printed to stdout.\n\n"
"LZMA SDK version %s - %s\n"
"LZMA utils version %s - %s\n"
"\nLZMAinfo is free software licensed under the GNU LGPL.\n\n",
			LZMA_SDK_VERSION_STRING, LZMA_SDK_COPYRIGHT_STRING,
			LZMA_UTILS_VERSION_STRING, LZMA_UTILS_COPYRIGHT_STRING);
		return 0;
	}

	/* Show information about files listed on the command line. */
	printf ("\n");
	for (i = 1; i < argc; i++) {
		file = fopen (argv[i], "rb");
		if (file == NULL) {
			fprintf (stderr, "%s: %s: %s\n", argv[0], argv[i],
					strerror (errno));
			continue;
		}
		ret |= lzmainfo (file, argv[i], argv[0]);
		printf ("\n");
	}
	return ret;
}