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