| 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 |  | 
| Alex Deymo | 0bc2611 | 2015-10-19 20:54:57 -0700 | [diff] [blame] | 17 | #include "update_engine/payload_generator/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 <bzlib.h> | 
| Alex Deymo | 0bc2611 | 2015-10-19 20:54:57 -0700 | [diff] [blame] | 20 | #include <stdlib.h> | 
 | 21 |  | 
 | 22 | #include <algorithm> | 
| Alex Deymo | 914c446 | 2015-06-26 17:17:05 -0700 | [diff] [blame] | 23 | #include <limits> | 
 | 24 |  | 
| Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 25 | #include "update_engine/common/utils.h" | 
| Andrew de los Reyes | d2135f3 | 2010-03-11 16:00:28 -0800 | [diff] [blame] | 26 |  | 
| Andrew de los Reyes | d2135f3 | 2010-03-11 16:00:28 -0800 | [diff] [blame] | 27 | namespace chromeos_update_engine { | 
 | 28 |  | 
| Alex Deymo | 246bf21 | 2016-03-22 19:27:33 -0700 | [diff] [blame] | 29 | bool BzipCompress(const brillo::Blob& in, brillo::Blob* out) { | 
| Andrew de los Reyes | d2135f3 | 2010-03-11 16:00:28 -0800 | [diff] [blame] | 30 |   TEST_AND_RETURN_FALSE(out); | 
 | 31 |   out->clear(); | 
| Alex Deymo | 246bf21 | 2016-03-22 19:27:33 -0700 | [diff] [blame] | 32 |   if (in.size() == 0) | 
| Andrew de los Reyes | d2135f3 | 2010-03-11 16:00:28 -0800 | [diff] [blame] | 33 |     return true; | 
| Alex Deymo | 246bf21 | 2016-03-22 19:27:33 -0700 | [diff] [blame] | 34 |  | 
 | 35 |   // We expect a compression ratio of about 35% with bzip2, so we start with | 
 | 36 |   // that much output space, which will then be doubled if needed. | 
 | 37 |   size_t buf_size = 40 + in.size() * 35 / 100; | 
| Andrew de los Reyes | d2135f3 | 2010-03-11 16:00:28 -0800 | [diff] [blame] | 38 |   out->resize(buf_size); | 
| Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 39 |  | 
| Alex Deymo | 246bf21 | 2016-03-22 19:27:33 -0700 | [diff] [blame] | 40 |   // Try increasing buffer size until it works | 
| Andrew de los Reyes | d2135f3 | 2010-03-11 16:00:28 -0800 | [diff] [blame] | 41 |   for (;;) { | 
| Alex Deymo | 914c446 | 2015-06-26 17:17:05 -0700 | [diff] [blame] | 42 |     if (buf_size > std::numeric_limits<uint32_t>::max()) | 
 | 43 |       return false; | 
| Andrew de los Reyes | 08c4e27 | 2010-04-15 14:02:17 -0700 | [diff] [blame] | 44 |     uint32_t data_size = buf_size; | 
| Alex Deymo | 246bf21 | 2016-03-22 19:27:33 -0700 | [diff] [blame] | 45 |     int rc = BZ2_bzBuffToBuffCompress( | 
 | 46 |         reinterpret_cast<char*>(out->data()), | 
 | 47 |         &data_size, | 
 | 48 |         reinterpret_cast<char*>(const_cast<uint8_t*>(in.data())), | 
 | 49 |         in.size(), | 
 | 50 |         9,   // Best compression | 
 | 51 |         0,   // Silent verbosity | 
 | 52 |         0);  // Default work factor | 
| Andrew de los Reyes | d2135f3 | 2010-03-11 16:00:28 -0800 | [diff] [blame] | 53 |     TEST_AND_RETURN_FALSE(rc == BZ_OUTBUFF_FULL || rc == BZ_OK); | 
 | 54 |     if (rc == BZ_OK) { | 
 | 55 |       // we're done! | 
 | 56 |       out->resize(data_size); | 
 | 57 |       return true; | 
 | 58 |     } | 
| Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 59 |  | 
| Andrew de los Reyes | d2135f3 | 2010-03-11 16:00:28 -0800 | [diff] [blame] | 60 |     // Data didn't fit; double the buffer size. | 
 | 61 |     buf_size *= 2; | 
 | 62 |     out->resize(buf_size); | 
 | 63 |   } | 
 | 64 | } | 
 | 65 |  | 
| Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 66 | }  // namespace chromeos_update_engine |