| 1 | |
| 2 | |
| 3 | // Decode an unsigned LEB128 |
| 4 | // Algorithm from Appendix C of the DWARF 2, and 3, spec section "7.6" |
| 5 | unsigned long ReadULEB128(char *addr) |
| 6 | { |
| 7 | unsigned long result = 0; |
| 8 | size_t shift = 0; |
| 9 | unsigned char byte; |
| 10 | |
| 11 | do |
| 12 | { |
| 13 | byte = *addr++; |
| 14 | result |= (byte & 0x7f) << shift; |
| 15 | shift += 7; |
| 16 | } |
| 17 | while ((byte & 0x80)); |
| 18 | |
| 19 | return result; |
| 20 | } |
| 21 | |
| 22 | |
| 23 | // Decode a signed LEB128 |
| 24 | // Algorithm from Appendix C of the DWARF 2, and 3, spec section "7.6" |
| 25 | long ReadLEB128(char *addr) |
| 26 | { |
| 27 | long result = 0; |
| 28 | size_t shift = 0; |
| 29 | unsigned char byte; |
| 30 | |
| 31 | do |
| 32 | { |
| 33 | byte = *addr++; |
| 34 | result |= (byte & 0x7f) << shift; |
| 35 | shift += 7; |
| 36 | } |
| 37 | while ((byte & 0x80)); |
| 38 | |
| 39 | if ((shift < (8 * sizeof(result))) && (byte & 0x40)) |
| 40 | { |
| 41 | result |= (~0 << shift); |
| 42 | } |
| 43 | |
| 44 | return result; |
| 45 | } |
| 46 | |