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