Coverage Report

Created: 2023-09-25 06:24

/src/capstonenext/utils.c
Line
Count
Source (jump to first uncovered line)
1
/* Capstone Disassembly Engine */
2
/* By Nguyen Anh Quynh <aquynh@gmail.com>, 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"
13
14
// count number of positive members in a list.
15
// NOTE: list must be guaranteed to end in 0
16
unsigned int count_positive(const uint16_t *list)
17
3.12M
{
18
3.12M
  unsigned int c;
19
20
4.16M
  for (c = 0; list[c] > 0; c++);
21
22
3.12M
  return c;
23
3.12M
}
24
25
// count number of positive members in a list.
26
// NOTE: list must be guaranteed to end in 0
27
unsigned int count_positive8(const unsigned char *list)
28
1.56M
{
29
1.56M
  unsigned int c;
30
31
2.28M
  for (c = 0; list[c] > 0; c++);
32
33
1.56M
  return c;
34
1.56M
}
35
36
char *cs_strdup(const char *str)
37
142k
{
38
142k
  size_t len = strlen(str) + 1;
39
142k
  void *new = cs_mem_malloc(len);
40
41
142k
  if (new == NULL)
42
0
    return NULL;
43
44
142k
  return (char *)memmove(new, str, len);
45
142k
}
46
47
// we need this since Windows doesn't have snprintf()
48
int cs_snprintf(char *buffer, size_t size, const char *fmt, ...)
49
96.0k
{
50
96.0k
  int ret;
51
52
96.0k
  va_list ap;
53
96.0k
  va_start(ap, fmt);
54
96.0k
  ret = cs_vsnprintf(buffer, size, fmt, ap);
55
96.0k
  va_end(ap);
56
57
96.0k
  return ret;
58
96.0k
}
59
60
bool arr_exist8(unsigned char *arr, unsigned char max, unsigned int id)
61
0
{
62
0
  int i;
63
64
0
  for (i = 0; i < max; i++) {
65
0
    if (arr[i] == id)
66
0
      return true;
67
0
  }
68
69
0
  return false;
70
0
}
71
72
bool arr_exist(uint16_t *arr, unsigned char max, unsigned int id)
73
353k
{
74
353k
  int i;
75
76
355k
  for (i = 0; i < max; i++) {
77
16.2k
    if (arr[i] == id)
78
14.6k
      return true;
79
16.2k
  }
80
81
338k
  return false;
82
353k
}
83
84
/// Reads 4 bytes in the endian order specified in MI->cs->mode.
85
uint32_t readBytes32(MCInst *MI, const uint8_t *Bytes)
86
191k
{
87
191k
  assert(MI && Bytes);
88
191k
  uint32_t Insn;
89
191k
  if (MODE_IS_BIG_ENDIAN(MI->csh->mode))
90
191k
    Insn = (Bytes[3] << 0) | (Bytes[2] << 8) | (Bytes[1] << 16) |
91
191k
           ((uint32_t)Bytes[0] << 24);
92
0
  else
93
0
    Insn = ((uint32_t)Bytes[3] << 24) | (Bytes[2] << 16) |
94
0
           (Bytes[1] << 8) | (Bytes[0] << 0);
95
191k
  return Insn;
96
191k
}
97
98
/// Reads 2 bytes in the endian order specified in MI->cs->mode.
99
uint16_t readBytes16(MCInst *MI, const uint8_t *Bytes)
100
0
{
101
0
  assert(MI && Bytes);
102
0
  uint16_t Insn;
103
0
  if (MODE_IS_BIG_ENDIAN(MI->csh->mode))
104
0
    Insn = (Bytes[0] << 8) | Bytes[1];
105
0
  else
106
0
    Insn = (Bytes[1] << 8) | Bytes[0];
107
108
0
  return Insn;
109
0
}