lz4_wrapper.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. #include <asm/unaligned.h>
  12. static u16 LZ4_readLE16(const void *src)
  13. {
  14. return get_unaligned_le16(src);
  15. }
  16. static void LZ4_copy4(void *dst, const void *src)
  17. {
  18. put_unaligned(get_unaligned((const u32 *)src), (u32 *)dst);
  19. }
  20. static void LZ4_copy8(void *dst, const void *src)
  21. {
  22. put_unaligned(get_unaligned((const u64 *)src), (u64 *)dst);
  23. }
  24. typedef uint8_t BYTE;
  25. typedef uint16_t U16;
  26. typedef uint32_t U32;
  27. typedef int32_t S32;
  28. typedef uint64_t U64;
  29. #define FORCE_INLINE static inline __attribute__((always_inline))
  30. /* lz4.c is unaltered (except removing unrelated code) from github.com/Cyan4973/lz4. */
  31. #include "lz4.c" /* #include for inlining, do not link! */
  32. #define LZ4F_BLOCKUNCOMPRESSED_FLAG 0x80000000U
  33. int ulz4fn(const void *src, size_t srcn, void *dst, size_t *dstn)
  34. {
  35. const void *end = dst + *dstn;
  36. const void *in = src;
  37. void *out = dst;
  38. int has_block_checksum;
  39. int ret;
  40. *dstn = 0;
  41. { /* With in-place decompression the header may become invalid later. */
  42. u32 magic;
  43. u8 flags, version, independent_blocks, has_content_size;
  44. u8 block_desc;
  45. if (srcn < sizeof(u32) + 3*sizeof(u8))
  46. return -EINVAL; /* input overrun */
  47. magic = get_unaligned_le32(in);
  48. in += sizeof(u32);
  49. flags = *(u8 *)in;
  50. in += sizeof(u8);
  51. block_desc = *(u8 *)in;
  52. in += sizeof(u8);
  53. version = (flags >> 6) & 0x3;
  54. independent_blocks = (flags >> 5) & 0x1;
  55. has_block_checksum = (flags >> 4) & 0x1;
  56. has_content_size = (flags >> 3) & 0x1;
  57. /* We assume there's always only a single, standard frame. */
  58. if (magic != LZ4F_MAGIC || version != 1)
  59. return -EPROTONOSUPPORT; /* unknown format */
  60. if ((flags & 0x03) || (block_desc & 0x8f))
  61. return -EINVAL; /* reserved bits must be zero */
  62. if (!independent_blocks)
  63. return -EPROTONOSUPPORT; /* we can't support this yet */
  64. if (has_content_size) {
  65. if (srcn < sizeof(u32) + 3*sizeof(u8) + sizeof(u64))
  66. return -EINVAL; /* input overrun */
  67. in += sizeof(u64);
  68. }
  69. /* Header checksum byte */
  70. in += sizeof(u8);
  71. }
  72. while (1) {
  73. u32 block_header, block_size;
  74. block_header = get_unaligned_le32(in);
  75. in += sizeof(u32);
  76. block_size = block_header & ~LZ4F_BLOCKUNCOMPRESSED_FLAG;
  77. if (in - src + block_size > srcn) {
  78. ret = -EINVAL; /* input overrun */
  79. break;
  80. }
  81. if (!block_size) {
  82. ret = 0; /* decompression successful */
  83. break;
  84. }
  85. if (block_header & LZ4F_BLOCKUNCOMPRESSED_FLAG) {
  86. size_t size = min((ptrdiff_t)block_size, end - out);
  87. memcpy(out, in, size);
  88. out += size;
  89. if (size < block_size) {
  90. ret = -ENOBUFS; /* output overrun */
  91. break;
  92. }
  93. } else {
  94. /* constant folding essential, do not touch params! */
  95. ret = LZ4_decompress_generic(in, out, block_size,
  96. end - out, endOnInputSize,
  97. full, 0, noDict, out, NULL, 0);
  98. if (ret < 0) {
  99. ret = -EPROTO; /* decompression error */
  100. break;
  101. }
  102. out += ret;
  103. }
  104. in += block_size;
  105. if (has_block_checksum)
  106. in += sizeof(u32);
  107. }
  108. *dstn = out - dst;
  109. return ret;
  110. }