| // Copyright 2026 Google LLC |
| // |
| // 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 |
| // |
| // https://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. |
| // |
| |
| #ifndef THIRD_PARTY_CREDENTIO_JUMBF_INTERNAL_SERIALIZATION_UTILS_H_ |
| #define THIRD_PARTY_CREDENTIO_JUMBF_INTERNAL_SERIALIZATION_UTILS_H_ |
| |
| #include <climits> |
| #include <cstddef> |
| #include <cstdint> |
| |
| #include "absl/status/status.h" |
| #include "absl/status/statusor.h" |
| #include "absl/strings/cord.h" |
| #include "absl/strings/str_cat.h" |
| #include "absl/strings/string_view.h" |
| #include "riegeli/bytes/backward_writer.h" |
| #include "riegeli/endian/endian_reading.h" |
| |
| static_assert(CHAR_BIT == 8); |
| |
| namespace jumbf_internal { |
| |
| // Consumes any integer type that is supported by riegeli::ReadBigEndian. |
| template <typename T> |
| absl::StatusOr<T> ConsumeInteger(absl::string_view* sv) { |
| if (sv->length() < sizeof(T)) { |
| return absl::OutOfRangeError( |
| absl::StrCat("not enough input bytes remaining; have ", sv->length(), |
| " need ", sizeof(T))); |
| } |
| |
| T value = riegeli::ReadBigEndian<T>(sv->data()); |
| sv->remove_prefix(sizeof(T)); |
| |
| return value; |
| } |
| |
| // Consumes a null terminated string from within `sv`. The string's null |
| // terminator must be inside the usable range of `sv`. |
| absl::StatusOr<absl::string_view> ConsumeNullTerminated(absl::string_view* sv); |
| |
| // Consumes the first `n` bytes of `sv`. |
| absl::StatusOr<absl::string_view> ConsumeBytes(absl::string_view* sv, size_t n); |
| |
| // Writes `contents` to `writer`. |
| absl::Status WriteRaw(absl::Cord content, riegeli::BackwardWriter* writer); |
| |
| // Writes `content` to `writer`, appending a NUL terminator. |
| absl::Status WriteNulTerminated(absl::Cord content, |
| riegeli::BackwardWriter* writer); |
| |
| // Writes `n` to `writer`. Only types with explicit template specializations |
| // are supported. |
| template <typename T> |
| absl::Status WriteInteger(T n, riegeli::BackwardWriter* writer); |
| |
| // Writes `length` zero bytes to `writer`. |
| absl::Status WritePadding(uint64_t length, riegeli::BackwardWriter* writer); |
| |
| } // namespace jumbf_internal |
| |
| #endif // THIRD_PARTY_CREDENTIO_JUMBF_INTERNAL_SERIALIZATION_UTILS_H_ |