2014-04-29 03:21:04 +00:00
|
|
|
/* Capstone Disassembly Engine */
|
2019-02-26 07:19:51 +00:00
|
|
|
/* By Nguyen Anh Quynh <aquynh@gmail.com>, 2013-2019 */
|
2013-11-27 04:11:31 +00:00
|
|
|
|
2015-04-09 17:28:19 +00:00
|
|
|
#if defined(CAPSTONE_HAS_OSXKERNEL)
|
2018-06-14 21:12:26 +00:00
|
|
|
#include <Availability.h>
|
2015-04-09 17:28:19 +00:00
|
|
|
#include <libkern/libkern.h>
|
|
|
|
#else
|
2014-01-03 09:08:58 +00:00
|
|
|
#include <stdlib.h>
|
2015-04-09 17:28:19 +00:00
|
|
|
#endif
|
2013-11-27 04:11:31 +00:00
|
|
|
#include <string.h>
|
2023-11-15 04:12:14 +00:00
|
|
|
#include <ctype.h>
|
2013-11-27 04:11:31 +00:00
|
|
|
|
|
|
|
#include "utils.h"
|
|
|
|
|
2013-12-03 03:10:26 +00:00
|
|
|
// count number of positive members in a list.
|
|
|
|
// NOTE: list must be guaranteed to end in 0
|
2018-07-20 04:36:50 +00:00
|
|
|
unsigned int count_positive(const uint16_t *list)
|
2015-03-25 07:02:13 +00:00
|
|
|
{
|
|
|
|
unsigned int c;
|
|
|
|
|
|
|
|
for (c = 0; list[c] > 0; c++);
|
|
|
|
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
|
|
|
// count number of positive members in a list.
|
|
|
|
// NOTE: list must be guaranteed to end in 0
|
2018-07-20 04:36:50 +00:00
|
|
|
unsigned int count_positive8(const unsigned char *list)
|
2013-12-03 03:10:26 +00:00
|
|
|
{
|
|
|
|
unsigned int c;
|
|
|
|
|
|
|
|
for (c = 0; list[c] > 0; c++);
|
|
|
|
|
|
|
|
return c;
|
|
|
|
}
|
2014-01-15 10:27:01 +00:00
|
|
|
|
|
|
|
char *cs_strdup(const char *str)
|
|
|
|
{
|
2022-10-11 16:56:03 +00:00
|
|
|
size_t len = strlen(str) + 1;
|
2014-01-15 10:27:01 +00:00
|
|
|
void *new = cs_mem_malloc(len);
|
|
|
|
|
|
|
|
if (new == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
return (char *)memmove(new, str, len);
|
|
|
|
}
|
2014-08-26 07:57:04 +00:00
|
|
|
|
2018-09-17 12:54:00 +00:00
|
|
|
// we need this since Windows doesn't have snprintf()
|
2014-08-26 07:57:04 +00:00
|
|
|
int cs_snprintf(char *buffer, size_t size, const char *fmt, ...)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
va_list ap;
|
|
|
|
va_start(ap, fmt);
|
|
|
|
ret = cs_vsnprintf(buffer, size, fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
2015-04-02 07:18:33 +00:00
|
|
|
|
|
|
|
bool arr_exist8(unsigned char *arr, unsigned char max, unsigned int id)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < max; i++) {
|
|
|
|
if (arr[i] == id)
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool arr_exist(uint16_t *arr, unsigned char max, unsigned int id)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < max; i++) {
|
|
|
|
if (arr[i] == id)
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-05-30 03:08:18 +00:00
|
|
|
/// Reads 4 bytes in the endian order specified in MI->cs->mode.
|
|
|
|
uint32_t readBytes32(MCInst *MI, const uint8_t *Bytes)
|
|
|
|
{
|
|
|
|
assert(MI && Bytes);
|
|
|
|
uint32_t Insn;
|
|
|
|
if (MODE_IS_BIG_ENDIAN(MI->csh->mode))
|
|
|
|
Insn = (Bytes[3] << 0) | (Bytes[2] << 8) | (Bytes[1] << 16) |
|
2023-05-30 03:09:37 +00:00
|
|
|
((uint32_t)Bytes[0] << 24);
|
2023-05-30 03:08:18 +00:00
|
|
|
else
|
2023-05-30 03:09:37 +00:00
|
|
|
Insn = ((uint32_t)Bytes[3] << 24) | (Bytes[2] << 16) |
|
|
|
|
(Bytes[1] << 8) | (Bytes[0] << 0);
|
2023-05-30 03:08:18 +00:00
|
|
|
return Insn;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Reads 2 bytes in the endian order specified in MI->cs->mode.
|
|
|
|
uint16_t readBytes16(MCInst *MI, const uint8_t *Bytes)
|
|
|
|
{
|
|
|
|
assert(MI && Bytes);
|
|
|
|
uint16_t Insn;
|
|
|
|
if (MODE_IS_BIG_ENDIAN(MI->csh->mode))
|
|
|
|
Insn = (Bytes[0] << 8) | Bytes[1];
|
|
|
|
else
|
|
|
|
Insn = (Bytes[1] << 8) | Bytes[0];
|
|
|
|
|
|
|
|
return Insn;
|
|
|
|
}
|
2023-11-15 04:12:14 +00:00
|
|
|
|
|
|
|
/// @brief Appends the string @p src to the string @p str. @p src is put to lower case.
|
|
|
|
/// @param str The string to append to.
|
|
|
|
/// @param str_size The lengt of @p str
|
|
|
|
/// @param src The string to append.
|
|
|
|
void append_to_str_lower(char *str, size_t str_size, const char *src) {
|
|
|
|
char *dest = strchr(str, '\0');
|
|
|
|
if (dest - str >= str_size) {
|
|
|
|
assert("str_size does not match actual string length." && 0);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
int i = dest - str;
|
|
|
|
for (int j = 0; (i < str_size) && (j < strlen(src)); ++i, ++j) {
|
|
|
|
str[i] = tolower(src[j]);
|
|
|
|
}
|
|
|
|
str[i] = '\0';
|
|
|
|
}
|