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