1 //===-- ReaderPrimitives.h - Bytecode file format reading prims -*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This header defines some basic functions for reading basic primitive types
11 // from a bytecode stream.
13 //===----------------------------------------------------------------------===//
15 #ifndef READERPRIMITIVES_H
16 #define READERPRIMITIVES_H
18 #include "Support/DataTypes.h"
23 static inline bool read(const unsigned char *&Buf, const unsigned char *EndBuf,
25 if (Buf+4 > EndBuf) return true;
27 Result = *(unsigned*)Buf;
29 Result = Buf[0] | (Buf[1] << 8) | (Buf[2] << 16) | (Buf[3] << 24);
35 static inline bool read(const unsigned char *&Buf, const unsigned char *EndBuf,
37 if (Buf+8 > EndBuf) return true;
40 Result = *(uint64_t*)Buf;
42 Result = Buf[0] | (Buf[1] << 8) | (Buf[2] << 16) | (Buf[3] << 24) |
43 ((uint64_t)(Buf[4] | (Buf[5] << 8) | (Buf[6] << 16) | (Buf[7] << 24)) <<32);
49 static inline bool read(const unsigned char *&Buf, const unsigned char *EndBuf,
51 return read(Buf, EndBuf, (unsigned &)Result);
54 static inline bool read(const unsigned char *&Buf, const unsigned char *EndBuf,
56 return read(Buf, EndBuf, (uint64_t &)Result);
60 // read_vbr - Read an unsigned integer encoded in variable bitrate format.
62 static inline bool read_vbr(const unsigned char *&Buf,
63 const unsigned char *EndBuf, unsigned &Result) {
64 unsigned Shift = Result = 0;
67 Result |= (unsigned)((*Buf++) & 0x7F) << Shift;
69 } while (Buf[-1] & 0x80 && Buf < EndBuf);
74 static inline bool read_vbr(const unsigned char *&Buf,
75 const unsigned char *EndBuf, uint64_t &Result) {
76 unsigned Shift = 0; Result = 0;
79 Result |= (uint64_t)((*Buf++) & 0x7F) << Shift;
81 } while (Buf[-1] & 0x80 && Buf < EndBuf);
85 // read_vbr (signed) - Read a signed number stored in sign-magnitude format
86 static inline bool read_vbr(const unsigned char *&Buf,
87 const unsigned char *EndBuf, int &Result) {
89 if (read_vbr(Buf, EndBuf, R)) return true;
91 Result = -(int)(R >> 1);
93 Result = (int)(R >> 1);
99 static inline bool read_vbr(const unsigned char *&Buf,
100 const unsigned char *EndBuf, int64_t &Result) {
102 if (read_vbr(Buf, EndBuf, R)) return true;
104 Result = -(int64_t)(R >> 1);
106 Result = (int64_t)(R >> 1);
111 // align32 - Round up to multiple of 32 bits...
112 static inline bool align32(const unsigned char *&Buf,
113 const unsigned char *EndBuf) {
114 Buf = (const unsigned char *)((unsigned long)(Buf+3) & (~3UL));
118 static inline bool read(const unsigned char *&Buf, const unsigned char *EndBuf,
119 std::string &Result, bool Aligned = true) {
121 if (read_vbr(Buf, EndBuf, Size)) return true; // Failure reading size?
122 if (Buf+Size > EndBuf) return true; // Size invalid?
124 Result = std::string((char*)Buf, Size);
127 if (Aligned) // If we should stay aligned do so...
128 if (align32(Buf, EndBuf)) return true; // Failure aligning?
133 static inline bool input_data(const unsigned char *&Buf,
134 const unsigned char *EndBuf,
135 void *Ptr, void *End, bool Align = false) {
136 unsigned char *Start = (unsigned char *)Ptr;
137 unsigned Amount = (unsigned char *)End - Start;
138 if (Buf+Amount > EndBuf) return true;
140 std::copy(Buf, Buf+Amount, Start);
143 unsigned char *E = (unsigned char *)End;
148 if (Align) return align32(Buf, EndBuf);
152 } // End llvm namespace