001-xz_wrapper-support-multiple-lzma-configuration-optio.patch 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. From dcb976fe4ee40e4bac8ae0dcc836629c625a6fd4 Mon Sep 17 00:00:00 2001
  2. From: Christian Marangi <[email protected]>
  3. Date: Fri, 14 Oct 2022 15:59:16 +0200
  4. Subject: [PATCH] xz_wrapper: support multiple lzma configuration options
  5. Add option to configure preset, lc, lp and pb lzma parameters.
  6. -Xpreset can be used to set the compression level.
  7. -Xe can be used to set the 'EXTREME' flag to use the lzma compression
  8. options tweaking additional settings on top of the compression level set.
  9. New option added:
  10. -Xpreset
  11. -Xe
  12. -Xlc
  13. -Xlp
  14. -Xpb
  15. Signed-off-by: Christian Marangi <[email protected]>
  16. ---
  17. squashfs-tools/xz_wrapper.c | 119 ++++++++++++++++++++++++++++++++++--
  18. 1 file changed, 115 insertions(+), 4 deletions(-)
  19. --- a/squashfs-tools/xz_wrapper.c
  20. +++ b/squashfs-tools/xz_wrapper.c
  21. @@ -44,7 +44,10 @@ static struct bcj bcj[] = {
  22. static int filter_count = 1;
  23. static int dictionary_size = 0;
  24. static float dictionary_percent = 0;
  25. -
  26. +static int preset = LZMA_PRESET_DEFAULT;
  27. +static int lc = -1;
  28. +static int lp = -1;
  29. +static int pb = -1;
  30. /*
  31. * This function is called by the options parsing code in mksquashfs.c
  32. @@ -53,6 +56,11 @@ static float dictionary_percent = 0;
  33. * Two specific options are supported:
  34. * -Xbcj
  35. * -Xdict-size
  36. + * -Xpreset
  37. + * -Xe
  38. + * -Xlc
  39. + * -Xlp
  40. + * -Xpb
  41. *
  42. * This function returns:
  43. * >=0 (number of additional args parsed) on success
  44. @@ -141,6 +149,85 @@ static int xz_options(char *argv[], int
  45. }
  46. return 1;
  47. + } else if(strcmp(argv[0], "-Xpreset") == 0) {
  48. + char *b;
  49. + long val;
  50. +
  51. + if(argc < 2) {
  52. + fprintf(stderr, "xz: -Xpreset missing preset-level "
  53. + "(valid value 0-9)\n");
  54. + goto failed;
  55. + }
  56. +
  57. + val = strtol(argv[1], &b, 10);
  58. + if (*b != '\0' || (int) val < 0 || (int) val & ~LZMA_PRESET_LEVEL_MASK) {
  59. + fprintf(stderr, "xz: -Xpreset can't be "
  60. + "negative or more than the max preset\n");
  61. + goto failed;
  62. + }
  63. +
  64. + preset &= ~LZMA_PRESET_LEVEL_MASK;
  65. + preset |= (int) val;
  66. +
  67. + return 1;
  68. + } else if(strcmp(argv[0], "-Xe") == 0) {
  69. + preset |= LZMA_PRESET_EXTREME;
  70. +
  71. + return 0;
  72. + } else if(strcmp(argv[0], "-Xlc") == 0) {
  73. + char *b;
  74. + long val;
  75. +
  76. + if(argc < 2) {
  77. + fprintf(stderr, "xz: -Xlc missing value\n");
  78. + goto failed;
  79. + }
  80. +
  81. + val = strtol(argv[1], &b, 10);
  82. + if (*b != '\0' || (int) val < LZMA_LCLP_MIN || (int) val > LZMA_LCLP_MAX) {
  83. + fprintf(stderr, "xz: -Xlc invalid value\n");
  84. + goto failed;
  85. + }
  86. +
  87. + lc = (int) val;
  88. +
  89. + return 1;
  90. + } else if(strcmp(argv[0], "-Xlp") == 0) {
  91. + char *b;
  92. + long val;
  93. +
  94. + if(argc < 2) {
  95. + fprintf(stderr, "xz: -Xlp missing value\n");
  96. + goto failed;
  97. + }
  98. +
  99. + val = strtol(argv[1], &b, 10);
  100. + if (*b != '\0' || (int) val < LZMA_LCLP_MIN || (int) val > LZMA_LCLP_MAX) {
  101. + fprintf(stderr, "xz: -Xlp invalid value\n");
  102. + goto failed;
  103. + }
  104. +
  105. + lp = (int) val;
  106. +
  107. + return 1;
  108. + } else if(strcmp(argv[0], "-Xpb") == 0) {
  109. + char *b;
  110. + long val;
  111. +
  112. + if(argc < 2) {
  113. + fprintf(stderr, "xz: -Xpb missing value\n");
  114. + goto failed;
  115. + }
  116. +
  117. + val = strtol(argv[1], &b, 10);
  118. + if (*b != '\0' || (int) val < LZMA_PB_MIN || (int) val > LZMA_PB_MAX) {
  119. + fprintf(stderr, "xz: -Xpb invalid value\n");
  120. + goto failed;
  121. + }
  122. +
  123. + pb = (int) val;
  124. +
  125. + return 1;
  126. }
  127. return -1;
  128. @@ -446,11 +533,20 @@ static int xz_compress(void *strm, void
  129. for(i = 0; i < stream->filters; i++) {
  130. struct filter *filter = &stream->filter[i];
  131. - if(lzma_lzma_preset(&stream->opt, LZMA_PRESET_DEFAULT))
  132. - goto failed;
  133. + if(lzma_lzma_preset(&stream->opt, preset))
  134. + goto failed;
  135. stream->opt.dict_size = stream->dictionary_size;
  136. + if (lc >= 0)
  137. + stream->opt.lc = lc;
  138. +
  139. + if (lp >= 0)
  140. + stream->opt.lp = lp;
  141. +
  142. + if (pb >= 0)
  143. + stream->opt.pb = pb;
  144. +
  145. filter->length = 0;
  146. res = lzma_stream_buffer_encode(filter->filter,
  147. LZMA_CHECK_CRC32, NULL, src, size, filter->buffer,
  148. @@ -521,13 +617,28 @@ static void xz_usage(FILE *stream)
  149. fprintf(stream, " header as either 2^n or as 2^n+2^(n+1).\n\t\t");
  150. fprintf(stream, "Example dict-sizes are 75%%, 50%%, 37.5%%, 25%%, or");
  151. fprintf(stream, " 32K, 16K, 8K\n\t\tetc.\n");
  152. + fprintf(stream, "\t -Xpreset <preset-level>\n");
  153. + fprintf(stream, "\t\tUse <preset-value> as the custom preset to use");
  154. + fprintf(stream, " on compress.\n\t\t<preset-level> should be 0 .. 9");
  155. + fprintf(stream, " (default 6)\n");
  156. + fprintf(stream, "\t -Xe\n");
  157. + fprintf(stream, "\t\tEnable additional compression settings by passing");
  158. + fprintf(stream, " the EXTREME\n\t\tflag to the compression flags.\n");
  159. + fprintf(stream, "\t -Xlc <value>\n");
  160. + fprintf(stream, "\t -Xlp <value>\n");
  161. + fprintf(stream, "\t -Xpb <value>\n");
  162. }
  163. static int option_args(char *option)
  164. {
  165. if(strcmp(option, "-Xbcj") == 0 ||
  166. - strcmp(option, "-Xdict-size") == 0)
  167. + strcmp(option, "-Xdict-size") == 0 ||
  168. + strcmp(option, "-Xpreset") == 0 ||
  169. + strcmp(option, "-Xe") == 0 ||
  170. + strcmp(option, "-Xlc") == 0 ||
  171. + strcmp(option, "-Xlp") == 0 ||
  172. + strcmp(option, "-Xpb") == 0)
  173. return 1;
  174. return 0;