• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2017 The Chromium OS 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 "bsdiff/patch_writer.h"
6 
7 #include <string.h>
8 
9 #include "bsdiff/brotli_compressor.h"
10 #include "bsdiff/bz2_compressor.h"
11 #include "bsdiff/constants.h"
12 #include "bsdiff/control_entry.h"
13 #include "bsdiff/logging.h"
14 
15 namespace {
16 
EncodeInt64(int64_t x,uint8_t * buf)17 void EncodeInt64(int64_t x, uint8_t* buf) {
18   uint64_t y = x < 0 ? (1ULL << 63ULL) - x : x;
19   for (int i = 0; i < 8; ++i) {
20     buf[i] = y & 0xff;
21     y /= 256;
22   }
23 }
24 
25 }  // namespace
26 
27 namespace bsdiff {
28 
BsdiffPatchWriter(const std::string & patch_filename)29 BsdiffPatchWriter::BsdiffPatchWriter(const std::string& patch_filename)
30     : patch_filename_(patch_filename), format_(BsdiffFormat::kLegacy) {
31   ctrl_stream_.reset(new BZ2Compressor());
32   diff_stream_.reset(new BZ2Compressor());
33   extra_stream_.reset(new BZ2Compressor());
34 }
35 
BsdiffPatchWriter(const std::string & patch_filename,CompressorType type,int quality)36 BsdiffPatchWriter::BsdiffPatchWriter(const std::string& patch_filename,
37                                      CompressorType type,
38                                      int quality)
39     : patch_filename_(patch_filename), format_(BsdiffFormat::kBsdf2) {
40   if (type == CompressorType::kBZ2) {
41     ctrl_stream_.reset(new BZ2Compressor());
42     diff_stream_.reset(new BZ2Compressor());
43     extra_stream_.reset(new BZ2Compressor());
44   } else if (type == CompressorType::kBrotli) {
45     ctrl_stream_.reset(new BrotliCompressor(quality));
46     diff_stream_.reset(new BrotliCompressor(quality));
47     extra_stream_.reset(new BrotliCompressor(quality));
48   }
49 }
50 
Init(size_t)51 bool BsdiffPatchWriter::Init(size_t /* new_size */) {
52   if (!(ctrl_stream_ && diff_stream_ && extra_stream_)) {
53     LOG(ERROR) << "uninitialized compressor stream";
54     return false;
55   }
56 
57   fp_ = fopen(patch_filename_.c_str(), "w");
58   if (!fp_) {
59     LOG(ERROR) << "Opening " << patch_filename_;
60     return false;
61   }
62   return true;
63 }
64 
WriteDiffStream(const uint8_t * data,size_t size)65 bool BsdiffPatchWriter::WriteDiffStream(const uint8_t* data, size_t size) {
66   return diff_stream_->Write(data, size);
67 }
68 
WriteExtraStream(const uint8_t * data,size_t size)69 bool BsdiffPatchWriter::WriteExtraStream(const uint8_t* data, size_t size) {
70   return extra_stream_->Write(data, size);
71 }
72 
AddControlEntry(const ControlEntry & entry)73 bool BsdiffPatchWriter::AddControlEntry(const ControlEntry& entry) {
74   // Generate the 24 byte control entry.
75   uint8_t buf[24];
76   EncodeInt64(entry.diff_size, buf);
77   EncodeInt64(entry.extra_size, buf + 8);
78   EncodeInt64(entry.offset_increment, buf + 16);
79   if (!ctrl_stream_->Write(buf, sizeof(buf)))
80     return false;
81   written_output_ += entry.diff_size + entry.extra_size;
82   return true;
83 }
84 
Close()85 bool BsdiffPatchWriter::Close() {
86   if (!fp_) {
87     LOG(ERROR) << "File not open.";
88     return false;
89   }
90 
91   if (!ctrl_stream_->Finish() || !diff_stream_->Finish() ||
92       !extra_stream_->Finish()) {
93     LOG(ERROR) << "Finalizing compressed streams.";
94     return false;
95   }
96 
97   auto ctrl_data = ctrl_stream_->GetCompressedData();
98   auto diff_data = diff_stream_->GetCompressedData();
99   auto extra_data = extra_stream_->GetCompressedData();
100 
101   if (!WriteHeader(ctrl_data.size(), diff_data.size()))
102     return false;
103 
104   if (fwrite(ctrl_data.data(), 1, ctrl_data.size(), fp_) != ctrl_data.size()) {
105     LOG(ERROR) << "Writing ctrl_data.";
106     return false;
107   }
108   if (fwrite(diff_data.data(), 1, diff_data.size(), fp_) != diff_data.size()) {
109     LOG(ERROR) << "Writing diff_data.";
110     return false;
111   }
112   if (fwrite(extra_data.data(), 1, extra_data.size(), fp_) !=
113       extra_data.size()) {
114     LOG(ERROR) << "Writing extra_data.";
115     return false;
116   }
117   if (fclose(fp_) != 0) {
118     LOG(ERROR) << "Closing the patch file.";
119     return false;
120   }
121   fp_ = nullptr;
122   return true;
123 }
124 
WriteHeader(uint64_t ctrl_size,uint64_t diff_size)125 bool BsdiffPatchWriter::WriteHeader(uint64_t ctrl_size, uint64_t diff_size) {
126   /* Header format is
127    * 0 8 magic header
128    * 8 8 length of compressed ctrl block
129    * 16  8 length of compressed diff block
130    * 24  8 length of new file
131    *
132    * File format is
133    * 0 32  Header
134    * 32  ??  compressed ctrl block
135    * ??  ??  compressed diff block
136    * ??  ??  compressed extra block
137    */
138   uint8_t header[32];
139   if (format_ == BsdiffFormat::kLegacy) {
140     // The magic header is "BSDIFF40" for legacy format.
141     memcpy(header, kLegacyMagicHeader, 8);
142   } else if (format_ == BsdiffFormat::kBsdf2) {
143     // The magic header for BSDF2 format:
144     // 0 5 BSDF2
145     // 5 1 compressed type for control stream
146     // 6 1 compressed type for diff stream
147     // 7 1 compressed type for extra stream
148     memcpy(header, kBSDF2MagicHeader, 5);
149     header[5] = static_cast<uint8_t>(ctrl_stream_->Type());
150     header[6] = static_cast<uint8_t>(diff_stream_->Type());
151     header[7] = static_cast<uint8_t>(extra_stream_->Type());
152   } else {
153     LOG(ERROR) << "Unsupported bsdiff format.";
154     return false;
155   }
156 
157   EncodeInt64(ctrl_size, header + 8);
158   EncodeInt64(diff_size, header + 16);
159   EncodeInt64(written_output_, header + 24);
160   if (fwrite(header, sizeof(header), 1, fp_) != 1) {
161     LOG(ERROR) << "writing to the patch file";
162     return false;
163   }
164   return true;
165 }
166 
167 }  // namespace bsdiff
168