Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 1 | // Copyright (c) 2010 The Chromium OS Authors. All rights reserved. |
Andrew de los Reyes | d2135f3 | 2010-03-11 16:00:28 -0800 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 5 | #include "update_engine/bzip.h" |
Alex Deymo | aab50e3 | 2014-11-10 19:55:35 -0800 | [diff] [blame] | 6 | |
Andrew de los Reyes | d2135f3 | 2010-03-11 16:00:28 -0800 | [diff] [blame] | 7 | #include <stdlib.h> |
| 8 | #include <algorithm> |
| 9 | #include <bzlib.h> |
Alex Deymo | 914c446 | 2015-06-26 17:17:05 -0700 | [diff] [blame] | 10 | #include <limits> |
| 11 | |
Andrew de los Reyes | d2135f3 | 2010-03-11 16:00:28 -0800 | [diff] [blame] | 12 | #include "update_engine/utils.h" |
| 13 | |
Andrew de los Reyes | d2135f3 | 2010-03-11 16:00:28 -0800 | [diff] [blame] | 14 | using std::string; |
| 15 | using std::vector; |
| 16 | |
| 17 | namespace chromeos_update_engine { |
| 18 | |
| 19 | namespace { |
| 20 | |
| 21 | // BzipData compresses or decompresses the input to the output. |
| 22 | // Returns true on success. |
| 23 | // Use one of BzipBuffToBuff*ompress as the template parameter to BzipData(). |
Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 24 | int BzipBuffToBuffDecompress(uint8_t* out, |
Andrew de los Reyes | 08c4e27 | 2010-04-15 14:02:17 -0700 | [diff] [blame] | 25 | uint32_t* out_length, |
Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 26 | const void* in, |
Andrew de los Reyes | 08c4e27 | 2010-04-15 14:02:17 -0700 | [diff] [blame] | 27 | uint32_t in_length) { |
Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 28 | return BZ2_bzBuffToBuffDecompress( |
| 29 | reinterpret_cast<char*>(out), |
| 30 | out_length, |
| 31 | reinterpret_cast<char*>(const_cast<void*>(in)), |
| 32 | in_length, |
| 33 | 0, // Silent verbosity |
| 34 | 0); // Normal algorithm |
Andrew de los Reyes | d2135f3 | 2010-03-11 16:00:28 -0800 | [diff] [blame] | 35 | } |
| 36 | |
Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 37 | int BzipBuffToBuffCompress(uint8_t* out, |
Andrew de los Reyes | 08c4e27 | 2010-04-15 14:02:17 -0700 | [diff] [blame] | 38 | uint32_t* out_length, |
Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 39 | const void* in, |
Andrew de los Reyes | 08c4e27 | 2010-04-15 14:02:17 -0700 | [diff] [blame] | 40 | uint32_t in_length) { |
Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 41 | return BZ2_bzBuffToBuffCompress( |
| 42 | reinterpret_cast<char*>(out), |
| 43 | out_length, |
| 44 | reinterpret_cast<char*>(const_cast<void*>(in)), |
| 45 | in_length, |
| 46 | 9, // Best compression |
| 47 | 0, // Silent verbosity |
| 48 | 0); // Default work factor |
Andrew de los Reyes | d2135f3 | 2010-03-11 16:00:28 -0800 | [diff] [blame] | 49 | } |
| 50 | |
Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 51 | template<int F(uint8_t* out, |
Andrew de los Reyes | 08c4e27 | 2010-04-15 14:02:17 -0700 | [diff] [blame] | 52 | uint32_t* out_length, |
Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 53 | const void* in, |
Andrew de los Reyes | 08c4e27 | 2010-04-15 14:02:17 -0700 | [diff] [blame] | 54 | uint32_t in_length)> |
Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 55 | bool BzipData(const void* const in, |
Alex Deymo | 914c446 | 2015-06-26 17:17:05 -0700 | [diff] [blame] | 56 | const size_t in_size, |
Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 57 | chromeos::Blob* const out) { |
Andrew de los Reyes | d2135f3 | 2010-03-11 16:00:28 -0800 | [diff] [blame] | 58 | TEST_AND_RETURN_FALSE(out); |
| 59 | out->clear(); |
| 60 | if (in_size == 0) { |
| 61 | return true; |
| 62 | } |
| 63 | // Try increasing buffer size until it works |
| 64 | size_t buf_size = in_size; |
| 65 | out->resize(buf_size); |
Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 66 | |
Andrew de los Reyes | d2135f3 | 2010-03-11 16:00:28 -0800 | [diff] [blame] | 67 | for (;;) { |
Alex Deymo | 914c446 | 2015-06-26 17:17:05 -0700 | [diff] [blame] | 68 | if (buf_size > std::numeric_limits<uint32_t>::max()) |
| 69 | return false; |
Andrew de los Reyes | 08c4e27 | 2010-04-15 14:02:17 -0700 | [diff] [blame] | 70 | uint32_t data_size = buf_size; |
Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 71 | int rc = F(out->data(), &data_size, in, in_size); |
Andrew de los Reyes | d2135f3 | 2010-03-11 16:00:28 -0800 | [diff] [blame] | 72 | TEST_AND_RETURN_FALSE(rc == BZ_OUTBUFF_FULL || rc == BZ_OK); |
| 73 | if (rc == BZ_OK) { |
| 74 | // we're done! |
| 75 | out->resize(data_size); |
| 76 | return true; |
| 77 | } |
Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 78 | |
Andrew de los Reyes | d2135f3 | 2010-03-11 16:00:28 -0800 | [diff] [blame] | 79 | // Data didn't fit; double the buffer size. |
| 80 | buf_size *= 2; |
| 81 | out->resize(buf_size); |
| 82 | } |
| 83 | } |
| 84 | |
Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 85 | } // namespace |
Andrew de los Reyes | d2135f3 | 2010-03-11 16:00:28 -0800 | [diff] [blame] | 86 | |
Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 87 | bool BzipDecompress(const chromeos::Blob& in, chromeos::Blob* out) { |
Alex Deymo | 914c446 | 2015-06-26 17:17:05 -0700 | [diff] [blame] | 88 | return BzipData<BzipBuffToBuffDecompress>(in.data(), in.size(), out); |
Andrew de los Reyes | d2135f3 | 2010-03-11 16:00:28 -0800 | [diff] [blame] | 89 | } |
| 90 | |
Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 91 | bool BzipCompress(const chromeos::Blob& in, chromeos::Blob* out) { |
| 92 | return BzipData<BzipBuffToBuffCompress>(in.data(), in.size(), out); |
Andrew de los Reyes | d2135f3 | 2010-03-11 16:00:28 -0800 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | namespace { |
Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 96 | template<bool F(const void* const in, |
Alex Deymo | 914c446 | 2015-06-26 17:17:05 -0700 | [diff] [blame] | 97 | const size_t in_size, |
Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 98 | chromeos::Blob* const out)> |
Alex Deymo | f329b93 | 2014-10-30 01:37:48 -0700 | [diff] [blame] | 99 | bool BzipString(const string& str, |
Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 100 | chromeos::Blob* out) { |
Andrew de los Reyes | d2135f3 | 2010-03-11 16:00:28 -0800 | [diff] [blame] | 101 | TEST_AND_RETURN_FALSE(out); |
Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 102 | chromeos::Blob temp; |
| 103 | TEST_AND_RETURN_FALSE(F(str.data(), str.size(), &temp)); |
Andrew de los Reyes | d2135f3 | 2010-03-11 16:00:28 -0800 | [diff] [blame] | 104 | out->clear(); |
| 105 | out->insert(out->end(), temp.begin(), temp.end()); |
| 106 | return true; |
| 107 | } |
Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 108 | } // namespace |
Andrew de los Reyes | d2135f3 | 2010-03-11 16:00:28 -0800 | [diff] [blame] | 109 | |
Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 110 | bool BzipCompressString(const string& str, chromeos::Blob* out) { |
Ben Chan | f9cb98c | 2014-09-21 18:31:30 -0700 | [diff] [blame] | 111 | return BzipString<BzipData<BzipBuffToBuffCompress>>(str, out); |
Andrew de los Reyes | d2135f3 | 2010-03-11 16:00:28 -0800 | [diff] [blame] | 112 | } |
| 113 | |
Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 114 | bool BzipDecompressString(const string& str, chromeos::Blob* out) { |
Ben Chan | f9cb98c | 2014-09-21 18:31:30 -0700 | [diff] [blame] | 115 | return BzipString<BzipData<BzipBuffToBuffDecompress>>(str, out); |
Andrew de los Reyes | d2135f3 | 2010-03-11 16:00:28 -0800 | [diff] [blame] | 116 | } |
| 117 | |
Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 118 | } // namespace chromeos_update_engine |