blob: c2388ca94671be927583312090ddbcf8eb036fbe [file] [log] [blame]
Alex Deymoaea4c1c2015-08-19 20:24:43 -07001//
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 Reyesd2135f32010-03-11 16:00:28 -080016
Alex Deymo0bc26112015-10-19 20:54:57 -070017#include "update_engine/payload_generator/bzip.h"
Alex Deymoaab50e32014-11-10 19:55:35 -080018
Andrew de los Reyesd2135f32010-03-11 16:00:28 -080019#include <bzlib.h>
Alex Deymo0bc26112015-10-19 20:54:57 -070020#include <stdlib.h>
21
22#include <algorithm>
Alex Deymo914c4462015-06-26 17:17:05 -070023#include <limits>
24
Alex Deymo39910dc2015-11-09 17:04:30 -080025#include "update_engine/common/utils.h"
Andrew de los Reyesd2135f32010-03-11 16:00:28 -080026
Andrew de los Reyesd2135f32010-03-11 16:00:28 -080027namespace chromeos_update_engine {
28
Alex Deymo246bf212016-03-22 19:27:33 -070029bool BzipCompress(const brillo::Blob& in, brillo::Blob* out) {
Andrew de los Reyesd2135f32010-03-11 16:00:28 -080030 TEST_AND_RETURN_FALSE(out);
31 out->clear();
Alex Deymo246bf212016-03-22 19:27:33 -070032 if (in.size() == 0)
Andrew de los Reyesd2135f32010-03-11 16:00:28 -080033 return true;
Alex Deymo246bf212016-03-22 19:27:33 -070034
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 Reyesd2135f32010-03-11 16:00:28 -080038 out->resize(buf_size);
Alex Vakulenkod2779df2014-06-16 13:19:00 -070039
Alex Deymo246bf212016-03-22 19:27:33 -070040 // Try increasing buffer size until it works
Andrew de los Reyesd2135f32010-03-11 16:00:28 -080041 for (;;) {
Alex Deymo914c4462015-06-26 17:17:05 -070042 if (buf_size > std::numeric_limits<uint32_t>::max())
43 return false;
Andrew de los Reyes08c4e272010-04-15 14:02:17 -070044 uint32_t data_size = buf_size;
Alex Deymo246bf212016-03-22 19:27:33 -070045 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 Reyesd2135f32010-03-11 16:00:28 -080053 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 Vakulenkod2779df2014-06-16 13:19:00 -070059
Andrew de los Reyesd2135f32010-03-11 16:00:28 -080060 // Data didn't fit; double the buffer size.
61 buf_size *= 2;
62 out->resize(buf_size);
63 }
64}
65
Alex Vakulenkod2779df2014-06-16 13:19:00 -070066} // namespace chromeos_update_engine