Zachary Turner | 120faca | 2017-02-27 22:11:43 +0000 | [diff] [blame] | 1 | //===- BinaryStreamWriter.cpp - Writes objects to a BinaryStream ----------===// |
Zachary Turner | 5acb4ac | 2016-06-10 05:09:12 +0000 | [diff] [blame] | 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 | |
Zachary Turner | d9dc282 | 2017-03-02 20:52:51 +0000 | [diff] [blame] | 10 | #include "llvm/Support/BinaryStreamWriter.h" |
Zachary Turner | 5acb4ac | 2016-06-10 05:09:12 +0000 | [diff] [blame] | 11 | |
Zachary Turner | ea4e607 | 2017-03-15 22:18:53 +0000 | [diff] [blame] | 12 | #include "llvm/Support/BinaryStreamError.h" |
Zachary Turner | d9dc282 | 2017-03-02 20:52:51 +0000 | [diff] [blame] | 13 | #include "llvm/Support/BinaryStreamReader.h" |
| 14 | #include "llvm/Support/BinaryStreamRef.h" |
Zachary Turner | 5acb4ac | 2016-06-10 05:09:12 +0000 | [diff] [blame] | 15 | |
| 16 | using namespace llvm; |
Zachary Turner | 5acb4ac | 2016-06-10 05:09:12 +0000 | [diff] [blame] | 17 | |
Zachary Turner | 120faca | 2017-02-27 22:11:43 +0000 | [diff] [blame] | 18 | BinaryStreamWriter::BinaryStreamWriter(WritableBinaryStreamRef S) |
| 19 | : Stream(S), Offset(0) {} |
Zachary Turner | 5acb4ac | 2016-06-10 05:09:12 +0000 | [diff] [blame] | 20 | |
Zachary Turner | 120faca | 2017-02-27 22:11:43 +0000 | [diff] [blame] | 21 | Error BinaryStreamWriter::writeBytes(ArrayRef<uint8_t> Buffer) { |
Zachary Turner | 5acb4ac | 2016-06-10 05:09:12 +0000 | [diff] [blame] | 22 | if (auto EC = Stream.writeBytes(Offset, Buffer)) |
| 23 | return EC; |
| 24 | Offset += Buffer.size(); |
| 25 | return Error::success(); |
| 26 | } |
| 27 | |
Zachary Turner | 120faca | 2017-02-27 22:11:43 +0000 | [diff] [blame] | 28 | Error BinaryStreamWriter::writeCString(StringRef Str) { |
Zachary Turner | 5acb4ac | 2016-06-10 05:09:12 +0000 | [diff] [blame] | 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 | |
Zachary Turner | 120faca | 2017-02-27 22:11:43 +0000 | [diff] [blame] | 37 | Error BinaryStreamWriter::writeFixedString(StringRef Str) { |
| 38 | return writeBytes(ArrayRef<uint8_t>(Str.bytes_begin(), Str.bytes_end())); |
Zachary Turner | 5acb4ac | 2016-06-10 05:09:12 +0000 | [diff] [blame] | 39 | } |
| 40 | |
Zachary Turner | 120faca | 2017-02-27 22:11:43 +0000 | [diff] [blame] | 41 | Error BinaryStreamWriter::writeStreamRef(BinaryStreamRef Ref) { |
| 42 | return writeStreamRef(Ref, Ref.getLength()); |
Zachary Turner | 5acb4ac | 2016-06-10 05:09:12 +0000 | [diff] [blame] | 43 | } |
| 44 | |
Zachary Turner | 120faca | 2017-02-27 22:11:43 +0000 | [diff] [blame] | 45 | Error BinaryStreamWriter::writeStreamRef(BinaryStreamRef Ref, uint32_t Length) { |
| 46 | BinaryStreamReader SrcReader(Ref.slice(0, Length)); |
Zachary Turner | 5acb4ac | 2016-06-10 05:09:12 +0000 | [diff] [blame] | 47 | // This is a bit tricky. If we just call readBytes, we are requiring that it |
Zachary Turner | 120faca | 2017-02-27 22:11:43 +0000 | [diff] [blame] | 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. |
Zachary Turner | 5acb4ac | 2016-06-10 05:09:12 +0000 | [diff] [blame] | 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 | } |
Zachary Turner | ea4e607 | 2017-03-15 22:18:53 +0000 | [diff] [blame] | 61 | |
| 62 | Error BinaryStreamWriter::padToAlignment(uint32_t Align) { |
| 63 | uint32_t NewOffset = alignTo(Offset, Align); |
| 64 | if (NewOffset > getLength()) |
| 65 | return make_error<BinaryStreamError>(stream_error_code::stream_too_short); |
| 66 | Offset = NewOffset; |
| 67 | return Error::success(); |
| 68 | } |