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