blob: a221e7861435c366324dd264f5edddaa666b57bb [file] [edit]
// Copyright 2026 Google Inc. All Rights Reserved.
//
// 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 "proto_reader.h"
#include <string.h>
#include "proto_wire.h"
#include "input_stream.h"
bool ReadVarint32(InputStream& input, uint32_t* value) {
uint64_t v;
if (!ReadVarint64(input, &v)) return false;
if (v > 0xFFFFFFFFULL) return false;
*value = static_cast<uint32_t>(v);
return true;
}
bool ReadVarint64(InputStream& input, uint64_t* value) {
uint64_t result = 0;
for (int shift = 0; shift < 70; shift += 7) {
uint8_t byte;
if (!input.Read(reinterpret_cast<char*>(&byte), 1)) return false;
uint64_t chunk = byte & 0x7f;
if (shift == 63 && chunk > 1) return false; // overflow
result |= (chunk << shift);
if (!(byte & 0x80)) {
*value = result;
return true;
}
}
return false; // too many bytes
}
bool ReadFixed32(InputStream& input, uint32_t* value) {
return input.Read(reinterpret_cast<char*>(value), sizeof(*value));
}
bool ReadFixed64(InputStream& input, uint64_t* value) {
return input.Read(reinterpret_cast<char*>(value), sizeof(*value));
}
bool ReadTag(InputStream& input, uint32_t* tag, int* field_number, int* wire_type) {
if (!ReadVarint32(input, tag)) return false;
*field_number = *tag >> kTagTypeBits;
*wire_type = *tag & kTagTypeMask;
return true;
}
bool ReadString(InputStream& input, std::string* value) {
uint32_t size;
if (!ReadVarint32(input, &size)) return false;
value->resize(size);
if (size == 0) return true;
return input.Read(&(*value)[0], size);
}
bool ReadFloat(InputStream& input, float* value) {
uint32_t raw;
if (!ReadFixed32(input, &raw)) return false;
memcpy(value, &raw, sizeof(*value));
return true;
}
bool ReadDouble(InputStream& input, double* value) {
uint64_t raw;
if (!ReadFixed64(input, &raw)) return false;
memcpy(value, &raw, sizeof(*value));
return true;
}
bool SkipField(InputStream& input, int wire_type) {
switch (wire_type) {
case WIRETYPE_VARINT: {
uint64_t dummy;
return ReadVarint64(input, &dummy);
}
case WIRETYPE_FIXED64: {
uint64_t dummy;
return ReadFixed64(input, &dummy);
}
case WIRETYPE_LENGTH_DELIMITED: {
uint32_t size;
if (!ReadVarint32(input, &size)) return false;
return input.Skip(size);
}
case WIRETYPE_FIXED32: {
uint32_t dummy;
return ReadFixed32(input, &dummy);
}
default:
return false;
}
}
bool Diff(const std::string& a, const std::string& b,
std::vector<std::string>* diffs, const std::string& prefix) {
if (a == b) return true;
diffs->push_back(prefix + ": '" + a + "' != '" + b + "'");
return false;
}
bool Diff(int32_t a, int32_t b, std::vector<std::string>* diffs,
const std::string& prefix) {
if (a == b) return true;
diffs->push_back(prefix + ": " + std::to_string(a) + " != " + std::to_string(b));
return false;
}
bool Diff(int64_t a, int64_t b, std::vector<std::string>* diffs,
const std::string& prefix) {
if (a == b) return true;
diffs->push_back(prefix + ": " + std::to_string(a) + " != " + std::to_string(b));
return false;
}
bool Diff(uint32_t a, uint32_t b, std::vector<std::string>* diffs,
const std::string& prefix) {
if (a == b) return true;
diffs->push_back(prefix + ": " + std::to_string(a) + " != " + std::to_string(b));
return false;
}
bool Diff(uint64_t a, uint64_t b, std::vector<std::string>* diffs,
const std::string& prefix) {
if (a == b) return true;
diffs->push_back(prefix + ": " + std::to_string(a) + " != " + std::to_string(b));
return false;
}
bool Diff(bool a, bool b, std::vector<std::string>* diffs,
const std::string& prefix) {
if (a == b) return true;
diffs->push_back(prefix + ": " + (a ? "true" : "false") + " != " + (b ? "true" : "false"));
return false;
}
bool Diff(float a, float b, std::vector<std::string>* diffs,
const std::string& prefix) {
if (a == b) return true;
diffs->push_back(prefix + ": " + std::to_string(a) + " != " + std::to_string(b));
return false;
}
bool Diff(double a, double b, std::vector<std::string>* diffs,
const std::string& prefix) {
if (a == b) return true;
diffs->push_back(prefix + ": " + std::to_string(a) + " != " + std::to_string(b));
return false;
}