xz_android.cc 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. //
  2. // Copyright (C) 2016 The Android Open Source Project
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. //
  8. // http://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software
  11. // distributed under the License is distributed on an "AS IS" BASIS,
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. // See the License for the specific language governing permissions and
  14. // limitations under the License.
  15. //
  16. #include "update_engine/payload_generator/xz.h"
  17. #include <elf.h>
  18. #include <endian.h>
  19. #include <algorithm>
  20. #include <7zCrc.h>
  21. #include <Xz.h>
  22. #include <XzEnc.h>
  23. #include <base/logging.h>
  24. namespace {
  25. bool xz_initialized = false;
  26. // An ISeqInStream implementation that reads all the data from the passed Blob.
  27. struct BlobReaderStream : public ISeqInStream {
  28. explicit BlobReaderStream(const brillo::Blob& data) : data_(data) {
  29. Read = &BlobReaderStream::ReadStatic;
  30. }
  31. static SRes ReadStatic(const ISeqInStream* p, void* buf, size_t* size) {
  32. auto* self = static_cast<BlobReaderStream*>(const_cast<ISeqInStream*>(p));
  33. *size = std::min(*size, self->data_.size() - self->pos_);
  34. memcpy(buf, self->data_.data() + self->pos_, *size);
  35. self->pos_ += *size;
  36. return SZ_OK;
  37. }
  38. const brillo::Blob& data_;
  39. // The current reader position.
  40. size_t pos_ = 0;
  41. };
  42. // An ISeqOutStream implementation that writes all the data to the passed Blob.
  43. struct BlobWriterStream : public ISeqOutStream {
  44. explicit BlobWriterStream(brillo::Blob* data) : data_(data) {
  45. Write = &BlobWriterStream::WriteStatic;
  46. }
  47. static size_t WriteStatic(const ISeqOutStream* p,
  48. const void* buf,
  49. size_t size) {
  50. auto* self = static_cast<const BlobWriterStream*>(p);
  51. const uint8_t* buffer = reinterpret_cast<const uint8_t*>(buf);
  52. self->data_->reserve(self->data_->size() + size);
  53. self->data_->insert(self->data_->end(), buffer, buffer + size);
  54. return size;
  55. }
  56. brillo::Blob* data_;
  57. };
  58. // Returns the filter id to be used to compress |data|.
  59. // Only BCJ filter for x86 and ARM ELF file are supported, returns 0 otherwise.
  60. int GetFilterID(const brillo::Blob& data) {
  61. if (data.size() < sizeof(Elf32_Ehdr) ||
  62. memcmp(data.data(), ELFMAG, SELFMAG) != 0)
  63. return 0;
  64. const Elf32_Ehdr* header = reinterpret_cast<const Elf32_Ehdr*>(data.data());
  65. // Only little-endian is supported.
  66. if (header->e_ident[EI_DATA] != ELFDATA2LSB)
  67. return 0;
  68. switch (le16toh(header->e_machine)) {
  69. case EM_386:
  70. case EM_X86_64:
  71. return XZ_ID_X86;
  72. case EM_ARM:
  73. // Both ARM and ARM Thumb instructions could be found in the same ARM ELF
  74. // file. We choose to use the ARM Thumb filter here because testing shows
  75. // that it usually works better than the ARM filter.
  76. return XZ_ID_ARMT;
  77. #ifdef EM_AARCH64
  78. case EM_AARCH64:
  79. // Neither the ARM nor the ARM Thumb filter works well with AArch64.
  80. return 0;
  81. #endif
  82. }
  83. return 0;
  84. }
  85. } // namespace
  86. namespace chromeos_update_engine {
  87. void XzCompressInit() {
  88. if (xz_initialized)
  89. return;
  90. xz_initialized = true;
  91. // Although we don't include a CRC32 for the stream, the xz file header has
  92. // a CRC32 of the header itself, which required the CRC table to be
  93. // initialized.
  94. CrcGenerateTable();
  95. }
  96. bool XzCompress(const brillo::Blob& in, brillo::Blob* out) {
  97. CHECK(xz_initialized) << "Initialize XzCompress first";
  98. out->clear();
  99. if (in.empty())
  100. return true;
  101. // Xz compression properties.
  102. CXzProps props;
  103. XzProps_Init(&props);
  104. // No checksum in the xz stream. xz-embedded (used by the decompressor) only
  105. // supports CRC32, but we already check the sha-1 of the whole blob during
  106. // payload application.
  107. props.checkId = XZ_CHECK_NO;
  108. // LZMA2 compression properties.
  109. CLzma2EncProps lzma2Props;
  110. Lzma2EncProps_Init(&lzma2Props);
  111. // LZMA compression "level 6" requires 9 MB of RAM to decompress in the worst
  112. // case.
  113. lzma2Props.lzmaProps.level = 6;
  114. lzma2Props.lzmaProps.numThreads = 1;
  115. // The input size data is used to reduce the dictionary size if possible.
  116. lzma2Props.lzmaProps.reduceSize = in.size();
  117. Lzma2EncProps_Normalize(&lzma2Props);
  118. props.lzma2Props = lzma2Props;
  119. props.filterProps.id = GetFilterID(in);
  120. BlobWriterStream out_writer(out);
  121. BlobReaderStream in_reader(in);
  122. SRes res = Xz_Encode(&out_writer, &in_reader, &props, nullptr /* progress */);
  123. return res == SZ_OK;
  124. }
  125. } // namespace chromeos_update_engine