clang-tools 8.0.0

RIFF.cpp
Go to the documentation of this file.
1 //===--- RIFF.cpp - Binary container file format --------------------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #include "RIFF.h"
11 #include "llvm/Support/Endian.h"
12
13 namespace clang {
14 namespace clangd {
15 namespace riff {
16
17 static llvm::Error makeError(const char *Msg) {
18 return llvm::createStringError(llvm::inconvertibleErrorCode(), Msg);
19 }
20
21 llvm::Expected<Chunk> readChunk(llvm::StringRef &Stream) {
22 if (Stream.size() < 8)
23 return makeError("incomplete chunk header");
24 Chunk C;
25 std::copy(Stream.begin(), Stream.begin() + 4, C.ID.begin());
26 Stream = Stream.drop_front(4);
27 uint32_t Len = llvm::support::endian::read32le(Stream.take_front(4).begin());
28 Stream = Stream.drop_front(4);
29 if (Stream.size() < Len)
30 return makeError("truncated chunk");
31 C.Data = Stream.take_front(Len);
32 Stream = Stream.drop_front(Len);
33 if (Len % 2 & !Stream.empty()) { // Skip padding byte.
34 if (Stream.front())
35 return makeError("nonzero padding byte");
36 Stream = Stream.drop_front();
37 }
38 return std::move(C);
39 }
40
41 llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, const Chunk &C) {
42 OS.write(C.ID.data(), C.ID.size());
43 char Size[4];
44 llvm::support::endian::write32le(Size, C.Data.size());
45 OS.write(Size, sizeof(Size));
46 OS << C.Data;
47 if (C.Data.size() % 2)
48 OS.write(0);
49 return OS;
50 }
51
52 llvm::Expected<File> readFile(llvm::StringRef Stream) {
53 auto RIFF = readChunk(Stream);
54 if (!RIFF)
55 return RIFF.takeError();
56 if (RIFF->ID != fourCC("RIFF"))
57 return makeError("not a RIFF container");
58 if (RIFF->Data.size() < 4)
59 return makeError("RIFF chunk too short");
60 File F;
61 std::copy(RIFF->Data.begin(), RIFF->Data.begin() + 4, F.Type.begin());
62 for (llvm::StringRef Body = RIFF->Data.drop_front(4); !Body.empty();)
63 if (auto Chunk = readChunk(Body)) {
64 F.Chunks.push_back(*Chunk);
65 } else
66 return Chunk.takeError();
67 return std::move(F);
68 }
69
70 llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, const File &F) {
71 // To avoid copies, we serialize the outer RIFF chunk "by hand".
72 size_t DataLen = 4; // Predict length of RIFF chunk data.
73 for (const auto &C : F.Chunks)
74 DataLen += 4 + 4 + C.Data.size() + (C.Data.size() % 2);
75 OS << "RIFF";
76 char Size[4];
77 llvm::support::endian::write32le(Size, DataLen);
78 OS.write(Size, sizeof(Size));
79 OS.write(F.Type.data(), F.Type.size());
80 for (const auto &C : F.Chunks)
81 OS << C;
82 return OS;
83 }
84
85 } // namespace riff
86 } // namespace clangd
87 } // namespace clang
clang::clangd::riff::Chunk::Data
llvm::StringRef Data
Definition: RIFF.h:51
clang::clangd::riff::Chunk::ID
FourCC ID
Definition: RIFF.h:50
RIFF.h
clang::clangd::riff::makeError
static llvm::Error makeError(const char *Msg)
Definition: RIFF.cpp:17
clang::clangd::riff::fourCC
constexpr FourCC fourCC(const char(&Literal)[5])
Definition: RIFF.h:45
clang::clangd::riff::readFile
llvm::Expected< File > readFile(llvm::StringRef Stream)
Definition: RIFF.cpp:52
clang::clangd::IndexFileFormat::RIFF
clang::clangd::riff::File::Chunks
std::vector< Chunk > Chunks
Definition: RIFF.h:59
clang::clangd::riff::File::Type
FourCC Type
Definition: RIFF.h:58
clang::clangd::riff::readChunk
llvm::Expected< Chunk > readChunk(llvm::StringRef &Stream)
Definition: RIFF.cpp:21
clang::clangd::riff::Chunk
Definition: RIFF.h:49
clang
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Definition: ChangeNamespace.cpp:18
clang::clangd::riff::operator<<
llvm::raw_ostream & operator<<(llvm::raw_ostream &OS, const Chunk &C)
Definition: RIFF.cpp:41
clang::clangd::riff::File
Definition: RIFF.h:57