11#include "llvm/Support/Endian.h"
17llvm::Expected<Chunk>
readChunk(llvm::StringRef &Stream) {
18 if (Stream.size() < 8)
19 return error(
"incomplete chunk header: {0} bytes available", Stream.size());
21 std::copy(Stream.begin(), Stream.begin() + 4,
C.ID.begin());
22 Stream = Stream.drop_front(4);
23 uint32_t Len = llvm::support::endian::read32le(Stream.take_front(4).begin());
24 Stream = Stream.drop_front(4);
25 if (Stream.size() < Len)
26 return error(
"truncated chunk: want {0}, got {1}", Len, Stream.size());
27 C.Data = Stream.take_front(Len);
28 Stream = Stream.drop_front(Len);
29 if ((Len % 2) && !Stream.empty()) {
31 return error(
"nonzero padding byte");
32 Stream = Stream.drop_front();
38 OS.write(
C.ID.data(),
C.ID.size());
40 llvm::support::endian::write32le(Size,
C.Data.size());
41 OS.write(Size,
sizeof(Size));
43 if (
C.Data.size() % 2)
48llvm::Expected<File>
readFile(llvm::StringRef Stream) {
51 return RIFF.takeError();
54 if (
RIFF->Data.size() < 4)
55 return error(
"RIFF chunk too short");
57 std::copy(
RIFF->Data.begin(),
RIFF->Data.begin() + 4, F.Type.begin());
58 for (llvm::StringRef Body =
RIFF->Data.drop_front(4); !Body.empty();)
60 F.Chunks.push_back(*
Chunk);
62 return Chunk.takeError();
69 for (
const auto &
C : F.Chunks)
70 DataLen += 4 + 4 +
C.Data.size() + (
C.Data.size() % 2);
73 llvm::support::endian::write32le(Size, DataLen);
74 OS.write(Size,
sizeof(Size));
75 OS.write(F.Type.data(), F.Type.size());
76 for (
const auto &
C : F.Chunks)
llvm::raw_ostream & operator<<(llvm::raw_ostream &OS, const Chunk &C)
llvm::Expected< File > readFile(llvm::StringRef Stream)
constexpr FourCC fourCC(const char(&Literal)[5])
constexpr llvm::StringRef fourCCStr(const FourCC &Data)
llvm::Expected< Chunk > readChunk(llvm::StringRef &Stream)
llvm::Error error(std::error_code EC, const char *Fmt, Ts &&... Vals)
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//