1*0b57cec5SDimitry Andric //===- BinaryStreamWriter.cpp - Writes objects to a BinaryStream ----------===// 2*0b57cec5SDimitry Andric // 3*0b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4*0b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 5*0b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6*0b57cec5SDimitry Andric // 7*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 8*0b57cec5SDimitry Andric 9*0b57cec5SDimitry Andric #include "llvm/Support/BinaryStreamWriter.h" 10*0b57cec5SDimitry Andric 11*0b57cec5SDimitry Andric #include "llvm/Support/BinaryStreamError.h" 12*0b57cec5SDimitry Andric #include "llvm/Support/BinaryStreamReader.h" 13*0b57cec5SDimitry Andric #include "llvm/Support/BinaryStreamRef.h" 14*0b57cec5SDimitry Andric #include "llvm/Support/LEB128.h" 15*0b57cec5SDimitry Andric 16*0b57cec5SDimitry Andric using namespace llvm; 17*0b57cec5SDimitry Andric 18*0b57cec5SDimitry Andric BinaryStreamWriter::BinaryStreamWriter(WritableBinaryStreamRef Ref) 19*0b57cec5SDimitry Andric : Stream(Ref) {} 20*0b57cec5SDimitry Andric 21*0b57cec5SDimitry Andric BinaryStreamWriter::BinaryStreamWriter(WritableBinaryStream &Stream) 22*0b57cec5SDimitry Andric : Stream(Stream) {} 23*0b57cec5SDimitry Andric 24*0b57cec5SDimitry Andric BinaryStreamWriter::BinaryStreamWriter(MutableArrayRef<uint8_t> Data, 25*0b57cec5SDimitry Andric llvm::support::endianness Endian) 26*0b57cec5SDimitry Andric : Stream(Data, Endian) {} 27*0b57cec5SDimitry Andric 28*0b57cec5SDimitry Andric Error BinaryStreamWriter::writeBytes(ArrayRef<uint8_t> Buffer) { 29*0b57cec5SDimitry Andric if (auto EC = Stream.writeBytes(Offset, Buffer)) 30*0b57cec5SDimitry Andric return EC; 31*0b57cec5SDimitry Andric Offset += Buffer.size(); 32*0b57cec5SDimitry Andric return Error::success(); 33*0b57cec5SDimitry Andric } 34*0b57cec5SDimitry Andric 35*0b57cec5SDimitry Andric Error BinaryStreamWriter::writeULEB128(uint64_t Value) { 36*0b57cec5SDimitry Andric uint8_t EncodedBytes[10] = {0}; 37*0b57cec5SDimitry Andric unsigned Size = encodeULEB128(Value, &EncodedBytes[0]); 38*0b57cec5SDimitry Andric return writeBytes({EncodedBytes, Size}); 39*0b57cec5SDimitry Andric } 40*0b57cec5SDimitry Andric 41*0b57cec5SDimitry Andric Error BinaryStreamWriter::writeSLEB128(int64_t Value) { 42*0b57cec5SDimitry Andric uint8_t EncodedBytes[10] = {0}; 43*0b57cec5SDimitry Andric unsigned Size = encodeSLEB128(Value, &EncodedBytes[0]); 44*0b57cec5SDimitry Andric return writeBytes({EncodedBytes, Size}); 45*0b57cec5SDimitry Andric } 46*0b57cec5SDimitry Andric 47*0b57cec5SDimitry Andric Error BinaryStreamWriter::writeCString(StringRef Str) { 48*0b57cec5SDimitry Andric if (auto EC = writeFixedString(Str)) 49*0b57cec5SDimitry Andric return EC; 50*0b57cec5SDimitry Andric if (auto EC = writeObject('\0')) 51*0b57cec5SDimitry Andric return EC; 52*0b57cec5SDimitry Andric 53*0b57cec5SDimitry Andric return Error::success(); 54*0b57cec5SDimitry Andric } 55*0b57cec5SDimitry Andric 56*0b57cec5SDimitry Andric Error BinaryStreamWriter::writeFixedString(StringRef Str) { 57*0b57cec5SDimitry Andric 58*0b57cec5SDimitry Andric return writeBytes(arrayRefFromStringRef(Str)); 59*0b57cec5SDimitry Andric } 60*0b57cec5SDimitry Andric 61*0b57cec5SDimitry Andric Error BinaryStreamWriter::writeStreamRef(BinaryStreamRef Ref) { 62*0b57cec5SDimitry Andric return writeStreamRef(Ref, Ref.getLength()); 63*0b57cec5SDimitry Andric } 64*0b57cec5SDimitry Andric 65*0b57cec5SDimitry Andric Error BinaryStreamWriter::writeStreamRef(BinaryStreamRef Ref, uint32_t Length) { 66*0b57cec5SDimitry Andric BinaryStreamReader SrcReader(Ref.slice(0, Length)); 67*0b57cec5SDimitry Andric // This is a bit tricky. If we just call readBytes, we are requiring that it 68*0b57cec5SDimitry Andric // return us the entire stream as a contiguous buffer. There is no guarantee 69*0b57cec5SDimitry Andric // this can be satisfied by returning a reference straight from the buffer, as 70*0b57cec5SDimitry Andric // an implementation may not store all data in a single contiguous buffer. So 71*0b57cec5SDimitry Andric // we iterate over each contiguous chunk, writing each one in succession. 72*0b57cec5SDimitry Andric while (SrcReader.bytesRemaining() > 0) { 73*0b57cec5SDimitry Andric ArrayRef<uint8_t> Chunk; 74*0b57cec5SDimitry Andric if (auto EC = SrcReader.readLongestContiguousChunk(Chunk)) 75*0b57cec5SDimitry Andric return EC; 76*0b57cec5SDimitry Andric if (auto EC = writeBytes(Chunk)) 77*0b57cec5SDimitry Andric return EC; 78*0b57cec5SDimitry Andric } 79*0b57cec5SDimitry Andric return Error::success(); 80*0b57cec5SDimitry Andric } 81*0b57cec5SDimitry Andric 82*0b57cec5SDimitry Andric std::pair<BinaryStreamWriter, BinaryStreamWriter> 83*0b57cec5SDimitry Andric BinaryStreamWriter::split(uint32_t Off) const { 84*0b57cec5SDimitry Andric assert(getLength() >= Off); 85*0b57cec5SDimitry Andric 86*0b57cec5SDimitry Andric WritableBinaryStreamRef First = Stream.drop_front(Offset); 87*0b57cec5SDimitry Andric 88*0b57cec5SDimitry Andric WritableBinaryStreamRef Second = First.drop_front(Off); 89*0b57cec5SDimitry Andric First = First.keep_front(Off); 90*0b57cec5SDimitry Andric BinaryStreamWriter W1{First}; 91*0b57cec5SDimitry Andric BinaryStreamWriter W2{Second}; 92*0b57cec5SDimitry Andric return std::make_pair(W1, W2); 93*0b57cec5SDimitry Andric } 94*0b57cec5SDimitry Andric 95*0b57cec5SDimitry Andric Error BinaryStreamWriter::padToAlignment(uint32_t Align) { 96*0b57cec5SDimitry Andric uint32_t NewOffset = alignTo(Offset, Align); 97*0b57cec5SDimitry Andric if (NewOffset > getLength()) 98*0b57cec5SDimitry Andric return make_error<BinaryStreamError>(stream_error_code::stream_too_short); 99*0b57cec5SDimitry Andric while (Offset < NewOffset) 100*0b57cec5SDimitry Andric if (auto EC = writeInteger('\0')) 101*0b57cec5SDimitry Andric return EC; 102*0b57cec5SDimitry Andric return Error::success(); 103*0b57cec5SDimitry Andric } 104