| // 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_FORMATS_RIFF_CHUNK_HEADER_H_ | |
| #define THIRD_PARTY_CREDENTIO_FORMATS_RIFF_CHUNK_HEADER_H_ | |
| #include <cstdint> | |
| #include <string> | |
| #include "absl/status/status.h" | |
| #include "absl/strings/substitute.h" | |
| #include "formats/riff/constants.h" | |
| #include "riegeli/bytes/reader.h" | |
| #include "riegeli/bytes/writer.h" | |
| #include "riegeli/endian/endian_reading.h" | |
| #include "riegeli/endian/endian_writing.h" | |
| namespace credentio { | |
| // Header of a chunk in a RIFF file. | |
| struct ChunkHeader { | |
| std::string id; // 4-character chunk ID. | |
| uint32_t data_size; // Size of the chunk data, not including the header. | |
| static uint64_t header_size() { return kRiffIdSize + sizeof(uint32_t); } | |
| absl::Status Read(riegeli::Reader& input) { | |
| if (!input.Read(kRiffIdSize, id)) { | |
| return input.StatusOrAnnotate( | |
| absl::DataLossError("kUnexpectedEof; chunk_id")); | |
| } | |
| if (!riegeli::ReadLittleEndian<uint32_t>(input, data_size)) { | |
| return input.StatusOrAnnotate( | |
| absl::DataLossError("kUnexpectedEof; chunk_size")); | |
| } | |
| return absl::OkStatus(); | |
| } | |
| absl::Status Write(riegeli::Writer& output) { | |
| if (id.size() != kRiffIdSize) { | |
| return absl::InvalidArgumentError(absl::Substitute( | |
| "chunk_id size is $0, expected $1", id.size(), kRiffIdSize)); | |
| } | |
| if (!output.Write(id)) { | |
| return output.StatusOrAnnotate( | |
| absl::DataLossError("kDataLoss; chunk_id")); | |
| } | |
| if (!riegeli::WriteLittleEndian<uint32_t>(data_size, output)) { | |
| return output.StatusOrAnnotate( | |
| absl::DataLossError("kDataLoss; chunk_size")); | |
| } | |
| return absl::OkStatus(); | |
| } | |
| uint64_t pad_size() const { return (data_size % 2 == 1) ? 1 : 0; } | |
| uint64_t chunk_size() const { | |
| return static_cast<uint64_t>(data_size) + 8 + pad_size(); | |
| } | |
| }; | |
| } // namespace credentio | |
| #endif // THIRD_PARTY_CREDENTIO_FORMATS_RIFF_CHUNK_HEADER_H_ |