1 //===- ByteStream.cpp - Reads stream data from a byte sequence ------------===// 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/DebugInfo/CodeView/ByteStream.h" 11 #include "llvm/DebugInfo/CodeView/CodeViewError.h" 12 #include "llvm/DebugInfo/CodeView/StreamReader.h" 13 #include <cstring> 14 15 using namespace llvm; 16 using namespace llvm::codeview; 17 18 static Error writeBytes(uint32_t Offset, ArrayRef<uint8_t> Src, 19 ArrayRef<uint8_t> Dest) { 20 return make_error<CodeViewError>(cv_error_code::operation_unsupported, 21 "ByteStream is immutable."); 22 } 23 24 static Error writeBytes(uint32_t Offset, ArrayRef<uint8_t> Src, 25 MutableArrayRef<uint8_t> Dest) { 26 if (Dest.size() < Src.size()) 27 return make_error<CodeViewError>(cv_error_code::insufficient_buffer); 28 if (Offset > Src.size() - Dest.size()) 29 return make_error<CodeViewError>(cv_error_code::insufficient_buffer); 30 31 ::memcpy(Dest.data() + Offset, Src.data(), Src.size()); 32 return Error::success(); 33 } 34 35 template <bool Writable> 36 Error ByteStream<Writable>::readBytes(uint32_t Offset, uint32_t Size, 37 ArrayRef<uint8_t> &Buffer) const { 38 if (Offset > Data.size()) 39 return make_error<CodeViewError>(cv_error_code::insufficient_buffer); 40 if (Data.size() < Size + Offset) 41 return make_error<CodeViewError>(cv_error_code::insufficient_buffer); 42 Buffer = Data.slice(Offset, Size); 43 return Error::success(); 44 } 45 46 template <bool Writable> 47 Error ByteStream<Writable>::readLongestContiguousChunk( 48 uint32_t Offset, ArrayRef<uint8_t> &Buffer) const { 49 if (Offset >= Data.size()) 50 return make_error<CodeViewError>(cv_error_code::insufficient_buffer); 51 Buffer = Data.slice(Offset); 52 return Error::success(); 53 } 54 55 template <bool Writable> 56 Error ByteStream<Writable>::writeBytes(uint32_t Offset, 57 ArrayRef<uint8_t> Buffer) const { 58 return ::writeBytes(Offset, Buffer, Data); 59 } 60 61 template <bool Writable> uint32_t ByteStream<Writable>::getLength() const { 62 return Data.size(); 63 } 64 65 template <bool Writable> Error ByteStream<Writable>::commit() const { 66 return Error::success(); 67 } 68 69 template <bool Writable> StringRef ByteStream<Writable>::str() const { 70 const char *CharData = reinterpret_cast<const char *>(Data.data()); 71 return StringRef(CharData, Data.size()); 72 } 73 74 namespace llvm { 75 namespace codeview { 76 template class ByteStream<true>; 77 template class ByteStream<false>; 78 } 79 } 80