• 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_reader.h"
6 
7 #include <string.h>
8 
9 #include <limits>
10 #include <vector>
11 
12 #include "bsdiff/brotli_decompressor.h"
13 #include "bsdiff/bspatch.h"
14 #include "bsdiff/bz2_decompressor.h"
15 #include "bsdiff/constants.h"
16 #include "bsdiff/logging.h"
17 #include "bsdiff/utils.h"
18 
19 namespace bsdiff {
20 
Init(const uint8_t * patch_data,size_t patch_size)21 bool BsdiffPatchReader::Init(const uint8_t* patch_data, size_t patch_size) {
22   //   File format:
23   //   0       8    magic header
24   //   8       8    X
25   //   16      8    Y
26   //   24      8    new_file_size
27   //   32      X    compressed control block
28   //   32+X    Y    compressed diff block
29   //   32+X+Y  ???  compressed extra block
30   // with control block a set of triples (x,y,z) meaning "add x bytes
31   // from oldfile to x bytes from the diff block; copy y bytes from the
32   // extra block; seek forwards in oldfile by z bytes".
33 
34   if (patch_size < 32) {
35     LOG(ERROR) << "Too small to be a bspatch.";
36     return false;
37   }
38   // Check for appropriate magic.
39   std::vector<CompressorType> compression_type;
40   if (memcmp(patch_data, kLegacyMagicHeader, 8) == 0) {
41     // The magic header is "BSDIFF40" for legacy format.
42     compression_type = {CompressorType::kBZ2, CompressorType::kBZ2,
43                         CompressorType::kBZ2};
44   } else if (memcmp(patch_data, kBSDF2MagicHeader, 5) == 0) {
45     // The magic header for BSDF2 format:
46     // 0 5 BSDF2
47     // 5 1 compressed type for control stream
48     // 6 1 compressed type for diff stream
49     // 7 1 compressed type for extra stream
50     for (size_t i = 5; i < 8; i++) {
51       uint8_t type = patch_data[i];
52       switch (type) {
53         case static_cast<uint8_t>(CompressorType::kBZ2):
54           compression_type.push_back(CompressorType::kBZ2);
55           break;
56         case static_cast<uint8_t>(CompressorType::kBrotli):
57           compression_type.push_back(CompressorType::kBrotli);
58           break;
59         default:
60           LOG(ERROR) << "Unsupported compression type: "
61                      << static_cast<int>(type);
62           return false;
63       }
64     }
65   } else {
66     LOG(ERROR) << "Not a bsdiff patch.";
67     return false;
68   }
69 
70   // Read lengths from header.
71   int64_t ctrl_len = ParseInt64(patch_data + 8);
72   int64_t diff_len = ParseInt64(patch_data + 16);
73   int64_t signed_newsize = ParseInt64(patch_data + 24);
74   // We already checked that the patch_size is at least 32 bytes.
75   if ((ctrl_len < 0) || (diff_len < 0) || (signed_newsize < 0) ||
76       (static_cast<int64_t>(patch_size) - 32 < ctrl_len) ||
77       (static_cast<int64_t>(patch_size) - 32 - ctrl_len < diff_len)) {
78     LOG(ERROR) << "Corrupt patch.  ctrl_len: " << ctrl_len
79                << ", data_len: " << diff_len
80                << ", new_file_size: " << signed_newsize
81                << ", patch_size: " << patch_size;
82     return false;
83   }
84   new_file_size_ = signed_newsize;
85 
86   ctrl_stream_ = CreateDecompressor(compression_type[0]);
87   diff_stream_ = CreateDecompressor(compression_type[1]);
88   extra_stream_ = CreateDecompressor(compression_type[2]);
89   if (!(ctrl_stream_ && diff_stream_ && extra_stream_)) {
90     LOG(ERROR) << "uninitialized decompressor stream";
91     return false;
92   }
93 
94   size_t offset = 32;
95   if (!ctrl_stream_->SetInputData(const_cast<uint8_t*>(patch_data) + offset,
96                                   ctrl_len)) {
97     LOG(ERROR) << "Failed to init ctrl stream, ctrl_len: " << ctrl_len;
98     return false;
99   }
100 
101   offset += ctrl_len;
102   if (!diff_stream_->SetInputData(const_cast<uint8_t*>(patch_data) + offset,
103                                   diff_len)) {
104     LOG(ERROR) << "Failed to init ctrl stream, diff_len: " << diff_len;
105     return false;
106   }
107 
108   offset += diff_len;
109   if (!extra_stream_->SetInputData(const_cast<uint8_t*>(patch_data) + offset,
110                                    patch_size - offset)) {
111     LOG(ERROR) << "Failed to init extra stream, extra_offset: " << offset
112                << ", patch_size: " << patch_size;
113     return false;
114   }
115   return true;
116 }
117 
ParseControlEntry(ControlEntry * control_entry)118 bool BsdiffPatchReader::ParseControlEntry(ControlEntry* control_entry) {
119   if (!control_entry)
120     return false;
121 
122   uint8_t buf[8];
123   if (!ctrl_stream_->Read(buf, 8))
124     return false;
125   int64_t diff_size = ParseInt64(buf);
126 
127   if (!ctrl_stream_->Read(buf, 8))
128     return false;
129   int64_t extra_size = ParseInt64(buf);
130 
131   // Sanity check.
132   if (diff_size < 0 || extra_size < 0) {
133     LOG(ERROR) << "Corrupt patch; diff_size: " << diff_size
134                << ", extra_size: " << extra_size;
135     return false;
136   }
137 
138   control_entry->diff_size = diff_size;
139   control_entry->extra_size = extra_size;
140 
141   if (!ctrl_stream_->Read(buf, 8))
142     return false;
143   control_entry->offset_increment = ParseInt64(buf);
144 
145   return true;
146 }
147 
ReadDiffStream(uint8_t * buf,size_t size)148 bool BsdiffPatchReader::ReadDiffStream(uint8_t* buf, size_t size) {
149   return diff_stream_->Read(buf, size);
150 }
151 
ReadExtraStream(uint8_t * buf,size_t size)152 bool BsdiffPatchReader::ReadExtraStream(uint8_t* buf, size_t size) {
153   return extra_stream_->Read(buf, size);
154 }
155 
Finish()156 bool BsdiffPatchReader::Finish() {
157   if (!ctrl_stream_->Close()) {
158     LOG(ERROR) << "Failed to close the control stream";
159     return false;
160   }
161 
162   if (!diff_stream_->Close()) {
163     LOG(ERROR) << "Failed to close the diff stream";
164     return false;
165   }
166 
167   if (!extra_stream_->Close()) {
168     LOG(ERROR) << "Failed to close the extra stream";
169     return false;
170   }
171   return true;
172 }
173 
174 }  // namespace bsdiff
175