clang-tools 10.0.0

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