Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(96)

Side by Side Diff: src/platform/update_engine/bzip.cc

Issue 855002: AU: Bzip2 utility functions that mirror Gzip utility functions. (Closed)
Patch Set: revert utils.* Created 10 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "update_engine/gzip.h"
6 #include <stdlib.h>
7 #include <algorithm>
8 #include <bzlib.h>
9 #include "update_engine/utils.h"
10
11 using std::max;
12 using std::string;
13 using std::vector;
14
15 namespace chromeos_update_engine {
16
17 int BzipBuffToBuffDecompress(char* out,
18 size_t* out_length,
19 const char* in,
20 size_t in_length) {
21 return BZ2_bzBuffToBuffDecompress(out,
22 out_length,
23 const_cast<char*>(in),
24 in_length,
25 0, // Silent verbosity
26 0); // Normal algorithm
27 }
28
29 int BzipBuffToBuffCompress(char* out,
30 size_t* out_length,
31 const char* in,
32 size_t in_length) {
33 return BZ2_bzBuffToBuffCompress(out,
34 out_length,
35 const_cast<char*>(in),
36 in_length,
37 9, // Best compression
38 0, // Silent verbosity
39 0); // Default work factor
40 }
41
42 template<int F(char* out,
43 size_t* out_length,
44 const char* in,
45 size_t in_length)>
46 bool BzipData(const char* const in,
47 const size_t in_size,
48 vector<char>* const out) {
49 TEST_AND_RETURN_FALSE(out);
50 out->clear();
51 if (in_size == 0) {
52 return true;
53 }
54 // Try increasing buffer size until it works
55 size_t buf_size = in_size;
56 out->resize(buf_size);
57
58 for (;;) {
59 size_t data_size = buf_size;
60 int rc = F(&(*out)[0], &data_size, in, in_size);
61 TEST_AND_RETURN_FALSE(rc == BZ_OUTBUFF_FULL || rc == BZ_OK);
62 if (rc == BZ_OK) {
63 // we're done!
64 out->resize(data_size);
65 return true;
66 }
67
68 // Data didn't fit; double the buffer size.
69 buf_size *= 2;
70 out->resize(buf_size);
71 }
72 }
73
74
75 bool BzipDecompress(const std::vector<char>& in, std::vector<char>* out) {
76 return BzipData<BzipBuffToBuffDecompress>(&in[0], in.size(), out);
77 }
78
79 bool BzipCompress(const std::vector<char>& in, std::vector<char>* out) {
80 return BzipData<BzipBuffToBuffCompress>(&in[0], in.size(), out);
81 }
82
83 namespace {
84 template<bool F(const char* const in,
85 const size_t in_size,
86 vector<char>* const out)>
87 bool BzipString(const std::string& str,
88 std::vector<char>* out) {
89 TEST_AND_RETURN_FALSE(out);
90 vector<char> temp;
91 TEST_AND_RETURN_FALSE(F(str.data(),
92 str.size(),
93 &temp));
94 out->clear();
95 out->insert(out->end(), temp.begin(), temp.end());
96 return true;
97 }
98 } // namespace {}
99
100 bool BzipCompressString(const std::string& str,
101 std::vector<char>* out) {
102 return BzipString<BzipData<BzipBuffToBuffCompress> >(str, out);
103 }
104
105 bool BzipDecompressString(const std::string& str,
106 std::vector<char>* out) {
107 return BzipString<BzipData<BzipBuffToBuffDecompress> >(str, out);
108 }
109
110 } // namespace chromeos_update_engine
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698