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