lz4_wrapper.c 2.7 KB

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