1 // SPDX-License-Identifier: 0BSD
3 ///////////////////////////////////////////////////////////////////////////////
6 /// \brief lzmainfo tool for compatibility with LZMA Utils
8 // Author: Lasse Collin
10 ///////////////////////////////////////////////////////////////////////////////
18 #include "tuklib_gettext.h"
19 #include "tuklib_progname.h"
20 #include "tuklib_exit.h"
33 _("Usage: %s [--help] [--version] [FILE]...\n"
34 "Show information stored in the .lzma file header"), progname
);
37 "\nWith no FILE, or when FILE is -, read standard input.\n"));
40 printf(_("Report bugs to <%s> (in English or Finnish).\n"),
42 printf(_("%s home page: <%s>\n"), PACKAGE_NAME
, PACKAGE_URL
);
44 tuklib_exit(EXIT_SUCCESS
, EXIT_FAILURE
, true);
52 puts("lzmainfo (" PACKAGE_NAME
") " LZMA_VERSION_STRING
);
53 tuklib_exit(EXIT_SUCCESS
, EXIT_FAILURE
, true);
57 /// Parse command line options.
59 parse_args(int argc
, char **argv
)
66 static const struct option long_opts
[] = {
67 { "help", no_argument
, NULL
, OPT_HELP
},
68 { "version", no_argument
, NULL
, OPT_VERSION
},
73 while ((c
= getopt_long(argc
, argv
, "", long_opts
, NULL
)) != -1) {
90 /// Primitive base-2 logarithm for integers
95 for (e
= 0; n
> 1; ++e
, n
/= 2) ;
100 /// Parse the .lzma header and display information about it.
102 lzmainfo(const char *name
, FILE *f
)
105 const size_t size
= fread(buf
, 1, sizeof(buf
), f
);
107 fprintf(stderr
, "%s: %s: %s\n", progname
, name
,
108 ferror(f
) ? strerror(errno
)
109 : _("File is too small to be a .lzma file"));
113 lzma_filter filter
= { .id
= LZMA_FILTER_LZMA1
};
115 // Parse the first five bytes.
116 switch (lzma_properties_decode(&filter
, NULL
, buf
, 5)) {
120 case LZMA_OPTIONS_ERROR
:
121 fprintf(stderr
, "%s: %s: %s\n", progname
, name
,
122 _("Not a .lzma file"));
126 fprintf(stderr
, "%s: %s\n", progname
, strerror(ENOMEM
));
130 fprintf(stderr
, "%s: %s\n", progname
,
131 _("Internal error (bug)"));
136 uint64_t uncompressed_size
= 0;
137 for (size_t i
= 0; i
< 8; ++i
)
138 uncompressed_size
|= (uint64_t)(buf
[5 + i
]) << (i
* 8);
140 // Display the results. We don't want to translate these and also
141 // will use MB instead of MiB, because someone could be parsing
142 // this output and we don't want to break that when people move
143 // from LZMA Utils to XZ Utils.
145 printf("%s\n", name
);
147 printf("Uncompressed size: ");
148 if (uncompressed_size
== UINT64_MAX
)
151 printf("%" PRIu64
" MB (%" PRIu64
" bytes)",
152 (uncompressed_size
/ 1024 + 512) / 1024,
155 lzma_options_lzma
*opt
= filter
.options
;
157 printf("\nDictionary size: "
158 "%" PRIu32
" MB (2^%" PRIu32
" bytes)\n"
159 "Literal context bits (lc): %" PRIu32
"\n"
160 "Literal pos bits (lp): %" PRIu32
"\n"
161 "Number of pos bits (pb): %" PRIu32
"\n",
162 (opt
->dict_size
/ 1024 + 512) / 1024,
163 my_log2(opt
->dict_size
), opt
->lc
, opt
->lp
, opt
->pb
);
172 main(int argc
, char **argv
)
174 tuklib_progname_init(argv
);
175 tuklib_gettext_init(PACKAGE
, LOCALEDIR
);
177 parse_args(argc
, argv
);
179 #ifdef TUKLIB_DOSLIKE
180 setmode(fileno(stdin
), O_BINARY
);
183 int ret
= EXIT_SUCCESS
;
185 // We print empty lines around the output only when reading from
186 // files specified on the command line. This is due to how
187 // LZMA Utils did it.
188 if (optind
== argc
) {
189 if (lzmainfo("(stdin)", stdin
))
195 if (strcmp(argv
[optind
], "-") == 0) {
196 if (lzmainfo("(stdin)", stdin
))
199 FILE *f
= fopen(argv
[optind
], "r");
202 fprintf(stderr
, "%s: %s: %s\n",
209 if (lzmainfo(argv
[optind
], f
))
215 } while (++optind
< argc
);
218 tuklib_exit(ret
, EXIT_FAILURE
, true);