• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
writeBytes(uint32_t Offset,ArrayRef<uint8_t> Src,ArrayRef<uint8_t> Dest)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 
writeBytes(uint32_t Offset,ArrayRef<uint8_t> Src,MutableArrayRef<uint8_t> Dest)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>
readBytes(uint32_t Offset,uint32_t Size,ArrayRef<uint8_t> & Buffer) const36 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>
readLongestContiguousChunk(uint32_t Offset,ArrayRef<uint8_t> & Buffer) const47 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>
writeBytes(uint32_t Offset,ArrayRef<uint8_t> Buffer) const56 Error ByteStream<Writable>::writeBytes(uint32_t Offset,
57                                        ArrayRef<uint8_t> Buffer) const {
58   return ::writeBytes(Offset, Buffer, Data);
59 }
60 
getLength() const61 template <bool Writable> uint32_t ByteStream<Writable>::getLength() const {
62   return Data.size();
63 }
64 
commit() const65 template <bool Writable> Error ByteStream<Writable>::commit() const {
66   return Error::success();
67 }
68 
str() const69 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