feat(step097): preserve B fixes for unified V15 source

This commit is contained in:
tei
2026-09-27 08:41:27 +09:00
parent b1239ac3b1
commit f3a44c7d5f
18 changed files with 711 additions and 36 deletions

View File

@@ -2,6 +2,7 @@
#include <algorithm>
#include <cmath>
#include <cstring>
#include <iterator>
#include <limits>
#include <map>
@@ -72,6 +73,84 @@ bool directionScaleIsCanonical(const float scale) {
return std::fabs(scale - canonical) <= tolerance;
}
uint32_t readU32LE(const uint8_t* data) {
return static_cast<uint32_t>(data[0]) | (static_cast<uint32_t>(data[1]) << 8) |
(static_cast<uint32_t>(data[2]) << 16) | (static_cast<uint32_t>(data[3]) << 24);
}
bool validNumericSamples(const DecodedFieldFrame& frame, const DecodedFieldPlane& plane) {
const uint64_t cells = static_cast<uint64_t>(frame.grid.width) * frame.grid.height;
size_t bytesPerSample = 0;
switch (plane.dtype) {
case FieldDType::Int16:
case FieldDType::UInt16:
bytesPerSample = 2;
break;
case FieldDType::UInt8:
case FieldDType::Int8:
bytesPerSample = 1;
break;
case FieldDType::Float32:
bytesPerSample = 4;
break;
case FieldDType::Bitset:
return false;
}
if (cells > std::numeric_limits<size_t>::max() / bytesPerSample ||
plane.storage->size() != static_cast<size_t>(cells) * bytesPerSample) return false;
for (uint64_t cell = 0; cell < cells; ++cell) {
// The shared validity mask is the protocol's no-data indicator. Invalid
// cells are deliberately ignored; their zero placeholder is not a field
// value and must not reject an otherwise usable frame.
if (((*frame.validityBits)[static_cast<size_t>(cell >> 3)] & (1u << (cell & 7u))) == 0) continue;
const uint8_t* sample = plane.storage->data();
double raw = 0.0;
switch (plane.dtype) {
case FieldDType::Int16: {
const auto offset = static_cast<size_t>(cell) * 2;
const uint16_t bits = static_cast<uint16_t>(sample[offset]) |
(static_cast<uint16_t>(sample[offset + 1]) << 8);
raw = static_cast<int16_t>(bits);
break;
}
case FieldDType::UInt16: {
const auto offset = static_cast<size_t>(cell) * 2;
raw = static_cast<uint16_t>(sample[offset]) |
(static_cast<uint16_t>(sample[offset + 1]) << 8);
break;
}
case FieldDType::UInt8:
raw = sample[static_cast<size_t>(cell)];
break;
case FieldDType::Int8:
raw = static_cast<int8_t>(sample[static_cast<size_t>(cell)]);
break;
case FieldDType::Float32: {
float value = 0.0f;
const uint32_t bits = readU32LE(sample + static_cast<size_t>(cell) * 4);
std::memcpy(&value, &bits, sizeof(value));
if (!std::isfinite(value)) return false;
raw = value;
break;
}
case FieldDType::Bitset:
return false;
}
if (!std::isfinite(raw)) return false;
const double scaledValue = raw * static_cast<double>(plane.scale);
if (!std::isfinite(scaledValue) ||
std::fabs(scaledValue) > static_cast<double>(std::numeric_limits<float>::max())) return false;
const float scaled = static_cast<float>(scaledValue);
const double decodedValue = static_cast<double>(scaled) + static_cast<double>(plane.offset);
if (!std::isfinite(decodedValue) ||
std::fabs(decodedValue) > static_cast<double>(std::numeric_limits<float>::max()) ||
!std::isfinite(static_cast<float>(decodedValue))) return false;
}
return true;
}
} // namespace
FieldProfileValidationResult FieldProfileValidator::validate(const ResolvedFieldProfileContract& contract,
@@ -163,6 +242,10 @@ FieldProfileValidationResult FieldProfileValidator::validate(const ResolvedField
if (!u || !v || u->count != v->count || u->unitID != v->unitID)
return result(FieldProfileValidationCode::InvalidVelocityGroup, "incomplete velocity group");
}
for (const auto plane : actual) {
if (plane->valueClass != FieldValueClass::Validity && !validNumericSamples(frame, *plane))
return result(FieldProfileValidationCode::InvalidFrame, "non-finite valid-cell numeric value");
}
return result(FieldProfileValidationCode::Accepted, "accepted");
}

View File

@@ -7,6 +7,7 @@
#include <rapidjson/writer.h>
#include <optional>
#include <set>
#include <string>
#include <string_view>
#include <utility>
@@ -15,24 +16,37 @@ namespace mbgl {
inline std::optional<RenderSegmentPlan> parseRenderSegmentPlanJSON(const std::string& json) {
rapidjson::Document document;
if (document.Parse(json.c_str()).HasParseError() || !document.IsObject() ||
if (document.Parse<rapidjson::kParseValidateEncodingFlag>(json.data(), json.size()).HasParseError() ||
!document.IsObject() ||
!document.HasMember("schemaVersion") || !document["schemaVersion"].IsUint() ||
document["schemaVersion"].GetUint() != 1 || !document.HasMember("segments") ||
!document["segments"].IsArray() || document.MemberCount() != 2) {
return std::nullopt;
}
std::set<std::string_view> rootKeys;
for (auto member = document.MemberBegin(); member != document.MemberEnd(); ++member) {
const std::string_view name(member->name.GetString(), member->name.GetStringLength());
if (!rootKeys.insert(name).second || (name != "schemaVersion" && name != "segments")) {
return std::nullopt;
}
}
RenderSegmentPlan plan;
for (const auto& value : document["segments"].GetArray()) {
if (!value.IsObject() || !value.HasMember("strategy") || !value["strategy"].IsString() ||
value.MemberCount() > 2) {
return std::nullopt;
}
std::set<std::string_view> segmentKeys;
for (auto member = value.MemberBegin(); member != value.MemberEnd(); ++member) {
const std::string_view name(member->name.GetString(), member->name.GetStringLength());
if (name != "strategy" && name != "startLayerId") return std::nullopt;
if (!segmentKeys.insert(name).second || (name != "strategy" && name != "startLayerId")) {
return std::nullopt;
}
}
RenderSegmentDefinition definition;
const std::string strategy = value["strategy"].GetString();
const auto& strategyValue = value["strategy"];
const std::string strategy(strategyValue.GetString(), strategyValue.GetStringLength());
if (strategy.find('\0') != std::string::npos) return std::nullopt;
if (strategy == "RETAINED") {
definition.strategy = RenderSegmentStrategy::Retained;
} else if (strategy == "CONTINUOUS") {
@@ -41,14 +55,26 @@ inline std::optional<RenderSegmentPlan> parseRenderSegmentPlanJSON(const std::st
return std::nullopt;
}
if (value.HasMember("startLayerId")) {
if (!value["startLayerId"].IsString()) return std::nullopt;
definition.startLayerId = value["startLayerId"].GetString();
const auto& startLayerId = value["startLayerId"];
if (!startLayerId.IsString()) return std::nullopt;
std::string layerId(startLayerId.GetString(), startLayerId.GetStringLength());
if (layerId.find('\0') != std::string::npos) return std::nullopt;
definition.startLayerId = std::move(layerId);
}
plan.segments.push_back(std::move(definition));
}
return plan;
}
// Android and iOS bridges use the same fail-closed conversion so malformed
// JSON always replaces (and therefore clears) a previously accepted plan.
inline RenderSegmentPlan renderSegmentPlanFromJSONOrInvalid(const std::string& json) {
if (auto plan = parseRenderSegmentPlanJSON(json)) return std::move(*plan);
RenderSegmentPlan invalid;
invalid.schemaVersion = 0;
return invalid;
}
template <typename Writer>
inline void writeOptionalString(Writer& writer, const char* key, const std::optional<std::string>& value) {
writer.Key(key);