shoco.odin 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. /*
  2. Copyright 2022 Jeroen van Rijn <[email protected]>.
  3. Made available under Odin's BSD-3 license.
  4. List of contributors:
  5. Jeroen van Rijn: Initial implementation.
  6. An implementation of [shoco](https://github.com/Ed-von-Schleck/shoco) by Christian Schramm.
  7. */
  8. // package shoco is an implementation of the shoco short string compressor
  9. package compress_shoco
  10. import "base:intrinsics"
  11. import "core:compress"
  12. Shoco_Pack :: struct {
  13. word: u32,
  14. bytes_packed: i8,
  15. bytes_unpacked: i8,
  16. offsets: [8]u16,
  17. masks: [8]i16,
  18. header_mask: u8,
  19. header: u8,
  20. }
  21. Shoco_Model :: struct {
  22. min_char: u8,
  23. max_char: u8,
  24. characters_by_id: []u8,
  25. ids_by_character: [256]i16,
  26. successors_by_bigram: []i8,
  27. successors_reversed: []u8,
  28. character_count: u8,
  29. successor_count: u8,
  30. max_successor_n: i8,
  31. packs: []Shoco_Pack,
  32. }
  33. compress_bound :: proc(uncompressed_size: int) -> (worst_case_compressed_size: int) {
  34. // Worst case compression happens when input is non-ASCII (128-255)
  35. // Encoded as 0x00 + the byte in question.
  36. return uncompressed_size * 2
  37. }
  38. decompress_bound :: proc(compressed_size: int, model := DEFAULT_MODEL) -> (maximum_decompressed_size: int) {
  39. // Best case compression is 2:1
  40. most: f64
  41. for pack in model.packs {
  42. val := f64(compressed_size) / f64(pack.bytes_packed) * f64(pack.bytes_unpacked)
  43. most = max(most, val)
  44. }
  45. return int(most)
  46. }
  47. find_best_encoding :: proc(indices: []i16, n_consecutive: i8, model := DEFAULT_MODEL) -> (res: int) {
  48. for p := len(model.packs); p > 0; p -= 1 {
  49. pack := model.packs[p - 1]
  50. if n_consecutive >= pack.bytes_unpacked {
  51. have_index := true
  52. for i := 0; i < int(pack.bytes_unpacked); i += 1 {
  53. if indices[i] > pack.masks[i] {
  54. have_index = false
  55. break
  56. }
  57. }
  58. if have_index {
  59. return p - 1
  60. }
  61. }
  62. }
  63. return -1
  64. }
  65. validate_model :: proc(model: Shoco_Model) -> (int, compress.Error) {
  66. if len(model.characters_by_id) != int(model.character_count) {
  67. return 0, .Unknown_Compression_Method
  68. }
  69. if len(model.successors_by_bigram) != int(model.character_count) * int(model.character_count) {
  70. return 0, .Unknown_Compression_Method
  71. }
  72. if len(model.successors_reversed) != int(model.successor_count) * int(model.max_char - model.min_char) {
  73. return 0, .Unknown_Compression_Method
  74. }
  75. // Model seems legit.
  76. return 0, nil
  77. }
  78. // Decompresses into provided buffer.
  79. decompress_slice_to_output_buffer :: proc(input: []u8, output: []u8, model := DEFAULT_MODEL) -> (size: int, err: compress.Error) {
  80. inp, inp_end := 0, len(input)
  81. out, out_end := 0, len(output)
  82. validate_model(model) or_return
  83. for inp < inp_end {
  84. val := i8(input[inp])
  85. mark := int(-1)
  86. for val < 0 {
  87. val <<= 1
  88. mark += 1
  89. }
  90. if mark > len(model.packs) {
  91. return out, .Unknown_Compression_Method
  92. }
  93. if mark < 0 {
  94. if out >= out_end {
  95. return out, .Output_Too_Short
  96. }
  97. // Ignore the sentinel value for non-ASCII chars
  98. if input[inp] == 0x00 {
  99. inp += 1
  100. if inp >= inp_end {
  101. return out, .Stream_Too_Short
  102. }
  103. }
  104. output[out] = input[inp]
  105. inp, out = inp + 1, out + 1
  106. } else {
  107. pack := model.packs[mark]
  108. if out + int(pack.bytes_unpacked) > out_end {
  109. return out, .Output_Too_Short
  110. } else if inp + int(pack.bytes_packed) > inp_end {
  111. return out, .Stream_Too_Short
  112. }
  113. code := intrinsics.unaligned_load((^u32)(&input[inp]))
  114. when ODIN_ENDIAN == .Little {
  115. code = intrinsics.byte_swap(code)
  116. }
  117. // Unpack the leading char
  118. offset := pack.offsets[0]
  119. mask := pack.masks[0]
  120. last_chr := model.characters_by_id[(code >> offset) & u32(mask)]
  121. output[out] = last_chr
  122. // Unpack the successor chars
  123. for i := 1; i < int(pack.bytes_unpacked); i += 1 {
  124. offset = pack.offsets[i]
  125. mask = pack.masks[i]
  126. index_major := u32(last_chr - model.min_char) * u32(model.successor_count)
  127. index_minor := (code >> offset) & u32(mask)
  128. last_chr = model.successors_reversed[index_major + index_minor]
  129. output[out + i] = last_chr
  130. }
  131. out += int(pack.bytes_unpacked)
  132. inp += int(pack.bytes_packed)
  133. }
  134. }
  135. return out, nil
  136. }
  137. decompress_slice_to_string :: proc(input: []u8, model := DEFAULT_MODEL, allocator := context.allocator) -> (res: string, err: compress.Error) {
  138. context.allocator = allocator
  139. if len(input) == 0 {
  140. return "", .Stream_Too_Short
  141. }
  142. max_output_size := decompress_bound(len(input), model)
  143. buf: [dynamic]u8
  144. resize(&buf, max_output_size) or_return
  145. length, result := decompress_slice_to_output_buffer(input, buf[:])
  146. resize(&buf, length) or_return
  147. return string(buf[:]), result
  148. }
  149. decompress :: proc{decompress_slice_to_output_buffer, decompress_slice_to_string}
  150. compress_string_to_buffer :: proc(input: string, output: []u8, model := DEFAULT_MODEL, allocator := context.allocator) -> (size: int, err: compress.Error) {
  151. inp, inp_end := 0, len(input)
  152. out, out_end := 0, len(output)
  153. output := output
  154. validate_model(model) or_return
  155. indices := make([]i16, model.max_successor_n + 1)
  156. defer delete(indices)
  157. last_resort := false
  158. encode: for inp < inp_end {
  159. if last_resort {
  160. last_resort = false
  161. if input[inp] & 0x80 == 0x80 {
  162. // Non-ASCII case
  163. if out + 2 > out_end {
  164. return out, .Output_Too_Short
  165. }
  166. // Put in a sentinel byte
  167. output[out] = 0x00
  168. out += 1
  169. } else {
  170. // An ASCII byte
  171. if out + 1 > out_end {
  172. return out, .Output_Too_Short
  173. }
  174. }
  175. output[out] = input[inp]
  176. out, inp = out + 1, inp + 1
  177. } else {
  178. // Find the longest string of known successors
  179. indices[0] = model.ids_by_character[input[inp]]
  180. last_chr_index := indices[0]
  181. if last_chr_index < 0 {
  182. last_resort = true
  183. continue encode
  184. }
  185. rest := inp_end - inp
  186. n_consecutive: i8 = 1
  187. for ; n_consecutive <= model.max_successor_n; n_consecutive += 1 {
  188. if inp_end > 0 && int(n_consecutive) == rest {
  189. break
  190. }
  191. current_index := model.ids_by_character[input[inp + int(n_consecutive)]]
  192. if current_index < 0 { // '\0' is always -1
  193. break
  194. }
  195. successor_index := model.successors_by_bigram[last_chr_index * i16(model.character_count) + current_index]
  196. if successor_index < 0 {
  197. break
  198. }
  199. indices[n_consecutive] = i16(successor_index)
  200. last_chr_index = current_index
  201. }
  202. if n_consecutive < 2 {
  203. last_resort = true
  204. continue encode
  205. }
  206. pack_n := find_best_encoding(indices, n_consecutive)
  207. if pack_n >= 0 {
  208. if out + int(model.packs[pack_n].bytes_packed) > out_end {
  209. return out, .Output_Too_Short
  210. }
  211. pack := model.packs[pack_n]
  212. code := pack.word
  213. for i := 0; i < int(pack.bytes_unpacked); i += 1 {
  214. code |= u32(indices[i]) << pack.offsets[i]
  215. }
  216. // In the little-endian world, we need to swap what's in the register to match the memory representation.
  217. when ODIN_ENDIAN == .Little {
  218. code = intrinsics.byte_swap(code)
  219. }
  220. out_ptr := raw_data(output[out:])
  221. switch pack.bytes_packed {
  222. case 4: intrinsics.unaligned_store((^u32)(out_ptr), code)
  223. case 2: intrinsics.unaligned_store((^u16)(out_ptr), u16(code))
  224. case 1: intrinsics.unaligned_store( (^u8)(out_ptr), u8(code))
  225. case:
  226. return out, .Unknown_Compression_Method
  227. }
  228. out += int(pack.bytes_packed)
  229. inp += int(pack.bytes_unpacked)
  230. } else {
  231. last_resort = true
  232. continue encode
  233. }
  234. }
  235. }
  236. return out, nil
  237. }
  238. compress_string :: proc(input: string, model := DEFAULT_MODEL, allocator := context.allocator) -> (output: []u8, err: compress.Error) {
  239. context.allocator = allocator
  240. if len(input) == 0 {
  241. return {}, .Stream_Too_Short
  242. }
  243. max_output_size := compress_bound(len(input))
  244. buf: [dynamic]u8
  245. resize(&buf, max_output_size) or_return
  246. length, result := compress_string_to_buffer(input, buf[:])
  247. resize(&buf, length) or_return
  248. return buf[:length], result
  249. }
  250. compress :: proc{compress_string_to_buffer, compress_string}