string_conversion.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338
  1. // SPDX-License-Identifier: 0BSD
  2. ///////////////////////////////////////////////////////////////////////////////
  3. //
  4. /// \file string_conversion.c
  5. /// \brief Conversion of strings to filter chain and vice versa
  6. //
  7. // Author: Lasse Collin
  8. //
  9. ///////////////////////////////////////////////////////////////////////////////
  10. #include "filter_common.h"
  11. /////////////////////
  12. // String building //
  13. /////////////////////
  14. /// How much memory to allocate for strings. For now, no realloc is used
  15. /// so this needs to be big enough even though there of course is
  16. /// an overflow check still.
  17. ///
  18. /// FIXME? Using a fixed size is wasteful if the application doesn't free
  19. /// the string fairly quickly but this can be improved later if needed.
  20. #define STR_ALLOC_SIZE 800
  21. typedef struct {
  22. char *buf;
  23. size_t pos;
  24. } lzma_str;
  25. static lzma_ret
  26. str_init(lzma_str *str, const lzma_allocator *allocator)
  27. {
  28. str->buf = lzma_alloc(STR_ALLOC_SIZE, allocator);
  29. if (str->buf == NULL)
  30. return LZMA_MEM_ERROR;
  31. str->pos = 0;
  32. return LZMA_OK;
  33. }
  34. static void
  35. str_free(lzma_str *str, const lzma_allocator *allocator)
  36. {
  37. lzma_free(str->buf, allocator);
  38. return;
  39. }
  40. static bool
  41. str_is_full(const lzma_str *str)
  42. {
  43. return str->pos == STR_ALLOC_SIZE - 1;
  44. }
  45. static lzma_ret
  46. str_finish(char **dest, lzma_str *str, const lzma_allocator *allocator)
  47. {
  48. if (str_is_full(str)) {
  49. // The preallocated buffer was too small.
  50. // This shouldn't happen as STR_ALLOC_SIZE should
  51. // be adjusted if new filters are added.
  52. lzma_free(str->buf, allocator);
  53. *dest = NULL;
  54. assert(0);
  55. return LZMA_PROG_ERROR;
  56. }
  57. str->buf[str->pos] = '\0';
  58. *dest = str->buf;
  59. return LZMA_OK;
  60. }
  61. static void
  62. str_append_str(lzma_str *str, const char *s)
  63. {
  64. const size_t len = strlen(s);
  65. const size_t limit = STR_ALLOC_SIZE - 1 - str->pos;
  66. const size_t copy_size = my_min(len, limit);
  67. memcpy(str->buf + str->pos, s, copy_size);
  68. str->pos += copy_size;
  69. return;
  70. }
  71. static void
  72. str_append_u32(lzma_str *str, uint32_t v, bool use_byte_suffix)
  73. {
  74. if (v == 0) {
  75. str_append_str(str, "0");
  76. } else {
  77. // NOTE: Don't use plain "B" because xz and the parser in this
  78. // file don't support it and at glance it may look like 8
  79. // (there cannot be a space before the suffix).
  80. static const char suffixes[4][4] = { "", "KiB", "MiB", "GiB" };
  81. size_t suf = 0;
  82. if (use_byte_suffix) {
  83. while ((v & 1023) == 0
  84. && suf < ARRAY_SIZE(suffixes) - 1) {
  85. v >>= 10;
  86. ++suf;
  87. }
  88. }
  89. // UINT32_MAX in base 10 would need 10 + 1 bytes. Remember
  90. // that initializing to "" initializes all elements to
  91. // zero so '\0'-termination gets handled by this.
  92. char buf[16] = "";
  93. size_t pos = sizeof(buf) - 1;
  94. do {
  95. buf[--pos] = '0' + (v % 10);
  96. v /= 10;
  97. } while (v != 0);
  98. str_append_str(str, buf + pos);
  99. str_append_str(str, suffixes[suf]);
  100. }
  101. return;
  102. }
  103. //////////////////////////////////////////////
  104. // Parsing and stringification declarations //
  105. //////////////////////////////////////////////
  106. /// Maximum length for filter and option names.
  107. /// 11 chars + terminating '\0' + sizeof(uint32_t) = 16 bytes
  108. #define NAME_LEN_MAX 11
  109. /// For option_map.flags: Use .u.map to do convert the input value
  110. /// to an integer. Without this flag, .u.range.{min,max} are used
  111. /// as the allowed range for the integer.
  112. #define OPTMAP_USE_NAME_VALUE_MAP 0x01
  113. /// For option_map.flags: Allow KiB/MiB/GiB in input string and use them in
  114. /// the stringified output if the value is an exact multiple of these.
  115. /// This is used e.g. for LZMA1/2 dictionary size.
  116. #define OPTMAP_USE_BYTE_SUFFIX 0x02
  117. /// For option_map.flags: If the integer value is zero then this option
  118. /// won't be included in the stringified output. It's used e.g. for
  119. /// BCJ filter start offset which usually is zero.
  120. #define OPTMAP_NO_STRFY_ZERO 0x04
  121. /// Possible values for option_map.type. Since OPTMAP_TYPE_UINT32 is 0,
  122. /// it doesn't need to be specified in the initializers as it is
  123. /// the implicit value.
  124. enum {
  125. OPTMAP_TYPE_UINT32,
  126. OPTMAP_TYPE_LZMA_MODE,
  127. OPTMAP_TYPE_LZMA_MATCH_FINDER,
  128. OPTMAP_TYPE_LZMA_PRESET,
  129. };
  130. /// This is for mapping string values in options to integers.
  131. /// The last element of an array must have "" as the name.
  132. /// It's used e.g. for match finder names in LZMA1/2.
  133. typedef struct {
  134. const char name[NAME_LEN_MAX + 1];
  135. const uint32_t value;
  136. } name_value_map;
  137. /// Each filter that has options needs an array of option_map structures.
  138. /// The array doesn't need to be terminated as the functions take the
  139. /// length of the array as an argument.
  140. ///
  141. /// When converting a string to filter options structure, option values
  142. /// will be handled in a few different ways:
  143. ///
  144. /// (1) If .type equals OPTMAP_TYPE_LZMA_PRESET then LZMA1/2 preset string
  145. /// is handled specially.
  146. ///
  147. /// (2) If .flags has OPTMAP_USE_NAME_VALUE_MAP set then the string is
  148. /// converted to an integer using the name_value_map pointed by .u.map.
  149. /// The last element in .u.map must have .name = "" as the terminator.
  150. ///
  151. /// (3) Otherwise the string is treated as a non-negative unsigned decimal
  152. /// integer which must be in the range set in .u.range. If .flags has
  153. /// OPTMAP_USE_BYTE_SUFFIX then KiB, MiB, and GiB suffixes are allowed.
  154. ///
  155. /// The integer value from (2) or (3) is then stored to filter_options
  156. /// at the offset specified in .offset using the type specified in .type
  157. /// (default is uint32_t).
  158. ///
  159. /// Stringifying a filter is done by processing a given number of options
  160. /// in order from the beginning of an option_map array. The integer is
  161. /// read from filter_options at .offset using the type from .type.
  162. ///
  163. /// If the integer is zero and .flags has OPTMAP_NO_STRFY_ZERO then the
  164. /// option is skipped.
  165. ///
  166. /// If .flags has OPTMAP_USE_NAME_VALUE_MAP set then .u.map will be used
  167. /// to convert the option to a string. If the map doesn't contain a string
  168. /// for the integer value then "UNKNOWN" is used.
  169. ///
  170. /// If .flags doesn't have OPTMAP_USE_NAME_VALUE_MAP set then the integer is
  171. /// converted to a decimal value. If OPTMAP_USE_BYTE_SUFFIX is used then KiB,
  172. /// MiB, or GiB suffix is used if the value is an exact multiple of these.
  173. /// Plain "B" suffix is never used.
  174. typedef struct {
  175. char name[NAME_LEN_MAX + 1];
  176. uint8_t type;
  177. uint8_t flags;
  178. uint16_t offset;
  179. union {
  180. // NVHPC has problems with unions that contain pointers that
  181. // are not the first members, so keep "map" at the top.
  182. const name_value_map *map;
  183. struct {
  184. uint32_t min;
  185. uint32_t max;
  186. } range;
  187. } u;
  188. } option_map;
  189. static const char *parse_options(const char **const str, const char *str_end,
  190. void *filter_options,
  191. const option_map *const optmap, const size_t optmap_size);
  192. /////////
  193. // BCJ //
  194. /////////
  195. #if defined(HAVE_ENCODER_X86) \
  196. || defined(HAVE_DECODER_X86) \
  197. || defined(HAVE_ENCODER_ARM) \
  198. || defined(HAVE_DECODER_ARM) \
  199. || defined(HAVE_ENCODER_ARMTHUMB) \
  200. || defined(HAVE_DECODER_ARMTHUMB) \
  201. || defined(HAVE_ENCODER_ARM64) \
  202. || defined(HAVE_DECODER_ARM64) \
  203. || defined(HAVE_ENCODER_POWERPC) \
  204. || defined(HAVE_DECODER_POWERPC) \
  205. || defined(HAVE_ENCODER_IA64) \
  206. || defined(HAVE_DECODER_IA64) \
  207. || defined(HAVE_ENCODER_SPARC) \
  208. || defined(HAVE_DECODER_SPARC) \
  209. || defined(HAVE_ENCODER_RISCV) \
  210. || defined(HAVE_DECODER_RISCV)
  211. static const option_map bcj_optmap[] = {
  212. {
  213. .name = "start",
  214. .flags = OPTMAP_NO_STRFY_ZERO | OPTMAP_USE_BYTE_SUFFIX,
  215. .offset = offsetof(lzma_options_bcj, start_offset),
  216. .u.range.min = 0,
  217. .u.range.max = UINT32_MAX,
  218. }
  219. };
  220. static const char *
  221. parse_bcj(const char **const str, const char *str_end, void *filter_options)
  222. {
  223. // filter_options was zeroed on allocation and that is enough
  224. // for the default value.
  225. return parse_options(str, str_end, filter_options,
  226. bcj_optmap, ARRAY_SIZE(bcj_optmap));
  227. }
  228. #endif
  229. ///////////
  230. // Delta //
  231. ///////////
  232. #if defined(HAVE_ENCODER_DELTA) || defined(HAVE_DECODER_DELTA)
  233. static const option_map delta_optmap[] = {
  234. {
  235. .name = "dist",
  236. .offset = offsetof(lzma_options_delta, dist),
  237. .u.range.min = LZMA_DELTA_DIST_MIN,
  238. .u.range.max = LZMA_DELTA_DIST_MAX,
  239. }
  240. };
  241. static const char *
  242. parse_delta(const char **const str, const char *str_end, void *filter_options)
  243. {
  244. lzma_options_delta *opts = filter_options;
  245. opts->type = LZMA_DELTA_TYPE_BYTE;
  246. opts->dist = LZMA_DELTA_DIST_MIN;
  247. return parse_options(str, str_end, filter_options,
  248. delta_optmap, ARRAY_SIZE(delta_optmap));
  249. }
  250. #endif
  251. ///////////////////
  252. // LZMA1 & LZMA2 //
  253. ///////////////////
  254. /// Help string for presets
  255. #define LZMA12_PRESET_STR "0-9[e]"
  256. static const char *
  257. parse_lzma12_preset(const char **const str, const char *str_end,
  258. uint32_t *preset)
  259. {
  260. assert(*str < str_end);
  261. *preset = (uint32_t)(**str - '0');
  262. // NOTE: Remember to update LZMA12_PRESET_STR if this is modified!
  263. while (++*str < str_end) {
  264. switch (**str) {
  265. case 'e':
  266. *preset |= LZMA_PRESET_EXTREME;
  267. break;
  268. default:
  269. return "Unsupported preset flag";
  270. }
  271. }
  272. return NULL;
  273. }
  274. static const char *
  275. set_lzma12_preset(const char **const str, const char *str_end,
  276. void *filter_options)
  277. {
  278. uint32_t preset;
  279. const char *errmsg = parse_lzma12_preset(str, str_end, &preset);
  280. if (errmsg != NULL)
  281. return errmsg;
  282. lzma_options_lzma *opts = filter_options;
  283. if (lzma_lzma_preset(opts, preset))
  284. return "Unsupported preset";
  285. return NULL;
  286. }
  287. static const name_value_map lzma12_mode_map[] = {
  288. { "fast", LZMA_MODE_FAST },
  289. { "normal", LZMA_MODE_NORMAL },
  290. { "", 0 }
  291. };
  292. static const name_value_map lzma12_mf_map[] = {
  293. { "hc3", LZMA_MF_HC3 },
  294. { "hc4", LZMA_MF_HC4 },
  295. { "bt2", LZMA_MF_BT2 },
  296. { "bt3", LZMA_MF_BT3 },
  297. { "bt4", LZMA_MF_BT4 },
  298. { "", 0 }
  299. };
  300. static const option_map lzma12_optmap[] = {
  301. {
  302. .name = "preset",
  303. .type = OPTMAP_TYPE_LZMA_PRESET,
  304. }, {
  305. .name = "dict",
  306. .flags = OPTMAP_USE_BYTE_SUFFIX,
  307. .offset = offsetof(lzma_options_lzma, dict_size),
  308. .u.range.min = LZMA_DICT_SIZE_MIN,
  309. // FIXME? The max is really max for encoding but decoding
  310. // would allow 4 GiB - 1 B.
  311. .u.range.max = (UINT32_C(1) << 30) + (UINT32_C(1) << 29),
  312. }, {
  313. .name = "lc",
  314. .offset = offsetof(lzma_options_lzma, lc),
  315. .u.range.min = LZMA_LCLP_MIN,
  316. .u.range.max = LZMA_LCLP_MAX,
  317. }, {
  318. .name = "lp",
  319. .offset = offsetof(lzma_options_lzma, lp),
  320. .u.range.min = LZMA_LCLP_MIN,
  321. .u.range.max = LZMA_LCLP_MAX,
  322. }, {
  323. .name = "pb",
  324. .offset = offsetof(lzma_options_lzma, pb),
  325. .u.range.min = LZMA_PB_MIN,
  326. .u.range.max = LZMA_PB_MAX,
  327. }, {
  328. .name = "mode",
  329. .type = OPTMAP_TYPE_LZMA_MODE,
  330. .flags = OPTMAP_USE_NAME_VALUE_MAP,
  331. .offset = offsetof(lzma_options_lzma, mode),
  332. .u.map = lzma12_mode_map,
  333. }, {
  334. .name = "nice",
  335. .offset = offsetof(lzma_options_lzma, nice_len),
  336. .u.range.min = 2,
  337. .u.range.max = 273,
  338. }, {
  339. .name = "mf",
  340. .type = OPTMAP_TYPE_LZMA_MATCH_FINDER,
  341. .flags = OPTMAP_USE_NAME_VALUE_MAP,
  342. .offset = offsetof(lzma_options_lzma, mf),
  343. .u.map = lzma12_mf_map,
  344. }, {
  345. .name = "depth",
  346. .offset = offsetof(lzma_options_lzma, depth),
  347. .u.range.min = 0,
  348. .u.range.max = UINT32_MAX,
  349. }
  350. };
  351. static const char *
  352. parse_lzma12(const char **const str, const char *str_end, void *filter_options)
  353. {
  354. lzma_options_lzma *opts = filter_options;
  355. // It cannot fail.
  356. const bool preset_ret = lzma_lzma_preset(opts, LZMA_PRESET_DEFAULT);
  357. assert(!preset_ret);
  358. (void)preset_ret;
  359. const char *errmsg = parse_options(str, str_end, filter_options,
  360. lzma12_optmap, ARRAY_SIZE(lzma12_optmap));
  361. if (errmsg != NULL)
  362. return errmsg;
  363. if (opts->lc + opts->lp > LZMA_LCLP_MAX)
  364. return "The sum of lc and lp must not exceed 4";
  365. return NULL;
  366. }
  367. /////////////////////////////////////////
  368. // Generic parsing and stringification //
  369. /////////////////////////////////////////
  370. static const struct {
  371. /// Name of the filter
  372. char name[NAME_LEN_MAX + 1];
  373. /// For lzma_str_to_filters:
  374. /// Size of the filter-specific options structure.
  375. uint32_t opts_size;
  376. /// Filter ID
  377. lzma_vli id;
  378. /// For lzma_str_to_filters:
  379. /// Function to parse the filter-specific options. The filter_options
  380. /// will already have been allocated using lzma_alloc_zero().
  381. const char *(*parse)(const char **str, const char *str_end,
  382. void *filter_options);
  383. /// For lzma_str_from_filters:
  384. /// If the flag LZMA_STR_ENCODER is used then the first
  385. /// strfy_encoder elements of optmap are stringified.
  386. /// With LZMA_STR_DECODER strfy_decoder is used.
  387. /// Currently encoders use all options that decoders do but if
  388. /// that changes then this needs to be changed too, for example,
  389. /// add a new OPTMAP flag to skip printing some decoder-only options.
  390. const option_map *optmap;
  391. uint8_t strfy_encoder;
  392. uint8_t strfy_decoder;
  393. /// For lzma_str_from_filters:
  394. /// If true, lzma_filter.options is allowed to be NULL. In that case,
  395. /// only the filter name is printed without any options.
  396. bool allow_null;
  397. } filter_name_map[] = {
  398. #if defined (HAVE_ENCODER_LZMA1) || defined(HAVE_DECODER_LZMA1)
  399. { "lzma1", sizeof(lzma_options_lzma), LZMA_FILTER_LZMA1,
  400. &parse_lzma12, lzma12_optmap, 9, 5, false },
  401. #endif
  402. #if defined(HAVE_ENCODER_LZMA2) || defined(HAVE_DECODER_LZMA2)
  403. { "lzma2", sizeof(lzma_options_lzma), LZMA_FILTER_LZMA2,
  404. &parse_lzma12, lzma12_optmap, 9, 2, false },
  405. #endif
  406. #if defined(HAVE_ENCODER_X86) || defined(HAVE_DECODER_X86)
  407. { "x86", sizeof(lzma_options_bcj), LZMA_FILTER_X86,
  408. &parse_bcj, bcj_optmap, 1, 1, true },
  409. #endif
  410. #if defined(HAVE_ENCODER_ARM) || defined(HAVE_DECODER_ARM)
  411. { "arm", sizeof(lzma_options_bcj), LZMA_FILTER_ARM,
  412. &parse_bcj, bcj_optmap, 1, 1, true },
  413. #endif
  414. #if defined(HAVE_ENCODER_ARMTHUMB) || defined(HAVE_DECODER_ARMTHUMB)
  415. { "armthumb", sizeof(lzma_options_bcj), LZMA_FILTER_ARMTHUMB,
  416. &parse_bcj, bcj_optmap, 1, 1, true },
  417. #endif
  418. #if defined(HAVE_ENCODER_ARM64) || defined(HAVE_DECODER_ARM64)
  419. { "arm64", sizeof(lzma_options_bcj), LZMA_FILTER_ARM64,
  420. &parse_bcj, bcj_optmap, 1, 1, true },
  421. #endif
  422. #if defined(HAVE_ENCODER_RISCV) || defined(HAVE_DECODER_RISCV)
  423. { "riscv", sizeof(lzma_options_bcj), LZMA_FILTER_RISCV,
  424. &parse_bcj, bcj_optmap, 1, 1, true },
  425. #endif
  426. #if defined(HAVE_ENCODER_POWERPC) || defined(HAVE_DECODER_POWERPC)
  427. { "powerpc", sizeof(lzma_options_bcj), LZMA_FILTER_POWERPC,
  428. &parse_bcj, bcj_optmap, 1, 1, true },
  429. #endif
  430. #if defined(HAVE_ENCODER_IA64) || defined(HAVE_DECODER_IA64)
  431. { "ia64", sizeof(lzma_options_bcj), LZMA_FILTER_IA64,
  432. &parse_bcj, bcj_optmap, 1, 1, true },
  433. #endif
  434. #if defined(HAVE_ENCODER_SPARC) || defined(HAVE_DECODER_SPARC)
  435. { "sparc", sizeof(lzma_options_bcj), LZMA_FILTER_SPARC,
  436. &parse_bcj, bcj_optmap, 1, 1, true },
  437. #endif
  438. #if defined(HAVE_ENCODER_DELTA) || defined(HAVE_DECODER_DELTA)
  439. { "delta", sizeof(lzma_options_delta), LZMA_FILTER_DELTA,
  440. &parse_delta, delta_optmap, 1, 1, false },
  441. #endif
  442. };
  443. /// Decodes options from a string for one filter (name1=value1,name2=value2).
  444. /// Caller must have allocated memory for filter_options already and set
  445. /// the initial default values. This is called from the filter-specific
  446. /// parse_* functions.
  447. ///
  448. /// The input string starts at *str and the address in str_end is the first
  449. /// char that is not part of the string anymore. So no '\0' terminator is
  450. /// used. *str is advanced every time something has been decoded successfully.
  451. static const char *
  452. parse_options(const char **const str, const char *str_end,
  453. void *filter_options,
  454. const option_map *const optmap, const size_t optmap_size)
  455. {
  456. while (*str < str_end && **str != '\0') {
  457. // Each option is of the form name=value.
  458. // Commas (',') separate options. Extra commas are ignored.
  459. // Ignoring extra commas makes it simpler if an optional
  460. // option stored in a shell variable which can be empty.
  461. if (**str == ',') {
  462. ++*str;
  463. continue;
  464. }
  465. // Find where the next name=value ends.
  466. const size_t str_len = (size_t)(str_end - *str);
  467. const char *name_eq_value_end = memchr(*str, ',', str_len);
  468. if (name_eq_value_end == NULL)
  469. name_eq_value_end = str_end;
  470. const char *equals_sign = memchr(*str, '=',
  471. (size_t)(name_eq_value_end - *str));
  472. // Fail if the '=' wasn't found or the option name is missing
  473. // (the first char is '=').
  474. if (equals_sign == NULL || **str == '=')
  475. return "Options must be 'name=value' pairs separated "
  476. "with commas";
  477. // Reject a too long option name so that the memcmp()
  478. // in the loop below won't read past the end of the
  479. // string in optmap[i].name.
  480. const size_t name_len = (size_t)(equals_sign - *str);
  481. if (name_len > NAME_LEN_MAX)
  482. return "Unknown option name";
  483. // Find the option name from optmap[].
  484. size_t i = 0;
  485. while (true) {
  486. if (i == optmap_size)
  487. return "Unknown option name";
  488. if (memcmp(*str, optmap[i].name, name_len) == 0
  489. && optmap[i].name[name_len] == '\0')
  490. break;
  491. ++i;
  492. }
  493. // The input string is good at least until the start of
  494. // the option value.
  495. *str = equals_sign + 1;
  496. // The code assumes that the option value isn't an empty
  497. // string so check it here.
  498. const size_t value_len = (size_t)(name_eq_value_end - *str);
  499. if (value_len == 0)
  500. return "Option value cannot be empty";
  501. // LZMA1/2 preset has its own parsing function.
  502. if (optmap[i].type == OPTMAP_TYPE_LZMA_PRESET) {
  503. const char *errmsg = set_lzma12_preset(str,
  504. name_eq_value_end, filter_options);
  505. if (errmsg != NULL)
  506. return errmsg;
  507. continue;
  508. }
  509. // It's an integer value.
  510. uint32_t v;
  511. if (optmap[i].flags & OPTMAP_USE_NAME_VALUE_MAP) {
  512. // The integer is picked from a string-to-integer map.
  513. //
  514. // Reject a too long value string so that the memcmp()
  515. // in the loop below won't read past the end of the
  516. // string in optmap[i].u.map[j].name.
  517. if (value_len > NAME_LEN_MAX)
  518. return "Invalid option value";
  519. const name_value_map *map = optmap[i].u.map;
  520. size_t j = 0;
  521. while (true) {
  522. // The array is terminated with an empty name.
  523. if (map[j].name[0] == '\0')
  524. return "Invalid option value";
  525. if (memcmp(*str, map[j].name, value_len) == 0
  526. && map[j].name[value_len]
  527. == '\0') {
  528. v = map[j].value;
  529. break;
  530. }
  531. ++j;
  532. }
  533. } else if (**str < '0' || **str > '9') {
  534. // Note that "max" isn't supported while it is
  535. // supported in xz. It's not useful here.
  536. return "Value is not a non-negative decimal integer";
  537. } else {
  538. // strtoul() has locale-specific behavior so it cannot
  539. // be relied on to get reproducible results since we
  540. // cannot change the locate in a thread-safe library.
  541. // It also needs '\0'-termination.
  542. //
  543. // Use a temporary pointer so that *str will point
  544. // to the beginning of the value string in case
  545. // an error occurs.
  546. const char *p = *str;
  547. v = 0;
  548. do {
  549. if (v > UINT32_MAX / 10)
  550. return "Value out of range";
  551. v *= 10;
  552. const uint32_t add = (uint32_t)(*p - '0');
  553. if (UINT32_MAX - add < v)
  554. return "Value out of range";
  555. v += add;
  556. ++p;
  557. } while (p < name_eq_value_end
  558. && *p >= '0' && *p <= '9');
  559. if (p < name_eq_value_end) {
  560. // Remember this position so that it can be
  561. // used for error messages that are
  562. // specifically about the suffix. (Out of
  563. // range values are about the whole value
  564. // and those error messages point to the
  565. // beginning of the number part,
  566. // not to the suffix.)
  567. const char *multiplier_start = p;
  568. // If multiplier suffix shouldn't be used
  569. // then don't allow them even if the value
  570. // would stay within limits. This is a somewhat
  571. // unnecessary check but it rejects silly
  572. // things like lzma2:pb=0MiB which xz allows.
  573. if ((optmap[i].flags & OPTMAP_USE_BYTE_SUFFIX)
  574. == 0) {
  575. *str = multiplier_start;
  576. return "This option does not support "
  577. "any integer suffixes";
  578. }
  579. uint32_t shift;
  580. switch (*p) {
  581. case 'k':
  582. case 'K':
  583. shift = 10;
  584. break;
  585. case 'm':
  586. case 'M':
  587. shift = 20;
  588. break;
  589. case 'g':
  590. case 'G':
  591. shift = 30;
  592. break;
  593. default:
  594. *str = multiplier_start;
  595. return "Invalid multiplier suffix "
  596. "(KiB, MiB, or GiB)";
  597. }
  598. ++p;
  599. // Allow "M", "Mi", "MB", "MiB" and the same
  600. // for the other five characters from the
  601. // switch-statement above. All are handled
  602. // as base-2 (perhaps a mistake, perhaps not).
  603. // Note that 'i' and 'B' are case sensitive.
  604. if (p < name_eq_value_end && *p == 'i')
  605. ++p;
  606. if (p < name_eq_value_end && *p == 'B')
  607. ++p;
  608. // Now we must have no chars remaining.
  609. if (p < name_eq_value_end) {
  610. *str = multiplier_start;
  611. return "Invalid multiplier suffix "
  612. "(KiB, MiB, or GiB)";
  613. }
  614. if (v > (UINT32_MAX >> shift))
  615. return "Value out of range";
  616. v <<= shift;
  617. }
  618. if (v < optmap[i].u.range.min
  619. || v > optmap[i].u.range.max)
  620. return "Value out of range";
  621. }
  622. // Set the value in filter_options. Enums are handled
  623. // specially since the underlying type isn't the same
  624. // as uint32_t on all systems.
  625. void *ptr = (char *)filter_options + optmap[i].offset;
  626. switch (optmap[i].type) {
  627. case OPTMAP_TYPE_LZMA_MODE:
  628. *(lzma_mode *)ptr = (lzma_mode)v;
  629. break;
  630. case OPTMAP_TYPE_LZMA_MATCH_FINDER:
  631. *(lzma_match_finder *)ptr = (lzma_match_finder)v;
  632. break;
  633. default:
  634. *(uint32_t *)ptr = v;
  635. break;
  636. }
  637. // This option has been successfully handled.
  638. *str = name_eq_value_end;
  639. }
  640. // No errors.
  641. return NULL;
  642. }
  643. /// Finds the name of the filter at the beginning of the string and
  644. /// calls filter_name_map[i].parse() to decode the filter-specific options.
  645. /// The caller must have set str_end so that exactly one filter and its
  646. /// options are present without any trailing characters.
  647. static const char *
  648. parse_filter(const char **const str, const char *str_end, lzma_filter *filter,
  649. const lzma_allocator *allocator, bool only_xz)
  650. {
  651. // Search for a colon or equals sign that would separate the filter
  652. // name from filter options. If neither is found, then the input
  653. // string only contains a filter name and there are no options.
  654. //
  655. // First assume that a colon or equals sign won't be found:
  656. const char *name_end = str_end;
  657. const char *opts_start = str_end;
  658. for (const char *p = *str; p < str_end; ++p) {
  659. if (*p == ':' || *p == '=') {
  660. name_end = p;
  661. // Filter options (name1=value1,name2=value2,...)
  662. // begin after the colon or equals sign.
  663. opts_start = p + 1;
  664. break;
  665. }
  666. }
  667. // Reject a too long filter name so that the memcmp()
  668. // in the loop below won't read past the end of the
  669. // string in filter_name_map[i].name.
  670. const size_t name_len = (size_t)(name_end - *str);
  671. if (name_len > NAME_LEN_MAX)
  672. return "Unknown filter name";
  673. for (size_t i = 0; i < ARRAY_SIZE(filter_name_map); ++i) {
  674. if (memcmp(*str, filter_name_map[i].name, name_len) == 0
  675. && filter_name_map[i].name[name_len] == '\0') {
  676. if (only_xz && filter_name_map[i].id
  677. >= LZMA_FILTER_RESERVED_START)
  678. return "This filter cannot be used in "
  679. "the .xz format";
  680. // Allocate the filter-specific options and
  681. // initialize the memory with zeros.
  682. void *options = lzma_alloc_zero(
  683. filter_name_map[i].opts_size,
  684. allocator);
  685. if (options == NULL)
  686. return "Memory allocation failed";
  687. // Filter name was found so the input string is good
  688. // at least this far.
  689. *str = opts_start;
  690. const char *errmsg = filter_name_map[i].parse(
  691. str, str_end, options);
  692. if (errmsg != NULL) {
  693. lzma_free(options, allocator);
  694. return errmsg;
  695. }
  696. // *filter is modified only when parsing is successful.
  697. filter->id = filter_name_map[i].id;
  698. filter->options = options;
  699. return NULL;
  700. }
  701. }
  702. return "Unknown filter name";
  703. }
  704. /// Converts the string to a filter chain (array of lzma_filter structures).
  705. ///
  706. /// *str is advanced every time something has been decoded successfully.
  707. /// This way the caller knows where in the string a possible error occurred.
  708. static const char *
  709. str_to_filters(const char **const str, lzma_filter *filters, uint32_t flags,
  710. const lzma_allocator *allocator)
  711. {
  712. const char *errmsg;
  713. // Skip leading spaces.
  714. while (**str == ' ')
  715. ++*str;
  716. if (**str == '\0')
  717. return "Empty string is not allowed, "
  718. "try \"6\" if a default value is needed";
  719. // Detect the type of the string.
  720. //
  721. // A string beginning with a digit or a string beginning with
  722. // one dash and a digit are treated as presets. Trailing spaces
  723. // will be ignored too (leading spaces were already ignored above).
  724. //
  725. // For example, "6", "7 ", "-9e", or " -3 " are treated as presets.
  726. // Strings like "-" or "- " aren't preset.
  727. #define MY_IS_DIGIT(c) ((c) >= '0' && (c) <= '9')
  728. if (MY_IS_DIGIT(**str) || (**str == '-' && MY_IS_DIGIT((*str)[1]))) {
  729. if (**str == '-')
  730. ++*str;
  731. // Ignore trailing spaces.
  732. const size_t str_len = strlen(*str);
  733. const char *str_end = memchr(*str, ' ', str_len);
  734. if (str_end != NULL) {
  735. // There is at least one trailing space. Check that
  736. // there are no chars other than spaces.
  737. for (size_t i = 1; str_end[i] != '\0'; ++i)
  738. if (str_end[i] != ' ')
  739. return "Unsupported preset";
  740. } else {
  741. // There are no trailing spaces. Use the whole string.
  742. str_end = *str + str_len;
  743. }
  744. uint32_t preset;
  745. errmsg = parse_lzma12_preset(str, str_end, &preset);
  746. if (errmsg != NULL)
  747. return errmsg;
  748. lzma_options_lzma *opts = lzma_alloc(sizeof(*opts), allocator);
  749. if (opts == NULL)
  750. return "Memory allocation failed";
  751. if (lzma_lzma_preset(opts, preset)) {
  752. lzma_free(opts, allocator);
  753. return "Unsupported preset";
  754. }
  755. filters[0].id = LZMA_FILTER_LZMA2;
  756. filters[0].options = opts;
  757. filters[1].id = LZMA_VLI_UNKNOWN;
  758. filters[1].options = NULL;
  759. return NULL;
  760. }
  761. // Not a preset so it must be a filter chain.
  762. //
  763. // If LZMA_STR_ALL_FILTERS isn't used we allow only filters that
  764. // can be used in .xz.
  765. const bool only_xz = (flags & LZMA_STR_ALL_FILTERS) == 0;
  766. // Use a temporary array so that we don't modify the caller-supplied
  767. // one until we know that no errors occurred.
  768. lzma_filter temp_filters[LZMA_FILTERS_MAX + 1];
  769. size_t i = 0;
  770. do {
  771. if (i == LZMA_FILTERS_MAX) {
  772. errmsg = "The maximum number of filters is four";
  773. goto error;
  774. }
  775. // Skip "--" if present.
  776. if ((*str)[0] == '-' && (*str)[1] == '-')
  777. *str += 2;
  778. // Locate the end of "filter:name1=value1,name2=value2",
  779. // stopping at the first "--" or a single space.
  780. const char *filter_end = *str;
  781. while (filter_end[0] != '\0') {
  782. if ((filter_end[0] == '-' && filter_end[1] == '-')
  783. || filter_end[0] == ' ')
  784. break;
  785. ++filter_end;
  786. }
  787. // Inputs that have "--" at the end or "-- " in the middle
  788. // will result in an empty filter name.
  789. if (filter_end == *str) {
  790. errmsg = "Filter name is missing";
  791. goto error;
  792. }
  793. errmsg = parse_filter(str, filter_end, &temp_filters[i],
  794. allocator, only_xz);
  795. if (errmsg != NULL)
  796. goto error;
  797. // Skip trailing spaces.
  798. while (**str == ' ')
  799. ++*str;
  800. ++i;
  801. } while (**str != '\0');
  802. // Seems to be good, terminate the array so that
  803. // basic validation can be done.
  804. temp_filters[i].id = LZMA_VLI_UNKNOWN;
  805. temp_filters[i].options = NULL;
  806. // Do basic validation if the application didn't prohibit it.
  807. if ((flags & LZMA_STR_NO_VALIDATION) == 0) {
  808. size_t dummy;
  809. const lzma_ret ret = lzma_validate_chain(temp_filters, &dummy);
  810. assert(ret == LZMA_OK || ret == LZMA_OPTIONS_ERROR);
  811. if (ret != LZMA_OK) {
  812. errmsg = "Invalid filter chain "
  813. "('lzma2' missing at the end?)";
  814. goto error;
  815. }
  816. }
  817. // All good. Copy the filters to the application supplied array.
  818. memcpy(filters, temp_filters, (i + 1) * sizeof(lzma_filter));
  819. return NULL;
  820. error:
  821. // Free the filter options that were successfully decoded.
  822. while (i-- > 0)
  823. lzma_free(temp_filters[i].options, allocator);
  824. return errmsg;
  825. }
  826. extern LZMA_API(const char *)
  827. lzma_str_to_filters(const char *str, int *error_pos, lzma_filter *filters,
  828. uint32_t flags, const lzma_allocator *allocator)
  829. {
  830. // If error_pos isn't NULL, *error_pos must always be set.
  831. // liblzma <= 5.4.6 and <= 5.6.1 have a bug and don't do this
  832. // when str == NULL or filters == NULL or flags are unsupported.
  833. if (error_pos != NULL)
  834. *error_pos = 0;
  835. if (str == NULL || filters == NULL)
  836. return "Unexpected NULL pointer argument(s) "
  837. "to lzma_str_to_filters()";
  838. // Validate the flags.
  839. const uint32_t supported_flags
  840. = LZMA_STR_ALL_FILTERS
  841. | LZMA_STR_NO_VALIDATION;
  842. if (flags & ~supported_flags)
  843. return "Unsupported flags to lzma_str_to_filters()";
  844. const char *used = str;
  845. const char *errmsg = str_to_filters(&used, filters, flags, allocator);
  846. if (error_pos != NULL) {
  847. const size_t n = (size_t)(used - str);
  848. *error_pos = n > INT_MAX ? INT_MAX : (int)n;
  849. }
  850. return errmsg;
  851. }
  852. /// Converts options of one filter to a string.
  853. ///
  854. /// The caller must have already put the filter name in the destination
  855. /// string. Since it is possible that no options will be needed, the caller
  856. /// won't have put a delimiter character (':' or '=') in the string yet.
  857. /// We will add it if at least one option will be added to the string.
  858. static void
  859. strfy_filter(lzma_str *dest, const char *delimiter,
  860. const option_map *optmap, size_t optmap_count,
  861. const void *filter_options)
  862. {
  863. for (size_t i = 0; i < optmap_count; ++i) {
  864. // No attempt is made to reverse LZMA1/2 preset.
  865. if (optmap[i].type == OPTMAP_TYPE_LZMA_PRESET)
  866. continue;
  867. // All options have integer values, some just are mapped
  868. // to a string with a name_value_map. LZMA1/2 preset
  869. // isn't reversed back to preset=PRESET form.
  870. uint32_t v;
  871. const void *ptr
  872. = (const char *)filter_options + optmap[i].offset;
  873. switch (optmap[i].type) {
  874. case OPTMAP_TYPE_LZMA_MODE:
  875. v = *(const lzma_mode *)ptr;
  876. break;
  877. case OPTMAP_TYPE_LZMA_MATCH_FINDER:
  878. v = *(const lzma_match_finder *)ptr;
  879. break;
  880. default:
  881. v = *(const uint32_t *)ptr;
  882. break;
  883. }
  884. // Skip this if this option should be omitted from
  885. // the string when the value is zero.
  886. if (v == 0 && (optmap[i].flags & OPTMAP_NO_STRFY_ZERO))
  887. continue;
  888. // Before the first option we add whatever delimiter
  889. // the caller gave us. For later options a comma is used.
  890. str_append_str(dest, delimiter);
  891. delimiter = ",";
  892. // Add the option name and equals sign.
  893. str_append_str(dest, optmap[i].name);
  894. str_append_str(dest, "=");
  895. if (optmap[i].flags & OPTMAP_USE_NAME_VALUE_MAP) {
  896. const name_value_map *map = optmap[i].u.map;
  897. size_t j = 0;
  898. while (true) {
  899. if (map[j].name[0] == '\0') {
  900. str_append_str(dest, "UNKNOWN");
  901. break;
  902. }
  903. if (map[j].value == v) {
  904. str_append_str(dest, map[j].name);
  905. break;
  906. }
  907. ++j;
  908. }
  909. } else {
  910. str_append_u32(dest, v,
  911. optmap[i].flags & OPTMAP_USE_BYTE_SUFFIX);
  912. }
  913. }
  914. return;
  915. }
  916. extern LZMA_API(lzma_ret)
  917. lzma_str_from_filters(char **output_str, const lzma_filter *filters,
  918. uint32_t flags, const lzma_allocator *allocator)
  919. {
  920. // On error *output_str is always set to NULL.
  921. // Do it as the very first step.
  922. if (output_str == NULL)
  923. return LZMA_PROG_ERROR;
  924. *output_str = NULL;
  925. if (filters == NULL)
  926. return LZMA_PROG_ERROR;
  927. // Validate the flags.
  928. const uint32_t supported_flags
  929. = LZMA_STR_ENCODER
  930. | LZMA_STR_DECODER
  931. | LZMA_STR_GETOPT_LONG
  932. | LZMA_STR_NO_SPACES;
  933. if (flags & ~supported_flags)
  934. return LZMA_OPTIONS_ERROR;
  935. // There must be at least one filter.
  936. if (filters[0].id == LZMA_VLI_UNKNOWN)
  937. return LZMA_OPTIONS_ERROR;
  938. // Allocate memory for the output string.
  939. lzma_str dest;
  940. return_if_error(str_init(&dest, allocator));
  941. const bool show_opts = (flags & (LZMA_STR_ENCODER | LZMA_STR_DECODER));
  942. const char *opt_delim = (flags & LZMA_STR_GETOPT_LONG) ? "=" : ":";
  943. for (size_t i = 0; filters[i].id != LZMA_VLI_UNKNOWN; ++i) {
  944. // If we reach LZMA_FILTERS_MAX, then the filters array
  945. // is too large since the ID cannot be LZMA_VLI_UNKNOWN here.
  946. if (i == LZMA_FILTERS_MAX) {
  947. str_free(&dest, allocator);
  948. return LZMA_OPTIONS_ERROR;
  949. }
  950. // Don't add a space between filters if the caller
  951. // doesn't want them.
  952. if (i > 0 && !(flags & LZMA_STR_NO_SPACES))
  953. str_append_str(&dest, " ");
  954. // Use dashes for xz getopt_long() compatible syntax but also
  955. // use dashes to separate filters when spaces weren't wanted.
  956. if ((flags & LZMA_STR_GETOPT_LONG)
  957. || (i > 0 && (flags & LZMA_STR_NO_SPACES)))
  958. str_append_str(&dest, "--");
  959. size_t j = 0;
  960. while (true) {
  961. if (j == ARRAY_SIZE(filter_name_map)) {
  962. // Filter ID in filters[i].id isn't supported.
  963. str_free(&dest, allocator);
  964. return LZMA_OPTIONS_ERROR;
  965. }
  966. if (filter_name_map[j].id == filters[i].id) {
  967. // Add the filter name.
  968. str_append_str(&dest, filter_name_map[j].name);
  969. // If only the filter names were wanted then
  970. // skip to the next filter. In this case
  971. // .options is ignored and may be NULL even
  972. // when the filter doesn't allow NULL options.
  973. if (!show_opts)
  974. break;
  975. if (filters[i].options == NULL) {
  976. if (!filter_name_map[j].allow_null) {
  977. // Filter-specific options
  978. // are missing but with
  979. // this filter the options
  980. // structure is mandatory.
  981. str_free(&dest, allocator);
  982. return LZMA_OPTIONS_ERROR;
  983. }
  984. // .options is allowed to be NULL.
  985. // There is no need to add any
  986. // options to the string.
  987. break;
  988. }
  989. // Options structure is available. Add
  990. // the filter options to the string.
  991. const size_t optmap_count
  992. = (flags & LZMA_STR_ENCODER)
  993. ? filter_name_map[j].strfy_encoder
  994. : filter_name_map[j].strfy_decoder;
  995. strfy_filter(&dest, opt_delim,
  996. filter_name_map[j].optmap,
  997. optmap_count,
  998. filters[i].options);
  999. break;
  1000. }
  1001. ++j;
  1002. }
  1003. }
  1004. return str_finish(output_str, &dest, allocator);
  1005. }
  1006. extern LZMA_API(lzma_ret)
  1007. lzma_str_list_filters(char **output_str, lzma_vli filter_id, uint32_t flags,
  1008. const lzma_allocator *allocator)
  1009. {
  1010. // On error *output_str is always set to NULL.
  1011. // Do it as the very first step.
  1012. if (output_str == NULL)
  1013. return LZMA_PROG_ERROR;
  1014. *output_str = NULL;
  1015. // Validate the flags.
  1016. const uint32_t supported_flags
  1017. = LZMA_STR_ALL_FILTERS
  1018. | LZMA_STR_ENCODER
  1019. | LZMA_STR_DECODER
  1020. | LZMA_STR_GETOPT_LONG;
  1021. if (flags & ~supported_flags)
  1022. return LZMA_OPTIONS_ERROR;
  1023. // Allocate memory for the output string.
  1024. lzma_str dest;
  1025. return_if_error(str_init(&dest, allocator));
  1026. // If only listing the filter names then separate them with spaces.
  1027. // Otherwise use newlines.
  1028. const bool show_opts = (flags & (LZMA_STR_ENCODER | LZMA_STR_DECODER));
  1029. const char *filter_delim = show_opts ? "\n" : " ";
  1030. const char *opt_delim = (flags & LZMA_STR_GETOPT_LONG) ? "=" : ":";
  1031. bool first_filter_printed = false;
  1032. for (size_t i = 0; i < ARRAY_SIZE(filter_name_map); ++i) {
  1033. // If we are printing only one filter then skip others.
  1034. if (filter_id != LZMA_VLI_UNKNOWN
  1035. && filter_id != filter_name_map[i].id)
  1036. continue;
  1037. // If we are printing only .xz filters then skip the others.
  1038. if (filter_name_map[i].id >= LZMA_FILTER_RESERVED_START
  1039. && (flags & LZMA_STR_ALL_FILTERS) == 0
  1040. && filter_id == LZMA_VLI_UNKNOWN)
  1041. continue;
  1042. // Add a new line if this isn't the first filter being
  1043. // written to the string.
  1044. if (first_filter_printed)
  1045. str_append_str(&dest, filter_delim);
  1046. first_filter_printed = true;
  1047. if (flags & LZMA_STR_GETOPT_LONG)
  1048. str_append_str(&dest, "--");
  1049. str_append_str(&dest, filter_name_map[i].name);
  1050. // If only the filter names were wanted then continue
  1051. // to the next filter.
  1052. if (!show_opts)
  1053. continue;
  1054. const option_map *optmap = filter_name_map[i].optmap;
  1055. const char *d = opt_delim;
  1056. const size_t end = (flags & LZMA_STR_ENCODER)
  1057. ? filter_name_map[i].strfy_encoder
  1058. : filter_name_map[i].strfy_decoder;
  1059. for (size_t j = 0; j < end; ++j) {
  1060. // The first option is delimited from the filter
  1061. // name using "=" or ":" and the rest of the options
  1062. // are separated with ",".
  1063. str_append_str(&dest, d);
  1064. d = ",";
  1065. // optname=<possible_values>
  1066. str_append_str(&dest, optmap[j].name);
  1067. str_append_str(&dest, "=<");
  1068. if (optmap[j].type == OPTMAP_TYPE_LZMA_PRESET) {
  1069. // LZMA1/2 preset has its custom help string.
  1070. str_append_str(&dest, LZMA12_PRESET_STR);
  1071. } else if (optmap[j].flags
  1072. & OPTMAP_USE_NAME_VALUE_MAP) {
  1073. // Separate the possible option values by "|".
  1074. const name_value_map *m = optmap[j].u.map;
  1075. for (size_t k = 0; m[k].name[0] != '\0'; ++k) {
  1076. if (k > 0)
  1077. str_append_str(&dest, "|");
  1078. str_append_str(&dest, m[k].name);
  1079. }
  1080. } else {
  1081. // Integer range is shown as min-max.
  1082. const bool use_byte_suffix = optmap[j].flags
  1083. & OPTMAP_USE_BYTE_SUFFIX;
  1084. str_append_u32(&dest, optmap[j].u.range.min,
  1085. use_byte_suffix);
  1086. str_append_str(&dest, "-");
  1087. str_append_u32(&dest, optmap[j].u.range.max,
  1088. use_byte_suffix);
  1089. }
  1090. str_append_str(&dest, ">");
  1091. }
  1092. }
  1093. // If no filters were added to the string then it must be because
  1094. // the caller provided an unsupported Filter ID.
  1095. if (!first_filter_printed) {
  1096. str_free(&dest, allocator);
  1097. return LZMA_OPTIONS_ERROR;
  1098. }
  1099. return str_finish(output_str, &dest, allocator);
  1100. }