zstd_decompress.c 88 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320
  1. /*
  2. * Copyright (c) Facebook, Inc.
  3. * All rights reserved.
  4. *
  5. * This source code is licensed under both the BSD-style license (found in the
  6. * LICENSE file in the root directory of this source tree) and the GPLv2 (found
  7. * in the COPYING file in the root directory of this source tree).
  8. * You may select, at your option, one of the above-listed licenses.
  9. */
  10. /// Zstandard educational decoder implementation
  11. /// See https://github.com/facebook/zstd/blob/dev/doc/zstd_compression_format.md
  12. #include <stdint.h> // uint8_t, etc.
  13. #include <stdlib.h> // malloc, free, exit
  14. #include <stdio.h> // fprintf
  15. #include <string.h> // memset, memcpy
  16. #include "zstd_decompress.h"
  17. /******* IMPORTANT CONSTANTS *********************************************/
  18. // Zstandard frame
  19. // "Magic_Number
  20. // 4 Bytes, little-endian format. Value : 0xFD2FB528"
  21. #define ZSTD_MAGIC_NUMBER 0xFD2FB528U
  22. // The size of `Block_Content` is limited by `Block_Maximum_Size`,
  23. #define ZSTD_BLOCK_SIZE_MAX ((size_t)128 * 1024)
  24. // literal blocks can't be larger than their block
  25. #define MAX_LITERALS_SIZE ZSTD_BLOCK_SIZE_MAX
  26. /******* UTILITY MACROS AND TYPES *********************************************/
  27. #define MAX(a, b) ((a) > (b) ? (a) : (b))
  28. #define MIN(a, b) ((a) < (b) ? (a) : (b))
  29. #if defined(ZDEC_NO_MESSAGE)
  30. #define MESSAGE(...)
  31. #else
  32. #define MESSAGE(...) fprintf(stderr, "" __VA_ARGS__)
  33. #endif
  34. /// This decoder calls exit(1) when it encounters an error, however a production
  35. /// library should propagate error codes
  36. #define ERROR(s) \
  37. do { \
  38. MESSAGE("Error: %s\n", s); \
  39. exit(1); \
  40. } while (0)
  41. #define INP_SIZE() \
  42. ERROR("Input buffer smaller than it should be or input is " \
  43. "corrupted")
  44. #define OUT_SIZE() ERROR("Output buffer too small for output")
  45. #define CORRUPTION() ERROR("Corruption detected while decompressing")
  46. #define BAD_ALLOC() ERROR("Memory allocation error")
  47. #define IMPOSSIBLE() ERROR("An impossibility has occurred")
  48. typedef uint8_t u8;
  49. typedef uint16_t u16;
  50. typedef uint32_t u32;
  51. typedef uint64_t u64;
  52. typedef int8_t i8;
  53. typedef int16_t i16;
  54. typedef int32_t i32;
  55. typedef int64_t i64;
  56. /******* END UTILITY MACROS AND TYPES *****************************************/
  57. /******* IMPLEMENTATION PRIMITIVE PROTOTYPES **********************************/
  58. /// The implementations for these functions can be found at the bottom of this
  59. /// file. They implement low-level functionality needed for the higher level
  60. /// decompression functions.
  61. /*** IO STREAM OPERATIONS *************/
  62. /// ostream_t/istream_t are used to wrap the pointers/length data passed into
  63. /// ZSTD_decompress, so that all IO operations are safely bounds checked
  64. /// They are written/read forward, and reads are treated as little-endian
  65. /// They should be used opaquely to ensure safety
  66. typedef struct {
  67. u8 *ptr;
  68. size_t len;
  69. } ostream_t;
  70. typedef struct {
  71. const u8 *ptr;
  72. size_t len;
  73. // Input often reads a few bits at a time, so maintain an internal offset
  74. int bit_offset;
  75. } istream_t;
  76. /// The following two functions are the only ones that allow the istream to be
  77. /// non-byte aligned
  78. /// Reads `num` bits from a bitstream, and updates the internal offset
  79. static inline u64 IO_read_bits(istream_t *const in, const int num_bits);
  80. /// Backs-up the stream by `num` bits so they can be read again
  81. static inline void IO_rewind_bits(istream_t *const in, const int num_bits);
  82. /// If the remaining bits in a byte will be unused, advance to the end of the
  83. /// byte
  84. static inline void IO_align_stream(istream_t *const in);
  85. /// Write the given byte into the output stream
  86. static inline void IO_write_byte(ostream_t *const out, u8 symb);
  87. /// Returns the number of bytes left to be read in this stream. The stream must
  88. /// be byte aligned.
  89. static inline size_t IO_istream_len(const istream_t *const in);
  90. /// Advances the stream by `len` bytes, and returns a pointer to the chunk that
  91. /// was skipped. The stream must be byte aligned.
  92. static inline const u8 *IO_get_read_ptr(istream_t *const in, size_t len);
  93. /// Advances the stream by `len` bytes, and returns a pointer to the chunk that
  94. /// was skipped so it can be written to.
  95. static inline u8 *IO_get_write_ptr(ostream_t *const out, size_t len);
  96. /// Advance the inner state by `len` bytes. The stream must be byte aligned.
  97. static inline void IO_advance_input(istream_t *const in, size_t len);
  98. /// Returns an `ostream_t` constructed from the given pointer and length.
  99. static inline ostream_t IO_make_ostream(u8 *out, size_t len);
  100. /// Returns an `istream_t` constructed from the given pointer and length.
  101. static inline istream_t IO_make_istream(const u8 *in, size_t len);
  102. /// Returns an `istream_t` with the same base as `in`, and length `len`.
  103. /// Then, advance `in` to account for the consumed bytes.
  104. /// `in` must be byte aligned.
  105. static inline istream_t IO_make_sub_istream(istream_t *const in, size_t len);
  106. /*** END IO STREAM OPERATIONS *********/
  107. /*** BITSTREAM OPERATIONS *************/
  108. /// Read `num` bits (up to 64) from `src + offset`, where `offset` is in bits,
  109. /// and return them interpreted as a little-endian unsigned integer.
  110. static inline u64 read_bits_LE(const u8 *src, const int num_bits,
  111. const size_t offset);
  112. /// Read bits from the end of a HUF or FSE bitstream. `offset` is in bits, so
  113. /// it updates `offset` to `offset - bits`, and then reads `bits` bits from
  114. /// `src + offset`. If the offset becomes negative, the extra bits at the
  115. /// bottom are filled in with `0` bits instead of reading from before `src`.
  116. static inline u64 STREAM_read_bits(const u8 *src, const int bits,
  117. i64 *const offset);
  118. /*** END BITSTREAM OPERATIONS *********/
  119. /*** BIT COUNTING OPERATIONS **********/
  120. /// Returns the index of the highest set bit in `num`, or `-1` if `num == 0`
  121. static inline int highest_set_bit(const u64 num);
  122. /*** END BIT COUNTING OPERATIONS ******/
  123. /*** HUFFMAN PRIMITIVES ***************/
  124. // Table decode method uses exponential memory, so we need to limit depth
  125. #define HUF_MAX_BITS (16)
  126. // Limit the maximum number of symbols to 256 so we can store a symbol in a byte
  127. #define HUF_MAX_SYMBS (256)
  128. /// Structure containing all tables necessary for efficient Huffman decoding
  129. typedef struct {
  130. u8 *symbols;
  131. u8 *num_bits;
  132. int max_bits;
  133. } HUF_dtable;
  134. /// Decode a single symbol and read in enough bits to refresh the state
  135. static inline u8 HUF_decode_symbol(const HUF_dtable *const dtable,
  136. u16 *const state, const u8 *const src,
  137. i64 *const offset);
  138. /// Read in a full state's worth of bits to initialize it
  139. static inline void HUF_init_state(const HUF_dtable *const dtable,
  140. u16 *const state, const u8 *const src,
  141. i64 *const offset);
  142. /// Decompresses a single Huffman stream, returns the number of bytes decoded.
  143. /// `src_len` must be the exact length of the Huffman-coded block.
  144. static size_t HUF_decompress_1stream(const HUF_dtable *const dtable,
  145. ostream_t *const out, istream_t *const in);
  146. /// Same as previous but decodes 4 streams, formatted as in the Zstandard
  147. /// specification.
  148. /// `src_len` must be the exact length of the Huffman-coded block.
  149. static size_t HUF_decompress_4stream(const HUF_dtable *const dtable,
  150. ostream_t *const out, istream_t *const in);
  151. /// Initialize a Huffman decoding table using the table of bit counts provided
  152. static void HUF_init_dtable(HUF_dtable *const table, const u8 *const bits,
  153. const int num_symbs);
  154. /// Initialize a Huffman decoding table using the table of weights provided
  155. /// Weights follow the definition provided in the Zstandard specification
  156. static void HUF_init_dtable_usingweights(HUF_dtable *const table,
  157. const u8 *const weights,
  158. const int num_symbs);
  159. /// Free the malloc'ed parts of a decoding table
  160. static void HUF_free_dtable(HUF_dtable *const dtable);
  161. /*** END HUFFMAN PRIMITIVES ***********/
  162. /*** FSE PRIMITIVES *******************/
  163. /// For more description of FSE see
  164. /// https://github.com/Cyan4973/FiniteStateEntropy/
  165. // FSE table decoding uses exponential memory, so limit the maximum accuracy
  166. #define FSE_MAX_ACCURACY_LOG (15)
  167. // Limit the maximum number of symbols so they can be stored in a single byte
  168. #define FSE_MAX_SYMBS (256)
  169. /// The tables needed to decode FSE encoded streams
  170. typedef struct {
  171. u8 *symbols;
  172. u8 *num_bits;
  173. u16 *new_state_base;
  174. int accuracy_log;
  175. } FSE_dtable;
  176. /// Return the symbol for the current state
  177. static inline u8 FSE_peek_symbol(const FSE_dtable *const dtable,
  178. const u16 state);
  179. /// Read the number of bits necessary to update state, update, and shift offset
  180. /// back to reflect the bits read
  181. static inline void FSE_update_state(const FSE_dtable *const dtable,
  182. u16 *const state, const u8 *const src,
  183. i64 *const offset);
  184. /// Combine peek and update: decode a symbol and update the state
  185. static inline u8 FSE_decode_symbol(const FSE_dtable *const dtable,
  186. u16 *const state, const u8 *const src,
  187. i64 *const offset);
  188. /// Read bits from the stream to initialize the state and shift offset back
  189. static inline void FSE_init_state(const FSE_dtable *const dtable,
  190. u16 *const state, const u8 *const src,
  191. i64 *const offset);
  192. /// Decompress two interleaved bitstreams (e.g. compressed Huffman weights)
  193. /// using an FSE decoding table. `src_len` must be the exact length of the
  194. /// block.
  195. static size_t FSE_decompress_interleaved2(const FSE_dtable *const dtable,
  196. ostream_t *const out,
  197. istream_t *const in);
  198. /// Initialize a decoding table using normalized frequencies.
  199. static void FSE_init_dtable(FSE_dtable *const dtable,
  200. const i16 *const norm_freqs, const int num_symbs,
  201. const int accuracy_log);
  202. /// Decode an FSE header as defined in the Zstandard format specification and
  203. /// use the decoded frequencies to initialize a decoding table.
  204. static void FSE_decode_header(FSE_dtable *const dtable, istream_t *const in,
  205. const int max_accuracy_log);
  206. /// Initialize an FSE table that will always return the same symbol and consume
  207. /// 0 bits per symbol, to be used for RLE mode in sequence commands
  208. static void FSE_init_dtable_rle(FSE_dtable *const dtable, const u8 symb);
  209. /// Free the malloc'ed parts of a decoding table
  210. static void FSE_free_dtable(FSE_dtable *const dtable);
  211. /*** END FSE PRIMITIVES ***************/
  212. /******* END IMPLEMENTATION PRIMITIVE PROTOTYPES ******************************/
  213. /******* ZSTD HELPER STRUCTS AND PROTOTYPES ***********************************/
  214. /// A small structure that can be reused in various places that need to access
  215. /// frame header information
  216. typedef struct {
  217. // The size of window that we need to be able to contiguously store for
  218. // references
  219. size_t window_size;
  220. // The total output size of this compressed frame
  221. size_t frame_content_size;
  222. // The dictionary id if this frame uses one
  223. u32 dictionary_id;
  224. // Whether or not the content of this frame has a checksum
  225. int content_checksum_flag;
  226. // Whether or not the output for this frame is in a single segment
  227. int single_segment_flag;
  228. } frame_header_t;
  229. /// The context needed to decode blocks in a frame
  230. typedef struct {
  231. frame_header_t header;
  232. // The total amount of data available for backreferences, to determine if an
  233. // offset too large to be correct
  234. size_t current_total_output;
  235. const u8 *dict_content;
  236. size_t dict_content_len;
  237. // Entropy encoding tables so they can be repeated by future blocks instead
  238. // of retransmitting
  239. HUF_dtable literals_dtable;
  240. FSE_dtable ll_dtable;
  241. FSE_dtable ml_dtable;
  242. FSE_dtable of_dtable;
  243. // The last 3 offsets for the special "repeat offsets".
  244. u64 previous_offsets[3];
  245. } frame_context_t;
  246. /// The decoded contents of a dictionary so that it doesn't have to be repeated
  247. /// for each frame that uses it
  248. struct dictionary_s {
  249. // Entropy tables
  250. HUF_dtable literals_dtable;
  251. FSE_dtable ll_dtable;
  252. FSE_dtable ml_dtable;
  253. FSE_dtable of_dtable;
  254. // Raw content for backreferences
  255. u8 *content;
  256. size_t content_size;
  257. // Offset history to prepopulate the frame's history
  258. u64 previous_offsets[3];
  259. u32 dictionary_id;
  260. };
  261. /// A tuple containing the parts necessary to decode and execute a ZSTD sequence
  262. /// command
  263. typedef struct {
  264. u32 literal_length;
  265. u32 match_length;
  266. u32 offset;
  267. } sequence_command_t;
  268. /// The decoder works top-down, starting at the high level like Zstd frames, and
  269. /// working down to lower more technical levels such as blocks, literals, and
  270. /// sequences. The high-level functions roughly follow the outline of the
  271. /// format specification:
  272. /// https://github.com/facebook/zstd/blob/dev/doc/zstd_compression_format.md
  273. /// Before the implementation of each high-level function declared here, the
  274. /// prototypes for their helper functions are defined and explained
  275. /// Decode a single Zstd frame, or error if the input is not a valid frame.
  276. /// Accepts a dict argument, which may be NULL indicating no dictionary.
  277. /// See
  278. /// https://github.com/facebook/zstd/blob/dev/doc/zstd_compression_format.md#frame-concatenation
  279. static void decode_frame(ostream_t *const out, istream_t *const in,
  280. const dictionary_t *const dict);
  281. // Decode data in a compressed block
  282. static void decompress_block(frame_context_t *const ctx, ostream_t *const out,
  283. istream_t *const in);
  284. // Decode the literals section of a block
  285. static size_t decode_literals(frame_context_t *const ctx, istream_t *const in,
  286. u8 **const literals);
  287. // Decode the sequences part of a block
  288. static size_t decode_sequences(frame_context_t *const ctx, istream_t *const in,
  289. sequence_command_t **const sequences);
  290. // Execute the decoded sequences on the literals block
  291. static void execute_sequences(frame_context_t *const ctx, ostream_t *const out,
  292. const u8 *const literals,
  293. const size_t literals_len,
  294. const sequence_command_t *const sequences,
  295. const size_t num_sequences);
  296. // Copies literals and returns the total literal length that was copied
  297. static u32 copy_literals(const size_t seq, istream_t *litstream,
  298. ostream_t *const out);
  299. // Given an offset code from a sequence command (either an actual offset value
  300. // or an index for previous offset), computes the correct offset and updates
  301. // the offset history
  302. static size_t compute_offset(sequence_command_t seq, u64 *const offset_hist);
  303. // Given an offset, match length, and total output, as well as the frame
  304. // context for the dictionary, determines if the dictionary is used and
  305. // executes the copy operation
  306. static void execute_match_copy(frame_context_t *const ctx, size_t offset,
  307. size_t match_length, size_t total_output,
  308. ostream_t *const out);
  309. /******* END ZSTD HELPER STRUCTS AND PROTOTYPES *******************************/
  310. size_t ZSTD_decompress(void *const dst, const size_t dst_len,
  311. const void *const src, const size_t src_len) {
  312. dictionary_t* const uninit_dict = create_dictionary();
  313. size_t const decomp_size = ZSTD_decompress_with_dict(dst, dst_len, src,
  314. src_len, uninit_dict);
  315. free_dictionary(uninit_dict);
  316. return decomp_size;
  317. }
  318. size_t ZSTD_decompress_with_dict(void *const dst, const size_t dst_len,
  319. const void *const src, const size_t src_len,
  320. dictionary_t* parsed_dict) {
  321. istream_t in = IO_make_istream(src, src_len);
  322. ostream_t out = IO_make_ostream(dst, dst_len);
  323. // "A content compressed by Zstandard is transformed into a Zstandard frame.
  324. // Multiple frames can be appended into a single file or stream. A frame is
  325. // totally independent, has a defined beginning and end, and a set of
  326. // parameters which tells the decoder how to decompress it."
  327. /* this decoder assumes decompression of a single frame */
  328. decode_frame(&out, &in, parsed_dict);
  329. return (size_t)(out.ptr - (u8 *)dst);
  330. }
  331. /******* FRAME DECODING ******************************************************/
  332. static void decode_data_frame(ostream_t *const out, istream_t *const in,
  333. const dictionary_t *const dict);
  334. static void init_frame_context(frame_context_t *const context,
  335. istream_t *const in,
  336. const dictionary_t *const dict);
  337. static void free_frame_context(frame_context_t *const context);
  338. static void parse_frame_header(frame_header_t *const header,
  339. istream_t *const in);
  340. static void frame_context_apply_dict(frame_context_t *const ctx,
  341. const dictionary_t *const dict);
  342. static void decompress_data(frame_context_t *const ctx, ostream_t *const out,
  343. istream_t *const in);
  344. static void decode_frame(ostream_t *const out, istream_t *const in,
  345. const dictionary_t *const dict) {
  346. const u32 magic_number = (u32)IO_read_bits(in, 32);
  347. if (magic_number == ZSTD_MAGIC_NUMBER) {
  348. // ZSTD frame
  349. decode_data_frame(out, in, dict);
  350. return;
  351. }
  352. // not a real frame or a skippable frame
  353. ERROR("Tried to decode non-ZSTD frame");
  354. }
  355. /// Decode a frame that contains compressed data. Not all frames do as there
  356. /// are skippable frames.
  357. /// See
  358. /// https://github.com/facebook/zstd/blob/dev/doc/zstd_compression_format.md#general-structure-of-zstandard-frame-format
  359. static void decode_data_frame(ostream_t *const out, istream_t *const in,
  360. const dictionary_t *const dict) {
  361. frame_context_t ctx;
  362. // Initialize the context that needs to be carried from block to block
  363. init_frame_context(&ctx, in, dict);
  364. if (ctx.header.frame_content_size != 0 &&
  365. ctx.header.frame_content_size > out->len) {
  366. OUT_SIZE();
  367. }
  368. decompress_data(&ctx, out, in);
  369. free_frame_context(&ctx);
  370. }
  371. /// Takes the information provided in the header and dictionary, and initializes
  372. /// the context for this frame
  373. static void init_frame_context(frame_context_t *const context,
  374. istream_t *const in,
  375. const dictionary_t *const dict) {
  376. // Most fields in context are correct when initialized to 0
  377. memset(context, 0, sizeof(frame_context_t));
  378. // Parse data from the frame header
  379. parse_frame_header(&context->header, in);
  380. // Set up the offset history for the repeat offset commands
  381. context->previous_offsets[0] = 1;
  382. context->previous_offsets[1] = 4;
  383. context->previous_offsets[2] = 8;
  384. // Apply details from the dict if it exists
  385. frame_context_apply_dict(context, dict);
  386. }
  387. static void free_frame_context(frame_context_t *const context) {
  388. HUF_free_dtable(&context->literals_dtable);
  389. FSE_free_dtable(&context->ll_dtable);
  390. FSE_free_dtable(&context->ml_dtable);
  391. FSE_free_dtable(&context->of_dtable);
  392. memset(context, 0, sizeof(frame_context_t));
  393. }
  394. static void parse_frame_header(frame_header_t *const header,
  395. istream_t *const in) {
  396. // "The first header's byte is called the Frame_Header_Descriptor. It tells
  397. // which other fields are present. Decoding this byte is enough to tell the
  398. // size of Frame_Header.
  399. //
  400. // Bit number Field name
  401. // 7-6 Frame_Content_Size_flag
  402. // 5 Single_Segment_flag
  403. // 4 Unused_bit
  404. // 3 Reserved_bit
  405. // 2 Content_Checksum_flag
  406. // 1-0 Dictionary_ID_flag"
  407. const u8 descriptor = (u8)IO_read_bits(in, 8);
  408. // decode frame header descriptor into flags
  409. const u8 frame_content_size_flag = descriptor >> 6;
  410. const u8 single_segment_flag = (descriptor >> 5) & 1;
  411. const u8 reserved_bit = (descriptor >> 3) & 1;
  412. const u8 content_checksum_flag = (descriptor >> 2) & 1;
  413. const u8 dictionary_id_flag = descriptor & 3;
  414. if (reserved_bit != 0) {
  415. CORRUPTION();
  416. }
  417. header->single_segment_flag = single_segment_flag;
  418. header->content_checksum_flag = content_checksum_flag;
  419. // decode window size
  420. if (!single_segment_flag) {
  421. // "Provides guarantees on maximum back-reference distance that will be
  422. // used within compressed data. This information is important for
  423. // decoders to allocate enough memory.
  424. //
  425. // Bit numbers 7-3 2-0
  426. // Field name Exponent Mantissa"
  427. u8 window_descriptor = (u8)IO_read_bits(in, 8);
  428. u8 exponent = window_descriptor >> 3;
  429. u8 mantissa = window_descriptor & 7;
  430. // Use the algorithm from the specification to compute window size
  431. // https://github.com/facebook/zstd/blob/dev/doc/zstd_compression_format.md#window_descriptor
  432. size_t window_base = (size_t)1 << (10 + exponent);
  433. size_t window_add = (window_base / 8) * mantissa;
  434. header->window_size = window_base + window_add;
  435. }
  436. // decode dictionary id if it exists
  437. if (dictionary_id_flag) {
  438. // "This is a variable size field, which contains the ID of the
  439. // dictionary required to properly decode the frame. Note that this
  440. // field is optional. When it's not present, it's up to the caller to
  441. // make sure it uses the correct dictionary. Format is little-endian."
  442. const int bytes_array[] = {0, 1, 2, 4};
  443. const int bytes = bytes_array[dictionary_id_flag];
  444. header->dictionary_id = (u32)IO_read_bits(in, bytes * 8);
  445. } else {
  446. header->dictionary_id = 0;
  447. }
  448. // decode frame content size if it exists
  449. if (single_segment_flag || frame_content_size_flag) {
  450. // "This is the original (uncompressed) size. This information is
  451. // optional. The Field_Size is provided according to value of
  452. // Frame_Content_Size_flag. The Field_Size can be equal to 0 (not
  453. // present), 1, 2, 4 or 8 bytes. Format is little-endian."
  454. //
  455. // if frame_content_size_flag == 0 but single_segment_flag is set, we
  456. // still have a 1 byte field
  457. const int bytes_array[] = {1, 2, 4, 8};
  458. const int bytes = bytes_array[frame_content_size_flag];
  459. header->frame_content_size = IO_read_bits(in, bytes * 8);
  460. if (bytes == 2) {
  461. // "When Field_Size is 2, the offset of 256 is added."
  462. header->frame_content_size += 256;
  463. }
  464. } else {
  465. header->frame_content_size = 0;
  466. }
  467. if (single_segment_flag) {
  468. // "The Window_Descriptor byte is optional. It is absent when
  469. // Single_Segment_flag is set. In this case, the maximum back-reference
  470. // distance is the content size itself, which can be any value from 1 to
  471. // 2^64-1 bytes (16 EB)."
  472. header->window_size = header->frame_content_size;
  473. }
  474. }
  475. /// Decompress the data from a frame block by block
  476. static void decompress_data(frame_context_t *const ctx, ostream_t *const out,
  477. istream_t *const in) {
  478. // "A frame encapsulates one or multiple blocks. Each block can be
  479. // compressed or not, and has a guaranteed maximum content size, which
  480. // depends on frame parameters. Unlike frames, each block depends on
  481. // previous blocks for proper decoding. However, each block can be
  482. // decompressed without waiting for its successor, allowing streaming
  483. // operations."
  484. int last_block = 0;
  485. do {
  486. // "Last_Block
  487. //
  488. // The lowest bit signals if this block is the last one. Frame ends
  489. // right after this block.
  490. //
  491. // Block_Type and Block_Size
  492. //
  493. // The next 2 bits represent the Block_Type, while the remaining 21 bits
  494. // represent the Block_Size. Format is little-endian."
  495. last_block = (int)IO_read_bits(in, 1);
  496. const int block_type = (int)IO_read_bits(in, 2);
  497. const size_t block_len = IO_read_bits(in, 21);
  498. switch (block_type) {
  499. case 0: {
  500. // "Raw_Block - this is an uncompressed block. Block_Size is the
  501. // number of bytes to read and copy."
  502. const u8 *const read_ptr = IO_get_read_ptr(in, block_len);
  503. u8 *const write_ptr = IO_get_write_ptr(out, block_len);
  504. // Copy the raw data into the output
  505. memcpy(write_ptr, read_ptr, block_len);
  506. ctx->current_total_output += block_len;
  507. break;
  508. }
  509. case 1: {
  510. // "RLE_Block - this is a single byte, repeated N times. In which
  511. // case, Block_Size is the size to regenerate, while the
  512. // "compressed" block is just 1 byte (the byte to repeat)."
  513. const u8 *const read_ptr = IO_get_read_ptr(in, 1);
  514. u8 *const write_ptr = IO_get_write_ptr(out, block_len);
  515. // Copy `block_len` copies of `read_ptr[0]` to the output
  516. memset(write_ptr, read_ptr[0], block_len);
  517. ctx->current_total_output += block_len;
  518. break;
  519. }
  520. case 2: {
  521. // "Compressed_Block - this is a Zstandard compressed block,
  522. // detailed in another section of this specification. Block_Size is
  523. // the compressed size.
  524. // Create a sub-stream for the block
  525. istream_t block_stream = IO_make_sub_istream(in, block_len);
  526. decompress_block(ctx, out, &block_stream);
  527. break;
  528. }
  529. case 3:
  530. // "Reserved - this is not a block. This value cannot be used with
  531. // current version of this specification."
  532. CORRUPTION();
  533. break;
  534. default:
  535. IMPOSSIBLE();
  536. }
  537. } while (!last_block);
  538. if (ctx->header.content_checksum_flag) {
  539. // This program does not support checking the checksum, so skip over it
  540. // if it's present
  541. IO_advance_input(in, 4);
  542. }
  543. }
  544. /******* END FRAME DECODING ***************************************************/
  545. /******* BLOCK DECOMPRESSION **************************************************/
  546. static void decompress_block(frame_context_t *const ctx, ostream_t *const out,
  547. istream_t *const in) {
  548. // "A compressed block consists of 2 sections :
  549. //
  550. // Literals_Section
  551. // Sequences_Section"
  552. // Part 1: decode the literals block
  553. u8 *literals = NULL;
  554. const size_t literals_size = decode_literals(ctx, in, &literals);
  555. // Part 2: decode the sequences block
  556. sequence_command_t *sequences = NULL;
  557. const size_t num_sequences =
  558. decode_sequences(ctx, in, &sequences);
  559. // Part 3: combine literals and sequence commands to generate output
  560. execute_sequences(ctx, out, literals, literals_size, sequences,
  561. num_sequences);
  562. free(literals);
  563. free(sequences);
  564. }
  565. /******* END BLOCK DECOMPRESSION **********************************************/
  566. /******* LITERALS DECODING ****************************************************/
  567. static size_t decode_literals_simple(istream_t *const in, u8 **const literals,
  568. const int block_type,
  569. const int size_format);
  570. static size_t decode_literals_compressed(frame_context_t *const ctx,
  571. istream_t *const in,
  572. u8 **const literals,
  573. const int block_type,
  574. const int size_format);
  575. static void decode_huf_table(HUF_dtable *const dtable, istream_t *const in);
  576. static void fse_decode_hufweights(ostream_t *weights, istream_t *const in,
  577. int *const num_symbs);
  578. static size_t decode_literals(frame_context_t *const ctx, istream_t *const in,
  579. u8 **const literals) {
  580. // "Literals can be stored uncompressed or compressed using Huffman prefix
  581. // codes. When compressed, an optional tree description can be present,
  582. // followed by 1 or 4 streams."
  583. //
  584. // "Literals_Section_Header
  585. //
  586. // Header is in charge of describing how literals are packed. It's a
  587. // byte-aligned variable-size bitfield, ranging from 1 to 5 bytes, using
  588. // little-endian convention."
  589. //
  590. // "Literals_Block_Type
  591. //
  592. // This field uses 2 lowest bits of first byte, describing 4 different block
  593. // types"
  594. //
  595. // size_format takes between 1 and 2 bits
  596. int block_type = (int)IO_read_bits(in, 2);
  597. int size_format = (int)IO_read_bits(in, 2);
  598. if (block_type <= 1) {
  599. // Raw or RLE literals block
  600. return decode_literals_simple(in, literals, block_type,
  601. size_format);
  602. } else {
  603. // Huffman compressed literals
  604. return decode_literals_compressed(ctx, in, literals, block_type,
  605. size_format);
  606. }
  607. }
  608. /// Decodes literals blocks in raw or RLE form
  609. static size_t decode_literals_simple(istream_t *const in, u8 **const literals,
  610. const int block_type,
  611. const int size_format) {
  612. size_t size;
  613. switch (size_format) {
  614. // These cases are in the form ?0
  615. // In this case, the ? bit is actually part of the size field
  616. case 0:
  617. case 2:
  618. // "Size_Format uses 1 bit. Regenerated_Size uses 5 bits (0-31)."
  619. IO_rewind_bits(in, 1);
  620. size = IO_read_bits(in, 5);
  621. break;
  622. case 1:
  623. // "Size_Format uses 2 bits. Regenerated_Size uses 12 bits (0-4095)."
  624. size = IO_read_bits(in, 12);
  625. break;
  626. case 3:
  627. // "Size_Format uses 2 bits. Regenerated_Size uses 20 bits (0-1048575)."
  628. size = IO_read_bits(in, 20);
  629. break;
  630. default:
  631. // Size format is in range 0-3
  632. IMPOSSIBLE();
  633. }
  634. if (size > MAX_LITERALS_SIZE) {
  635. CORRUPTION();
  636. }
  637. *literals = malloc(size);
  638. if (!*literals) {
  639. BAD_ALLOC();
  640. }
  641. switch (block_type) {
  642. case 0: {
  643. // "Raw_Literals_Block - Literals are stored uncompressed."
  644. const u8 *const read_ptr = IO_get_read_ptr(in, size);
  645. memcpy(*literals, read_ptr, size);
  646. break;
  647. }
  648. case 1: {
  649. // "RLE_Literals_Block - Literals consist of a single byte value repeated N times."
  650. const u8 *const read_ptr = IO_get_read_ptr(in, 1);
  651. memset(*literals, read_ptr[0], size);
  652. break;
  653. }
  654. default:
  655. IMPOSSIBLE();
  656. }
  657. return size;
  658. }
  659. /// Decodes Huffman compressed literals
  660. static size_t decode_literals_compressed(frame_context_t *const ctx,
  661. istream_t *const in,
  662. u8 **const literals,
  663. const int block_type,
  664. const int size_format) {
  665. size_t regenerated_size, compressed_size;
  666. // Only size_format=0 has 1 stream, so default to 4
  667. int num_streams = 4;
  668. switch (size_format) {
  669. case 0:
  670. // "A single stream. Both Compressed_Size and Regenerated_Size use 10
  671. // bits (0-1023)."
  672. num_streams = 1;
  673. // Fall through as it has the same size format
  674. /* fallthrough */
  675. case 1:
  676. // "4 streams. Both Compressed_Size and Regenerated_Size use 10 bits
  677. // (0-1023)."
  678. regenerated_size = IO_read_bits(in, 10);
  679. compressed_size = IO_read_bits(in, 10);
  680. break;
  681. case 2:
  682. // "4 streams. Both Compressed_Size and Regenerated_Size use 14 bits
  683. // (0-16383)."
  684. regenerated_size = IO_read_bits(in, 14);
  685. compressed_size = IO_read_bits(in, 14);
  686. break;
  687. case 3:
  688. // "4 streams. Both Compressed_Size and Regenerated_Size use 18 bits
  689. // (0-262143)."
  690. regenerated_size = IO_read_bits(in, 18);
  691. compressed_size = IO_read_bits(in, 18);
  692. break;
  693. default:
  694. // Impossible
  695. IMPOSSIBLE();
  696. }
  697. if (regenerated_size > MAX_LITERALS_SIZE) {
  698. CORRUPTION();
  699. }
  700. *literals = malloc(regenerated_size);
  701. if (!*literals) {
  702. BAD_ALLOC();
  703. }
  704. ostream_t lit_stream = IO_make_ostream(*literals, regenerated_size);
  705. istream_t huf_stream = IO_make_sub_istream(in, compressed_size);
  706. if (block_type == 2) {
  707. // Decode the provided Huffman table
  708. // "This section is only present when Literals_Block_Type type is
  709. // Compressed_Literals_Block (2)."
  710. HUF_free_dtable(&ctx->literals_dtable);
  711. decode_huf_table(&ctx->literals_dtable, &huf_stream);
  712. } else {
  713. // If the previous Huffman table is being repeated, ensure it exists
  714. if (!ctx->literals_dtable.symbols) {
  715. CORRUPTION();
  716. }
  717. }
  718. size_t symbols_decoded;
  719. if (num_streams == 1) {
  720. symbols_decoded = HUF_decompress_1stream(&ctx->literals_dtable, &lit_stream, &huf_stream);
  721. } else {
  722. symbols_decoded = HUF_decompress_4stream(&ctx->literals_dtable, &lit_stream, &huf_stream);
  723. }
  724. if (symbols_decoded != regenerated_size) {
  725. CORRUPTION();
  726. }
  727. return regenerated_size;
  728. }
  729. // Decode the Huffman table description
  730. static void decode_huf_table(HUF_dtable *const dtable, istream_t *const in) {
  731. // "All literal values from zero (included) to last present one (excluded)
  732. // are represented by Weight with values from 0 to Max_Number_of_Bits."
  733. // "This is a single byte value (0-255), which describes how to decode the list of weights."
  734. const u8 header = IO_read_bits(in, 8);
  735. u8 weights[HUF_MAX_SYMBS];
  736. memset(weights, 0, sizeof(weights));
  737. int num_symbs;
  738. if (header >= 128) {
  739. // "This is a direct representation, where each Weight is written
  740. // directly as a 4 bits field (0-15). The full representation occupies
  741. // ((Number_of_Symbols+1)/2) bytes, meaning it uses a last full byte
  742. // even if Number_of_Symbols is odd. Number_of_Symbols = headerByte -
  743. // 127"
  744. num_symbs = header - 127;
  745. const size_t bytes = (num_symbs + 1) / 2;
  746. const u8 *const weight_src = IO_get_read_ptr(in, bytes);
  747. for (int i = 0; i < num_symbs; i++) {
  748. // "They are encoded forward, 2
  749. // weights to a byte with the first weight taking the top four bits
  750. // and the second taking the bottom four (e.g. the following
  751. // operations could be used to read the weights: Weight[0] =
  752. // (Byte[0] >> 4), Weight[1] = (Byte[0] & 0xf), etc.)."
  753. if (i % 2 == 0) {
  754. weights[i] = weight_src[i / 2] >> 4;
  755. } else {
  756. weights[i] = weight_src[i / 2] & 0xf;
  757. }
  758. }
  759. } else {
  760. // The weights are FSE encoded, decode them before we can construct the
  761. // table
  762. istream_t fse_stream = IO_make_sub_istream(in, header);
  763. ostream_t weight_stream = IO_make_ostream(weights, HUF_MAX_SYMBS);
  764. fse_decode_hufweights(&weight_stream, &fse_stream, &num_symbs);
  765. }
  766. // Construct the table using the decoded weights
  767. HUF_init_dtable_usingweights(dtable, weights, num_symbs);
  768. }
  769. static void fse_decode_hufweights(ostream_t *weights, istream_t *const in,
  770. int *const num_symbs) {
  771. const int MAX_ACCURACY_LOG = 7;
  772. FSE_dtable dtable;
  773. // "An FSE bitstream starts by a header, describing probabilities
  774. // distribution. It will create a Decoding Table. For a list of Huffman
  775. // weights, maximum accuracy is 7 bits."
  776. FSE_decode_header(&dtable, in, MAX_ACCURACY_LOG);
  777. // Decode the weights
  778. *num_symbs = FSE_decompress_interleaved2(&dtable, weights, in);
  779. FSE_free_dtable(&dtable);
  780. }
  781. /******* END LITERALS DECODING ************************************************/
  782. /******* SEQUENCE DECODING ****************************************************/
  783. /// The combination of FSE states needed to decode sequences
  784. typedef struct {
  785. FSE_dtable ll_table;
  786. FSE_dtable of_table;
  787. FSE_dtable ml_table;
  788. u16 ll_state;
  789. u16 of_state;
  790. u16 ml_state;
  791. } sequence_states_t;
  792. /// Different modes to signal to decode_seq_tables what to do
  793. typedef enum {
  794. seq_literal_length = 0,
  795. seq_offset = 1,
  796. seq_match_length = 2,
  797. } seq_part_t;
  798. typedef enum {
  799. seq_predefined = 0,
  800. seq_rle = 1,
  801. seq_fse = 2,
  802. seq_repeat = 3,
  803. } seq_mode_t;
  804. /// The predefined FSE distribution tables for `seq_predefined` mode
  805. static const i16 SEQ_LITERAL_LENGTH_DEFAULT_DIST[36] = {
  806. 4, 3, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 1, 1, 2, 2,
  807. 2, 2, 2, 2, 2, 2, 2, 3, 2, 1, 1, 1, 1, 1, -1, -1, -1, -1};
  808. static const i16 SEQ_OFFSET_DEFAULT_DIST[29] = {
  809. 1, 1, 1, 1, 1, 1, 2, 2, 2, 1, 1, 1, 1, 1, 1,
  810. 1, 1, 1, 1, 1, 1, 1, 1, 1, -1, -1, -1, -1, -1};
  811. static const i16 SEQ_MATCH_LENGTH_DEFAULT_DIST[53] = {
  812. 1, 4, 3, 2, 2, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  813. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  814. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, -1, -1, -1, -1, -1, -1, -1};
  815. /// The sequence decoding baseline and number of additional bits to read/add
  816. /// https://github.com/facebook/zstd/blob/dev/doc/zstd_compression_format.md#the-codes-for-literals-lengths-match-lengths-and-offsets
  817. static const u32 SEQ_LITERAL_LENGTH_BASELINES[36] = {
  818. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11,
  819. 12, 13, 14, 15, 16, 18, 20, 22, 24, 28, 32, 40,
  820. 48, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768, 65536};
  821. static const u8 SEQ_LITERAL_LENGTH_EXTRA_BITS[36] = {
  822. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1,
  823. 1, 1, 2, 2, 3, 3, 4, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16};
  824. static const u32 SEQ_MATCH_LENGTH_BASELINES[53] = {
  825. 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
  826. 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30,
  827. 31, 32, 33, 34, 35, 37, 39, 41, 43, 47, 51, 59, 67, 83,
  828. 99, 131, 259, 515, 1027, 2051, 4099, 8195, 16387, 32771, 65539};
  829. static const u8 SEQ_MATCH_LENGTH_EXTRA_BITS[53] = {
  830. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  831. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1,
  832. 2, 2, 3, 3, 4, 4, 5, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16};
  833. /// Offset decoding is simpler so we just need a maximum code value
  834. static const u8 SEQ_MAX_CODES[3] = {35, (u8)-1, 52};
  835. static void decompress_sequences(frame_context_t *const ctx,
  836. istream_t *const in,
  837. sequence_command_t *const sequences,
  838. const size_t num_sequences);
  839. static sequence_command_t decode_sequence(sequence_states_t *const state,
  840. const u8 *const src,
  841. i64 *const offset);
  842. static void decode_seq_table(FSE_dtable *const table, istream_t *const in,
  843. const seq_part_t type, const seq_mode_t mode);
  844. static size_t decode_sequences(frame_context_t *const ctx, istream_t *in,
  845. sequence_command_t **const sequences) {
  846. // "A compressed block is a succession of sequences . A sequence is a
  847. // literal copy command, followed by a match copy command. A literal copy
  848. // command specifies a length. It is the number of bytes to be copied (or
  849. // extracted) from the literal section. A match copy command specifies an
  850. // offset and a length. The offset gives the position to copy from, which
  851. // can be within a previous block."
  852. size_t num_sequences;
  853. // "Number_of_Sequences
  854. //
  855. // This is a variable size field using between 1 and 3 bytes. Let's call its
  856. // first byte byte0."
  857. u8 header = IO_read_bits(in, 8);
  858. if (header == 0) {
  859. // "There are no sequences. The sequence section stops there.
  860. // Regenerated content is defined entirely by literals section."
  861. *sequences = NULL;
  862. return 0;
  863. } else if (header < 128) {
  864. // "Number_of_Sequences = byte0 . Uses 1 byte."
  865. num_sequences = header;
  866. } else if (header < 255) {
  867. // "Number_of_Sequences = ((byte0-128) << 8) + byte1 . Uses 2 bytes."
  868. num_sequences = ((header - 128) << 8) + IO_read_bits(in, 8);
  869. } else {
  870. // "Number_of_Sequences = byte1 + (byte2<<8) + 0x7F00 . Uses 3 bytes."
  871. num_sequences = IO_read_bits(in, 16) + 0x7F00;
  872. }
  873. *sequences = malloc(num_sequences * sizeof(sequence_command_t));
  874. if (!*sequences) {
  875. BAD_ALLOC();
  876. }
  877. decompress_sequences(ctx, in, *sequences, num_sequences);
  878. return num_sequences;
  879. }
  880. /// Decompress the FSE encoded sequence commands
  881. static void decompress_sequences(frame_context_t *const ctx, istream_t *in,
  882. sequence_command_t *const sequences,
  883. const size_t num_sequences) {
  884. // "The Sequences_Section regroup all symbols required to decode commands.
  885. // There are 3 symbol types : literals lengths, offsets and match lengths.
  886. // They are encoded together, interleaved, in a single bitstream."
  887. // "Symbol compression modes
  888. //
  889. // This is a single byte, defining the compression mode of each symbol
  890. // type."
  891. //
  892. // Bit number : Field name
  893. // 7-6 : Literals_Lengths_Mode
  894. // 5-4 : Offsets_Mode
  895. // 3-2 : Match_Lengths_Mode
  896. // 1-0 : Reserved
  897. u8 compression_modes = IO_read_bits(in, 8);
  898. if ((compression_modes & 3) != 0) {
  899. // Reserved bits set
  900. CORRUPTION();
  901. }
  902. // "Following the header, up to 3 distribution tables can be described. When
  903. // present, they are in this order :
  904. //
  905. // Literals lengths
  906. // Offsets
  907. // Match Lengths"
  908. // Update the tables we have stored in the context
  909. decode_seq_table(&ctx->ll_dtable, in, seq_literal_length,
  910. (compression_modes >> 6) & 3);
  911. decode_seq_table(&ctx->of_dtable, in, seq_offset,
  912. (compression_modes >> 4) & 3);
  913. decode_seq_table(&ctx->ml_dtable, in, seq_match_length,
  914. (compression_modes >> 2) & 3);
  915. sequence_states_t states;
  916. // Initialize the decoding tables
  917. {
  918. states.ll_table = ctx->ll_dtable;
  919. states.of_table = ctx->of_dtable;
  920. states.ml_table = ctx->ml_dtable;
  921. }
  922. const size_t len = IO_istream_len(in);
  923. const u8 *const src = IO_get_read_ptr(in, len);
  924. // "After writing the last bit containing information, the compressor writes
  925. // a single 1-bit and then fills the byte with 0-7 0 bits of padding."
  926. const int padding = 8 - highest_set_bit(src[len - 1]);
  927. // The offset starts at the end because FSE streams are read backwards
  928. i64 bit_offset = (i64)(len * 8 - (size_t)padding);
  929. // "The bitstream starts with initial state values, each using the required
  930. // number of bits in their respective accuracy, decoded previously from
  931. // their normalized distribution.
  932. //
  933. // It starts by Literals_Length_State, followed by Offset_State, and finally
  934. // Match_Length_State."
  935. FSE_init_state(&states.ll_table, &states.ll_state, src, &bit_offset);
  936. FSE_init_state(&states.of_table, &states.of_state, src, &bit_offset);
  937. FSE_init_state(&states.ml_table, &states.ml_state, src, &bit_offset);
  938. for (size_t i = 0; i < num_sequences; i++) {
  939. // Decode sequences one by one
  940. sequences[i] = decode_sequence(&states, src, &bit_offset);
  941. }
  942. if (bit_offset != 0) {
  943. CORRUPTION();
  944. }
  945. }
  946. // Decode a single sequence and update the state
  947. static sequence_command_t decode_sequence(sequence_states_t *const states,
  948. const u8 *const src,
  949. i64 *const offset) {
  950. // "Each symbol is a code in its own context, which specifies Baseline and
  951. // Number_of_Bits to add. Codes are FSE compressed, and interleaved with raw
  952. // additional bits in the same bitstream."
  953. // Decode symbols, but don't update states
  954. const u8 of_code = FSE_peek_symbol(&states->of_table, states->of_state);
  955. const u8 ll_code = FSE_peek_symbol(&states->ll_table, states->ll_state);
  956. const u8 ml_code = FSE_peek_symbol(&states->ml_table, states->ml_state);
  957. // Offset doesn't need a max value as it's not decoded using a table
  958. if (ll_code > SEQ_MAX_CODES[seq_literal_length] ||
  959. ml_code > SEQ_MAX_CODES[seq_match_length]) {
  960. CORRUPTION();
  961. }
  962. // Read the interleaved bits
  963. sequence_command_t seq;
  964. // "Decoding starts by reading the Number_of_Bits required to decode Offset.
  965. // It then does the same for Match_Length, and then for Literals_Length."
  966. seq.offset = ((u32)1 << of_code) + STREAM_read_bits(src, of_code, offset);
  967. seq.match_length =
  968. SEQ_MATCH_LENGTH_BASELINES[ml_code] +
  969. STREAM_read_bits(src, SEQ_MATCH_LENGTH_EXTRA_BITS[ml_code], offset);
  970. seq.literal_length =
  971. SEQ_LITERAL_LENGTH_BASELINES[ll_code] +
  972. STREAM_read_bits(src, SEQ_LITERAL_LENGTH_EXTRA_BITS[ll_code], offset);
  973. // "If it is not the last sequence in the block, the next operation is to
  974. // update states. Using the rules pre-calculated in the decoding tables,
  975. // Literals_Length_State is updated, followed by Match_Length_State, and
  976. // then Offset_State."
  977. // If the stream is complete don't read bits to update state
  978. if (*offset != 0) {
  979. FSE_update_state(&states->ll_table, &states->ll_state, src, offset);
  980. FSE_update_state(&states->ml_table, &states->ml_state, src, offset);
  981. FSE_update_state(&states->of_table, &states->of_state, src, offset);
  982. }
  983. return seq;
  984. }
  985. /// Given a sequence part and table mode, decode the FSE distribution
  986. /// Errors if the mode is `seq_repeat` without a pre-existing table in `table`
  987. static void decode_seq_table(FSE_dtable *const table, istream_t *const in,
  988. const seq_part_t type, const seq_mode_t mode) {
  989. // Constant arrays indexed by seq_part_t
  990. const i16 *const default_distributions[] = {SEQ_LITERAL_LENGTH_DEFAULT_DIST,
  991. SEQ_OFFSET_DEFAULT_DIST,
  992. SEQ_MATCH_LENGTH_DEFAULT_DIST};
  993. const size_t default_distribution_lengths[] = {36, 29, 53};
  994. const size_t default_distribution_accuracies[] = {6, 5, 6};
  995. const size_t max_accuracies[] = {9, 8, 9};
  996. if (mode != seq_repeat) {
  997. // Free old one before overwriting
  998. FSE_free_dtable(table);
  999. }
  1000. switch (mode) {
  1001. case seq_predefined: {
  1002. // "Predefined_Mode : uses a predefined distribution table."
  1003. const i16 *distribution = default_distributions[type];
  1004. const size_t symbs = default_distribution_lengths[type];
  1005. const size_t accuracy_log = default_distribution_accuracies[type];
  1006. FSE_init_dtable(table, distribution, symbs, accuracy_log);
  1007. break;
  1008. }
  1009. case seq_rle: {
  1010. // "RLE_Mode : it's a single code, repeated Number_of_Sequences times."
  1011. const u8 symb = IO_get_read_ptr(in, 1)[0];
  1012. FSE_init_dtable_rle(table, symb);
  1013. break;
  1014. }
  1015. case seq_fse: {
  1016. // "FSE_Compressed_Mode : standard FSE compression. A distribution table
  1017. // will be present "
  1018. FSE_decode_header(table, in, max_accuracies[type]);
  1019. break;
  1020. }
  1021. case seq_repeat:
  1022. // "Repeat_Mode : re-use distribution table from previous compressed
  1023. // block."
  1024. // Nothing to do here, table will be unchanged
  1025. if (!table->symbols) {
  1026. // This mode is invalid if we don't already have a table
  1027. CORRUPTION();
  1028. }
  1029. break;
  1030. default:
  1031. // Impossible, as mode is from 0-3
  1032. IMPOSSIBLE();
  1033. break;
  1034. }
  1035. }
  1036. /******* END SEQUENCE DECODING ************************************************/
  1037. /******* SEQUENCE EXECUTION ***************************************************/
  1038. static void execute_sequences(frame_context_t *const ctx, ostream_t *const out,
  1039. const u8 *const literals,
  1040. const size_t literals_len,
  1041. const sequence_command_t *const sequences,
  1042. const size_t num_sequences) {
  1043. istream_t litstream = IO_make_istream(literals, literals_len);
  1044. u64 *const offset_hist = ctx->previous_offsets;
  1045. size_t total_output = ctx->current_total_output;
  1046. for (size_t i = 0; i < num_sequences; i++) {
  1047. const sequence_command_t seq = sequences[i];
  1048. {
  1049. const u32 literals_size = copy_literals(seq.literal_length, &litstream, out);
  1050. total_output += literals_size;
  1051. }
  1052. size_t const offset = compute_offset(seq, offset_hist);
  1053. size_t const match_length = seq.match_length;
  1054. execute_match_copy(ctx, offset, match_length, total_output, out);
  1055. total_output += match_length;
  1056. }
  1057. // Copy any leftover literals
  1058. {
  1059. size_t len = IO_istream_len(&litstream);
  1060. copy_literals(len, &litstream, out);
  1061. total_output += len;
  1062. }
  1063. ctx->current_total_output = total_output;
  1064. }
  1065. static u32 copy_literals(const size_t literal_length, istream_t *litstream,
  1066. ostream_t *const out) {
  1067. // If the sequence asks for more literals than are left, the
  1068. // sequence must be corrupted
  1069. if (literal_length > IO_istream_len(litstream)) {
  1070. CORRUPTION();
  1071. }
  1072. u8 *const write_ptr = IO_get_write_ptr(out, literal_length);
  1073. const u8 *const read_ptr =
  1074. IO_get_read_ptr(litstream, literal_length);
  1075. // Copy literals to output
  1076. memcpy(write_ptr, read_ptr, literal_length);
  1077. return literal_length;
  1078. }
  1079. static size_t compute_offset(sequence_command_t seq, u64 *const offset_hist) {
  1080. size_t offset;
  1081. // Offsets are special, we need to handle the repeat offsets
  1082. if (seq.offset <= 3) {
  1083. // "The first 3 values define a repeated offset and we will call
  1084. // them Repeated_Offset1, Repeated_Offset2, and Repeated_Offset3.
  1085. // They are sorted in recency order, with Repeated_Offset1 meaning
  1086. // 'most recent one'".
  1087. // Use 0 indexing for the array
  1088. u32 idx = seq.offset - 1;
  1089. if (seq.literal_length == 0) {
  1090. // "There is an exception though, when current sequence's
  1091. // literals length is 0. In this case, repeated offsets are
  1092. // shifted by one, so Repeated_Offset1 becomes Repeated_Offset2,
  1093. // Repeated_Offset2 becomes Repeated_Offset3, and
  1094. // Repeated_Offset3 becomes Repeated_Offset1 - 1_byte."
  1095. idx++;
  1096. }
  1097. if (idx == 0) {
  1098. offset = offset_hist[0];
  1099. } else {
  1100. // If idx == 3 then literal length was 0 and the offset was 3,
  1101. // as per the exception listed above
  1102. offset = idx < 3 ? offset_hist[idx] : offset_hist[0] - 1;
  1103. // If idx == 1 we don't need to modify offset_hist[2], since
  1104. // we're using the second-most recent code
  1105. if (idx > 1) {
  1106. offset_hist[2] = offset_hist[1];
  1107. }
  1108. offset_hist[1] = offset_hist[0];
  1109. offset_hist[0] = offset;
  1110. }
  1111. } else {
  1112. // When it's not a repeat offset:
  1113. // "if (Offset_Value > 3) offset = Offset_Value - 3;"
  1114. offset = seq.offset - 3;
  1115. // Shift back history
  1116. offset_hist[2] = offset_hist[1];
  1117. offset_hist[1] = offset_hist[0];
  1118. offset_hist[0] = offset;
  1119. }
  1120. return offset;
  1121. }
  1122. static void execute_match_copy(frame_context_t *const ctx, size_t offset,
  1123. size_t match_length, size_t total_output,
  1124. ostream_t *const out) {
  1125. u8 *write_ptr = IO_get_write_ptr(out, match_length);
  1126. if (total_output <= ctx->header.window_size) {
  1127. // In this case offset might go back into the dictionary
  1128. if (offset > total_output + ctx->dict_content_len) {
  1129. // The offset goes beyond even the dictionary
  1130. CORRUPTION();
  1131. }
  1132. if (offset > total_output) {
  1133. // "The rest of the dictionary is its content. The content act
  1134. // as a "past" in front of data to compress or decompress, so it
  1135. // can be referenced in sequence commands."
  1136. const size_t dict_copy =
  1137. MIN(offset - total_output, match_length);
  1138. const size_t dict_offset =
  1139. ctx->dict_content_len - (offset - total_output);
  1140. memcpy(write_ptr, ctx->dict_content + dict_offset, dict_copy);
  1141. write_ptr += dict_copy;
  1142. match_length -= dict_copy;
  1143. }
  1144. } else if (offset > ctx->header.window_size) {
  1145. CORRUPTION();
  1146. }
  1147. // We must copy byte by byte because the match length might be larger
  1148. // than the offset
  1149. // ex: if the output so far was "abc", a command with offset=3 and
  1150. // match_length=6 would produce "abcabcabc" as the new output
  1151. for (size_t j = 0; j < match_length; j++) {
  1152. *write_ptr = *(write_ptr - offset);
  1153. write_ptr++;
  1154. }
  1155. }
  1156. /******* END SEQUENCE EXECUTION ***********************************************/
  1157. /******* OUTPUT SIZE COUNTING *************************************************/
  1158. /// Get the decompressed size of an input stream so memory can be allocated in
  1159. /// advance.
  1160. /// This implementation assumes `src` points to a single ZSTD-compressed frame
  1161. size_t ZSTD_get_decompressed_size(const void *src, const size_t src_len) {
  1162. istream_t in = IO_make_istream(src, src_len);
  1163. // get decompressed size from ZSTD frame header
  1164. {
  1165. const u32 magic_number = (u32)IO_read_bits(&in, 32);
  1166. if (magic_number == ZSTD_MAGIC_NUMBER) {
  1167. // ZSTD frame
  1168. frame_header_t header;
  1169. parse_frame_header(&header, &in);
  1170. if (header.frame_content_size == 0 && !header.single_segment_flag) {
  1171. // Content size not provided, we can't tell
  1172. return (size_t)-1;
  1173. }
  1174. return header.frame_content_size;
  1175. } else {
  1176. // not a real frame or skippable frame
  1177. ERROR("ZSTD frame magic number did not match");
  1178. }
  1179. }
  1180. }
  1181. /******* END OUTPUT SIZE COUNTING *********************************************/
  1182. /******* DICTIONARY PARSING ***************************************************/
  1183. dictionary_t* create_dictionary() {
  1184. dictionary_t* const dict = calloc(1, sizeof(dictionary_t));
  1185. if (!dict) {
  1186. BAD_ALLOC();
  1187. }
  1188. return dict;
  1189. }
  1190. /// Free an allocated dictionary
  1191. void free_dictionary(dictionary_t *const dict) {
  1192. HUF_free_dtable(&dict->literals_dtable);
  1193. FSE_free_dtable(&dict->ll_dtable);
  1194. FSE_free_dtable(&dict->of_dtable);
  1195. FSE_free_dtable(&dict->ml_dtable);
  1196. free(dict->content);
  1197. memset(dict, 0, sizeof(dictionary_t));
  1198. free(dict);
  1199. }
  1200. #if !defined(ZDEC_NO_DICTIONARY)
  1201. #define DICT_SIZE_ERROR() ERROR("Dictionary size cannot be less than 8 bytes")
  1202. #define NULL_SRC() ERROR("Tried to create dictionary with pointer to null src");
  1203. static void init_dictionary_content(dictionary_t *const dict,
  1204. istream_t *const in);
  1205. void parse_dictionary(dictionary_t *const dict, const void *src,
  1206. size_t src_len) {
  1207. const u8 *byte_src = (const u8 *)src;
  1208. memset(dict, 0, sizeof(dictionary_t));
  1209. if (src == NULL) { /* cannot initialize dictionary with null src */
  1210. NULL_SRC();
  1211. }
  1212. if (src_len < 8) {
  1213. DICT_SIZE_ERROR();
  1214. }
  1215. istream_t in = IO_make_istream(byte_src, src_len);
  1216. const u32 magic_number = IO_read_bits(&in, 32);
  1217. if (magic_number != 0xEC30A437) {
  1218. // raw content dict
  1219. IO_rewind_bits(&in, 32);
  1220. init_dictionary_content(dict, &in);
  1221. return;
  1222. }
  1223. dict->dictionary_id = IO_read_bits(&in, 32);
  1224. // "Entropy_Tables : following the same format as the tables in compressed
  1225. // blocks. They are stored in following order : Huffman tables for literals,
  1226. // FSE table for offsets, FSE table for match lengths, and FSE table for
  1227. // literals lengths. It's finally followed by 3 offset values, populating
  1228. // recent offsets (instead of using {1,4,8}), stored in order, 4-bytes
  1229. // little-endian each, for a total of 12 bytes. Each recent offset must have
  1230. // a value < dictionary size."
  1231. decode_huf_table(&dict->literals_dtable, &in);
  1232. decode_seq_table(&dict->of_dtable, &in, seq_offset, seq_fse);
  1233. decode_seq_table(&dict->ml_dtable, &in, seq_match_length, seq_fse);
  1234. decode_seq_table(&dict->ll_dtable, &in, seq_literal_length, seq_fse);
  1235. // Read in the previous offset history
  1236. dict->previous_offsets[0] = IO_read_bits(&in, 32);
  1237. dict->previous_offsets[1] = IO_read_bits(&in, 32);
  1238. dict->previous_offsets[2] = IO_read_bits(&in, 32);
  1239. // Ensure the provided offsets aren't too large
  1240. // "Each recent offset must have a value < dictionary size."
  1241. for (int i = 0; i < 3; i++) {
  1242. if (dict->previous_offsets[i] > src_len) {
  1243. ERROR("Dictionary corrupted");
  1244. }
  1245. }
  1246. // "Content : The rest of the dictionary is its content. The content act as
  1247. // a "past" in front of data to compress or decompress, so it can be
  1248. // referenced in sequence commands."
  1249. init_dictionary_content(dict, &in);
  1250. }
  1251. static void init_dictionary_content(dictionary_t *const dict,
  1252. istream_t *const in) {
  1253. // Copy in the content
  1254. dict->content_size = IO_istream_len(in);
  1255. dict->content = malloc(dict->content_size);
  1256. if (!dict->content) {
  1257. BAD_ALLOC();
  1258. }
  1259. const u8 *const content = IO_get_read_ptr(in, dict->content_size);
  1260. memcpy(dict->content, content, dict->content_size);
  1261. }
  1262. static void HUF_copy_dtable(HUF_dtable *const dst,
  1263. const HUF_dtable *const src) {
  1264. if (src->max_bits == 0) {
  1265. memset(dst, 0, sizeof(HUF_dtable));
  1266. return;
  1267. }
  1268. const size_t size = (size_t)1 << src->max_bits;
  1269. dst->max_bits = src->max_bits;
  1270. dst->symbols = malloc(size);
  1271. dst->num_bits = malloc(size);
  1272. if (!dst->symbols || !dst->num_bits) {
  1273. BAD_ALLOC();
  1274. }
  1275. memcpy(dst->symbols, src->symbols, size);
  1276. memcpy(dst->num_bits, src->num_bits, size);
  1277. }
  1278. static void FSE_copy_dtable(FSE_dtable *const dst, const FSE_dtable *const src) {
  1279. if (src->accuracy_log == 0) {
  1280. memset(dst, 0, sizeof(FSE_dtable));
  1281. return;
  1282. }
  1283. size_t size = (size_t)1 << src->accuracy_log;
  1284. dst->accuracy_log = src->accuracy_log;
  1285. dst->symbols = malloc(size);
  1286. dst->num_bits = malloc(size);
  1287. dst->new_state_base = malloc(size * sizeof(u16));
  1288. if (!dst->symbols || !dst->num_bits || !dst->new_state_base) {
  1289. BAD_ALLOC();
  1290. }
  1291. memcpy(dst->symbols, src->symbols, size);
  1292. memcpy(dst->num_bits, src->num_bits, size);
  1293. memcpy(dst->new_state_base, src->new_state_base, size * sizeof(u16));
  1294. }
  1295. /// A dictionary acts as initializing values for the frame context before
  1296. /// decompression, so we implement it by applying it's predetermined
  1297. /// tables and content to the context before beginning decompression
  1298. static void frame_context_apply_dict(frame_context_t *const ctx,
  1299. const dictionary_t *const dict) {
  1300. // If the content pointer is NULL then it must be an empty dict
  1301. if (!dict || !dict->content)
  1302. return;
  1303. // If the requested dictionary_id is non-zero, the correct dictionary must
  1304. // be present
  1305. if (ctx->header.dictionary_id != 0 &&
  1306. ctx->header.dictionary_id != dict->dictionary_id) {
  1307. ERROR("Wrong dictionary provided");
  1308. }
  1309. // Copy the dict content to the context for references during sequence
  1310. // execution
  1311. ctx->dict_content = dict->content;
  1312. ctx->dict_content_len = dict->content_size;
  1313. // If it's a formatted dict copy the precomputed tables in so they can
  1314. // be used in the table repeat modes
  1315. if (dict->dictionary_id != 0) {
  1316. // Deep copy the entropy tables so they can be freed independently of
  1317. // the dictionary struct
  1318. HUF_copy_dtable(&ctx->literals_dtable, &dict->literals_dtable);
  1319. FSE_copy_dtable(&ctx->ll_dtable, &dict->ll_dtable);
  1320. FSE_copy_dtable(&ctx->of_dtable, &dict->of_dtable);
  1321. FSE_copy_dtable(&ctx->ml_dtable, &dict->ml_dtable);
  1322. // Copy the repeated offsets
  1323. memcpy(ctx->previous_offsets, dict->previous_offsets,
  1324. sizeof(ctx->previous_offsets));
  1325. }
  1326. }
  1327. #else // ZDEC_NO_DICTIONARY is defined
  1328. static void frame_context_apply_dict(frame_context_t *const ctx,
  1329. const dictionary_t *const dict) {
  1330. (void)ctx;
  1331. if (dict && dict->content) ERROR("dictionary not supported");
  1332. }
  1333. #endif
  1334. /******* END DICTIONARY PARSING ***********************************************/
  1335. /******* IO STREAM OPERATIONS *************************************************/
  1336. /// Reads `num` bits from a bitstream, and updates the internal offset
  1337. static inline u64 IO_read_bits(istream_t *const in, const int num_bits) {
  1338. if (num_bits > 64 || num_bits <= 0) {
  1339. ERROR("Attempt to read an invalid number of bits");
  1340. }
  1341. const size_t bytes = (num_bits + in->bit_offset + 7) / 8;
  1342. const size_t full_bytes = (num_bits + in->bit_offset) / 8;
  1343. if (bytes > in->len) {
  1344. INP_SIZE();
  1345. }
  1346. const u64 result = read_bits_LE(in->ptr, num_bits, in->bit_offset);
  1347. in->bit_offset = (num_bits + in->bit_offset) % 8;
  1348. in->ptr += full_bytes;
  1349. in->len -= full_bytes;
  1350. return result;
  1351. }
  1352. /// If a non-zero number of bits have been read from the current byte, advance
  1353. /// the offset to the next byte
  1354. static inline void IO_rewind_bits(istream_t *const in, int num_bits) {
  1355. if (num_bits < 0) {
  1356. ERROR("Attempting to rewind stream by a negative number of bits");
  1357. }
  1358. // move the offset back by `num_bits` bits
  1359. const int new_offset = in->bit_offset - num_bits;
  1360. // determine the number of whole bytes we have to rewind, rounding up to an
  1361. // integer number (e.g. if `new_offset == -5`, `bytes == 1`)
  1362. const i64 bytes = -(new_offset - 7) / 8;
  1363. in->ptr -= bytes;
  1364. in->len += bytes;
  1365. // make sure the resulting `bit_offset` is positive, as mod in C does not
  1366. // convert numbers from negative to positive (e.g. -22 % 8 == -6)
  1367. in->bit_offset = ((new_offset % 8) + 8) % 8;
  1368. }
  1369. /// If the remaining bits in a byte will be unused, advance to the end of the
  1370. /// byte
  1371. static inline void IO_align_stream(istream_t *const in) {
  1372. if (in->bit_offset != 0) {
  1373. if (in->len == 0) {
  1374. INP_SIZE();
  1375. }
  1376. in->ptr++;
  1377. in->len--;
  1378. in->bit_offset = 0;
  1379. }
  1380. }
  1381. /// Write the given byte into the output stream
  1382. static inline void IO_write_byte(ostream_t *const out, u8 symb) {
  1383. if (out->len == 0) {
  1384. OUT_SIZE();
  1385. }
  1386. out->ptr[0] = symb;
  1387. out->ptr++;
  1388. out->len--;
  1389. }
  1390. /// Returns the number of bytes left to be read in this stream. The stream must
  1391. /// be byte aligned.
  1392. static inline size_t IO_istream_len(const istream_t *const in) {
  1393. return in->len;
  1394. }
  1395. /// Returns a pointer where `len` bytes can be read, and advances the internal
  1396. /// state. The stream must be byte aligned.
  1397. static inline const u8 *IO_get_read_ptr(istream_t *const in, size_t len) {
  1398. if (len > in->len) {
  1399. INP_SIZE();
  1400. }
  1401. if (in->bit_offset != 0) {
  1402. ERROR("Attempting to operate on a non-byte aligned stream");
  1403. }
  1404. const u8 *const ptr = in->ptr;
  1405. in->ptr += len;
  1406. in->len -= len;
  1407. return ptr;
  1408. }
  1409. /// Returns a pointer to write `len` bytes to, and advances the internal state
  1410. static inline u8 *IO_get_write_ptr(ostream_t *const out, size_t len) {
  1411. if (len > out->len) {
  1412. OUT_SIZE();
  1413. }
  1414. u8 *const ptr = out->ptr;
  1415. out->ptr += len;
  1416. out->len -= len;
  1417. return ptr;
  1418. }
  1419. /// Advance the inner state by `len` bytes
  1420. static inline void IO_advance_input(istream_t *const in, size_t len) {
  1421. if (len > in->len) {
  1422. INP_SIZE();
  1423. }
  1424. if (in->bit_offset != 0) {
  1425. ERROR("Attempting to operate on a non-byte aligned stream");
  1426. }
  1427. in->ptr += len;
  1428. in->len -= len;
  1429. }
  1430. /// Returns an `ostream_t` constructed from the given pointer and length
  1431. static inline ostream_t IO_make_ostream(u8 *out, size_t len) {
  1432. return (ostream_t) { out, len };
  1433. }
  1434. /// Returns an `istream_t` constructed from the given pointer and length
  1435. static inline istream_t IO_make_istream(const u8 *in, size_t len) {
  1436. return (istream_t) { in, len, 0 };
  1437. }
  1438. /// Returns an `istream_t` with the same base as `in`, and length `len`
  1439. /// Then, advance `in` to account for the consumed bytes
  1440. /// `in` must be byte aligned
  1441. static inline istream_t IO_make_sub_istream(istream_t *const in, size_t len) {
  1442. // Consume `len` bytes of the parent stream
  1443. const u8 *const ptr = IO_get_read_ptr(in, len);
  1444. // Make a substream using the pointer to those `len` bytes
  1445. return IO_make_istream(ptr, len);
  1446. }
  1447. /******* END IO STREAM OPERATIONS *********************************************/
  1448. /******* BITSTREAM OPERATIONS *************************************************/
  1449. /// Read `num` bits (up to 64) from `src + offset`, where `offset` is in bits
  1450. static inline u64 read_bits_LE(const u8 *src, const int num_bits,
  1451. const size_t offset) {
  1452. if (num_bits > 64) {
  1453. ERROR("Attempt to read an invalid number of bits");
  1454. }
  1455. // Skip over bytes that aren't in range
  1456. src += offset / 8;
  1457. size_t bit_offset = offset % 8;
  1458. u64 res = 0;
  1459. int shift = 0;
  1460. int left = num_bits;
  1461. while (left > 0) {
  1462. u64 mask = left >= 8 ? 0xff : (((u64)1 << left) - 1);
  1463. // Read the next byte, shift it to account for the offset, and then mask
  1464. // out the top part if we don't need all the bits
  1465. res += (((u64)*src++ >> bit_offset) & mask) << shift;
  1466. shift += 8 - bit_offset;
  1467. left -= 8 - bit_offset;
  1468. bit_offset = 0;
  1469. }
  1470. return res;
  1471. }
  1472. /// Read bits from the end of a HUF or FSE bitstream. `offset` is in bits, so
  1473. /// it updates `offset` to `offset - bits`, and then reads `bits` bits from
  1474. /// `src + offset`. If the offset becomes negative, the extra bits at the
  1475. /// bottom are filled in with `0` bits instead of reading from before `src`.
  1476. static inline u64 STREAM_read_bits(const u8 *const src, const int bits,
  1477. i64 *const offset) {
  1478. *offset = *offset - bits;
  1479. size_t actual_off = *offset;
  1480. size_t actual_bits = bits;
  1481. // Don't actually read bits from before the start of src, so if `*offset <
  1482. // 0` fix actual_off and actual_bits to reflect the quantity to read
  1483. if (*offset < 0) {
  1484. actual_bits += *offset;
  1485. actual_off = 0;
  1486. }
  1487. u64 res = read_bits_LE(src, actual_bits, actual_off);
  1488. if (*offset < 0) {
  1489. // Fill in the bottom "overflowed" bits with 0's
  1490. res = -*offset >= 64 ? 0 : (res << -*offset);
  1491. }
  1492. return res;
  1493. }
  1494. /******* END BITSTREAM OPERATIONS *********************************************/
  1495. /******* BIT COUNTING OPERATIONS **********************************************/
  1496. /// Returns `x`, where `2^x` is the largest power of 2 less than or equal to
  1497. /// `num`, or `-1` if `num == 0`.
  1498. static inline int highest_set_bit(const u64 num) {
  1499. for (int i = 63; i >= 0; i--) {
  1500. if (((u64)1 << i) <= num) {
  1501. return i;
  1502. }
  1503. }
  1504. return -1;
  1505. }
  1506. /******* END BIT COUNTING OPERATIONS ******************************************/
  1507. /******* HUFFMAN PRIMITIVES ***************************************************/
  1508. static inline u8 HUF_decode_symbol(const HUF_dtable *const dtable,
  1509. u16 *const state, const u8 *const src,
  1510. i64 *const offset) {
  1511. // Look up the symbol and number of bits to read
  1512. const u8 symb = dtable->symbols[*state];
  1513. const u8 bits = dtable->num_bits[*state];
  1514. const u16 rest = STREAM_read_bits(src, bits, offset);
  1515. // Shift `bits` bits out of the state, keeping the low order bits that
  1516. // weren't necessary to determine this symbol. Then add in the new bits
  1517. // read from the stream.
  1518. *state = ((*state << bits) + rest) & (((u16)1 << dtable->max_bits) - 1);
  1519. return symb;
  1520. }
  1521. static inline void HUF_init_state(const HUF_dtable *const dtable,
  1522. u16 *const state, const u8 *const src,
  1523. i64 *const offset) {
  1524. // Read in a full `dtable->max_bits` bits to initialize the state
  1525. const u8 bits = dtable->max_bits;
  1526. *state = STREAM_read_bits(src, bits, offset);
  1527. }
  1528. static size_t HUF_decompress_1stream(const HUF_dtable *const dtable,
  1529. ostream_t *const out,
  1530. istream_t *const in) {
  1531. const size_t len = IO_istream_len(in);
  1532. if (len == 0) {
  1533. INP_SIZE();
  1534. }
  1535. const u8 *const src = IO_get_read_ptr(in, len);
  1536. // "Each bitstream must be read backward, that is starting from the end down
  1537. // to the beginning. Therefore it's necessary to know the size of each
  1538. // bitstream.
  1539. //
  1540. // It's also necessary to know exactly which bit is the latest. This is
  1541. // detected by a final bit flag : the highest bit of latest byte is a
  1542. // final-bit-flag. Consequently, a last byte of 0 is not possible. And the
  1543. // final-bit-flag itself is not part of the useful bitstream. Hence, the
  1544. // last byte contains between 0 and 7 useful bits."
  1545. const int padding = 8 - highest_set_bit(src[len - 1]);
  1546. // Offset starts at the end because HUF streams are read backwards
  1547. i64 bit_offset = len * 8 - padding;
  1548. u16 state;
  1549. HUF_init_state(dtable, &state, src, &bit_offset);
  1550. size_t symbols_written = 0;
  1551. while (bit_offset > -dtable->max_bits) {
  1552. // Iterate over the stream, decoding one symbol at a time
  1553. IO_write_byte(out, HUF_decode_symbol(dtable, &state, src, &bit_offset));
  1554. symbols_written++;
  1555. }
  1556. // "The process continues up to reading the required number of symbols per
  1557. // stream. If a bitstream is not entirely and exactly consumed, hence
  1558. // reaching exactly its beginning position with all bits consumed, the
  1559. // decoding process is considered faulty."
  1560. // When all symbols have been decoded, the final state value shouldn't have
  1561. // any data from the stream, so it should have "read" dtable->max_bits from
  1562. // before the start of `src`
  1563. // Therefore `offset`, the edge to start reading new bits at, should be
  1564. // dtable->max_bits before the start of the stream
  1565. if (bit_offset != -dtable->max_bits) {
  1566. CORRUPTION();
  1567. }
  1568. return symbols_written;
  1569. }
  1570. static size_t HUF_decompress_4stream(const HUF_dtable *const dtable,
  1571. ostream_t *const out, istream_t *const in) {
  1572. // "Compressed size is provided explicitly : in the 4-streams variant,
  1573. // bitstreams are preceded by 3 unsigned little-endian 16-bits values. Each
  1574. // value represents the compressed size of one stream, in order. The last
  1575. // stream size is deducted from total compressed size and from previously
  1576. // decoded stream sizes"
  1577. const size_t csize1 = IO_read_bits(in, 16);
  1578. const size_t csize2 = IO_read_bits(in, 16);
  1579. const size_t csize3 = IO_read_bits(in, 16);
  1580. istream_t in1 = IO_make_sub_istream(in, csize1);
  1581. istream_t in2 = IO_make_sub_istream(in, csize2);
  1582. istream_t in3 = IO_make_sub_istream(in, csize3);
  1583. istream_t in4 = IO_make_sub_istream(in, IO_istream_len(in));
  1584. size_t total_output = 0;
  1585. // Decode each stream independently for simplicity
  1586. // If we wanted to we could decode all 4 at the same time for speed,
  1587. // utilizing more execution units
  1588. total_output += HUF_decompress_1stream(dtable, out, &in1);
  1589. total_output += HUF_decompress_1stream(dtable, out, &in2);
  1590. total_output += HUF_decompress_1stream(dtable, out, &in3);
  1591. total_output += HUF_decompress_1stream(dtable, out, &in4);
  1592. return total_output;
  1593. }
  1594. /// Initializes a Huffman table using canonical Huffman codes
  1595. /// For more explanation on canonical Huffman codes see
  1596. /// http://www.cs.uofs.edu/~mccloske/courses/cmps340/huff_canonical_dec2015.html
  1597. /// Codes within a level are allocated in symbol order (i.e. smaller symbols get
  1598. /// earlier codes)
  1599. static void HUF_init_dtable(HUF_dtable *const table, const u8 *const bits,
  1600. const int num_symbs) {
  1601. memset(table, 0, sizeof(HUF_dtable));
  1602. if (num_symbs > HUF_MAX_SYMBS) {
  1603. ERROR("Too many symbols for Huffman");
  1604. }
  1605. u8 max_bits = 0;
  1606. u16 rank_count[HUF_MAX_BITS + 1];
  1607. memset(rank_count, 0, sizeof(rank_count));
  1608. // Count the number of symbols for each number of bits, and determine the
  1609. // depth of the tree
  1610. for (int i = 0; i < num_symbs; i++) {
  1611. if (bits[i] > HUF_MAX_BITS) {
  1612. ERROR("Huffman table depth too large");
  1613. }
  1614. max_bits = MAX(max_bits, bits[i]);
  1615. rank_count[bits[i]]++;
  1616. }
  1617. const size_t table_size = 1 << max_bits;
  1618. table->max_bits = max_bits;
  1619. table->symbols = malloc(table_size);
  1620. table->num_bits = malloc(table_size);
  1621. if (!table->symbols || !table->num_bits) {
  1622. free(table->symbols);
  1623. free(table->num_bits);
  1624. BAD_ALLOC();
  1625. }
  1626. // "Symbols are sorted by Weight. Within same Weight, symbols keep natural
  1627. // order. Symbols with a Weight of zero are removed. Then, starting from
  1628. // lowest weight, prefix codes are distributed in order."
  1629. u32 rank_idx[HUF_MAX_BITS + 1];
  1630. // Initialize the starting codes for each rank (number of bits)
  1631. rank_idx[max_bits] = 0;
  1632. for (int i = max_bits; i >= 1; i--) {
  1633. rank_idx[i - 1] = rank_idx[i] + rank_count[i] * (1 << (max_bits - i));
  1634. // The entire range takes the same number of bits so we can memset it
  1635. memset(&table->num_bits[rank_idx[i]], i, rank_idx[i - 1] - rank_idx[i]);
  1636. }
  1637. if (rank_idx[0] != table_size) {
  1638. CORRUPTION();
  1639. }
  1640. // Allocate codes and fill in the table
  1641. for (int i = 0; i < num_symbs; i++) {
  1642. if (bits[i] != 0) {
  1643. // Allocate a code for this symbol and set its range in the table
  1644. const u16 code = rank_idx[bits[i]];
  1645. // Since the code doesn't care about the bottom `max_bits - bits[i]`
  1646. // bits of state, it gets a range that spans all possible values of
  1647. // the lower bits
  1648. const u16 len = 1 << (max_bits - bits[i]);
  1649. memset(&table->symbols[code], i, len);
  1650. rank_idx[bits[i]] += len;
  1651. }
  1652. }
  1653. }
  1654. static void HUF_init_dtable_usingweights(HUF_dtable *const table,
  1655. const u8 *const weights,
  1656. const int num_symbs) {
  1657. // +1 because the last weight is not transmitted in the header
  1658. if (num_symbs + 1 > HUF_MAX_SYMBS) {
  1659. ERROR("Too many symbols for Huffman");
  1660. }
  1661. u8 bits[HUF_MAX_SYMBS];
  1662. u64 weight_sum = 0;
  1663. for (int i = 0; i < num_symbs; i++) {
  1664. // Weights are in the same range as bit count
  1665. if (weights[i] > HUF_MAX_BITS) {
  1666. CORRUPTION();
  1667. }
  1668. weight_sum += weights[i] > 0 ? (u64)1 << (weights[i] - 1) : 0;
  1669. }
  1670. // Find the first power of 2 larger than the sum
  1671. const int max_bits = highest_set_bit(weight_sum) + 1;
  1672. const u64 left_over = ((u64)1 << max_bits) - weight_sum;
  1673. // If the left over isn't a power of 2, the weights are invalid
  1674. if (left_over & (left_over - 1)) {
  1675. CORRUPTION();
  1676. }
  1677. // left_over is used to find the last weight as it's not transmitted
  1678. // by inverting 2^(weight - 1) we can determine the value of last_weight
  1679. const int last_weight = highest_set_bit(left_over) + 1;
  1680. for (int i = 0; i < num_symbs; i++) {
  1681. // "Number_of_Bits = Number_of_Bits ? Max_Number_of_Bits + 1 - Weight : 0"
  1682. bits[i] = weights[i] > 0 ? (max_bits + 1 - weights[i]) : 0;
  1683. }
  1684. bits[num_symbs] =
  1685. max_bits + 1 - last_weight; // Last weight is always non-zero
  1686. HUF_init_dtable(table, bits, num_symbs + 1);
  1687. }
  1688. static void HUF_free_dtable(HUF_dtable *const dtable) {
  1689. free(dtable->symbols);
  1690. free(dtable->num_bits);
  1691. memset(dtable, 0, sizeof(HUF_dtable));
  1692. }
  1693. /******* END HUFFMAN PRIMITIVES ***********************************************/
  1694. /******* FSE PRIMITIVES *******************************************************/
  1695. /// For more description of FSE see
  1696. /// https://github.com/Cyan4973/FiniteStateEntropy/
  1697. /// Allow a symbol to be decoded without updating state
  1698. static inline u8 FSE_peek_symbol(const FSE_dtable *const dtable,
  1699. const u16 state) {
  1700. return dtable->symbols[state];
  1701. }
  1702. /// Consumes bits from the input and uses the current state to determine the
  1703. /// next state
  1704. static inline void FSE_update_state(const FSE_dtable *const dtable,
  1705. u16 *const state, const u8 *const src,
  1706. i64 *const offset) {
  1707. const u8 bits = dtable->num_bits[*state];
  1708. const u16 rest = STREAM_read_bits(src, bits, offset);
  1709. *state = dtable->new_state_base[*state] + rest;
  1710. }
  1711. /// Decodes a single FSE symbol and updates the offset
  1712. static inline u8 FSE_decode_symbol(const FSE_dtable *const dtable,
  1713. u16 *const state, const u8 *const src,
  1714. i64 *const offset) {
  1715. const u8 symb = FSE_peek_symbol(dtable, *state);
  1716. FSE_update_state(dtable, state, src, offset);
  1717. return symb;
  1718. }
  1719. static inline void FSE_init_state(const FSE_dtable *const dtable,
  1720. u16 *const state, const u8 *const src,
  1721. i64 *const offset) {
  1722. // Read in a full `accuracy_log` bits to initialize the state
  1723. const u8 bits = dtable->accuracy_log;
  1724. *state = STREAM_read_bits(src, bits, offset);
  1725. }
  1726. static size_t FSE_decompress_interleaved2(const FSE_dtable *const dtable,
  1727. ostream_t *const out,
  1728. istream_t *const in) {
  1729. const size_t len = IO_istream_len(in);
  1730. if (len == 0) {
  1731. INP_SIZE();
  1732. }
  1733. const u8 *const src = IO_get_read_ptr(in, len);
  1734. // "Each bitstream must be read backward, that is starting from the end down
  1735. // to the beginning. Therefore it's necessary to know the size of each
  1736. // bitstream.
  1737. //
  1738. // It's also necessary to know exactly which bit is the latest. This is
  1739. // detected by a final bit flag : the highest bit of latest byte is a
  1740. // final-bit-flag. Consequently, a last byte of 0 is not possible. And the
  1741. // final-bit-flag itself is not part of the useful bitstream. Hence, the
  1742. // last byte contains between 0 and 7 useful bits."
  1743. const int padding = 8 - highest_set_bit(src[len - 1]);
  1744. i64 offset = len * 8 - padding;
  1745. u16 state1, state2;
  1746. // "The first state (State1) encodes the even indexed symbols, and the
  1747. // second (State2) encodes the odd indexes. State1 is initialized first, and
  1748. // then State2, and they take turns decoding a single symbol and updating
  1749. // their state."
  1750. FSE_init_state(dtable, &state1, src, &offset);
  1751. FSE_init_state(dtable, &state2, src, &offset);
  1752. // Decode until we overflow the stream
  1753. // Since we decode in reverse order, overflowing the stream is offset going
  1754. // negative
  1755. size_t symbols_written = 0;
  1756. while (1) {
  1757. // "The number of symbols to decode is determined by tracking bitStream
  1758. // overflow condition: If updating state after decoding a symbol would
  1759. // require more bits than remain in the stream, it is assumed the extra
  1760. // bits are 0. Then, the symbols for each of the final states are
  1761. // decoded and the process is complete."
  1762. IO_write_byte(out, FSE_decode_symbol(dtable, &state1, src, &offset));
  1763. symbols_written++;
  1764. if (offset < 0) {
  1765. // There's still a symbol to decode in state2
  1766. IO_write_byte(out, FSE_peek_symbol(dtable, state2));
  1767. symbols_written++;
  1768. break;
  1769. }
  1770. IO_write_byte(out, FSE_decode_symbol(dtable, &state2, src, &offset));
  1771. symbols_written++;
  1772. if (offset < 0) {
  1773. // There's still a symbol to decode in state1
  1774. IO_write_byte(out, FSE_peek_symbol(dtable, state1));
  1775. symbols_written++;
  1776. break;
  1777. }
  1778. }
  1779. return symbols_written;
  1780. }
  1781. static void FSE_init_dtable(FSE_dtable *const dtable,
  1782. const i16 *const norm_freqs, const int num_symbs,
  1783. const int accuracy_log) {
  1784. if (accuracy_log > FSE_MAX_ACCURACY_LOG) {
  1785. ERROR("FSE accuracy too large");
  1786. }
  1787. if (num_symbs > FSE_MAX_SYMBS) {
  1788. ERROR("Too many symbols for FSE");
  1789. }
  1790. dtable->accuracy_log = accuracy_log;
  1791. const size_t size = (size_t)1 << accuracy_log;
  1792. dtable->symbols = malloc(size * sizeof(u8));
  1793. dtable->num_bits = malloc(size * sizeof(u8));
  1794. dtable->new_state_base = malloc(size * sizeof(u16));
  1795. if (!dtable->symbols || !dtable->num_bits || !dtable->new_state_base) {
  1796. BAD_ALLOC();
  1797. }
  1798. // Used to determine how many bits need to be read for each state,
  1799. // and where the destination range should start
  1800. // Needs to be u16 because max value is 2 * max number of symbols,
  1801. // which can be larger than a byte can store
  1802. u16 state_desc[FSE_MAX_SYMBS];
  1803. // "Symbols are scanned in their natural order for "less than 1"
  1804. // probabilities. Symbols with this probability are being attributed a
  1805. // single cell, starting from the end of the table. These symbols define a
  1806. // full state reset, reading Accuracy_Log bits."
  1807. int high_threshold = size;
  1808. for (int s = 0; s < num_symbs; s++) {
  1809. // Scan for low probability symbols to put at the top
  1810. if (norm_freqs[s] == -1) {
  1811. dtable->symbols[--high_threshold] = s;
  1812. state_desc[s] = 1;
  1813. }
  1814. }
  1815. // "All remaining symbols are sorted in their natural order. Starting from
  1816. // symbol 0 and table position 0, each symbol gets attributed as many cells
  1817. // as its probability. Cell allocation is spread, not linear."
  1818. // Place the rest in the table
  1819. const u16 step = (size >> 1) + (size >> 3) + 3;
  1820. const u16 mask = size - 1;
  1821. u16 pos = 0;
  1822. for (int s = 0; s < num_symbs; s++) {
  1823. if (norm_freqs[s] <= 0) {
  1824. continue;
  1825. }
  1826. state_desc[s] = norm_freqs[s];
  1827. for (int i = 0; i < norm_freqs[s]; i++) {
  1828. // Give `norm_freqs[s]` states to symbol s
  1829. dtable->symbols[pos] = s;
  1830. // "A position is skipped if already occupied, typically by a "less
  1831. // than 1" probability symbol."
  1832. do {
  1833. pos = (pos + step) & mask;
  1834. } while (pos >=
  1835. high_threshold);
  1836. // Note: no other collision checking is necessary as `step` is
  1837. // coprime to `size`, so the cycle will visit each position exactly
  1838. // once
  1839. }
  1840. }
  1841. if (pos != 0) {
  1842. CORRUPTION();
  1843. }
  1844. // Now we can fill baseline and num bits
  1845. for (size_t i = 0; i < size; i++) {
  1846. u8 symbol = dtable->symbols[i];
  1847. u16 next_state_desc = state_desc[symbol]++;
  1848. // Fills in the table appropriately, next_state_desc increases by symbol
  1849. // over time, decreasing number of bits
  1850. dtable->num_bits[i] = (u8)(accuracy_log - highest_set_bit(next_state_desc));
  1851. // Baseline increases until the bit threshold is passed, at which point
  1852. // it resets to 0
  1853. dtable->new_state_base[i] =
  1854. ((u16)next_state_desc << dtable->num_bits[i]) - size;
  1855. }
  1856. }
  1857. /// Decode an FSE header as defined in the Zstandard format specification and
  1858. /// use the decoded frequencies to initialize a decoding table.
  1859. static void FSE_decode_header(FSE_dtable *const dtable, istream_t *const in,
  1860. const int max_accuracy_log) {
  1861. // "An FSE distribution table describes the probabilities of all symbols
  1862. // from 0 to the last present one (included) on a normalized scale of 1 <<
  1863. // Accuracy_Log .
  1864. //
  1865. // It's a bitstream which is read forward, in little-endian fashion. It's
  1866. // not necessary to know its exact size, since it will be discovered and
  1867. // reported by the decoding process.
  1868. if (max_accuracy_log > FSE_MAX_ACCURACY_LOG) {
  1869. ERROR("FSE accuracy too large");
  1870. }
  1871. // The bitstream starts by reporting on which scale it operates.
  1872. // Accuracy_Log = low4bits + 5. Note that maximum Accuracy_Log for literal
  1873. // and match lengths is 9, and for offsets is 8. Higher values are
  1874. // considered errors."
  1875. const int accuracy_log = 5 + IO_read_bits(in, 4);
  1876. if (accuracy_log > max_accuracy_log) {
  1877. ERROR("FSE accuracy too large");
  1878. }
  1879. // "Then follows each symbol value, from 0 to last present one. The number
  1880. // of bits used by each field is variable. It depends on :
  1881. //
  1882. // Remaining probabilities + 1 : example : Presuming an Accuracy_Log of 8,
  1883. // and presuming 100 probabilities points have already been distributed, the
  1884. // decoder may read any value from 0 to 255 - 100 + 1 == 156 (inclusive).
  1885. // Therefore, it must read log2sup(156) == 8 bits.
  1886. //
  1887. // Value decoded : small values use 1 less bit : example : Presuming values
  1888. // from 0 to 156 (inclusive) are possible, 255-156 = 99 values are remaining
  1889. // in an 8-bits field. They are used this way : first 99 values (hence from
  1890. // 0 to 98) use only 7 bits, values from 99 to 156 use 8 bits. "
  1891. i32 remaining = 1 << accuracy_log;
  1892. i16 frequencies[FSE_MAX_SYMBS];
  1893. int symb = 0;
  1894. while (remaining > 0 && symb < FSE_MAX_SYMBS) {
  1895. // Log of the number of possible values we could read
  1896. int bits = highest_set_bit(remaining + 1) + 1;
  1897. u16 val = IO_read_bits(in, bits);
  1898. // Try to mask out the lower bits to see if it qualifies for the "small
  1899. // value" threshold
  1900. const u16 lower_mask = ((u16)1 << (bits - 1)) - 1;
  1901. const u16 threshold = ((u16)1 << bits) - 1 - (remaining + 1);
  1902. if ((val & lower_mask) < threshold) {
  1903. IO_rewind_bits(in, 1);
  1904. val = val & lower_mask;
  1905. } else if (val > lower_mask) {
  1906. val = val - threshold;
  1907. }
  1908. // "Probability is obtained from Value decoded by following formula :
  1909. // Proba = value - 1"
  1910. const i16 proba = (i16)val - 1;
  1911. // "It means value 0 becomes negative probability -1. -1 is a special
  1912. // probability, which means "less than 1". Its effect on distribution
  1913. // table is described in next paragraph. For the purpose of calculating
  1914. // cumulated distribution, it counts as one."
  1915. remaining -= proba < 0 ? -proba : proba;
  1916. frequencies[symb] = proba;
  1917. symb++;
  1918. // "When a symbol has a probability of zero, it is followed by a 2-bits
  1919. // repeat flag. This repeat flag tells how many probabilities of zeroes
  1920. // follow the current one. It provides a number ranging from 0 to 3. If
  1921. // it is a 3, another 2-bits repeat flag follows, and so on."
  1922. if (proba == 0) {
  1923. // Read the next two bits to see how many more 0s
  1924. int repeat = IO_read_bits(in, 2);
  1925. while (1) {
  1926. for (int i = 0; i < repeat && symb < FSE_MAX_SYMBS; i++) {
  1927. frequencies[symb++] = 0;
  1928. }
  1929. if (repeat == 3) {
  1930. repeat = IO_read_bits(in, 2);
  1931. } else {
  1932. break;
  1933. }
  1934. }
  1935. }
  1936. }
  1937. IO_align_stream(in);
  1938. // "When last symbol reaches cumulated total of 1 << Accuracy_Log, decoding
  1939. // is complete. If the last symbol makes cumulated total go above 1 <<
  1940. // Accuracy_Log, distribution is considered corrupted."
  1941. if (remaining != 0 || symb >= FSE_MAX_SYMBS) {
  1942. CORRUPTION();
  1943. }
  1944. // Initialize the decoding table using the determined weights
  1945. FSE_init_dtable(dtable, frequencies, symb, accuracy_log);
  1946. }
  1947. static void FSE_init_dtable_rle(FSE_dtable *const dtable, const u8 symb) {
  1948. dtable->symbols = malloc(sizeof(u8));
  1949. dtable->num_bits = malloc(sizeof(u8));
  1950. dtable->new_state_base = malloc(sizeof(u16));
  1951. if (!dtable->symbols || !dtable->num_bits || !dtable->new_state_base) {
  1952. BAD_ALLOC();
  1953. }
  1954. // This setup will always have a state of 0, always return symbol `symb`,
  1955. // and never consume any bits
  1956. dtable->symbols[0] = symb;
  1957. dtable->num_bits[0] = 0;
  1958. dtable->new_state_base[0] = 0;
  1959. dtable->accuracy_log = 0;
  1960. }
  1961. static void FSE_free_dtable(FSE_dtable *const dtable) {
  1962. free(dtable->symbols);
  1963. free(dtable->num_bits);
  1964. free(dtable->new_state_base);
  1965. memset(dtable, 0, sizeof(FSE_dtable));
  1966. }
  1967. /******* END FSE PRIMITIVES ***************************************************/