blob: 1b66cd185e60366d9eeb2d29e8b5091289249a36 [file] [edit]
// 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.
//
#include "formats/riff/create_riff.h"
#include <cstdint>
#include <string>
#include "absl/log/check.h"
#include "absl/strings/str_cat.h"
#include "absl/strings/string_view.h"
#include "riegeli/endian/endian_writing.h"
namespace credentio_riff {
std::string Chunk(absl::string_view chunk_id, absl::string_view data) {
CHECK_EQ(chunk_id.length(), 4);
// RIFF is little-endian.
char size_buf[4];
riegeli::WriteLittleEndian<uint32_t>(static_cast<uint32_t>(data.size()),
size_buf);
absl::string_view size_bytes(size_buf, 4);
// Pad byte (not included in the length value).
absl::string_view pad = (data.length() % 2 == 1) ? absl::string_view("\0", 1)
: absl::string_view();
return absl::StrCat(chunk_id, size_bytes, data, pad);
}
std::string RiffChunk(absl::string_view form, absl::string_view chunks) {
CHECK_EQ(form.length(), 4);
return Chunk("RIFF", absl::StrCat(form, chunks));
}
std::string ListChunk(absl::string_view type, absl::string_view chunks) {
CHECK_EQ(type.length(), 4);
return Chunk("LIST", absl::StrCat(type, chunks));
}
} // namespace credentio_riff