blob: 8423580f9b46d73871c9a6ee780c10df72ed6113 [file] [log] [blame]
Sam McCall50f36312018-09-04 16:16:50 +00001//===--- RIFF.cpp - Binary container file format --------------------------===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Sam McCall50f36312018-09-04 16:16:50 +00006//
7//===----------------------------------------------------------------------===//
8
9#include "RIFF.h"
Sam McCall30667c92020-07-08 21:49:38 +020010#include "support/Logger.h"
Sam McCall50f36312018-09-04 16:16:50 +000011#include "llvm/Support/Endian.h"
12
Sam McCall50f36312018-09-04 16:16:50 +000013namespace clang {
14namespace clangd {
15namespace riff {
16
Ilya Biryukovf2001aa2019-01-07 15:45:19 +000017llvm::Expected<Chunk> readChunk(llvm::StringRef &Stream) {
Sam McCall50f36312018-09-04 16:16:50 +000018 if (Stream.size() < 8)
Sam McCall30667c92020-07-08 21:49:38 +020019 return error("incomplete chunk header: {0} bytes available", Stream.size());
Sam McCall50f36312018-09-04 16:16:50 +000020 Chunk C;
21 std::copy(Stream.begin(), Stream.begin() + 4, C.ID.begin());
22 Stream = Stream.drop_front(4);
Ilya Biryukovf2001aa2019-01-07 15:45:19 +000023 uint32_t Len = llvm::support::endian::read32le(Stream.take_front(4).begin());
Sam McCall50f36312018-09-04 16:16:50 +000024 Stream = Stream.drop_front(4);
25 if (Stream.size() < Len)
Sam McCall30667c92020-07-08 21:49:38 +020026 return error("truncated chunk: want {0}, got {1}", Len, Stream.size());
Sam McCall50f36312018-09-04 16:16:50 +000027 C.Data = Stream.take_front(Len);
28 Stream = Stream.drop_front(Len);
Simon Pilgrim73a6a362020-08-11 11:36:21 +010029 if ((Len % 2) && !Stream.empty()) { // Skip padding byte.
Sam McCall50f36312018-09-04 16:16:50 +000030 if (Stream.front())
Sam McCall30667c92020-07-08 21:49:38 +020031 return error("nonzero padding byte");
Sam McCall50f36312018-09-04 16:16:50 +000032 Stream = Stream.drop_front();
33 }
Sam McCalld85264b2018-09-05 07:52:49 +000034 return std::move(C);
Simon Pilgrim008da022018-09-11 13:42:15 +000035}
Sam McCall50f36312018-09-04 16:16:50 +000036
Ilya Biryukovf2001aa2019-01-07 15:45:19 +000037llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, const Chunk &C) {
Sam McCalld85264b2018-09-05 07:52:49 +000038 OS.write(C.ID.data(), C.ID.size());
Sam McCall50f36312018-09-04 16:16:50 +000039 char Size[4];
Ilya Biryukovf2001aa2019-01-07 15:45:19 +000040 llvm::support::endian::write32le(Size, C.Data.size());
Sam McCall50f36312018-09-04 16:16:50 +000041 OS.write(Size, sizeof(Size));
42 OS << C.Data;
43 if (C.Data.size() % 2)
44 OS.write(0);
45 return OS;
46}
47
Ilya Biryukovf2001aa2019-01-07 15:45:19 +000048llvm::Expected<File> readFile(llvm::StringRef Stream) {
Sam McCall50f36312018-09-04 16:16:50 +000049 auto RIFF = readChunk(Stream);
50 if (!RIFF)
51 return RIFF.takeError();
52 if (RIFF->ID != fourCC("RIFF"))
Sam McCall30667c92020-07-08 21:49:38 +020053 return error("not a RIFF container: root is {0}", fourCCStr(RIFF->ID));
Sam McCall50f36312018-09-04 16:16:50 +000054 if (RIFF->Data.size() < 4)
Sam McCall30667c92020-07-08 21:49:38 +020055 return error("RIFF chunk too short");
Sam McCall50f36312018-09-04 16:16:50 +000056 File F;
57 std::copy(RIFF->Data.begin(), RIFF->Data.begin() + 4, F.Type.begin());
Ilya Biryukovf2001aa2019-01-07 15:45:19 +000058 for (llvm::StringRef Body = RIFF->Data.drop_front(4); !Body.empty();)
Sam McCall50f36312018-09-04 16:16:50 +000059 if (auto Chunk = readChunk(Body)) {
60 F.Chunks.push_back(*Chunk);
61 } else
62 return Chunk.takeError();
Sam McCalld85264b2018-09-05 07:52:49 +000063 return std::move(F);
Sam McCall50f36312018-09-04 16:16:50 +000064}
65
Ilya Biryukovf2001aa2019-01-07 15:45:19 +000066llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, const File &F) {
Sam McCall50f36312018-09-04 16:16:50 +000067 // 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];
Ilya Biryukovf2001aa2019-01-07 15:45:19 +000073 llvm::support::endian::write32le(Size, DataLen);
Sam McCall50f36312018-09-04 16:16:50 +000074 OS.write(Size, sizeof(Size));
Sam McCalld85264b2018-09-05 07:52:49 +000075 OS.write(F.Type.data(), F.Type.size());
Sam McCall50f36312018-09-04 16:16:50 +000076 for (const auto &C : F.Chunks)
77 OS << C;
78 return OS;
79}
80
81} // namespace riff
82} // namespace clangd
83} // namespace clang