string_conversion.c 36 KB

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