clang-tools  14.0.0git
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 "support/Logger.h"
11 #include "llvm/Support/Endian.h"
12 
13 namespace clang {
14 namespace clangd {
15 namespace riff {
16 
17 llvm::Expected<Chunk> readChunk(llvm::StringRef &Stream) {
18  if (Stream.size() < 8)
19  return error("incomplete chunk header: {0} bytes available", Stream.size());
20  Chunk C;
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()) { // Skip padding byte.
30  if (Stream.front())
31  return error("nonzero padding byte");
32  Stream = Stream.drop_front();
33  }
34  return std::move(C);
35 }
36 
37 llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, const Chunk &C) {
38  OS.write(C.ID.data(), C.ID.size());
39  char Size[4];
40  llvm::support::endian::write32le(Size, C.Data.size());
41  OS.write(Size, sizeof(Size));
42  OS << C.Data;
43  if (C.Data.size() % 2)
44  OS.write(0);
45  return OS;
46 }
47 
48 llvm::Expected<File> readFile(llvm::StringRef Stream) {
49  auto RIFF = readChunk(Stream);
50  if (!RIFF)
51  return RIFF.takeError();
52  if (RIFF->ID != fourCC("RIFF"))
53  return error("not a RIFF container: root is {0}", fourCCStr(RIFF->ID));
54  if (RIFF->Data.size() < 4)
55  return error("RIFF chunk too short");
56  File F;
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();)
59  if (auto Chunk = readChunk(Body)) {
60  F.Chunks.push_back(*Chunk);
61  } else
62  return Chunk.takeError();
63  return std::move(F);
64 }
65 
66 llvm::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(Size, DataLen);
74  OS.write(Size, sizeof(Size));
75  OS.write(F.Type.data(), 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
clang::clangd::riff::Chunk
Definition: RIFF.h:51
clang::clangd::riff::fourCC
constexpr FourCC fourCC(const char(&Literal)[5])
Definition: RIFF.h:44
clang::clangd::IndexFileFormat::RIFF
@ RIFF
clang::clangd::error
llvm::Error error(std::error_code EC, const char *Fmt, Ts &&... Vals)
Definition: Logger.h:80
clang::clangd::riff::operator<<
llvm::raw_ostream & operator<<(llvm::raw_ostream &OS, const Chunk &C)
Definition: RIFF.cpp:37
clang::clangd::riff::readChunk
llvm::Expected< Chunk > readChunk(llvm::StringRef &Stream)
Definition: RIFF.cpp:17
clang::clangd::riff::fourCCStr
constexpr llvm::StringRef fourCCStr(const FourCC &Data)
Definition: RIFF.h:47
RIFF.h
clang::clangd::riff::readFile
llvm::Expected< File > readFile(llvm::StringRef Stream)
Definition: RIFF.cpp:48
clang::clangd::riff::File
Definition: RIFF.h:59
Logger.h
C
const Criteria C
Definition: FunctionCognitiveComplexityCheck.cpp:93
clang
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Definition: ApplyReplacements.h:27
OS
llvm::raw_string_ostream OS
Definition: TraceTests.cpp:163