blob: e863b9a281bcfdb966a27d6e600fb26afec2c489 [file] [log] [blame]
Tianjie Xu65288122017-10-13 15:10:58 -07001// 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>
Alex Deymo7c4bb572018-03-19 20:25:08 +010010#include <vector>
Tianjie Xu65288122017-10-13 15:10:58 -070011
Tianjie Xu4d10c3e2017-10-26 14:02:06 -070012#include "bsdiff/brotli_decompressor.h"
Tianjie Xu65288122017-10-13 15:10:58 -070013#include "bsdiff/bspatch.h"
14#include "bsdiff/bz2_decompressor.h"
Tianjie Xu4d10c3e2017-10-26 14:02:06 -070015#include "bsdiff/constants.h"
Tianjie Xu65288122017-10-13 15:10:58 -070016#include "bsdiff/logging.h"
17#include "bsdiff/utils.h"
18
Tianjie Xu65288122017-10-13 15:10:58 -070019namespace bsdiff {
20
Tianjie Xu65288122017-10-13 15:10:58 -070021bool BsdiffPatchReader::Init(const uint8_t* patch_data, size_t patch_size) {
Tianjie Xub4cba642017-11-14 22:46:38 -080022 // File format:
23 // 0 8 magic header
Tianjie Xu65288122017-10-13 15:10:58 -070024 // 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
Tianjie Xub4cba642017-11-14 22:46:38 -080034 if (patch_size < 32) {
Tianjie Xu18480eb2017-11-29 16:21:43 -080035 LOG(ERROR) << "Too small to be a bspatch.";
Tianjie Xub4cba642017-11-14 22:46:38 -080036 return false;
37 }
Tianjie Xu65288122017-10-13 15:10:58 -070038 // Check for appropriate magic.
Tianjie Xub4cba642017-11-14 22:46:38 -080039 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: "
Tianjie Xu18480eb2017-11-29 16:21:43 -080061 << static_cast<int>(type);
Tianjie Xub4cba642017-11-14 22:46:38 -080062 return false;
63 }
64 }
65 } else {
Tianjie Xu18480eb2017-11-29 16:21:43 -080066 LOG(ERROR) << "Not a bsdiff patch.";
Tianjie Xu65288122017-10-13 15:10:58 -070067 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);
Alex Deymo7c4bb572018-03-19 20:25:08 +010074 // We already checked that the patch_size is at least 32 bytes.
Tianjie Xu65288122017-10-13 15:10:58 -070075 if ((ctrl_len < 0) || (diff_len < 0) || (signed_newsize < 0) ||
Alex Deymo7c4bb572018-03-19 20:25:08 +010076 (static_cast<int64_t>(patch_size) - 32 < ctrl_len) ||
77 (static_cast<int64_t>(patch_size) - 32 - ctrl_len < diff_len)) {
Tianjie Xu65288122017-10-13 15:10:58 -070078 LOG(ERROR) << "Corrupt patch. ctrl_len: " << ctrl_len
79 << ", data_len: " << diff_len
80 << ", new_file_size: " << signed_newsize
Tianjie Xu18480eb2017-11-29 16:21:43 -080081 << ", patch_size: " << patch_size;
Tianjie Xu65288122017-10-13 15:10:58 -070082 return false;
83 }
84 new_file_size_ = signed_newsize;
85
Tianjie Xub4cba642017-11-14 22:46:38 -080086 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_)) {
Tianjie Xu18480eb2017-11-29 16:21:43 -080090 LOG(ERROR) << "uninitialized decompressor stream";
Tianjie Xub4cba642017-11-14 22:46:38 -080091 return false;
92 }
Tianjie Xu65288122017-10-13 15:10:58 -070093
Tianjie Xub4cba642017-11-14 22:46:38 -080094 size_t offset = 32;
Tianjie Xu65288122017-10-13 15:10:58 -070095 if (!ctrl_stream_->SetInputData(const_cast<uint8_t*>(patch_data) + offset,
96 ctrl_len)) {
Tianjie Xu18480eb2017-11-29 16:21:43 -080097 LOG(ERROR) << "Failed to init ctrl stream, ctrl_len: " << ctrl_len;
Tianjie Xu65288122017-10-13 15:10:58 -070098 return false;
99 }
100
101 offset += ctrl_len;
102 if (!diff_stream_->SetInputData(const_cast<uint8_t*>(patch_data) + offset,
103 diff_len)) {
Tianjie Xu18480eb2017-11-29 16:21:43 -0800104 LOG(ERROR) << "Failed to init ctrl stream, diff_len: " << diff_len;
Tianjie Xu65288122017-10-13 15:10:58 -0700105 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
Tianjie Xu18480eb2017-11-29 16:21:43 -0800112 << ", patch_size: " << patch_size;
Tianjie Xu65288122017-10-13 15:10:58 -0700113 return false;
114 }
115 return true;
116}
117
118bool 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
Tianjie Xu18480eb2017-11-29 16:21:43 -0800134 << ", extra_size: " << extra_size;
Tianjie Xu65288122017-10-13 15:10:58 -0700135 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
148bool BsdiffPatchReader::ReadDiffStream(uint8_t* buf, size_t size) {
149 return diff_stream_->Read(buf, size);
150}
151
152bool BsdiffPatchReader::ReadExtraStream(uint8_t* buf, size_t size) {
153 return extra_stream_->Read(buf, size);
154}
155
156bool BsdiffPatchReader::Finish() {
157 if (!ctrl_stream_->Close()) {
Tianjie Xu18480eb2017-11-29 16:21:43 -0800158 LOG(ERROR) << "Failed to close the control stream";
Tianjie Xu65288122017-10-13 15:10:58 -0700159 return false;
160 }
161
162 if (!diff_stream_->Close()) {
Tianjie Xu18480eb2017-11-29 16:21:43 -0800163 LOG(ERROR) << "Failed to close the diff stream";
Tianjie Xu65288122017-10-13 15:10:58 -0700164 return false;
165 }
166
167 if (!extra_stream_->Close()) {
Tianjie Xu18480eb2017-11-29 16:21:43 -0800168 LOG(ERROR) << "Failed to close the extra stream";
Tianjie Xu65288122017-10-13 15:10:58 -0700169 return false;
170 }
171 return true;
172}
173
174} // namespace bsdiff