LCOV - code coverage report
Current view: top level - lib/Support - BinaryStreamReader.cpp (source / functions) Hit Total Coverage
Test: llvm-toolchain.info Lines: 66 68 97.1 %
Date: 2018-10-20 13:21:21 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       23258 : BinaryStreamReader::BinaryStreamReader(BinaryStreamRef Ref) : Stream(Ref) {}
      19             : 
      20       19502 : BinaryStreamReader::BinaryStreamReader(BinaryStream &Stream) : Stream(Stream) {}
      21             : 
      22         824 : BinaryStreamReader::BinaryStreamReader(ArrayRef<uint8_t> Data,
      23         824 :                                        endianness Endian)
      24         824 :     : Stream(Data, Endian) {}
      25             : 
      26         573 : BinaryStreamReader::BinaryStreamReader(StringRef Data, endianness Endian)
      27         573 :     : Stream(Data, Endian) {}
      28             : 
      29       26750 : Error BinaryStreamReader::readLongestContiguousChunk(
      30             :     ArrayRef<uint8_t> &Buffer) {
      31       53500 :   if (auto EC = Stream.readLongestContiguousChunk(Offset, Buffer))
      32             :     return EC;
      33       26526 :   Offset += Buffer.size();
      34             :   return Error::success();
      35             : }
      36             : 
      37      165689 : Error BinaryStreamReader::readBytes(ArrayRef<uint8_t> &Buffer, uint32_t Size) {
      38      331378 :   if (auto EC = Stream.readBytes(Offset, Size, Buffer))
      39             :     return EC;
      40      165670 :   Offset += Size;
      41             :   return Error::success();
      42             : }
      43             : 
      44       23549 : Error BinaryStreamReader::readCString(StringRef &Dest) {
      45       23549 :   uint32_t OriginalOffset = getOffset();
      46             :   uint32_t FoundOffset = 0;
      47             :   while (true) {
      48       23576 :     uint32_t ThisOffset = getOffset();
      49       23576 :     ArrayRef<uint8_t> Buffer;
      50       47152 :     if (auto EC = readLongestContiguousChunk(Buffer))
      51             :       return EC;
      52       23576 :     StringRef S(reinterpret_cast<const char *>(Buffer.begin()), Buffer.size());
      53       23576 :     size_t Pos = S.find_first_of('\0');
      54       23576 :     if (LLVM_LIKELY(Pos != StringRef::npos)) {
      55       23549 :       FoundOffset = Pos + ThisOffset;
      56       23549 :       break;
      57             :     }
      58          27 :   }
      59             :   assert(FoundOffset >= OriginalOffset);
      60             : 
      61             :   setOffset(OriginalOffset);
      62       23549 :   size_t Length = FoundOffset - OriginalOffset;
      63             : 
      64       47098 :   if (auto EC = readFixedString(Dest, Length))
      65             :     return EC;
      66             : 
      67             :   // Now set the offset back to after the null terminator.
      68       23549 :   setOffset(FoundOffset + 1);
      69             :   return Error::success();
      70             : }
      71             : 
      72         142 : Error BinaryStreamReader::readWideString(ArrayRef<UTF16> &Dest) {
      73             :   uint32_t Length = 0;
      74         142 :   uint32_t OriginalOffset = getOffset();
      75             :   const UTF16 *C;
      76             :   while (true) {
      77        2698 :     if (auto EC = readObject(C))
      78             :       return EC;
      79        1349 :     if (*C == 0x0000)
      80             :       break;
      81        1207 :     ++Length;
      82        1207 :   }
      83         142 :   uint32_t NewOffset = getOffset();
      84             :   setOffset(OriginalOffset);
      85             : 
      86         284 :   if (auto EC = readArray(Dest, Length))
      87             :     return EC;
      88             :   setOffset(NewOffset);
      89             :   return Error::success();
      90             : }
      91             : 
      92       24412 : Error BinaryStreamReader::readFixedString(StringRef &Dest, uint32_t Length) {
      93       24412 :   ArrayRef<uint8_t> Bytes;
      94       48824 :   if (auto EC = readBytes(Bytes, Length))
      95             :     return EC;
      96       24397 :   Dest = StringRef(reinterpret_cast<const char *>(Bytes.begin()), Bytes.size());
      97             :   return Error::success();
      98             : }
      99             : 
     100         291 : Error BinaryStreamReader::readStreamRef(BinaryStreamRef &Ref) {
     101         291 :   return readStreamRef(Ref, bytesRemaining());
     102             : }
     103             : 
     104        8117 : Error BinaryStreamReader::readStreamRef(BinaryStreamRef &Ref, uint32_t Length) {
     105        8117 :   if (bytesRemaining() < Length)
     106           0 :     return make_error<BinaryStreamError>(stream_error_code::stream_too_short);
     107        8117 :   Ref = Stream.slice(Offset, Length);
     108        8117 :   Offset += Length;
     109             :   return Error::success();
     110             : }
     111             : 
     112        2297 : Error BinaryStreamReader::readSubstream(BinarySubstreamRef &Stream,
     113             :                                         uint32_t Size) {
     114        2297 :   Stream.Offset = getOffset();
     115        2297 :   return readStreamRef(Stream.StreamData, Size);
     116             : }
     117             : 
     118        7595 : Error BinaryStreamReader::skip(uint32_t Amount) {
     119        7595 :   if (Amount > bytesRemaining())
     120           0 :     return make_error<BinaryStreamError>(stream_error_code::stream_too_short);
     121        7595 :   Offset += Amount;
     122             :   return Error::success();
     123             : }
     124             : 
     125        3331 : Error BinaryStreamReader::padToAlignment(uint32_t Align) {
     126        3331 :   uint32_t NewOffset = alignTo(Offset, Align);
     127        3331 :   return skip(NewOffset - Offset);
     128             : }
     129             : 
     130        9775 : uint8_t BinaryStreamReader::peek() const {
     131        9775 :   ArrayRef<uint8_t> Buffer;
     132        9775 :   auto EC = Stream.readBytes(Offset, 1, Buffer);
     133             :   assert(!EC && "Cannot peek an empty buffer!");
     134        9775 :   llvm::consumeError(std::move(EC));
     135        9775 :   return Buffer[0];
     136             : }
     137             : 
     138             : std::pair<BinaryStreamReader, BinaryStreamReader>
     139         492 : BinaryStreamReader::split(uint32_t Off) const {
     140             :   assert(getLength() >= Off);
     141             : 
     142         492 :   BinaryStreamRef First = Stream.drop_front(Offset);
     143             : 
     144         492 :   BinaryStreamRef Second = First.drop_front(Off);
     145         984 :   First = First.keep_front(Off);
     146         984 :   BinaryStreamReader W1{First};
     147         984 :   BinaryStreamReader W2{Second};
     148         492 :   return std::make_pair(W1, W2);
     149             : }

Generated by: LCOV version 1.13