clang-tools  14.0.0git
RIFF.h
Go to the documentation of this file.
1 //===--- RIFF.h - Binary container file format -------------------*- C++-*-===//
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 // Tools for reading and writing data in RIFF containers.
10 //
11 // A chunk consists of:
12 // - ID : char[4]
13 // - Length : uint32
14 // - Data : byte[Length]
15 // - Padding : byte[Length % 2]
16 // The semantics of a chunk's Data are determined by its ID.
17 // The format makes it easy to skip over uninteresting or unknown chunks.
18 //
19 // A RIFF file is a single chunk with ID "RIFF". Its Data is:
20 // - Type : char[4]
21 // - Chunks : chunk[]
22 //
23 // This means that a RIFF file consists of:
24 // - "RIFF" : char[4]
25 // - File length - 8 : uint32
26 // - File type : char[4]
27 // - Chunks : chunk[]
28 //
29 //===----------------------------------------------------------------------===//
30 #ifndef LLVM_CLANG_TOOLS_EXTRA_CLANGD_RIFF_H
31 #define LLVM_CLANG_TOOLS_EXTRA_CLANGD_RIFF_H
32 #include "llvm/ADT/StringRef.h"
33 #include "llvm/Support/Error.h"
34 #include "llvm/Support/ScopedPrinter.h"
35 #include <array>
36 
37 namespace clang {
38 namespace clangd {
39 namespace riff {
40 
41 // A FourCC identifies a chunk in a file, or the type of file itself.
42 using FourCC = std::array<char, 4>;
43 // Get a FourCC from a string literal, e.g. fourCC("RIFF").
44 inline constexpr FourCC fourCC(const char (&Literal)[5]) {
45  return FourCC{{Literal[0], Literal[1], Literal[2], Literal[3]}};
46 }
47 inline constexpr llvm::StringRef fourCCStr(const FourCC &Data) {
48  return llvm::StringRef(&Data[0], Data.size());
49 }
50 // A chunk is a section in a RIFF container.
51 struct Chunk {
53  llvm::StringRef Data;
54 };
55 inline bool operator==(const Chunk &L, const Chunk &R) {
56  return std::tie(L.ID, L.Data) == std::tie(R.ID, R.Data);
57 }
58 // A File is a RIFF container, which is a typed chunk sequence.
59 struct File {
61  std::vector<Chunk> Chunks;
62 };
63 inline bool operator==(const File &L, const File &R) {
64  return std::tie(L.Type, L.Chunks) == std::tie(R.Type, R.Chunks);
65 }
66 
67 // Reads a single chunk from the start of Stream.
68 // Stream is updated to exclude the consumed chunk.
69 llvm::Expected<Chunk> readChunk(llvm::StringRef &Stream);
70 
71 // Serialize a single chunk to OS.
72 llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, const Chunk &);
73 
74 // Parses a RIFF file consisting of a single RIFF chunk.
75 llvm::Expected<File> readFile(llvm::StringRef Stream);
76 
77 // Serialize a RIFF file (i.e. a single RIFF chunk) to OS.
78 llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, const File &);
79 
80 } // namespace riff
81 } // namespace clangd
82 } // namespace clang
83 #endif
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::riff::Chunk::ID
FourCC ID
Definition: RIFF.h:52
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
clang::clangd::riff::Chunk::Data
llvm::StringRef Data
Definition: RIFF.h:53
clang::clangd::riff::readFile
llvm::Expected< File > readFile(llvm::StringRef Stream)
Definition: RIFF.cpp:48
clang::clangd::riff::File
Definition: RIFF.h:59
clang::clangd::riff::operator==
bool operator==(const Chunk &L, const Chunk &R)
Definition: RIFF.h:55
clang
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Definition: ApplyReplacements.h:27
OS
llvm::raw_string_ostream OS
Definition: TraceTests.cpp:163
clang::clangd::riff::FourCC
std::array< char, 4 > FourCC
Definition: RIFF.h:42
clang::clangd::riff::File::Chunks
std::vector< Chunk > Chunks
Definition: RIFF.h:61
clang::clangd::riff::File::Type
FourCC Type
Definition: RIFF.h:60