Home | History | Annotate | Download | only in Support
      1 //===- llvm/Support/LEB128.h - [SU]LEB128 utility functions -----*- C++ -*-===//
      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 // This file declares some utility functions for encoding SLEB128 and
     11 // ULEB128 values.
     12 //
     13 //===----------------------------------------------------------------------===//
     14 
     15 #ifndef LLVM_SUPPORT_LEB128_H
     16 #define LLVM_SUPPORT_LEB128_H
     17 
     18 #include "llvm/Support/raw_ostream.h"
     19 
     20 namespace llvm {
     21 
     22 /// Utility function to encode a SLEB128 value to an output stream.
     23 inline void encodeSLEB128(int64_t Value, raw_ostream &OS,
     24                           unsigned Padding = 0) {
     25   bool More;
     26   do {
     27     uint8_t Byte = Value & 0x7f;
     28     // NOTE: this assumes that this signed shift is an arithmetic right shift.
     29     Value >>= 7;
     30     More = !((((Value == 0 ) && ((Byte & 0x40) == 0)) ||
     31               ((Value == -1) && ((Byte & 0x40) != 0))));
     32     if (More || Padding != 0)
     33       Byte |= 0x80; // Mark this byte to show that more bytes will follow.
     34     OS << char(Byte);
     35   } while (More);
     36 
     37   // Pad with 0x80 and emit a terminating byte at the end.
     38   if (Padding != 0) {
     39     uint8_t PadValue = Value < 0 ? 0x7f : 0x00;
     40     for (; Padding != 1; --Padding)
     41       OS << char(PadValue | 0x80);
     42     OS << char(PadValue);
     43   }
     44 }
     45 
     46 /// Utility function to encode a SLEB128 value to a buffer. Returns
     47 /// the length in bytes of the encoded value.
     48 inline unsigned encodeSLEB128(int64_t Value, uint8_t *p, unsigned Padding = 0) {
     49   uint8_t *orig_p = p;
     50   bool More;
     51   do {
     52     uint8_t Byte = Value & 0x7f;
     53     // NOTE: this assumes that this signed shift is an arithmetic right shift.
     54     Value >>= 7;
     55     More = !((((Value == 0 ) && ((Byte & 0x40) == 0)) ||
     56               ((Value == -1) && ((Byte & 0x40) != 0))));
     57     if (More || Padding != 0)
     58       Byte |= 0x80; // Mark this byte to show that more bytes will follow.
     59     *p++ = Byte;
     60   } while (More);
     61 
     62   // Pad with 0x80 and emit a terminating byte at the end.
     63   if (Padding != 0) {
     64     uint8_t PadValue = Value < 0 ? 0x7f : 0x00;
     65     for (; Padding != 1; --Padding)
     66       *p++ = (PadValue | 0x80);
     67     *p++ = PadValue;
     68   }
     69   return (unsigned)(p - orig_p);
     70 }
     71 
     72 /// Utility function to encode a ULEB128 value to an output stream.
     73 inline void encodeULEB128(uint64_t Value, raw_ostream &OS,
     74                           unsigned Padding = 0) {
     75   do {
     76     uint8_t Byte = Value & 0x7f;
     77     Value >>= 7;
     78     if (Value != 0 || Padding != 0)
     79       Byte |= 0x80; // Mark this byte to show that more bytes will follow.
     80     OS << char(Byte);
     81   } while (Value != 0);
     82 
     83   // Pad with 0x80 and emit a null byte at the end.
     84   if (Padding != 0) {
     85     for (; Padding != 1; --Padding)
     86       OS << '\x80';
     87     OS << '\x00';
     88   }
     89 }
     90 
     91 /// Utility function to encode a ULEB128 value to a buffer. Returns
     92 /// the length in bytes of the encoded value.
     93 inline unsigned encodeULEB128(uint64_t Value, uint8_t *p,
     94                               unsigned Padding = 0) {
     95   uint8_t *orig_p = p;
     96   do {
     97     uint8_t Byte = Value & 0x7f;
     98     Value >>= 7;
     99     if (Value != 0 || Padding != 0)
    100       Byte |= 0x80; // Mark this byte to show that more bytes will follow.
    101     *p++ = Byte;
    102   } while (Value != 0);
    103 
    104   // Pad with 0x80 and emit a null byte at the end.
    105   if (Padding != 0) {
    106     for (; Padding != 1; --Padding)
    107       *p++ = '\x80';
    108     *p++ = '\x00';
    109   }
    110   return (unsigned)(p - orig_p);
    111 }
    112 
    113 /// Utility function to decode a ULEB128 value.
    114 inline uint64_t decodeULEB128(const uint8_t *p, unsigned *n = nullptr,
    115                               const uint8_t *end = nullptr,
    116                               const char **error = nullptr) {
    117   const uint8_t *orig_p = p;
    118   uint64_t Value = 0;
    119   unsigned Shift = 0;
    120   if (error)
    121     *error = nullptr;
    122   do {
    123     if (end && p == end) {
    124       if (error)
    125         *error = "malformed uleb128, extends past end";
    126       if (n)
    127         *n = (unsigned)(p - orig_p);
    128       return 0;
    129     }
    130     uint64_t Slice = *p & 0x7f;
    131     if (Shift >= 64 || Slice << Shift >> Shift != Slice) {
    132       if (error)
    133         *error = "uleb128 too big for uint64";
    134       if (n)
    135         *n = (unsigned)(p - orig_p);
    136       return 0;
    137     }
    138     Value += uint64_t(*p & 0x7f) << Shift;
    139     Shift += 7;
    140   } while (*p++ >= 128);
    141   if (n)
    142     *n = (unsigned)(p - orig_p);
    143   return Value;
    144 }
    145 
    146 /// Utility function to decode a SLEB128 value.
    147 inline int64_t decodeSLEB128(const uint8_t *p, unsigned *n = nullptr,
    148                              const uint8_t *end = nullptr,
    149                              const char **error = nullptr) {
    150   const uint8_t *orig_p = p;
    151   int64_t Value = 0;
    152   unsigned Shift = 0;
    153   uint8_t Byte;
    154   do {
    155     if (end && p == end) {
    156       if (error)
    157         *error = "malformed sleb128, extends past end";
    158       if (n)
    159         *n = (unsigned)(p - orig_p);
    160       return 0;
    161     }
    162     Byte = *p++;
    163     Value |= (int64_t(Byte & 0x7f) << Shift);
    164     Shift += 7;
    165   } while (Byte >= 128);
    166   // Sign extend negative numbers.
    167   if (Byte & 0x40)
    168     Value |= (-1ULL) << Shift;
    169   if (n)
    170     *n = (unsigned)(p - orig_p);
    171   return Value;
    172 }
    173 
    174 /// Utility function to get the size of the ULEB128-encoded value.
    175 extern unsigned getULEB128Size(uint64_t Value);
    176 
    177 /// Utility function to get the size of the SLEB128-encoded value.
    178 extern unsigned getSLEB128Size(int64_t Value);
    179 
    180 } // namespace llvm
    181 
    182 #endif // LLVM_SYSTEM_LEB128_H
    183