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 "support/Logger.h"
11#include "llvm/Support/Endian.h"
12
13namespace clang {
14namespace clangd {
15namespace riff {
16
17llvm::Expected<Chunk> readChunk(llvm::StringRef &Stream) {
18 if (Stream.size() < 8)
19 return error(Fmt: "incomplete chunk header: {0} bytes available", Vals: Stream.size());
20 Chunk C;
21 std::copy(first: Stream.begin(), last: Stream.begin() + 4, result: C.ID.begin());
22 Stream = Stream.drop_front(N: 4);
23 uint32_t Len = llvm::support::endian::read32le(P: Stream.take_front(N: 4).begin());
24 Stream = Stream.drop_front(N: 4);
25 if (Stream.size() < Len)
26 return error(Fmt: "truncated chunk: want {0}, got {1}", Vals&: Len, Vals: Stream.size());
27 C.Data = Stream.take_front(N: Len);
28 Stream = Stream.drop_front(N: Len);
29 if ((Len % 2) && !Stream.empty()) { // Skip padding byte.
30 if (Stream.front())
31 return error(Fmt: "nonzero padding byte");
32 Stream = Stream.drop_front();
33 }
34 return std::move(C);
35}
36
37llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, const Chunk &C) {
38 OS.write(Ptr: C.ID.data(), Size: C.ID.size());
39 char Size[4];
40 llvm::support::endian::write32le(P: Size, V: C.Data.size());
41 OS.write(Ptr: Size, Size: sizeof(Size));
42 OS << C.Data;
43 if (C.Data.size() % 2)
44 OS.write(C: 0);
45 return OS;
46}
47
48llvm::Expected<File> readFile(llvm::StringRef Stream) {
49 auto RIFF = readChunk(Stream);
50 if (!RIFF)
51 return RIFF.takeError();
52 if (RIFF->ID != fourCC(Literal: "RIFF"))
53 return error(Fmt: "not a RIFF container: root is {0}", Vals: fourCCStr(Data: RIFF->ID));
54 if (RIFF->Data.size() < 4)
55 return error(Fmt: "RIFF chunk too short");
56 File F;
57 std::copy(first: RIFF->Data.begin(), last: RIFF->Data.begin() + 4, result: F.Type.begin());
58 for (llvm::StringRef Body = RIFF->Data.drop_front(N: 4); !Body.empty();)
59 if (auto Chunk = readChunk(Stream&: Body)) {
60 F.Chunks.push_back(x: *Chunk);
61 } else
62 return Chunk.takeError();
63 return std::move(F);
64}
65
66llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, const File &F) {
67 // To avoid copies, we serialize the outer RIFF chunk "by hand".
68 size_t DataLen = 4; // Predict length of RIFF chunk data.
69 for (const auto &C : F.Chunks)
70 DataLen += 4 + 4 + C.Data.size() + (C.Data.size() % 2);
71 OS << "RIFF";
72 char Size[4];
73 llvm::support::endian::write32le(P: Size, V: DataLen);
74 OS.write(Ptr: Size, Size: sizeof(Size));
75 OS.write(Ptr: F.Type.data(), Size: F.Type.size());
76 for (const auto &C : F.Chunks)
77 OS << C;
78 return OS;
79}
80
81} // namespace riff
82} // namespace clangd
83} // namespace clang
84

source code of clang-tools-extra/clangd/RIFF.cpp