aboutsummaryrefslogtreecommitdiff
path: root/src/headerparser.cc
blob: 7a7b8754573a0a2083236abc6f08ee29f9db04f3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
// Copyright 2008 Google Inc.
// Author: Lincoln Smith
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include <config.h>
#include "headerparser.h"
#include "logging.h"
#include "varint_bigendian.h"
#include "vcdiff_defs.h"

namespace open_vcdiff {

// *** Methods for ParseableChunk

void ParseableChunk::Advance(size_t number_of_bytes) {
  if (number_of_bytes > UnparsedSize()) {
    VCD_DFATAL << "Internal error: position advanced by " << number_of_bytes
               << " bytes, current unparsed size " << UnparsedSize()
               << VCD_ENDL;
    position_ = end_;
    return;
  }
  position_ += number_of_bytes;
}

void ParseableChunk::SetPosition(const char* position) {
  if (position < start_) {
    VCD_DFATAL << "Internal error: new data position " << position
               << " is beyond start of data " << start_ << VCD_ENDL;
    position_ = start_;
    return;
  }
  if (position > end_) {
    VCD_DFATAL << "Internal error: new data position " << position
               << " is beyond end of data " << end_ << VCD_ENDL;
    position_ = end_;
    return;
  }
  position_ = position;
}

void ParseableChunk::FinishExcept(size_t number_of_bytes) {
  if (number_of_bytes > UnparsedSize()) {
    VCD_DFATAL << "Internal error: specified number of remaining bytes "
               << number_of_bytes << " is greater than unparsed data size "
               << UnparsedSize() << VCD_ENDL;
    Finish();
    return;
  }
  position_ = end_ - number_of_bytes;
}

// *** Methods for VCDiffHeaderParser

VCDiffHeaderParser::VCDiffHeaderParser(const char* header_start,
                                       const char* data_end)
    : parseable_chunk_(header_start, data_end - header_start),
      return_code_(RESULT_SUCCESS),
      delta_encoding_length_(0),
      delta_encoding_start_(NULL) { }

bool VCDiffHeaderParser::ParseByte(unsigned char* value) {
  if (RESULT_SUCCESS != return_code_) {
    return false;
  }
  if (parseable_chunk_.Empty()) {
    return_code_ = RESULT_END_OF_DATA;
    return false;
  }
  *value = static_cast<unsigned char>(*parseable_chunk_.UnparsedData());
  parseable_chunk_.Advance(1);
  return true;
}

bool VCDiffHeaderParser::ParseInt32(const char* variable_description,
                                    int32_t* value) {
  if (RESULT_SUCCESS != return_code_) {
    return false;
  }
  int32_t parsed_value =
      VarintBE<int32_t>::Parse(parseable_chunk_.End(),
                               parseable_chunk_.UnparsedDataAddr());
  switch (parsed_value) {
    case RESULT_ERROR:
      VCD_ERROR << "Expected " << variable_description
                << "; found invalid variable-length integer" << VCD_ENDL;
      return_code_ = RESULT_ERROR;
      return false;
    case RESULT_END_OF_DATA:
      return_code_ = RESULT_END_OF_DATA;
      return false;
    default:
      *value = parsed_value;
      return true;
  }
}

// When an unsigned 32-bit integer is expected, parse a signed 64-bit value
// instead, then check the value limit.  The uint32_t type can't be parsed
// directly because two negative values are given special meanings (RESULT_ERROR
// and RESULT_END_OF_DATA) and could not be expressed in an unsigned format.
bool VCDiffHeaderParser::ParseUInt32(const char* variable_description,
                                     uint32_t* value) {
  if (RESULT_SUCCESS != return_code_) {
    return false;
  }
  int64_t parsed_value =
      VarintBE<int64_t>::Parse(parseable_chunk_.End(),
                               parseable_chunk_.UnparsedDataAddr());
  switch (parsed_value) {
    case RESULT_ERROR:
      VCD_ERROR << "Expected " << variable_description
                << "; found invalid variable-length integer" << VCD_ENDL;
      return_code_ = RESULT_ERROR;
      return false;
    case RESULT_END_OF_DATA:
      return_code_ = RESULT_END_OF_DATA;
      return false;
    default:
      if (parsed_value > 0xFFFFFFFF) {
        VCD_ERROR << "Value of " << variable_description << "(" << parsed_value
                  << ") is too large for unsigned 32-bit integer" << VCD_ENDL;
        return_code_ = RESULT_ERROR;
        return false;
      }
      *value = static_cast<uint32_t>(parsed_value);
      return true;
  }
}

// A VCDChecksum represents an unsigned 32-bit value returned by adler32(),
// but isn't a uint32_t.
bool VCDiffHeaderParser::ParseChecksum(const char* variable_description,
                                       VCDChecksum* value) {
  uint32_t parsed_value = 0;
  if (!ParseUInt32(variable_description, &parsed_value)) {
    return false;
  }
  *value = static_cast<VCDChecksum>(parsed_value);
  return true;
}

bool VCDiffHeaderParser::ParseSize(const char* variable_description,
                                   size_t* value) {
  int32_t parsed_value = 0;
  if (!ParseInt32(variable_description, &parsed_value)) {
    return false;
  }
  *value = static_cast<size_t>(parsed_value);
  return true;
}

bool VCDiffHeaderParser::ParseSourceSegmentLengthAndPosition(
    size_t from_size,
    const char* from_boundary_name,
    const char* from_name,
    size_t* source_segment_length,
    size_t* source_segment_position) {
  // Verify the length and position values
  if (!ParseSize("source segment length", source_segment_length)) {
    return false;
  }
  // Guard against overflow by checking source length first
  if (*source_segment_length > from_size) {
    VCD_ERROR << "Source segment length (" << *source_segment_length
              << ") is larger than " << from_name << " (" << from_size
              << ")" << VCD_ENDL;
    return_code_ = RESULT_ERROR;
    return false;
  }
  if (!ParseSize("source segment position", source_segment_position)) {
    return false;
  }
  if ((*source_segment_position >= from_size) &&
      (*source_segment_length > 0)) {
    VCD_ERROR << "Source segment position (" << *source_segment_position
              << ") is past " << from_boundary_name
              << " (" << from_size << ")" << VCD_ENDL;
    return_code_ = RESULT_ERROR;
    return false;
  }
  const size_t source_segment_end = *source_segment_position +
                                    *source_segment_length;
  if (source_segment_end > from_size) {
    VCD_ERROR << "Source segment end position (" << source_segment_end
              << ") is past " << from_boundary_name
              << " (" << from_size << ")" << VCD_ENDL;
    return_code_ = RESULT_ERROR;
    return false;
  }
  return true;
}

bool VCDiffHeaderParser::ParseWinIndicatorAndSourceSegment(
    size_t dictionary_size,
    size_t decoded_target_size,
    bool allow_vcd_target,
    unsigned char* win_indicator,
    size_t* source_segment_length,
    size_t* source_segment_position) {
  if (!ParseByte(win_indicator)) {
    return false;
  }
  unsigned char source_target_flags =
      *win_indicator & (VCD_SOURCE | VCD_TARGET);
  switch (source_target_flags) {
    case VCD_SOURCE:
      return ParseSourceSegmentLengthAndPosition(dictionary_size,
                                                 "end of dictionary",
                                                 "dictionary",
                                                 source_segment_length,
                                                 source_segment_position);
    case VCD_TARGET:
      if (!allow_vcd_target) {
        VCD_ERROR << "Delta file contains VCD_TARGET flag, which is not "
                     "allowed by current decoder settings" << VCD_ENDL;
        return_code_ = RESULT_ERROR;
        return false;
      }
      return ParseSourceSegmentLengthAndPosition(decoded_target_size,
                                                 "current target position",
                                                 "target file",
                                                 source_segment_length,
                                                 source_segment_position);
    case VCD_SOURCE | VCD_TARGET:
      VCD_ERROR << "Win_Indicator must not have both VCD_SOURCE"
                   " and VCD_TARGET set" << VCD_ENDL;
      return_code_ = RESULT_ERROR;
      return false;
    default:
      return true;
  }
}

bool VCDiffHeaderParser::ParseWindowLengths(size_t* target_window_length) {
  if (delta_encoding_start_) {
    VCD_DFATAL << "Internal error: VCDiffHeaderParser::ParseWindowLengths "
                  "was called twice for the same delta window" << VCD_ENDL;
    return_code_ = RESULT_ERROR;
    return false;
  }
  if (!ParseSize("length of the delta encoding", &delta_encoding_length_)) {
    return false;
  }
  delta_encoding_start_ = UnparsedData();
  if (!ParseSize("size of the target window", target_window_length)) {
    return false;
  }
  return true;
}

const char* VCDiffHeaderParser::EndOfDeltaWindow() const {
  if (!delta_encoding_start_) {
    VCD_DFATAL << "Internal error: VCDiffHeaderParser::GetDeltaWindowEnd "
                  "was called before ParseWindowLengths" << VCD_ENDL;
    return NULL;
  }
  return delta_encoding_start_ + delta_encoding_length_;
}

bool VCDiffHeaderParser::ParseDeltaIndicator() {
  unsigned char delta_indicator;
  if (!ParseByte(&delta_indicator)) {
    return false;
  }
  if (delta_indicator & (VCD_DATACOMP | VCD_INSTCOMP | VCD_ADDRCOMP)) {
    VCD_ERROR << "Secondary compression of delta file sections "
                 "is not supported" << VCD_ENDL;
    return_code_ = RESULT_ERROR;
    return false;
  }
  return true;
}

bool VCDiffHeaderParser::ParseSectionLengths(
    bool has_checksum,
    size_t* add_and_run_data_length,
    size_t* instructions_and_sizes_length,
    size_t* addresses_length,
    VCDChecksum* checksum) {
  ParseSize("length of data for ADDs and RUNs", add_and_run_data_length);
  ParseSize("length of instructions section", instructions_and_sizes_length);
  ParseSize("length of addresses for COPYs", addresses_length);
  if (has_checksum) {
    ParseChecksum("Adler32 checksum value", checksum);
  }
  if (RESULT_SUCCESS != return_code_) {
    return false;
  }
  if (!delta_encoding_start_) {
    VCD_DFATAL << "Internal error: VCDiffHeaderParser::ParseSectionLengths "
                  "was called before ParseWindowLengths" << VCD_ENDL;
    return_code_ = RESULT_ERROR;
    return false;
  }
  const size_t delta_encoding_header_length =
      UnparsedData() - delta_encoding_start_;
  if (delta_encoding_length_ !=
          (delta_encoding_header_length +
           *add_and_run_data_length +
           *instructions_and_sizes_length +
           *addresses_length)) {
    VCD_ERROR << "The length of the delta encoding does not match "
                 "the size of the header plus the sizes of the data sections"
              << VCD_ENDL;
    return_code_ = RESULT_ERROR;
    return false;
  }
  return true;
}

}  // namespace open_vcdiff