lz4_wrapper.c 3.0 KB

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