| 1 | /* Capstone Disassembly Engine */cxx . . $ `<file>/utils.c`/ |
| 2 | /* By Nguyen Anh Quynh <[email protected]>, 2013-2019 */ |
| 3 | |
| 4 | #if defined(CAPSTONE_HAS_OSXKERNEL) |
| 5 | #include <Availability.h> |
| 6 | #include <libkern/libkern.h> |
| 7 | #else |
| 8 | #include <stdlib.h> |
| 9 | #endif |
| 10 | #include <string.h> |
| 11 | |
| 12 | #include "utils.h"cxx . . $ `<file>/utils.h`/ |
| 13 | |
| 14 | // count number of positive members in a list. |
| 15 | // NOTE: list must be guaranteed to end in 0 |
| 16 | unsigned int count_positivecxx . . $ count_positive(ad0aaa07bfcd0593).(const uint16_tcxx . . $ uint16_t# *list) |
| 17 | { |
| 18 | unsigned int c; |
| 19 | |
| 20 | for (c = 0; list[c] > 0; c++); |
| 21 | |
| 22 | return c; |
| 23 | } |
| 24 | |
| 25 | // count number of positive members in a list. |
| 26 | // NOTE: list must be guaranteed to end in 0 |
| 27 | unsigned int count_positive8cxx . . $ count_positive8(ca1608e2c87725e2).(const unsigned char *list) |
| 28 | { |
| 29 | unsigned int c; |
| 30 | |
| 31 | for (c = 0; list[c] > 0; c++); |
| 32 | |
| 33 | return c; |
| 34 | } |
| 35 | |
| 36 | char *cs_strdupcxx . . $ cs_strdup(a8b8e32347182212).(const char *str) |
| 37 | { |
| 38 | size_t len = strlencxx . . $ strlen(751346f8406fd082).(str) + 1; |
| 39 | void *new = cs_mem_malloccxx . . $ cs_mem_malloc.(len); |
| 40 | |
| 41 | if (new == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!) |
| 42 | return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!; |
| 43 | |
| 44 | return (char *)memmovecxx . . $ memmove(cc817cb175f282d).(new, str, len); |
| 45 | } |
| 46 | |
| 47 | // we need this since Windows doesn't have snprintf() |
| 48 | int cs_snprintfcxx . . $ cs_snprintf(ea502d73e74c754c).(char *buffer, size_t size, const char *fmt, ...) |
| 49 | { |
| 50 | int ret; |
| 51 | |
| 52 | va_listcxx . . $ va_list# ap; |
| 53 | va_startcxx . . $ `<external>/8293021631987149590/stdarg.h:51:9`!(ap, fmt); |
| 54 | ret = cs_vsnprintfcxx . . $ cs_vsnprintf.(buffer, size, fmt, ap); |
| 55 | va_endcxx . . $ `<external>/8293021631987149590/stdarg.h:53:9`!(ap); |
| 56 | |
| 57 | return ret; |
| 58 | } |
| 59 | |
| 60 | boolcxx . . $ `<external>/13015445228558014418/stdbool.h:36:9`! arr_exist8cxx . . $ arr_exist8(ece34ac52943d40).(unsigned char *arr, unsigned char max, unsigned int id) |
| 61 | { |
| 62 | int i; |
| 63 | |
| 64 | for (i = 0; i < max; i++) { |
| 65 | if (arr[i] == id) |
| 66 | return truecxx . . $ `<external>/13015445228558014418/stdbool.h:37:9`!; |
| 67 | } |
| 68 | |
| 69 | return falsecxx . . $ `<external>/13015445228558014418/stdbool.h:38:9`!; |
| 70 | } |
| 71 | |
| 72 | boolcxx . . $ `<external>/13015445228558014418/stdbool.h:36:9`! arr_existcxx . . $ arr_exist(c35293ec828d81e3).(uint16_tcxx . . $ uint16_t# *arr, unsigned char max, unsigned int id) |
| 73 | { |
| 74 | int i; |
| 75 | |
| 76 | for (i = 0; i < max; i++) { |
| 77 | if (arr[i] == id) |
| 78 | return truecxx . . $ `<external>/13015445228558014418/stdbool.h:37:9`!; |
| 79 | } |
| 80 | |
| 81 | return falsecxx . . $ `<external>/13015445228558014418/stdbool.h:38:9`!; |
| 82 | } |
| 83 | |
| 84 | // binary search for encoding in IndexType array |
| 85 | // return -1 if not found, or index if found |
| 86 | unsigned int binsearch_IndexTypeEncodingcxx . . $ binsearch_IndexTypeEncoding(fdc2fa575a0c2ee7).(const struct IndexTypecxx . . $ IndexType# *index, size_t size, uint16_tcxx . . $ uint16_t# encoding) |
| 87 | { |
| 88 | // binary searching since the index is sorted in encoding order |
| 89 | size_t left, right, m; |
| 90 | |
| 91 | right = size - 1; |
| 92 | |
| 93 | if (encoding < index[0].encodingcxx . . $ IndexType#encoding. || encoding > index[right].encodingcxx . . $ IndexType#encoding.) |
| 94 | // not found |
| 95 | return -1; |
| 96 | |
| 97 | left = 0; |
| 98 | |
| 99 | while(left <= right) { |
| 100 | m = (left + right) / 2; |
| 101 | if (encoding == index[m].encodingcxx . . $ IndexType#encoding.) { |
| 102 | return m; |
| 103 | } |
| 104 | |
| 105 | if (encoding < index[m].encodingcxx . . $ IndexType#encoding.) |
| 106 | right = m - 1; |
| 107 | else |
| 108 | left = m + 1; |
| 109 | } |
| 110 | |
| 111 | // not found |
| 112 | return -1; |
| 113 | } |
| 114 | |
| 115 | /// Reads 4 bytes in the endian order specified in MI->cs->mode. |
| 116 | uint32_tcxx . . $ uint32_t# readBytes32cxx . . $ readBytes32(baba937dc08fe273).(MCInstcxx . . $ MCInst# *MI, const uint8_tcxx . . $ uint8_t# *Bytes) |
| 117 | { |
| 118 | assertcxx . . $ `<external>/4398592474888995393/assert.h:169:12`!(MI && Bytes); |
| 119 | uint32_tcxx . . $ uint32_t# Insn; |
| 120 | if (MODE_IS_BIG_ENDIANcxx . . $ `cs_priv.h:89:9`!(MI->csh->mode)) |
| 121 | Insn = (Bytes[3] << 0) | (Bytes[2] << 8) | (Bytes[1] << 16) | |
| 122 | ((uint32_tcxx . . $ uint32_t#)Bytes[0] << 24); |
| 123 | else |
| 124 | Insn = ((uint32_tcxx . . $ uint32_t#)Bytes[3] << 24) | (Bytes[2] << 16) | |
| 125 | (Bytes[1] << 8) | (Bytes[0] << 0); |
| 126 | return Insn; |
| 127 | } |
| 128 | |
| 129 | /// Reads 2 bytes in the endian order specified in MI->cs->mode. |
| 130 | uint16_tcxx . . $ uint16_t# readBytes16cxx . . $ readBytes16(c1497bf24a45deb).(MCInstcxx . . $ MCInst# *MI, const uint8_tcxx . . $ uint8_t# *Bytes) |
| 131 | { |
| 132 | assertcxx . . $ `<external>/4398592474888995393/assert.h:169:12`!(MI && Bytes); |
| 133 | uint16_tcxx . . $ uint16_t# Insn; |
| 134 | if (MODE_IS_BIG_ENDIANcxx . . $ `cs_priv.h:89:9`!(MI->csh->mode)) |
| 135 | Insn = (Bytes[0] << 8) | Bytes[1]; |
| 136 | else |
| 137 | Insn = (Bytes[1] << 8) | Bytes[0]; |
| 138 | |
| 139 | return Insn; |
| 140 | } |