lz4_wrapper.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. // SPDX-License-Identifier: GPL 2.0+ OR BSD-3-Clause
  2. /*
  3. * Copyright 2015 Google Inc.
  4. */
  5. #include <common.h>
  6. #include <compiler.h>
  7. #include <linux/kernel.h>
  8. #include <linux/types.h>
  9. static u16 LZ4_readLE16(const void *src) { return le16_to_cpu(*(u16 *)src); }
  10. static void LZ4_copy4(void *dst, const void *src) { *(u32 *)dst = *(u32 *)src; }
  11. static void LZ4_copy8(void *dst, const void *src) { *(u64 *)dst = *(u64 *)src; }
  12. typedef uint8_t BYTE;
  13. typedef uint16_t U16;
  14. typedef uint32_t U32;
  15. typedef int32_t S32;
  16. typedef uint64_t U64;
  17. #define FORCE_INLINE static inline __attribute__((always_inline))
  18. /* Unaltered (except removing unrelated code) from github.com/Cyan4973/lz4. */
  19. #include "lz4.c" /* #include for inlining, do not link! */
  20. #define LZ4F_MAGIC 0x184D2204
  21. struct lz4_frame_header {
  22. u32 magic;
  23. union {
  24. u8 flags;
  25. struct {
  26. u8 reserved0:2;
  27. u8 has_content_checksum:1;
  28. u8 has_content_size:1;
  29. u8 has_block_checksum:1;
  30. u8 independent_blocks:1;
  31. u8 version:2;
  32. };
  33. };
  34. union {
  35. u8 block_descriptor;
  36. struct {
  37. u8 reserved1:4;
  38. u8 max_block_size:3;
  39. u8 reserved2:1;
  40. };
  41. };
  42. /* + u64 content_size iff has_content_size is set */
  43. /* + u8 header_checksum */
  44. } __packed;
  45. struct lz4_block_header {
  46. union {
  47. u32 raw;
  48. struct {
  49. u32 size:31;
  50. u32 not_compressed:1;
  51. };
  52. };
  53. /* + size bytes of data */
  54. /* + u32 block_checksum iff has_block_checksum is set */
  55. } __packed;
  56. int ulz4fn(const void *src, size_t srcn, void *dst, size_t *dstn)
  57. {
  58. const void *end = dst + *dstn;
  59. const void *in = src;
  60. void *out = dst;
  61. int has_block_checksum;
  62. int ret;
  63. *dstn = 0;
  64. { /* With in-place decompression the header may become invalid later. */
  65. const struct lz4_frame_header *h = in;
  66. if (srcn < sizeof(*h) + sizeof(u64) + sizeof(u8))
  67. return -EINVAL; /* input overrun */
  68. /* We assume there's always only a single, standard frame. */
  69. if (le32_to_cpu(h->magic) != LZ4F_MAGIC || h->version != 1)
  70. return -EPROTONOSUPPORT; /* unknown format */
  71. if (h->reserved0 || h->reserved1 || h->reserved2)
  72. return -EINVAL; /* reserved must be zero */
  73. if (!h->independent_blocks)
  74. return -EPROTONOSUPPORT; /* we can't support this yet */
  75. has_block_checksum = h->has_block_checksum;
  76. in += sizeof(*h);
  77. if (h->has_content_size)
  78. in += sizeof(u64);
  79. in += sizeof(u8);
  80. }
  81. while (1) {
  82. struct lz4_block_header b;
  83. b.raw = le32_to_cpu(*(u32 *)in);
  84. in += sizeof(struct lz4_block_header);
  85. if (in - src + b.size > srcn) {
  86. ret = -EINVAL; /* input overrun */
  87. break;
  88. }
  89. if (!b.size) {
  90. ret = 0; /* decompression successful */
  91. break;
  92. }
  93. if (b.not_compressed) {
  94. size_t size = min((ptrdiff_t)b.size, end - out);
  95. memcpy(out, in, size);
  96. out += size;
  97. if (size < b.size) {
  98. ret = -ENOBUFS; /* output overrun */
  99. break;
  100. }
  101. } else {
  102. /* constant folding essential, do not touch params! */
  103. ret = LZ4_decompress_generic(in, out, b.size,
  104. end - out, endOnInputSize,
  105. full, 0, noDict, out, NULL, 0);
  106. if (ret < 0) {
  107. ret = -EPROTO; /* decompression error */
  108. break;
  109. }
  110. out += ret;
  111. }
  112. in += b.size;
  113. if (has_block_checksum)
  114. in += sizeof(u32);
  115. }
  116. *dstn = out - dst;
  117. return ret;
  118. }