LCOV - code coverage report
Current view: top level - lib/Support - BinaryStreamReader.cpp (source / functions) Hit Total Coverage
Test: llvm-toolchain.info Lines: 81 87 93.1 %
Date: 2017-09-14 15:23:50 Functions: 16 16 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : //===- BinaryStreamReader.cpp - Reads objects from a binary stream --------===//
       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/BinaryStreamReader.h"
      11             : 
      12             : #include "llvm/Support/BinaryStreamError.h"
      13             : #include "llvm/Support/BinaryStreamRef.h"
      14             : 
      15             : using namespace llvm;
      16             : using endianness = llvm::support::endianness;
      17             : 
      18       30126 : BinaryStreamReader::BinaryStreamReader(BinaryStreamRef Ref) : Stream(Ref) {}
      19             : 
      20        9448 : BinaryStreamReader::BinaryStreamReader(BinaryStream &Stream) : Stream(Stream) {}
      21             : 
      22         494 : BinaryStreamReader::BinaryStreamReader(ArrayRef<uint8_t> Data,
      23         494 :                                        endianness Endian)
      24         494 :     : Stream(Data, Endian) {}
      25             : 
      26         358 : BinaryStreamReader::BinaryStreamReader(StringRef Data, endianness Endian)
      27         358 :     : Stream(Data, Endian) {}
      28             : 
      29       17245 : Error BinaryStreamReader::readLongestContiguousChunk(
      30             :     ArrayRef<uint8_t> &Buffer) {
      31       51581 :   if (auto EC = Stream.readLongestContiguousChunk(Offset, Buffer))
      32         308 :     return EC;
      33       17091 :   Offset += Buffer.size();
      34       51273 :   return Error::success();
      35             : }
      36             : 
      37       97965 : Error BinaryStreamReader::readBytes(ArrayRef<uint8_t> &Buffer, uint32_t Size) {
      38      293878 :   if (auto EC = Stream.readBytes(Offset, Size, Buffer))
      39          34 :     return EC;
      40       97948 :   Offset += Size;
      41      293844 :   return Error::success();
      42             : }
      43             : 
      44       15556 : Error BinaryStreamReader::readCString(StringRef &Dest) {
      45       15556 :   uint32_t OriginalOffset = getOffset();
      46       15556 :   uint32_t FoundOffset = 0;
      47             :   while (true) {
      48       15583 :     uint32_t ThisOffset = getOffset();
      49       15583 :     ArrayRef<uint8_t> Buffer;
      50       46749 :     if (auto EC = readLongestContiguousChunk(Buffer))
      51           0 :       return EC;
      52       31166 :     StringRef S(reinterpret_cast<const char *>(Buffer.begin()), Buffer.size());
      53       15583 :     size_t Pos = S.find_first_of('\0');
      54       15583 :     if (LLVM_LIKELY(Pos != StringRef::npos)) {
      55       15556 :       FoundOffset = Pos + ThisOffset;
      56       15556 :       break;
      57             :     }
      58          27 :   }
      59             :   assert(FoundOffset >= OriginalOffset);
      60             : 
      61       31112 :   setOffset(OriginalOffset);
      62       15556 :   size_t Length = FoundOffset - OriginalOffset;
      63             : 
      64       46668 :   if (auto EC = readFixedString(Dest, Length))
      65           0 :     return EC;
      66             : 
      67             :   // Now set the offset back to after the null terminator.
      68       31112 :   setOffset(FoundOffset + 1);
      69       46668 :   return Error::success();
      70             : }
      71             : 
      72          96 : Error BinaryStreamReader::readWideString(ArrayRef<UTF16> &Dest) {
      73          96 :   uint32_t Length = 0;
      74          96 :   uint32_t OriginalOffset = getOffset();
      75             :   const UTF16 *C;
      76             :   while (true) {
      77        2817 :     if (auto EC = readObject(C))
      78           0 :       return EC;
      79         939 :     if (*C == 0x0000)
      80             :       break;
      81         843 :     ++Length;
      82         843 :   }
      83          96 :   uint32_t NewOffset = getOffset();
      84         192 :   setOffset(OriginalOffset);
      85             : 
      86         288 :   if (auto EC = readArray(Dest, Length))
      87           0 :     return EC;
      88         192 :   setOffset(NewOffset);
      89         288 :   return Error::success();
      90             : }
      91             : 
      92       16093 : Error BinaryStreamReader::readFixedString(StringRef &Dest, uint32_t Length) {
      93       16093 :   ArrayRef<uint8_t> Bytes;
      94       48264 :   if (auto EC = readBytes(Bytes, Length))
      95          30 :     return EC;
      96       16078 :   Dest = StringRef(reinterpret_cast<const char *>(Bytes.begin()), Bytes.size());
      97       48234 :   return Error::success();
      98             : }
      99             : 
     100         170 : Error BinaryStreamReader::readStreamRef(BinaryStreamRef &Ref) {
     101         170 :   return readStreamRef(Ref, bytesRemaining());
     102             : }
     103             : 
     104        5714 : Error BinaryStreamReader::readStreamRef(BinaryStreamRef &Ref, uint32_t Length) {
     105        5714 :   if (bytesRemaining() < Length)
     106           0 :     return make_error<BinaryStreamError>(stream_error_code::stream_too_short);
     107       17142 :   Ref = Stream.slice(Offset, Length);
     108        5714 :   Offset += Length;
     109       17142 :   return Error::success();
     110             : }
     111             : 
     112        1772 : Error BinaryStreamReader::readSubstream(BinarySubstreamRef &Stream,
     113             :                                         uint32_t Size) {
     114        1772 :   Stream.Offset = getOffset();
     115        1772 :   return readStreamRef(Stream.StreamData, Size);
     116             : }
     117             : 
     118        4420 : Error BinaryStreamReader::skip(uint32_t Amount) {
     119        4420 :   if (Amount > bytesRemaining())
     120           0 :     return make_error<BinaryStreamError>(stream_error_code::stream_too_short);
     121        4420 :   Offset += Amount;
     122       13260 :   return Error::success();
     123             : }
     124             : 
     125        1633 : Error BinaryStreamReader::padToAlignment(uint32_t Align) {
     126        3266 :   uint32_t NewOffset = alignTo(Offset, Align);
     127        1633 :   return skip(NewOffset - Offset);
     128             : }
     129             : 
     130        7753 : uint8_t BinaryStreamReader::peek() const {
     131        7753 :   ArrayRef<uint8_t> Buffer;
     132       15506 :   auto EC = Stream.readBytes(Offset, 1, Buffer);
     133             :   assert(!EC && "Cannot peek an empty buffer!");
     134       23259 :   llvm::consumeError(std::move(EC));
     135       15506 :   return Buffer[0];
     136             : }
     137             : 
     138             : std::pair<BinaryStreamReader, BinaryStreamReader>
     139         309 : BinaryStreamReader::split(uint32_t Off) const {
     140             :   assert(getLength() >= Off);
     141             : 
     142         618 :   BinaryStreamRef First = Stream.drop_front(Offset);
     143             : 
     144         618 :   BinaryStreamRef Second = First.drop_front(Off);
     145         927 :   First = First.keep_front(Off);
     146         927 :   BinaryStreamReader W1{First};
     147         927 :   BinaryStreamReader W2{Second};
     148         618 :   return std::make_pair(W1, W2);
     149             : }

Generated by: LCOV version 1.13