string_conversion.c 36 KB

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