1 //===- BinaryStreamWriter.cpp - Writes objects to a BinaryStream ----------===// 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 #include "llvm/Support/BinaryStreamWriter.h" 11 12 #include "llvm/Support/BinaryStreamError.h" 13 #include "llvm/Support/BinaryStreamReader.h" 14 #include "llvm/Support/BinaryStreamRef.h" 15 16 using namespace llvm; 17 18 BinaryStreamWriter::BinaryStreamWriter(WritableBinaryStreamRef S) 19 : Stream(S), Offset(0) {} 20 21 Error BinaryStreamWriter::writeBytes(ArrayRef<uint8_t> Buffer) { 22 if (auto EC = Stream.writeBytes(Offset, Buffer)) 23 return EC; 24 Offset += Buffer.size(); 25 return Error::success(); 26 } 27 28 Error BinaryStreamWriter::writeCString(StringRef Str) { 29 if (auto EC = writeFixedString(Str)) 30 return EC; 31 if (auto EC = writeObject('\0')) 32 return EC; 33 34 return Error::success(); 35 } 36 37 Error BinaryStreamWriter::writeFixedString(StringRef Str) { 38 return writeBytes(ArrayRef<uint8_t>(Str.bytes_begin(), Str.bytes_end())); 39 } 40 41 Error BinaryStreamWriter::writeStreamRef(BinaryStreamRef Ref) { 42 return writeStreamRef(Ref, Ref.getLength()); 43 } 44 45 Error BinaryStreamWriter::writeStreamRef(BinaryStreamRef Ref, uint32_t Length) { 46 BinaryStreamReader SrcReader(Ref.slice(0, Length)); 47 // This is a bit tricky. If we just call readBytes, we are requiring that it 48 // return us the entire stream as a contiguous buffer. There is no guarantee 49 // this can be satisfied by returning a reference straight from the buffer, as 50 // an implementation may not store all data in a single contiguous buffer. So 51 // we iterate over each contiguous chunk, writing each one in succession. 52 while (SrcReader.bytesRemaining() > 0) { 53 ArrayRef<uint8_t> Chunk; 54 if (auto EC = SrcReader.readLongestContiguousChunk(Chunk)) 55 return EC; 56 if (auto EC = writeBytes(Chunk)) 57 return EC; 58 } 59 return Error::success(); 60 } 61 62 std::pair<BinaryStreamWriter, BinaryStreamWriter> 63 BinaryStreamWriter::split(uint32_t Off) const { 64 assert(getLength() >= Off); 65 66 WritableBinaryStreamRef First = Stream.drop_front(Offset); 67 68 WritableBinaryStreamRef Second = First.drop_front(Off); 69 First = First.keep_front(Off); 70 BinaryStreamWriter W1{First}; 71 BinaryStreamWriter W2{Second}; 72 return std::make_pair(W1, W2); 73 } 74 75 Error BinaryStreamWriter::padToAlignment(uint32_t Align) { 76 uint32_t NewOffset = alignTo(Offset, Align); 77 if (NewOffset > getLength()) 78 return make_error<BinaryStreamError>(stream_error_code::stream_too_short); 79 Offset = NewOffset; 80 return Error::success(); 81 } 82