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 <array>
35
36namespace clang {
37namespace clangd {
38namespace riff {
39
40// A FourCC identifies a chunk in a file, or the type of file itself.
41using FourCC = std::array<char, 4>;
42// Get a FourCC from a string literal, e.g. fourCC("RIFF").
43inline constexpr FourCC fourCC(const char (&Literal)[5]) {
44 return FourCC{._M_elems: {Literal[0], Literal[1], Literal[2], Literal[3]}};
45}
46inline constexpr llvm::StringRef fourCCStr(const FourCC &Data) {
47 return llvm::StringRef(&Data[0], Data.size());
48}
49// A chunk is a section in a RIFF container.
50struct Chunk {
51 FourCC ID;
52 llvm::StringRef Data;
53};
54inline bool operator==(const Chunk &L, const Chunk &R) {
55 return std::tie(args: L.ID, args: L.Data) == std::tie(args: R.ID, args: R.Data);
56}
57// A File is a RIFF container, which is a typed chunk sequence.
58struct File {
59 FourCC Type;
60 std::vector<Chunk> Chunks;
61};
62inline bool operator==(const File &L, const File &R) {
63 return std::tie(args: L.Type, args: L.Chunks) == std::tie(args: R.Type, args: R.Chunks);
64}
65
66// Reads a single chunk from the start of Stream.
67// Stream is updated to exclude the consumed chunk.
68llvm::Expected<Chunk> readChunk(llvm::StringRef &Stream);
69
70// Serialize a single chunk to OS.
71llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, const Chunk &);
72
73// Parses a RIFF file consisting of a single RIFF chunk.
74llvm::Expected<File> readFile(llvm::StringRef Stream);
75
76// Serialize a RIFF file (i.e. a single RIFF chunk) to OS.
77llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, const File &);
78
79} // namespace riff
80} // namespace clangd
81} // namespace clang
82#endif
83

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