Coverage Report

Created: 2026-03-03 06:14

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/capstonenext/cs.c
Line
Count
Source
1
/* Capstone Disassembly Engine */
2
/* By Nguyen Anh Quynh <aquynh@gmail.com>, 2013-2019 */
3
4
#include "SStream.h"
5
#if defined(CAPSTONE_HAS_OSXKERNEL)
6
#include <Availability.h>
7
#include <libkern/libkern.h>
8
#else
9
#include <stddef.h>
10
#include <stdio.h>
11
#include <stdlib.h>
12
#endif
13
14
#include <string.h>
15
#include <capstone/capstone.h>
16
17
#include "utils.h"
18
#include "MCRegisterInfo.h"
19
#include "Mapping.h"
20
21
#if defined(_KERNEL_MODE)
22
#include "windows\winkernel_mm.h"
23
#endif
24
25
// Issue #681: Windows kernel does not support formatting float point
26
#if defined(_KERNEL_MODE) && !defined(CAPSTONE_DIET)
27
#if defined(CAPSTONE_HAS_ARM) || defined(CAPSTONE_HAS_AARCH64) || \
28
  defined(CAPSTONE_HAS_M68K)
29
#define CAPSTONE_STR_INTERNAL(x) #x
30
#define CAPSTONE_STR(x) CAPSTONE_STR_INTERNAL(x)
31
#define CAPSTONE_MSVC_WRANING_PREFIX \
32
  __FILE__ "(" CAPSTONE_STR(__LINE__) ") : warning message : "
33
34
#pragma message( \
35
  CAPSTONE_MSVC_WRANING_PREFIX \
36
  "Windows driver does not support full features for selected architecture(s). Define CAPSTONE_DIET to compile Capstone with only supported features. See issue #681 for details.")
37
38
#undef CAPSTONE_MSVC_WRANING_PREFIX
39
#undef CAPSTONE_STR
40
#undef CAPSTONE_STR_INTERNAL
41
#endif
42
#endif // defined(_KERNEL_MODE) && !defined(CAPSTONE_DIET)
43
44
#if !defined(CAPSTONE_HAS_OSXKERNEL) && !defined(CAPSTONE_DIET) && \
45
  !defined(_KERNEL_MODE)
46
27.2k
#define INSN_CACHE_SIZE 32
47
#else
48
// reduce stack variable size for kernel/firmware
49
#define INSN_CACHE_SIZE 8
50
#endif
51
52
// default SKIPDATA mnemonic
53
#ifndef CAPSTONE_DIET
54
27.2k
#define SKIPDATA_MNEM ".byte"
55
#else // No printing is available in diet mode
56
#define SKIPDATA_MNEM NULL
57
#endif
58
59
#include "arch/AArch64/AArch64Module.h"
60
#include "arch/ARM/ARMModule.h"
61
#include "arch/EVM/EVMModule.h"
62
#include "arch/WASM/WASMModule.h"
63
#include "arch/M680X/M680XModule.h"
64
#include "arch/M68K/M68KModule.h"
65
#include "arch/Mips/MipsModule.h"
66
#include "arch/PowerPC/PPCModule.h"
67
#include "arch/Sparc/SparcModule.h"
68
#include "arch/SystemZ/SystemZModule.h"
69
#include "arch/TMS320C64x/TMS320C64xModule.h"
70
#include "arch/X86/X86Module.h"
71
#include "arch/XCore/XCoreModule.h"
72
#include "arch/RISCV/RISCVModule.h"
73
#include "arch/MOS65XX/MOS65XXModule.h"
74
#include "arch/BPF/BPFModule.h"
75
#include "arch/SH/SHModule.h"
76
#include "arch/TriCore/TriCoreModule.h"
77
#include "arch/Alpha/AlphaModule.h"
78
#include "arch/HPPA/HPPAModule.h"
79
#include "arch/LoongArch/LoongArchModule.h"
80
#include "arch/Xtensa/XtensaModule.h"
81
#include "arch/ARC/ARCModule.h"
82
83
typedef struct cs_arch_config {
84
  // constructor initialization
85
  cs_err (*arch_init)(cs_struct *);
86
  // support cs_option()
87
  cs_err (*arch_option)(cs_struct *, cs_opt_type, size_t value);
88
  // bitmask for finding disallowed modes for an arch:
89
  // to be called in cs_open()/cs_option()
90
  cs_mode arch_disallowed_mode_mask;
91
} cs_arch_config;
92
93
#define CS_ARCH_CONFIG_ARM \
94
  { \
95
    ARM_global_init, ARM_option, \
96
      ~(CS_MODE_LITTLE_ENDIAN | CS_MODE_ARM | CS_MODE_V8 | \
97
        CS_MODE_MCLASS | CS_MODE_THUMB | \
98
        CS_MODE_BIG_ENDIAN), \
99
  }
100
#define CS_ARCH_CONFIG_AARCH64 \
101
  { \
102
    AArch64_global_init, AArch64_option, \
103
      ~(CS_MODE_LITTLE_ENDIAN | CS_MODE_ARM | \
104
        CS_MODE_BIG_ENDIAN | CS_MODE_APPLE_PROPRIETARY), \
105
  }
106
#define CS_ARCH_CONFIG_MIPS \
107
  { \
108
    Mips_global_init, Mips_option, \
109
      ~(CS_MODE_LITTLE_ENDIAN | CS_MODE_BIG_ENDIAN | \
110
        CS_MODE_MIPS16 | CS_MODE_MIPS32 | CS_MODE_MIPS64 | \
111
        CS_MODE_MICRO | CS_MODE_MIPS1 | CS_MODE_MIPS2 | \
112
        CS_MODE_MIPS32R2 | CS_MODE_MIPS32R3 | \
113
        CS_MODE_MIPS32R5 | CS_MODE_MIPS32R6 | \
114
        CS_MODE_MIPS3 | CS_MODE_MIPS4 | CS_MODE_MIPS5 | \
115
        CS_MODE_MIPS64R2 | CS_MODE_MIPS64R3 | \
116
        CS_MODE_MIPS64R5 | CS_MODE_MIPS64R6 | \
117
        CS_MODE_OCTEON | CS_MODE_OCTEONP | \
118
        CS_MODE_NANOMIPS | CS_MODE_NMS1 | CS_MODE_I7200 | \
119
        CS_MODE_MIPS_NOFLOAT | CS_MODE_MIPS_PTR64), \
120
  }
121
#define CS_ARCH_CONFIG_X86 \
122
  { \
123
    X86_global_init, X86_option, \
124
      ~(CS_MODE_LITTLE_ENDIAN | CS_MODE_32 | CS_MODE_64 | \
125
        CS_MODE_16), \
126
  }
127
#define CS_ARCH_CONFIG_PPC \
128
  { \
129
    PPC_global_init, PPC_option, \
130
      ~(CS_MODE_LITTLE_ENDIAN | CS_MODE_32 | CS_MODE_64 | \
131
        CS_MODE_BIG_ENDIAN | CS_MODE_QPX | CS_MODE_PS | \
132
        CS_MODE_BOOKE | CS_MODE_SPE | CS_MODE_AIX_OS | \
133
        CS_MODE_PWR7 | CS_MODE_PWR8 | CS_MODE_PWR9 | \
134
        CS_MODE_PWR10 | CS_MODE_PPC_ISA_FUTURE | \
135
        CS_MODE_MSYNC | CS_MODE_MODERN_AIX_AS), \
136
  }
137
#define CS_ARCH_CONFIG_SPARC \
138
  { \
139
    Sparc_global_init, Sparc_option, \
140
      ~(CS_MODE_LITTLE_ENDIAN | CS_MODE_BIG_ENDIAN | \
141
        CS_MODE_V9 | CS_MODE_64 | CS_MODE_32), \
142
  }
143
#define CS_ARCH_CONFIG_SYSTEMZ \
144
  { \
145
    SystemZ_global_init, SystemZ_option, \
146
      ~(CS_MODE_BIG_ENDIAN | CS_MODE_SYSTEMZ_ARCH8 | \
147
        CS_MODE_SYSTEMZ_ARCH9 | CS_MODE_SYSTEMZ_ARCH10 | \
148
        CS_MODE_SYSTEMZ_ARCH11 | CS_MODE_SYSTEMZ_ARCH12 | \
149
        CS_MODE_SYSTEMZ_ARCH13 | CS_MODE_SYSTEMZ_ARCH14 | \
150
        CS_MODE_SYSTEMZ_Z10 | CS_MODE_SYSTEMZ_Z196 | \
151
        CS_MODE_SYSTEMZ_ZEC12 | CS_MODE_SYSTEMZ_Z13 | \
152
        CS_MODE_SYSTEMZ_Z14 | CS_MODE_SYSTEMZ_Z15 | \
153
        CS_MODE_SYSTEMZ_Z16 | CS_MODE_SYSTEMZ_GENERIC), \
154
  }
155
#define CS_ARCH_CONFIG_XCORE \
156
  { \
157
    XCore_global_init, XCore_option, ~(CS_MODE_BIG_ENDIAN), \
158
  }
159
#define CS_ARCH_CONFIG_M68K \
160
  { \
161
    M68K_global_init, M68K_option, \
162
      ~(CS_MODE_BIG_ENDIAN | CS_MODE_M68K_000 | \
163
        CS_MODE_M68K_010 | CS_MODE_M68K_020 | \
164
        CS_MODE_M68K_030 | CS_MODE_M68K_040 | \
165
        CS_MODE_M68K_060), \
166
  }
167
#define CS_ARCH_CONFIG_TMS320C64X \
168
  { \
169
    TMS320C64x_global_init, TMS320C64x_option, \
170
      ~(CS_MODE_LITTLE_ENDIAN | CS_MODE_BIG_ENDIAN), \
171
  }
172
#define CS_ARCH_CONFIG_M680X \
173
  { \
174
    M680X_global_init, M680X_option, \
175
      ~(CS_MODE_M680X_6301 | CS_MODE_M680X_6309 | \
176
        CS_MODE_M680X_6800 | CS_MODE_M680X_6801 | \
177
        CS_MODE_M680X_6805 | CS_MODE_M680X_6808 | \
178
        CS_MODE_M680X_6809 | CS_MODE_M680X_6811 | \
179
        CS_MODE_M680X_CPU12 | CS_MODE_M680X_HCS08 | \
180
        CS_MODE_M680X_RS08), \
181
  }
182
#define CS_ARCH_CONFIG_EVM \
183
  { \
184
    EVM_global_init, EVM_option, 0, \
185
  }
186
#define CS_ARCH_CONFIG_MOS65XX \
187
  { \
188
    MOS65XX_global_init, MOS65XX_option, \
189
      ~(CS_MODE_LITTLE_ENDIAN | CS_MODE_MOS65XX_6502 | \
190
        CS_MODE_MOS65XX_65C02 | CS_MODE_MOS65XX_W65C02 | \
191
        CS_MODE_MOS65XX_65816_LONG_MX), \
192
  }
193
#define CS_ARCH_CONFIG_WASM \
194
  { \
195
    WASM_global_init, WASM_option, 0, \
196
  }
197
#define CS_ARCH_CONFIG_BPF \
198
  { \
199
    BPF_global_init, BPF_option, \
200
      ~(CS_MODE_LITTLE_ENDIAN | CS_MODE_BPF_CLASSIC | \
201
        CS_MODE_BPF_EXTENDED | CS_MODE_BIG_ENDIAN), \
202
  }
203
#define CS_ARCH_CONFIG_RISCV \
204
  { \
205
    RISCV_global_init, RISCV_option, \
206
      ~(CS_MODE_RISCV32 | CS_MODE_RISCV64 | \
207
        CS_MODE_RISCV_C | CS_MODE_RISCV_FD | \
208
        CS_MODE_RISCV_V | CS_MODE_RISCV_ZFINX | \
209
        CS_MODE_RISCV_ZCMP_ZCMT_ZCE | \
210
        CS_MODE_RISCV_ZICFISS | CS_MODE_RISCV_E | \
211
        CS_MODE_RISCV_A | CS_MODE_RISCV_COREV | \
212
        CS_MODE_RISCV_SIFIVE | CS_MODE_RISCV_THEAD | \
213
        CS_MODE_RISCV_ZBA | CS_MODE_RISCV_ZBB | \
214
        CS_MODE_RISCV_ZBC | CS_MODE_RISCV_ZBKB | \
215
        CS_MODE_RISCV_ZBKC | CS_MODE_RISCV_ZBKX | \
216
        CS_MODE_RISCV_ZBS), \
217
  }
218
#define CS_ARCH_CONFIG_SH \
219
  { \
220
    SH_global_init, SH_option, \
221
      ~(CS_MODE_SH2 | CS_MODE_SH2A | CS_MODE_SH3 | \
222
        CS_MODE_SH4 | CS_MODE_SH4A | CS_MODE_SHFPU | \
223
        CS_MODE_SHDSP | CS_MODE_BIG_ENDIAN), \
224
  }
225
#define CS_ARCH_CONFIG_TRICORE \
226
  { \
227
    TRICORE_global_init, TRICORE_option, \
228
      ~(CS_MODE_TRICORE_110 | CS_MODE_TRICORE_120 | \
229
        CS_MODE_TRICORE_130 | CS_MODE_TRICORE_131 | \
230
        CS_MODE_TRICORE_160 | CS_MODE_TRICORE_161 | \
231
        CS_MODE_TRICORE_162 | CS_MODE_TRICORE_180 | \
232
        CS_MODE_LITTLE_ENDIAN), \
233
  }
234
#define CS_ARCH_CONFIG_ALPHA \
235
  { \
236
    ALPHA_global_init, ALPHA_option, \
237
      ~(CS_MODE_LITTLE_ENDIAN | CS_MODE_BIG_ENDIAN), \
238
  }
239
#define CS_ARCH_CONFIG_LOONGARCH \
240
  { \
241
    LoongArch_global_init, LoongArch_option, \
242
      ~(CS_MODE_LITTLE_ENDIAN | CS_MODE_LOONGARCH32 | \
243
        CS_MODE_LOONGARCH64), \
244
  }
245
#define CS_ARCH_CONFIG_XTENSA \
246
  { \
247
    Xtensa_global_init, Xtensa_option, \
248
      ~(CS_MODE_XTENSA_ESP32 | CS_MODE_XTENSA_ESP32S2 | \
249
        CS_MODE_XTENSA_ESP8266), \
250
  }
251
252
#define CS_ARCH_CONFIG_ARC \
253
  { \
254
    ARC_global_init, ARC_option, ~(CS_MODE_LITTLE_ENDIAN), \
255
  }
256
257
#ifdef CAPSTONE_USE_ARCH_REGISTRATION
258
static cs_arch_config arch_configs[MAX_ARCH];
259
static uint32_t all_arch;
260
#else
261
static const cs_arch_config arch_configs[MAX_ARCH] = {
262
#ifdef CAPSTONE_HAS_ARM
263
  CS_ARCH_CONFIG_ARM,
264
#else
265
  { NULL, NULL, 0 },
266
#endif
267
#ifdef CAPSTONE_HAS_AARCH64
268
  CS_ARCH_CONFIG_AARCH64,
269
#else
270
  { NULL, NULL, 0 },
271
#endif
272
#ifdef CAPSTONE_HAS_SYSTEMZ
273
  CS_ARCH_CONFIG_SYSTEMZ,
274
#else
275
  { NULL, NULL, 0 },
276
#endif
277
#ifdef CAPSTONE_HAS_MIPS
278
  CS_ARCH_CONFIG_MIPS,
279
#else
280
  { NULL, NULL, 0 },
281
#endif
282
#ifdef CAPSTONE_HAS_X86
283
  CS_ARCH_CONFIG_X86,
284
#else
285
  { NULL, NULL, 0 },
286
#endif
287
#ifdef CAPSTONE_HAS_POWERPC
288
  CS_ARCH_CONFIG_PPC,
289
#else
290
  { NULL, NULL, 0 },
291
#endif
292
#ifdef CAPSTONE_HAS_SPARC
293
  CS_ARCH_CONFIG_SPARC,
294
#else
295
  { NULL, NULL, 0 },
296
#endif
297
#ifdef CAPSTONE_HAS_XCORE
298
  CS_ARCH_CONFIG_XCORE,
299
#else
300
  { NULL, NULL, 0 },
301
#endif
302
#ifdef CAPSTONE_HAS_M68K
303
  CS_ARCH_CONFIG_M68K,
304
#else
305
  { NULL, NULL, 0 },
306
#endif
307
#ifdef CAPSTONE_HAS_TMS320C64X
308
  CS_ARCH_CONFIG_TMS320C64X,
309
#else
310
  { NULL, NULL, 0 },
311
#endif
312
#ifdef CAPSTONE_HAS_M680X
313
  CS_ARCH_CONFIG_M680X,
314
#else
315
  { NULL, NULL, 0 },
316
#endif
317
#ifdef CAPSTONE_HAS_EVM
318
  CS_ARCH_CONFIG_EVM,
319
#else
320
  { NULL, NULL, 0 },
321
#endif
322
#ifdef CAPSTONE_HAS_MOS65XX
323
  CS_ARCH_CONFIG_MOS65XX,
324
#else
325
  { NULL, NULL, 0 },
326
#endif
327
#ifdef CAPSTONE_HAS_WASM
328
  CS_ARCH_CONFIG_WASM,
329
#else
330
  { NULL, NULL, 0 },
331
#endif
332
#ifdef CAPSTONE_HAS_BPF
333
  CS_ARCH_CONFIG_BPF,
334
#else
335
  { NULL, NULL, 0 },
336
#endif
337
#ifdef CAPSTONE_HAS_RISCV
338
  CS_ARCH_CONFIG_RISCV,
339
#else
340
  { NULL, NULL, 0 },
341
#endif
342
#ifdef CAPSTONE_HAS_SH
343
  CS_ARCH_CONFIG_SH,
344
#else
345
  { NULL, NULL, 0 },
346
#endif
347
#ifdef CAPSTONE_HAS_TRICORE
348
  CS_ARCH_CONFIG_TRICORE,
349
#else
350
  { NULL, NULL, 0 },
351
#endif
352
#ifdef CAPSTONE_HAS_ALPHA
353
  CS_ARCH_CONFIG_ALPHA,
354
#else
355
  { NULL, NULL, 0 },
356
#endif
357
#ifdef CAPSTONE_HAS_HPPA
358
  {
359
    HPPA_global_init,
360
    HPPA_option,
361
    ~(CS_MODE_LITTLE_ENDIAN | CS_MODE_BIG_ENDIAN | CS_MODE_HPPA_11 |
362
      CS_MODE_HPPA_20 | CS_MODE_HPPA_20W),
363
  },
364
#else
365
  { NULL, NULL, 0 },
366
#endif
367
#ifdef CAPSTONE_HAS_LOONGARCH
368
  CS_ARCH_CONFIG_LOONGARCH,
369
#else
370
  { NULL, NULL, 0 },
371
#endif
372
#ifdef CAPSTONE_HAS_XTENSA
373
  CS_ARCH_CONFIG_XTENSA,
374
#else
375
  { NULL, NULL, 0 },
376
#endif
377
#ifdef CAPSTONE_HAS_ARC
378
  CS_ARCH_CONFIG_ARC,
379
#else
380
  { NULL, NULL, 0 },
381
#endif
382
};
383
384
// bitmask of enabled architectures
385
static const uint32_t all_arch = 0
386
#ifdef CAPSTONE_HAS_ARM
387
         | (1 << CS_ARCH_ARM)
388
#endif
389
#if defined(CAPSTONE_HAS_AARCH64) || defined(CAPSTONE_HAS_ARM64)
390
         | (1 << CS_ARCH_AARCH64)
391
#endif
392
#ifdef CAPSTONE_HAS_MIPS
393
         | (1 << CS_ARCH_MIPS)
394
#endif
395
#ifdef CAPSTONE_HAS_X86
396
         | (1 << CS_ARCH_X86)
397
#endif
398
#ifdef CAPSTONE_HAS_POWERPC
399
         | (1 << CS_ARCH_PPC)
400
#endif
401
#ifdef CAPSTONE_HAS_SPARC
402
         | (1 << CS_ARCH_SPARC)
403
#endif
404
#ifdef CAPSTONE_HAS_SYSTEMZ
405
         | (1 << CS_ARCH_SYSTEMZ)
406
#endif
407
#ifdef CAPSTONE_HAS_XCORE
408
         | (1 << CS_ARCH_XCORE)
409
#endif
410
#ifdef CAPSTONE_HAS_M68K
411
         | (1 << CS_ARCH_M68K)
412
#endif
413
#ifdef CAPSTONE_HAS_TMS320C64X
414
         | (1 << CS_ARCH_TMS320C64X)
415
#endif
416
#ifdef CAPSTONE_HAS_M680X
417
         | (1 << CS_ARCH_M680X)
418
#endif
419
#ifdef CAPSTONE_HAS_EVM
420
         | (1 << CS_ARCH_EVM)
421
#endif
422
#ifdef CAPSTONE_HAS_MOS65XX
423
         | (1 << CS_ARCH_MOS65XX)
424
#endif
425
#ifdef CAPSTONE_HAS_WASM
426
         | (1 << CS_ARCH_WASM)
427
#endif
428
#ifdef CAPSTONE_HAS_BPF
429
         | (1 << CS_ARCH_BPF)
430
#endif
431
#ifdef CAPSTONE_HAS_RISCV
432
         | (1 << CS_ARCH_RISCV)
433
#endif
434
#ifdef CAPSTONE_HAS_SH
435
         | (1 << CS_ARCH_SH)
436
#endif
437
#ifdef CAPSTONE_HAS_TRICORE
438
         | (1 << CS_ARCH_TRICORE)
439
#endif
440
#ifdef CAPSTONE_HAS_ALPHA
441
         | (1 << CS_ARCH_ALPHA)
442
#endif
443
#ifdef CAPSTONE_HAS_HPPA
444
         | (1 << CS_ARCH_HPPA)
445
#endif
446
#ifdef CAPSTONE_HAS_LOONGARCH
447
         | (1 << CS_ARCH_LOONGARCH)
448
#endif
449
#ifdef CAPSTONE_HAS_XTENSA
450
         | (1 << CS_ARCH_XTENSA)
451
#endif
452
#ifdef CAPSTONE_HAS_ARC
453
         | (1 << CS_ARCH_ARC)
454
#endif
455
  ;
456
#endif
457
458
#if defined(CAPSTONE_USE_SYS_DYN_MEM)
459
#if !defined(CAPSTONE_HAS_OSXKERNEL) && !defined(_KERNEL_MODE)
460
// default
461
cs_malloc_t cs_mem_malloc = malloc;
462
cs_calloc_t cs_mem_calloc = calloc;
463
cs_realloc_t cs_mem_realloc = realloc;
464
cs_free_t cs_mem_free = free;
465
#if defined(_WIN32_WCE)
466
cs_vsnprintf_t cs_vsnprintf = _vsnprintf;
467
#else
468
cs_vsnprintf_t cs_vsnprintf = vsnprintf;
469
#endif // defined(_WIN32_WCE)
470
471
#elif defined(_KERNEL_MODE)
472
// Windows driver
473
cs_malloc_t cs_mem_malloc = cs_winkernel_malloc;
474
cs_calloc_t cs_mem_calloc = cs_winkernel_calloc;
475
cs_realloc_t cs_mem_realloc = cs_winkernel_realloc;
476
cs_free_t cs_mem_free = cs_winkernel_free;
477
cs_vsnprintf_t cs_vsnprintf = cs_winkernel_vsnprintf;
478
#else
479
// OSX kernel
480
extern void *kern_os_malloc(size_t size);
481
extern void kern_os_free(void *addr);
482
extern void *kern_os_realloc(void *addr, size_t nsize);
483
484
static void *cs_kern_os_calloc(size_t num, size_t size)
485
{
486
  return kern_os_malloc(num * size); // malloc bzeroes the buffer
487
}
488
489
cs_malloc_t cs_mem_malloc = kern_os_malloc;
490
cs_calloc_t cs_mem_calloc = cs_kern_os_calloc;
491
cs_realloc_t cs_mem_realloc = kern_os_realloc;
492
cs_free_t cs_mem_free = kern_os_free;
493
cs_vsnprintf_t cs_vsnprintf = vsnprintf;
494
#endif // !defined(CAPSTONE_HAS_OSXKERNEL) && !defined(_KERNEL_MODE)
495
#else
496
// User-defined
497
cs_malloc_t cs_mem_malloc = NULL;
498
cs_calloc_t cs_mem_calloc = NULL;
499
cs_realloc_t cs_mem_realloc = NULL;
500
cs_free_t cs_mem_free = NULL;
501
cs_vsnprintf_t cs_vsnprintf = NULL;
502
503
#endif // defined(CAPSTONE_USE_SYS_DYN_MEM)
504
505
CAPSTONE_EXPORT
506
unsigned int CAPSTONE_API cs_version(int *major, int *minor)
507
0
{
508
0
  if (major != NULL && minor != NULL) {
509
0
    *major = CS_API_MAJOR;
510
0
    *minor = CS_API_MINOR;
511
0
  }
512
513
0
  return (CS_API_MAJOR << 8) + CS_API_MINOR;
514
0
}
515
516
#define CS_ARCH_REGISTER(id) \
517
  cs_arch_config cfg = CS_ARCH_CONFIG_##id; \
518
  arch_configs[CS_ARCH_##id] = cfg; \
519
  all_arch |= 1 << CS_ARCH_##id
520
521
CAPSTONE_EXPORT
522
void CAPSTONE_API cs_arch_register_arm(void)
523
0
{
524
#if defined(CAPSTONE_USE_ARCH_REGISTRATION) && defined(CAPSTONE_HAS_ARM)
525
  CS_ARCH_REGISTER(ARM);
526
#endif
527
0
}
528
529
CAPSTONE_EXPORT
530
void CAPSTONE_API cs_arch_register_aarch64(void)
531
0
{
532
#if defined(CAPSTONE_USE_ARCH_REGISTRATION) && defined(CAPSTONE_HAS_AARCH64)
533
  CS_ARCH_REGISTER(AARCH64);
534
#endif
535
0
}
536
537
CAPSTONE_EXPORT
538
void CAPSTONE_API cs_arch_register_mips(void)
539
0
{
540
#if defined(CAPSTONE_USE_ARCH_REGISTRATION) && defined(CAPSTONE_HAS_MIPS)
541
  CS_ARCH_REGISTER(MIPS);
542
#endif
543
0
}
544
545
CAPSTONE_EXPORT
546
void CAPSTONE_API cs_arch_register_x86(void)
547
0
{
548
#if defined(CAPSTONE_USE_ARCH_REGISTRATION) && defined(CAPSTONE_HAS_X86)
549
  CS_ARCH_REGISTER(X86);
550
#endif
551
0
}
552
553
CAPSTONE_EXPORT
554
void CAPSTONE_API cs_arch_register_powerpc(void)
555
0
{
556
#if defined(CAPSTONE_USE_ARCH_REGISTRATION) && defined(CAPSTONE_HAS_POWERPC)
557
  CS_ARCH_REGISTER(PPC);
558
#endif
559
0
}
560
561
CAPSTONE_EXPORT
562
void CAPSTONE_API cs_arch_register_sparc(void)
563
0
{
564
#if defined(CAPSTONE_USE_ARCH_REGISTRATION) && defined(CAPSTONE_HAS_SPARC)
565
  CS_ARCH_REGISTER(SPARC);
566
#endif
567
0
}
568
569
CAPSTONE_EXPORT
570
void CAPSTONE_API cs_arch_register_systemz(void)
571
0
{
572
#if defined(CAPSTONE_USE_ARCH_REGISTRATION) && defined(CAPSTONE_HAS_SYSTEMZ)
573
  CS_ARCH_REGISTER(SYSTEMZ);
574
#endif
575
0
}
576
577
CAPSTONE_EXPORT
578
void CAPSTONE_API cs_arch_register_xcore(void)
579
0
{
580
#if defined(CAPSTONE_USE_ARCH_REGISTRATION) && defined(CAPSTONE_HAS_XCORE)
581
  CS_ARCH_REGISTER(XCORE);
582
#endif
583
0
}
584
585
CAPSTONE_EXPORT
586
void CAPSTONE_API cs_arch_register_m68k(void)
587
0
{
588
#if defined(CAPSTONE_USE_ARCH_REGISTRATION) && defined(CAPSTONE_HAS_M68K)
589
  CS_ARCH_REGISTER(M68K);
590
#endif
591
0
}
592
593
CAPSTONE_EXPORT
594
void CAPSTONE_API cs_arch_register_tms320c64x(void)
595
0
{
596
#if defined(CAPSTONE_USE_ARCH_REGISTRATION) && defined(CAPSTONE_HAS_TMS320C64X)
597
  CS_ARCH_REGISTER(TMS320C64X);
598
#endif
599
0
}
600
601
CAPSTONE_EXPORT
602
void CAPSTONE_API cs_arch_register_m680x(void)
603
0
{
604
#if defined(CAPSTONE_USE_ARCH_REGISTRATION) && defined(CAPSTONE_HAS_M680X)
605
  CS_ARCH_REGISTER(M680X);
606
#endif
607
0
}
608
609
CAPSTONE_EXPORT
610
void CAPSTONE_API cs_arch_register_evm(void)
611
0
{
612
#if defined(CAPSTONE_USE_ARCH_REGISTRATION) && defined(CAPSTONE_HAS_EVM)
613
  CS_ARCH_REGISTER(EVM);
614
#endif
615
0
}
616
617
CAPSTONE_EXPORT
618
void CAPSTONE_API cs_arch_register_mos65xx(void)
619
0
{
620
#if defined(CAPSTONE_USE_ARCH_REGISTRATION) && defined(CAPSTONE_HAS_MOS65XX)
621
  CS_ARCH_REGISTER(MOS65XX);
622
#endif
623
0
}
624
625
CAPSTONE_EXPORT
626
void CAPSTONE_API cs_arch_register_wasm(void)
627
0
{
628
#if defined(CAPSTONE_USE_ARCH_REGISTRATION) && defined(CAPSTONE_HAS_WASM)
629
  CS_ARCH_REGISTER(WASM);
630
#endif
631
0
}
632
633
CAPSTONE_EXPORT
634
void CAPSTONE_API cs_arch_register_bpf(void)
635
0
{
636
#if defined(CAPSTONE_USE_ARCH_REGISTRATION) && defined(CAPSTONE_HAS_BPF)
637
  CS_ARCH_REGISTER(BPF);
638
#endif
639
0
}
640
641
CAPSTONE_EXPORT
642
void CAPSTONE_API cs_arch_register_riscv(void)
643
0
{
644
#if defined(CAPSTONE_USE_ARCH_REGISTRATION) && defined(CAPSTONE_HAS_RISCV)
645
  CS_ARCH_REGISTER(RISCV);
646
#endif
647
0
}
648
649
CAPSTONE_EXPORT
650
void CAPSTONE_API cs_arch_register_sh(void)
651
0
{
652
#if defined(CAPSTONE_USE_ARCH_REGISTRATION) && defined(CAPSTONE_HAS_SH)
653
  CS_ARCH_REGISTER(SH);
654
#endif
655
0
}
656
657
CAPSTONE_EXPORT
658
void CAPSTONE_API cs_arch_register_tricore(void)
659
0
{
660
#if defined(CAPSTONE_USE_ARCH_REGISTRATION) && defined(CAPSTONE_HAS_TRICORE)
661
  CS_ARCH_REGISTER(TRICORE);
662
#endif
663
0
}
664
665
CAPSTONE_EXPORT
666
void CAPSTONE_API cs_arch_register_alpha(void)
667
0
{
668
#if defined(CAPSTONE_USE_ARCH_REGISTRATION) && defined(CAPSTONE_HAS_ALPHA)
669
  CS_ARCH_REGISTER(ALPHA);
670
#endif
671
0
}
672
673
CAPSTONE_EXPORT
674
void CAPSTONE_API cs_arch_register_loongarch(void)
675
0
{
676
#if defined(CAPSTONE_USE_ARCH_REGISTRATION) && defined(CAPSTONE_HAS_LOONGARCH)
677
  CS_ARCH_REGISTER(LOONGARCH);
678
#endif
679
0
}
680
681
CAPSTONE_EXPORT
682
void CAPSTONE_API cs_arch_register_arc(void)
683
0
{
684
#if defined(CAPSTONE_USE_ARCH_REGISTRATION) && defined(CAPSTONE_HAS_ARC)
685
  CS_ARCH_REGISTER(ARC);
686
#endif
687
0
}
688
689
CAPSTONE_EXPORT
690
bool CAPSTONE_API cs_support(int query)
691
0
{
692
0
  if (query == CS_ARCH_ALL)
693
0
    return all_arch ==
694
0
           ((1 << CS_ARCH_ARM) | (1 << CS_ARCH_AARCH64) |
695
0
      (1 << CS_ARCH_MIPS) | (1 << CS_ARCH_X86) |
696
0
      (1 << CS_ARCH_PPC) | (1 << CS_ARCH_SPARC) |
697
0
      (1 << CS_ARCH_SYSTEMZ) | (1 << CS_ARCH_XCORE) |
698
0
      (1 << CS_ARCH_M68K) | (1 << CS_ARCH_TMS320C64X) |
699
0
      (1 << CS_ARCH_M680X) | (1 << CS_ARCH_EVM) |
700
0
      (1 << CS_ARCH_RISCV) | (1 << CS_ARCH_MOS65XX) |
701
0
      (1 << CS_ARCH_WASM) | (1 << CS_ARCH_BPF) |
702
0
      (1 << CS_ARCH_SH) | (1 << CS_ARCH_TRICORE) |
703
0
      (1 << CS_ARCH_ALPHA) | (1 << CS_ARCH_HPPA) |
704
0
      (1 << CS_ARCH_LOONGARCH) | (1 << CS_ARCH_XTENSA) |
705
0
      (1 << CS_ARCH_ARC));
706
707
0
  if ((unsigned int)query < CS_ARCH_MAX)
708
0
    return all_arch & (1 << query);
709
710
0
  if (query == CS_SUPPORT_DIET) {
711
#ifdef CAPSTONE_DIET
712
    return true;
713
#else
714
0
    return false;
715
0
#endif
716
0
  }
717
718
0
  if (query == CS_SUPPORT_X86_REDUCE) {
719
#if defined(CAPSTONE_HAS_X86) && defined(CAPSTONE_X86_REDUCE)
720
    return true;
721
#else
722
0
    return false;
723
0
#endif
724
0
  }
725
726
  // unsupported query
727
0
  return false;
728
0
}
729
730
CAPSTONE_EXPORT
731
cs_err CAPSTONE_API cs_errno(csh handle)
732
0
{
733
0
  struct cs_struct *ud;
734
0
  if (!handle)
735
0
    return CS_ERR_CSH;
736
737
0
  ud = (struct cs_struct *)(uintptr_t)handle;
738
739
0
  return ud->errnum;
740
0
}
741
742
CAPSTONE_EXPORT
743
const char *CAPSTONE_API cs_strerror(cs_err code)
744
0
{
745
0
  switch (code) {
746
0
  default:
747
0
    return "Unknown error code";
748
0
  case CS_ERR_OK:
749
0
    return "OK (CS_ERR_OK)";
750
0
  case CS_ERR_MEM:
751
0
    return "Out of memory (CS_ERR_MEM)";
752
0
  case CS_ERR_ARCH:
753
0
    return "Invalid/unsupported architecture(CS_ERR_ARCH)";
754
0
  case CS_ERR_HANDLE:
755
0
    return "Invalid handle (CS_ERR_HANDLE)";
756
0
  case CS_ERR_CSH:
757
0
    return "Invalid csh (CS_ERR_CSH)";
758
0
  case CS_ERR_MODE:
759
0
    return "Invalid mode (CS_ERR_MODE)";
760
0
  case CS_ERR_OPTION:
761
0
    return "Invalid option (CS_ERR_OPTION)";
762
0
  case CS_ERR_DETAIL:
763
0
    return "Details are unavailable (CS_ERR_DETAIL)";
764
0
  case CS_ERR_MEMSETUP:
765
0
    return "Dynamic memory management uninitialized (CS_ERR_MEMSETUP)";
766
0
  case CS_ERR_VERSION:
767
0
    return "Different API version between core & binding (CS_ERR_VERSION)";
768
0
  case CS_ERR_DIET:
769
0
    return "Information irrelevant in diet engine (CS_ERR_DIET)";
770
0
  case CS_ERR_SKIPDATA:
771
0
    return "Information irrelevant for 'data' instruction in SKIPDATA mode (CS_ERR_SKIPDATA)";
772
0
  case CS_ERR_X86_ATT:
773
0
    return "AT&T syntax is unavailable (CS_ERR_X86_ATT)";
774
0
  case CS_ERR_X86_INTEL:
775
0
    return "INTEL syntax is unavailable (CS_ERR_X86_INTEL)";
776
0
  case CS_ERR_X86_MASM:
777
0
    return "MASM syntax is unavailable (CS_ERR_X86_MASM)";
778
0
  }
779
0
}
780
781
CAPSTONE_EXPORT
782
cs_err CAPSTONE_API cs_open(cs_arch arch, cs_mode mode, csh *handle)
783
27.2k
{
784
27.2k
  cs_err err = CS_ERR_ARCH;
785
27.2k
  struct cs_struct *ud = NULL;
786
787
27.2k
  if (!cs_mem_malloc || !cs_mem_calloc || !cs_mem_realloc ||
788
27.2k
      !cs_mem_free || !cs_vsnprintf) {
789
    // Error: before cs_open(), dynamic memory management must be initialized
790
    // with cs_option(CS_OPT_MEM)
791
0
    err = CS_ERR_MEMSETUP;
792
0
    goto fail;
793
0
  }
794
795
27.2k
  if (arch >= CS_ARCH_MAX || !arch_configs[arch].arch_init) {
796
0
    err = CS_ERR_ARCH;
797
0
    goto fail;
798
0
  }
799
800
  // verify if requested mode is valid
801
27.2k
  if (mode & arch_configs[arch].arch_disallowed_mode_mask) {
802
6
    err = CS_ERR_MODE;
803
6
    goto fail;
804
6
  }
805
806
27.2k
  ud = cs_mem_calloc(1, sizeof(*ud));
807
27.2k
  if (!ud) {
808
0
    err = CS_ERR_MEM;
809
0
    goto fail;
810
0
  }
811
812
27.2k
  ud->errnum = CS_ERR_OK;
813
27.2k
  ud->arch = arch;
814
27.2k
  ud->mode = mode;
815
  // by default, do not break instruction into details
816
27.2k
  ud->detail_opt = CS_OPT_OFF;
817
27.2k
  ud->PrintBranchImmAsAddress = true;
818
819
  // default skipdata setup
820
27.2k
  ud->skipdata_setup.mnemonic = SKIPDATA_MNEM;
821
822
27.2k
  if ((err = arch_configs[ud->arch].arch_init(ud)))
823
0
    goto fail;
824
825
27.2k
  if ((err = populate_insn_map_cache(ud)))
826
0
    goto fail;
827
828
27.2k
  *handle = (uintptr_t)ud;
829
27.2k
  return CS_ERR_OK;
830
831
6
fail:
832
6
  if (ud) {
833
0
    cs_mem_free(ud);
834
0
  }
835
6
  *handle = 0;
836
6
  return err;
837
27.2k
}
838
839
CAPSTONE_EXPORT
840
cs_err CAPSTONE_API cs_close(csh *handle)
841
27.2k
{
842
27.2k
  struct cs_struct *ud = NULL;
843
27.2k
  struct insn_mnem *next = NULL, *tmp = NULL;
844
845
27.2k
  if (*handle == 0)
846
    // invalid handle
847
0
    return CS_ERR_CSH;
848
849
27.2k
  ud = (struct cs_struct *)(*handle);
850
851
27.2k
  if (ud->printer_info)
852
26.0k
    cs_mem_free(ud->printer_info);
853
854
  // free the linked list of customized mnemonic
855
27.2k
  tmp = ud->mnem_list;
856
27.2k
  while (tmp) {
857
0
    next = tmp->next;
858
0
    cs_mem_free(tmp);
859
0
    tmp = next;
860
0
  }
861
862
27.2k
  cs_mem_free(ud->insn_cache);
863
864
27.2k
  memset(ud, 0, sizeof(*ud));
865
27.2k
  cs_mem_free(ud);
866
867
  // invalidate this handle by ZERO out its value.
868
  // this is to make sure it is unusable after cs_close()
869
27.2k
  *handle = 0;
870
871
27.2k
  return CS_ERR_OK;
872
27.2k
}
873
874
/// replace str1 in target with str2; target starts with str1
875
/// output is put into result (which is array of char with size CS_MNEMONIC_SIZE)
876
/// return 0 on success, -1 on failure
877
#ifndef CAPSTONE_DIET
878
static int str_replace(char *result, char *target, const char *str1, char *str2)
879
0
{
880
0
  size_t target_len = strlen(target);
881
0
  size_t str1_len = strlen(str1);
882
0
  if (target_len < str1_len) {
883
0
    return -1;
884
0
  }
885
886
  // only perform replacement if the output fits into result
887
0
  if (target_len - str1_len + strlen(str2) <= CS_MNEMONIC_SIZE - 1) {
888
    // copy str2 to beginning of result
889
    // skip str1 - already replaced by str2
890
0
    snprintf(result, CS_MNEMONIC_SIZE, "%s%s", str2,
891
0
       target + str1_len);
892
893
0
    return 0;
894
0
  } else
895
0
    return -1;
896
0
}
897
#endif
898
899
// fill insn with mnemonic & operands info
900
static void fill_insn(struct cs_struct *handle, cs_insn *insn, SStream *OS,
901
          MCInst *mci, PostPrinter_t postprinter,
902
          const uint8_t *code)
903
1.47M
{
904
1.47M
#ifndef CAPSTONE_DIET
905
1.47M
  char *sp;
906
1.47M
#endif
907
1.47M
  SStream_trimls(OS);
908
1.47M
  uint16_t copy_size = MIN(sizeof(insn->bytes), insn->size);
909
910
  // fill the instruction bytes.
911
  // we might skip some redundant bytes in front in the case of X86
912
1.47M
  memcpy(insn->bytes, code + insn->size - copy_size, copy_size);
913
1.47M
  insn->op_str[0] = '\0';
914
1.47M
  insn->size = copy_size;
915
916
  // alias instruction might have ID saved in OpcodePub
917
1.47M
  if (MCInst_getOpcodePub(mci))
918
50.0k
    insn->id = MCInst_getOpcodePub(mci);
919
920
  // post printer handles some corner cases (hacky)
921
1.47M
  if (postprinter)
922
354k
    postprinter((csh)handle, insn, OS, mci);
923
924
1.47M
#ifndef CAPSTONE_DIET
925
1.47M
  memset(insn->mnemonic, '\0', sizeof(insn->mnemonic));
926
1.47M
  memset(insn->op_str, '\0', sizeof(insn->op_str));
927
1.47M
  SStream_extract_mnem_opstr(OS, insn->mnemonic, sizeof(insn->mnemonic),
928
1.47M
           insn->op_str, sizeof(insn->op_str));
929
8.15M
  for (sp = insn->mnemonic; *sp; sp++) {
930
6.67M
    if (*sp == '|') // lock|rep prefix for x86
931
32.7k
      *sp = ' ';
932
6.67M
  }
933
934
  // we might have customized mnemonic
935
1.47M
  if (handle->mnem_list) {
936
0
    struct insn_mnem *tmp = handle->mnem_list;
937
0
    while (tmp) {
938
0
      if (tmp->insn.id == insn->id) {
939
0
        char str[CS_MNEMONIC_SIZE] = { 0 };
940
941
0
        if (!str_replace(str, insn->mnemonic,
942
0
             cs_insn_name((csh)handle,
943
0
                    insn->id),
944
0
             tmp->insn.mnemonic)) {
945
          // copy result to mnemonic
946
0
          CS_ASSERT_RET(sizeof(insn->mnemonic) ==
947
0
                  sizeof(str));
948
0
          (void)memcpy(insn->mnemonic, str,
949
0
                 sizeof(insn->mnemonic));
950
0
          insn->mnemonic[sizeof(insn->mnemonic) -
951
0
                   1] = '\0';
952
0
        }
953
954
0
        break;
955
0
      }
956
0
      tmp = tmp->next;
957
0
    }
958
0
  }
959
1.47M
#endif
960
1.47M
}
961
962
// how many bytes will we skip when encountering data (CS_OPT_SKIPDATA)?
963
// this very much depends on instruction alignment requirement of each arch.
964
static uint8_t skipdata_size(cs_struct *handle)
965
0
{
966
0
  switch (handle->arch) {
967
0
  default:
968
    // should never reach
969
0
    return (uint8_t)-1;
970
0
  case CS_ARCH_ARM:
971
    // skip 2 bytes on Thumb mode.
972
0
    if (handle->mode & CS_MODE_THUMB)
973
0
      return 2;
974
    // otherwise, skip 4 bytes
975
0
    return 4;
976
0
  case CS_ARCH_AARCH64:
977
0
  case CS_ARCH_MIPS:
978
0
  case CS_ARCH_PPC:
979
0
  case CS_ARCH_SPARC:
980
    // skip 4 bytes
981
0
    return 4;
982
0
  case CS_ARCH_SYSTEMZ:
983
    // SystemZ instruction's length can be 2, 4 or 6 bytes,
984
    // so we just skip 2 bytes
985
0
    return 2;
986
0
  case CS_ARCH_X86:
987
    // X86 has no restriction on instruction alignment
988
0
    return 1;
989
0
  case CS_ARCH_XCORE:
990
    // XCore instruction's length can be 2 or 4 bytes,
991
    // so we just skip 2 bytes
992
0
    return 2;
993
0
  case CS_ARCH_M68K:
994
    // M68K has 2 bytes instruction alignment but contain multibyte instruction so we skip 2 bytes
995
0
    return 2;
996
0
  case CS_ARCH_TMS320C64X:
997
    // TMS320C64x alignment is 4.
998
0
    return 4;
999
0
  case CS_ARCH_M680X:
1000
    // M680X alignment is 1.
1001
0
    return 1;
1002
0
  case CS_ARCH_EVM:
1003
    // EVM alignment is 1.
1004
0
    return 1;
1005
0
  case CS_ARCH_WASM:
1006
    //WASM alignment is 1
1007
0
    return 1;
1008
0
  case CS_ARCH_MOS65XX:
1009
    // MOS65XX alignment is 1.
1010
0
    return 1;
1011
0
  case CS_ARCH_BPF:
1012
    // both classic and extended BPF have alignment 8.
1013
0
    return 8;
1014
0
  case CS_ARCH_RISCV:
1015
    // special compress mode
1016
0
    if (handle->mode & CS_MODE_RISCV_C)
1017
0
      return 2;
1018
0
    return 4;
1019
0
  case CS_ARCH_SH:
1020
0
    return 2;
1021
0
  case CS_ARCH_TRICORE:
1022
    // TriCore instruction's length can be 2 or 4 bytes,
1023
    // so we just skip 2 bytes
1024
0
    return 2;
1025
0
  case CS_ARCH_ALPHA:
1026
    // Alpha alignment is 4.
1027
0
    return 4;
1028
0
  case CS_ARCH_HPPA:
1029
    // Hppa alignment is 4.
1030
0
    return 4;
1031
0
  case CS_ARCH_LOONGARCH:
1032
    // LoongArch alignment is 4.
1033
0
    return 4;
1034
0
  case CS_ARCH_ARC:
1035
    // ARC instruction's length can be 2, 4, 6 or 8 bytes,
1036
    // therefore, skip 2 bytes
1037
0
    return 2;
1038
0
  }
1039
0
}
1040
1041
CAPSTONE_EXPORT
1042
cs_err CAPSTONE_API cs_option(csh ud, cs_opt_type type, uintptr_t value)
1043
30.1k
{
1044
30.1k
  struct cs_struct *handle;
1045
30.1k
  cs_opt_mnem *opt;
1046
1047
  // cs_option() can be called with NULL handle just for CS_OPT_MEM
1048
  // This is supposed to be executed before all other APIs (even cs_open())
1049
30.1k
  if (type == CS_OPT_MEM) {
1050
0
    cs_opt_mem *mem = (cs_opt_mem *)value;
1051
1052
0
    cs_mem_malloc = mem->malloc;
1053
0
    cs_mem_calloc = mem->calloc;
1054
0
    cs_mem_realloc = mem->realloc;
1055
0
    cs_mem_free = mem->free;
1056
0
    cs_vsnprintf = mem->vsnprintf;
1057
1058
0
    return CS_ERR_OK;
1059
0
  }
1060
1061
30.1k
  handle = (struct cs_struct *)(uintptr_t)ud;
1062
30.1k
  if (!handle)
1063
0
    return CS_ERR_CSH;
1064
1065
30.1k
  switch (type) {
1066
2.90k
  default:
1067
2.90k
    break;
1068
1069
2.90k
  case CS_OPT_UNSIGNED:
1070
0
    handle->imm_unsigned = (cs_opt_value)value;
1071
0
    return CS_ERR_OK;
1072
1073
27.2k
  case CS_OPT_DETAIL:
1074
27.2k
    handle->detail_opt |= (cs_opt_value)value;
1075
27.2k
    return CS_ERR_OK;
1076
1077
0
  case CS_OPT_SKIPDATA:
1078
0
    handle->skipdata = (value == CS_OPT_ON);
1079
0
    if (handle->skipdata) {
1080
0
      if (handle->skipdata_size == 0) {
1081
        // set the default skipdata size
1082
0
        handle->skipdata_size = skipdata_size(handle);
1083
0
      }
1084
0
    }
1085
0
    return CS_ERR_OK;
1086
1087
0
  case CS_OPT_SKIPDATA_SETUP:
1088
0
    if (value) {
1089
0
      handle->skipdata_setup = *((cs_opt_skipdata *)value);
1090
0
      if (handle->skipdata_setup.mnemonic == NULL) {
1091
0
        handle->skipdata_setup.mnemonic = SKIPDATA_MNEM;
1092
0
      }
1093
0
    }
1094
0
    return CS_ERR_OK;
1095
1096
0
  case CS_OPT_MNEMONIC:
1097
0
    opt = (cs_opt_mnem *)value;
1098
0
    if (opt->id) {
1099
0
      if (opt->mnemonic) {
1100
0
        struct insn_mnem *tmp;
1101
1102
        // add new instruction, or replace existing instruction
1103
        // 1. find if we already had this insn in the linked list
1104
0
        tmp = handle->mnem_list;
1105
0
        while (tmp) {
1106
0
          if (tmp->insn.id == opt->id) {
1107
            // found this instruction, so replace its mnemonic
1108
0
            (void)strncpy(
1109
0
              tmp->insn.mnemonic,
1110
0
              opt->mnemonic,
1111
0
              sizeof(tmp->insn
1112
0
                       .mnemonic) -
1113
0
                1);
1114
0
            tmp->insn.mnemonic
1115
0
              [sizeof(tmp->insn.mnemonic) -
1116
0
               1] = '\0';
1117
0
            break;
1118
0
          }
1119
0
          tmp = tmp->next;
1120
0
        }
1121
1122
        // 2. add this instruction if we have not had it yet
1123
0
        if (!tmp) {
1124
0
          tmp = cs_mem_malloc(sizeof(*tmp));
1125
0
          if (!tmp) {
1126
0
            return CS_ERR_MEM;
1127
0
          }
1128
1129
0
          tmp->insn.id = opt->id;
1130
0
          (void)strncpy(
1131
0
            tmp->insn.mnemonic,
1132
0
            opt->mnemonic,
1133
0
            sizeof(tmp->insn.mnemonic) - 1);
1134
0
          tmp->insn.mnemonic
1135
0
            [sizeof(tmp->insn.mnemonic) - 1] =
1136
0
            '\0';
1137
          // this new instruction is heading the list
1138
0
          tmp->next = handle->mnem_list;
1139
0
          handle->mnem_list = tmp;
1140
0
        }
1141
0
        return CS_ERR_OK;
1142
0
      } else {
1143
0
        struct insn_mnem *prev, *tmp;
1144
1145
        // we want to delete an existing instruction
1146
        // iterate the list to find the instruction to remove it
1147
0
        tmp = handle->mnem_list;
1148
0
        prev = tmp;
1149
0
        while (tmp) {
1150
0
          if (tmp->insn.id == opt->id) {
1151
            // delete this instruction
1152
0
            if (tmp == prev) {
1153
              // head of the list
1154
0
              handle->mnem_list =
1155
0
                tmp->next;
1156
0
            } else {
1157
0
              prev->next = tmp->next;
1158
0
            }
1159
0
            cs_mem_free(tmp);
1160
0
            break;
1161
0
          }
1162
0
          prev = tmp;
1163
0
          tmp = tmp->next;
1164
0
        }
1165
0
      }
1166
0
    }
1167
0
    return CS_ERR_OK;
1168
1169
0
  case CS_OPT_MODE:
1170
    // verify if requested mode is valid
1171
0
    if (value &
1172
0
        arch_configs[handle->arch].arch_disallowed_mode_mask) {
1173
0
      return CS_ERR_OPTION;
1174
0
    }
1175
0
    break;
1176
0
  case CS_OPT_ONLY_OFFSET_BRANCH:
1177
0
    handle->PrintBranchImmAsAddress = value == CS_OPT_ON ? false :
1178
0
                       true;
1179
0
    return CS_ERR_OK;
1180
30.1k
  }
1181
1182
2.90k
  if (!arch_configs[handle->arch].arch_option)
1183
0
    return CS_ERR_ARCH;
1184
1185
2.90k
  return arch_configs[handle->arch].arch_option(handle, type, value);
1186
2.90k
}
1187
1188
// generate @op_str for data instruction of SKIPDATA
1189
#ifndef CAPSTONE_DIET
1190
static void skipdata_opstr(char *opstr, const uint8_t *buffer, size_t size)
1191
0
{
1192
0
  char *p = opstr;
1193
0
  int len;
1194
0
  size_t i;
1195
0
  size_t available = sizeof(((cs_insn *)NULL)->op_str);
1196
1197
0
  if (!size) {
1198
0
    opstr[0] = '\0';
1199
0
    return;
1200
0
  }
1201
1202
0
  len = cs_snprintf(p, available, "0x%02x", buffer[0]);
1203
0
  p += len;
1204
0
  available -= len;
1205
1206
0
  for (i = 1; i < size; i++) {
1207
0
    len = cs_snprintf(p, available, ", 0x%02x", buffer[i]);
1208
0
    if (len < 0) {
1209
0
      break;
1210
0
    }
1211
0
    if ((size_t)len > available - 1) {
1212
0
      break;
1213
0
    }
1214
0
    p += len;
1215
0
    available -= len;
1216
0
  }
1217
0
}
1218
#endif
1219
1220
// dynamically allocate memory to contain disasm insn
1221
// NOTE: caller must free() the allocated memory itself to avoid memory leaking
1222
CAPSTONE_EXPORT
1223
size_t CAPSTONE_API cs_disasm(csh ud, const uint8_t *buffer, size_t size,
1224
            uint64_t offset, size_t count, cs_insn **insn)
1225
27.2k
{
1226
27.2k
  struct cs_struct *handle;
1227
27.2k
  MCInst mci;
1228
27.2k
  uint16_t insn_size;
1229
27.2k
  size_t c = 0, i;
1230
27.2k
  unsigned int f = 0; // index of the next instruction in the cache
1231
27.2k
  cs_insn *insn_cache; // cache contains disassembled instructions
1232
27.2k
  void *total = NULL;
1233
27.2k
  size_t total_size =
1234
27.2k
    0; // total size of output buffer containing all insns
1235
27.2k
  bool r;
1236
27.2k
  void *tmp;
1237
27.2k
  size_t skipdata_bytes;
1238
27.2k
  uint64_t offset_org; // save all the original info of the buffer
1239
27.2k
  size_t size_org;
1240
27.2k
  const uint8_t *buffer_org;
1241
27.2k
  unsigned int cache_size = INSN_CACHE_SIZE;
1242
27.2k
  size_t next_offset;
1243
1244
27.2k
  handle = (struct cs_struct *)(uintptr_t)ud;
1245
27.2k
  if (!handle) {
1246
    // FIXME: how to handle this case:
1247
    // handle->errnum = CS_ERR_HANDLE;
1248
0
    return 0;
1249
0
  }
1250
1251
27.2k
  handle->errnum = CS_ERR_OK;
1252
1253
27.2k
#ifdef CAPSTONE_USE_SYS_DYN_MEM
1254
27.2k
  if (count > 0 && count <= INSN_CACHE_SIZE)
1255
0
    cache_size = (unsigned int)count;
1256
27.2k
#endif
1257
1258
  // save the original offset for SKIPDATA
1259
27.2k
  buffer_org = buffer;
1260
27.2k
  offset_org = offset;
1261
27.2k
  size_org = size;
1262
1263
27.2k
  total_size = sizeof(cs_insn) * cache_size;
1264
27.2k
  total = cs_mem_calloc(sizeof(cs_insn), cache_size);
1265
27.2k
  if (total == NULL) {
1266
    // insufficient memory
1267
0
    handle->errnum = CS_ERR_MEM;
1268
0
    return 0;
1269
0
  }
1270
1271
27.2k
  insn_cache = total;
1272
1273
1.50M
  while (size > 0) {
1274
1.48M
    MCInst_Init(&mci, handle->arch);
1275
1.48M
    mci.csh = handle;
1276
1277
    // relative branches need to know the address & size of current insn
1278
1.48M
    mci.address = offset;
1279
1280
1.48M
    if (handle->detail_opt) {
1281
      // allocate memory for @detail pointer
1282
1.48M
      insn_cache->detail =
1283
1.48M
        cs_mem_calloc(1, sizeof(cs_detail));
1284
1.48M
      if (!insn_cache->detail) {
1285
0
        insn_cache = (cs_insn *)total;
1286
0
        for (i = 0; i < c; i++, insn_cache++)
1287
0
          cs_mem_free(insn_cache->detail);
1288
1289
0
        cs_mem_free(total);
1290
0
        *insn = NULL;
1291
1292
0
        handle->errnum = CS_ERR_MEM;
1293
0
        return 0;
1294
0
      }
1295
1.48M
    } else {
1296
0
      insn_cache->detail = NULL;
1297
0
    }
1298
1299
    // save all the information for non-detailed mode
1300
1.48M
    mci.flat_insn = insn_cache;
1301
1.48M
    mci.flat_insn->address = offset;
1302
#ifdef CAPSTONE_DIET
1303
    // zero out mnemonic & op_str
1304
    mci.flat_insn->mnemonic[0] = '\0';
1305
    mci.flat_insn->op_str[0] = '\0';
1306
#endif
1307
1308
1.48M
    r = handle->disasm(ud, buffer, size, &mci, &insn_size, offset,
1309
1.48M
           handle->getinsn_info);
1310
1.48M
    if (r) {
1311
1.47M
      SStream ss;
1312
1.47M
      SStream_Init(&ss);
1313
1314
1.47M
      mci.flat_insn->size = insn_size;
1315
1316
      // map internal instruction opcode to public insn ID
1317
1318
1.47M
      handle->insn_id(handle, insn_cache, mci.Opcode);
1319
1320
1.47M
      SStream_opt_unum(&ss, handle->imm_unsigned);
1321
1.47M
      handle->printer(&mci, &ss, handle->printer_info);
1322
1.47M
      fill_insn(handle, insn_cache, &ss, &mci,
1323
1.47M
          handle->post_printer, buffer);
1324
1325
      // adjust for pseudo opcode (X86)
1326
1.47M
      if (handle->arch == CS_ARCH_X86 &&
1327
322k
          insn_cache->id != X86_INS_VCMP)
1328
320k
        insn_cache->id += mci.popcode_adjust;
1329
1330
1.47M
      next_offset = insn_size;
1331
1.47M
    } else {
1332
      // encounter a broken instruction
1333
1334
      // free memory of @detail pointer
1335
15.4k
      if (handle->detail_opt) {
1336
15.4k
        cs_mem_free(insn_cache->detail);
1337
15.4k
      }
1338
1339
      // if there is no request to skip data, or remaining data is too small,
1340
      // then bail out
1341
15.4k
      if (!handle->skipdata || handle->skipdata_size > size)
1342
15.4k
        break;
1343
1344
0
      if (handle->skipdata_setup.callback) {
1345
0
        skipdata_bytes = handle->skipdata_setup.callback(
1346
0
          buffer_org, size_org,
1347
0
          (size_t)(offset - offset_org),
1348
0
          handle->skipdata_setup.user_data);
1349
0
        if (skipdata_bytes > size)
1350
          // remaining data is not enough
1351
0
          break;
1352
1353
0
        if (!skipdata_bytes)
1354
          // user requested not to skip data, so bail out
1355
0
          break;
1356
0
      } else
1357
0
        skipdata_bytes = handle->skipdata_size;
1358
1359
      // we have to skip some amount of data, depending on arch & mode
1360
      // invalid ID for this "data" instruction
1361
0
      insn_cache->id = 0;
1362
0
      insn_cache->address = offset;
1363
0
      insn_cache->size = (uint16_t)MIN(
1364
0
        skipdata_bytes, sizeof(insn_cache->bytes));
1365
0
      memcpy(insn_cache->bytes, buffer,
1366
0
             MIN(skipdata_bytes, sizeof(insn_cache->bytes)));
1367
#ifdef CAPSTONE_DIET
1368
      insn_cache->mnemonic[0] = '\0';
1369
      insn_cache->op_str[0] = '\0';
1370
#else
1371
0
      strncpy(insn_cache->mnemonic,
1372
0
        handle->skipdata_setup.mnemonic,
1373
0
        sizeof(insn_cache->mnemonic) - 1);
1374
0
      skipdata_opstr(insn_cache->op_str, buffer,
1375
0
               skipdata_bytes);
1376
0
#endif
1377
0
      insn_cache->detail = NULL;
1378
1379
0
      next_offset = skipdata_bytes;
1380
0
    }
1381
1382
    // one more instruction entering the cache
1383
1.47M
    f++;
1384
1385
    // one more instruction disassembled
1386
1.47M
    c++;
1387
1.47M
    if (count > 0 && c == count)
1388
      // already got requested number of instructions
1389
0
      break;
1390
1391
1.47M
    if (f == cache_size) {
1392
      // full cache, so expand the cache to contain incoming insns
1393
15.5k
      cache_size = cache_size * 8 / 5; // * 1.6 ~ golden ratio
1394
15.5k
      total_size += (sizeof(cs_insn) * cache_size);
1395
15.5k
      tmp = cs_mem_realloc(total, total_size);
1396
15.5k
      if (tmp == NULL) { // insufficient memory
1397
0
        if (handle->detail_opt) {
1398
0
          insn_cache = (cs_insn *)total;
1399
0
          for (i = 0; i < c; i++, insn_cache++)
1400
0
            cs_mem_free(insn_cache->detail);
1401
0
        }
1402
1403
0
        cs_mem_free(total);
1404
0
        *insn = NULL;
1405
0
        handle->errnum = CS_ERR_MEM;
1406
0
        return 0;
1407
0
      }
1408
1409
15.5k
      total = tmp;
1410
      // continue to fill in the cache after the last instruction
1411
15.5k
      insn_cache = (cs_insn *)((char *)total +
1412
15.5k
             sizeof(cs_insn) * c);
1413
1414
      // reset f back to 0, so we fill in the cache from beginning
1415
15.5k
      f = 0;
1416
15.5k
    } else
1417
1.45M
      insn_cache++;
1418
1419
1.47M
    buffer += next_offset;
1420
1.47M
    size -= next_offset;
1421
1.47M
    offset += next_offset;
1422
1.47M
  }
1423
1424
27.2k
  if (!c) {
1425
    // we did not disassemble any instruction
1426
688
    cs_mem_free(total);
1427
688
    total = NULL;
1428
26.5k
  } else if (f != cache_size) {
1429
    // total did not fully use the last cache, so downsize it
1430
26.5k
    tmp = cs_mem_realloc(total,
1431
26.5k
             total_size - (cache_size - f) *
1432
26.5k
                sizeof(*insn_cache));
1433
26.5k
    if (tmp == NULL) { // insufficient memory
1434
      // free all detail pointers
1435
0
      if (handle->detail_opt) {
1436
0
        insn_cache = (cs_insn *)total;
1437
0
        for (i = 0; i < c; i++, insn_cache++)
1438
0
          cs_mem_free(insn_cache->detail);
1439
0
      }
1440
1441
0
      cs_mem_free(total);
1442
0
      *insn = NULL;
1443
1444
0
      handle->errnum = CS_ERR_MEM;
1445
0
      return 0;
1446
0
    }
1447
1448
26.5k
    total = tmp;
1449
26.5k
  }
1450
1451
27.2k
  *insn = total;
1452
1453
27.2k
  return c;
1454
27.2k
}
1455
1456
CAPSTONE_EXPORT
1457
void CAPSTONE_API cs_free(cs_insn *insn, size_t count)
1458
26.5k
{
1459
26.5k
  size_t i;
1460
1461
  // free all detail pointers
1462
1.50M
  for (i = 0; i < count; i++)
1463
1.47M
    cs_mem_free(insn[i].detail);
1464
1465
  // then free pointer to cs_insn array
1466
26.5k
  cs_mem_free(insn);
1467
26.5k
}
1468
1469
CAPSTONE_EXPORT
1470
cs_insn *CAPSTONE_API cs_malloc(csh ud)
1471
0
{
1472
0
  cs_insn *insn;
1473
0
  struct cs_struct *handle = (struct cs_struct *)(uintptr_t)ud;
1474
1475
0
  insn = cs_mem_malloc(sizeof(cs_insn));
1476
0
  if (!insn) {
1477
    // insufficient memory
1478
0
    handle->errnum = CS_ERR_MEM;
1479
0
    return NULL;
1480
0
  } else {
1481
0
    if (handle->detail_opt) {
1482
      // allocate memory for @detail pointer
1483
0
      insn->detail = cs_mem_malloc(sizeof(cs_detail));
1484
0
      if (insn->detail == NULL) { // insufficient memory
1485
0
        cs_mem_free(insn);
1486
0
        handle->errnum = CS_ERR_MEM;
1487
0
        return NULL;
1488
0
      }
1489
0
    } else
1490
0
      insn->detail = NULL;
1491
0
  }
1492
1493
0
  return insn;
1494
0
}
1495
1496
// iterator for instruction "single-stepping"
1497
CAPSTONE_EXPORT
1498
bool CAPSTONE_API cs_disasm_iter(csh ud, const uint8_t **code, size_t *size,
1499
         uint64_t *address, cs_insn *insn)
1500
0
{
1501
0
  struct cs_struct *handle;
1502
0
  uint16_t insn_size;
1503
0
  MCInst mci;
1504
0
  bool r;
1505
1506
0
  handle = (struct cs_struct *)(uintptr_t)ud;
1507
0
  if (!handle) {
1508
0
    return false;
1509
0
  }
1510
1511
0
  handle->errnum = CS_ERR_OK;
1512
1513
0
  MCInst_Init(&mci, handle->arch);
1514
0
  mci.csh = handle;
1515
1516
  // relative branches need to know the address & size of current insn
1517
0
  mci.address = *address;
1518
1519
  // save all the information for non-detailed mode
1520
0
  mci.flat_insn = insn;
1521
0
  mci.flat_insn->address = *address;
1522
#ifdef CAPSTONE_DIET
1523
  // zero out mnemonic & op_str
1524
  mci.flat_insn->mnemonic[0] = '\0';
1525
  mci.flat_insn->op_str[0] = '\0';
1526
#endif
1527
1528
0
  r = handle->disasm(ud, *code, *size, &mci, &insn_size, *address,
1529
0
         handle->getinsn_info);
1530
0
  if (r) {
1531
0
    SStream ss;
1532
0
    SStream_Init(&ss);
1533
1534
0
    mci.flat_insn->size = insn_size;
1535
1536
    // map internal instruction opcode to public insn ID
1537
0
    handle->insn_id(handle, insn, mci.Opcode);
1538
1539
0
    SStream_opt_unum(&ss, handle->imm_unsigned);
1540
0
    handle->printer(&mci, &ss, handle->printer_info);
1541
1542
0
    fill_insn(handle, insn, &ss, &mci, handle->post_printer, *code);
1543
1544
    // adjust for pseudo opcode (X86)
1545
0
    if (handle->arch == CS_ARCH_X86)
1546
0
      insn->id += mci.popcode_adjust;
1547
1548
0
    *code += insn_size;
1549
0
    *size -= insn_size;
1550
0
    *address += insn_size;
1551
0
  } else { // encounter a broken instruction
1552
0
    size_t skipdata_bytes;
1553
1554
    // if there is no request to skip data, or remaining data is too small,
1555
    // then bail out
1556
0
    if (!handle->skipdata || handle->skipdata_size > *size)
1557
0
      return false;
1558
1559
0
    if (handle->skipdata_setup.callback) {
1560
0
      skipdata_bytes = handle->skipdata_setup.callback(
1561
0
        *code, *size, 0,
1562
0
        handle->skipdata_setup.user_data);
1563
0
      if (skipdata_bytes > *size)
1564
        // remaining data is not enough
1565
0
        return false;
1566
1567
0
      if (!skipdata_bytes)
1568
        // user requested not to skip data, so bail out
1569
0
        return false;
1570
0
    } else
1571
0
      skipdata_bytes = handle->skipdata_size;
1572
1573
    // we have to skip some amount of data, depending on arch & mode
1574
0
    insn->id = 0; // invalid ID for this "data" instruction
1575
0
    insn->address = *address;
1576
0
    insn->size = (uint16_t)MIN(skipdata_bytes, sizeof(insn->bytes));
1577
0
    memcpy(insn->bytes, *code,
1578
0
           MIN(skipdata_bytes, sizeof(insn->bytes)));
1579
#ifdef CAPSTONE_DIET
1580
    insn->mnemonic[0] = '\0';
1581
    insn->op_str[0] = '\0';
1582
#else
1583
0
    strncpy(insn->mnemonic, handle->skipdata_setup.mnemonic,
1584
0
      sizeof(insn->mnemonic) - 1);
1585
0
    skipdata_opstr(insn->op_str, *code, skipdata_bytes);
1586
0
#endif
1587
1588
0
    *code += skipdata_bytes;
1589
0
    *size -= skipdata_bytes;
1590
0
    *address += skipdata_bytes;
1591
0
  }
1592
1593
0
  return true;
1594
0
}
1595
1596
// return friendly name of register in a string
1597
CAPSTONE_EXPORT
1598
const char *CAPSTONE_API cs_reg_name(csh ud, unsigned int reg)
1599
1.46M
{
1600
1.46M
  struct cs_struct *handle = (struct cs_struct *)(uintptr_t)ud;
1601
1602
1.46M
  if (!handle || handle->reg_name == NULL) {
1603
0
    return NULL;
1604
0
  }
1605
1606
1.46M
  return handle->reg_name(ud, reg);
1607
1.46M
}
1608
1609
CAPSTONE_EXPORT
1610
const char *CAPSTONE_API cs_insn_name(csh ud, unsigned int insn)
1611
1.47M
{
1612
1.47M
  struct cs_struct *handle = (struct cs_struct *)(uintptr_t)ud;
1613
1614
1.47M
  if (!handle || handle->insn_name == NULL) {
1615
0
    return NULL;
1616
0
  }
1617
1618
1.47M
  return handle->insn_name(ud, insn);
1619
1.47M
}
1620
1621
CAPSTONE_EXPORT
1622
const char *CAPSTONE_API cs_group_name(csh ud, unsigned int group)
1623
1.25M
{
1624
1.25M
  struct cs_struct *handle = (struct cs_struct *)(uintptr_t)ud;
1625
1626
1.25M
  if (!handle || handle->group_name == NULL) {
1627
0
    return NULL;
1628
0
  }
1629
1630
1.25M
  return handle->group_name(ud, group);
1631
1.25M
}
1632
1633
CAPSTONE_EXPORT
1634
bool CAPSTONE_API cs_insn_group(csh ud, const cs_insn *insn,
1635
        unsigned int group_id)
1636
0
{
1637
0
  struct cs_struct *handle;
1638
0
  if (!ud)
1639
0
    return false;
1640
1641
0
  handle = (struct cs_struct *)(uintptr_t)ud;
1642
1643
0
  if (!handle->detail_opt) {
1644
0
    handle->errnum = CS_ERR_DETAIL;
1645
0
    return false;
1646
0
  }
1647
1648
0
  if (!insn->id) {
1649
0
    handle->errnum = CS_ERR_SKIPDATA;
1650
0
    return false;
1651
0
  }
1652
1653
0
  if (!insn->detail) {
1654
0
    handle->errnum = CS_ERR_DETAIL;
1655
0
    return false;
1656
0
  }
1657
1658
0
  return arr_exist8(insn->detail->groups, insn->detail->groups_count,
1659
0
        group_id);
1660
0
}
1661
1662
CAPSTONE_EXPORT
1663
bool CAPSTONE_API cs_reg_read(csh ud, const cs_insn *insn, unsigned int reg_id)
1664
0
{
1665
0
  struct cs_struct *handle;
1666
0
  if (!ud)
1667
0
    return false;
1668
1669
0
  handle = (struct cs_struct *)(uintptr_t)ud;
1670
1671
0
  if (!handle->detail_opt) {
1672
0
    handle->errnum = CS_ERR_DETAIL;
1673
0
    return false;
1674
0
  }
1675
1676
0
  if (!insn->id) {
1677
0
    handle->errnum = CS_ERR_SKIPDATA;
1678
0
    return false;
1679
0
  }
1680
1681
0
  if (!insn->detail) {
1682
0
    handle->errnum = CS_ERR_DETAIL;
1683
0
    return false;
1684
0
  }
1685
1686
0
  return arr_exist(insn->detail->regs_read, insn->detail->regs_read_count,
1687
0
       reg_id);
1688
0
}
1689
1690
CAPSTONE_EXPORT
1691
bool CAPSTONE_API cs_reg_write(csh ud, const cs_insn *insn, unsigned int reg_id)
1692
0
{
1693
0
  struct cs_struct *handle;
1694
0
  if (!ud)
1695
0
    return false;
1696
1697
0
  handle = (struct cs_struct *)(uintptr_t)ud;
1698
1699
0
  if (!handle->detail_opt) {
1700
0
    handle->errnum = CS_ERR_DETAIL;
1701
0
    return false;
1702
0
  }
1703
1704
0
  if (!insn->id) {
1705
0
    handle->errnum = CS_ERR_SKIPDATA;
1706
0
    return false;
1707
0
  }
1708
1709
0
  if (!insn->detail) {
1710
0
    handle->errnum = CS_ERR_DETAIL;
1711
0
    return false;
1712
0
  }
1713
1714
0
  return arr_exist(insn->detail->regs_write,
1715
0
       insn->detail->regs_write_count, reg_id);
1716
0
}
1717
1718
CAPSTONE_EXPORT
1719
int CAPSTONE_API cs_op_count(csh ud, const cs_insn *insn, unsigned int op_type)
1720
0
{
1721
0
  struct cs_struct *handle;
1722
0
  unsigned int count = 0, i;
1723
0
  if (!ud)
1724
0
    return -1;
1725
1726
0
  handle = (struct cs_struct *)(uintptr_t)ud;
1727
1728
0
  if (!handle->detail_opt) {
1729
0
    handle->errnum = CS_ERR_DETAIL;
1730
0
    return -1;
1731
0
  }
1732
1733
0
  if (!insn->id) {
1734
0
    handle->errnum = CS_ERR_SKIPDATA;
1735
0
    return -1;
1736
0
  }
1737
1738
0
  if (!insn->detail) {
1739
0
    handle->errnum = CS_ERR_DETAIL;
1740
0
    return -1;
1741
0
  }
1742
1743
0
  handle->errnum = CS_ERR_OK;
1744
1745
0
  switch (handle->arch) {
1746
0
  default:
1747
0
    handle->errnum = CS_ERR_HANDLE;
1748
0
    return -1;
1749
0
  case CS_ARCH_ARM:
1750
0
    for (i = 0; i < insn->detail->arm.op_count; i++)
1751
0
      if (insn->detail->arm.operands[i].type ==
1752
0
          (arm_op_type)op_type)
1753
0
        count++;
1754
0
    break;
1755
0
  case CS_ARCH_AARCH64:
1756
0
    for (i = 0; i < insn->detail->aarch64.op_count; i++)
1757
0
      if (insn->detail->aarch64.operands[i].type ==
1758
0
          (aarch64_op_type)op_type)
1759
0
        count++;
1760
0
    break;
1761
0
  case CS_ARCH_X86:
1762
0
    for (i = 0; i < insn->detail->x86.op_count; i++)
1763
0
      if (insn->detail->x86.operands[i].type ==
1764
0
          (x86_op_type)op_type)
1765
0
        count++;
1766
0
    break;
1767
0
  case CS_ARCH_MIPS:
1768
0
    for (i = 0; i < insn->detail->mips.op_count; i++)
1769
0
      if (insn->detail->mips.operands[i].type ==
1770
0
          (mips_op_type)op_type)
1771
0
        count++;
1772
0
    break;
1773
0
  case CS_ARCH_PPC:
1774
0
    for (i = 0; i < insn->detail->ppc.op_count; i++)
1775
0
      if (insn->detail->ppc.operands[i].type ==
1776
0
          (ppc_op_type)op_type)
1777
0
        count++;
1778
0
    break;
1779
0
  case CS_ARCH_SPARC:
1780
0
    for (i = 0; i < insn->detail->sparc.op_count; i++)
1781
0
      if (insn->detail->sparc.operands[i].type ==
1782
0
          (sparc_op_type)op_type)
1783
0
        count++;
1784
0
    break;
1785
0
  case CS_ARCH_SYSTEMZ:
1786
0
    for (i = 0; i < insn->detail->systemz.op_count; i++)
1787
0
      if (insn->detail->systemz.operands[i].type ==
1788
0
          (systemz_op_type)op_type)
1789
0
        count++;
1790
0
    break;
1791
0
  case CS_ARCH_XCORE:
1792
0
    for (i = 0; i < insn->detail->xcore.op_count; i++)
1793
0
      if (insn->detail->xcore.operands[i].type ==
1794
0
          (xcore_op_type)op_type)
1795
0
        count++;
1796
0
    break;
1797
0
  case CS_ARCH_M68K:
1798
0
    for (i = 0; i < insn->detail->m68k.op_count; i++)
1799
0
      if (insn->detail->m68k.operands[i].type ==
1800
0
          (m68k_op_type)op_type)
1801
0
        count++;
1802
0
    break;
1803
0
  case CS_ARCH_TMS320C64X:
1804
0
    for (i = 0; i < insn->detail->tms320c64x.op_count; i++)
1805
0
      if (insn->detail->tms320c64x.operands[i].type ==
1806
0
          (tms320c64x_op_type)op_type)
1807
0
        count++;
1808
0
    break;
1809
0
  case CS_ARCH_M680X:
1810
0
    for (i = 0; i < insn->detail->m680x.op_count; i++)
1811
0
      if (insn->detail->m680x.operands[i].type ==
1812
0
          (m680x_op_type)op_type)
1813
0
        count++;
1814
0
    break;
1815
0
  case CS_ARCH_EVM:
1816
0
    break;
1817
0
  case CS_ARCH_MOS65XX:
1818
0
    for (i = 0; i < insn->detail->mos65xx.op_count; i++)
1819
0
      if (insn->detail->mos65xx.operands[i].type ==
1820
0
          (mos65xx_op_type)op_type)
1821
0
        count++;
1822
0
    break;
1823
0
  case CS_ARCH_WASM:
1824
0
    for (i = 0; i < insn->detail->wasm.op_count; i++)
1825
0
      if (insn->detail->wasm.operands[i].type ==
1826
0
          (wasm_op_type)op_type)
1827
0
        count++;
1828
0
    break;
1829
0
  case CS_ARCH_BPF:
1830
0
    for (i = 0; i < insn->detail->bpf.op_count; i++)
1831
0
      if (insn->detail->bpf.operands[i].type ==
1832
0
          (bpf_op_type)op_type)
1833
0
        count++;
1834
0
    break;
1835
0
  case CS_ARCH_RISCV:
1836
0
    for (i = 0; i < insn->detail->riscv.op_count; i++)
1837
0
      if (insn->detail->riscv.operands[i].type ==
1838
0
          (riscv_op_type)op_type)
1839
0
        count++;
1840
0
    break;
1841
0
  case CS_ARCH_TRICORE:
1842
0
    for (i = 0; i < insn->detail->tricore.op_count; i++)
1843
0
      if (insn->detail->tricore.operands[i].type ==
1844
0
          (tricore_op_type)op_type)
1845
0
        count++;
1846
0
    break;
1847
0
  case CS_ARCH_ALPHA:
1848
0
    for (i = 0; i < insn->detail->alpha.op_count; i++)
1849
0
      if (insn->detail->alpha.operands[i].type ==
1850
0
          (alpha_op_type)op_type)
1851
0
        count++;
1852
0
    break;
1853
0
  case CS_ARCH_HPPA:
1854
0
    for (i = 0; i < insn->detail->hppa.op_count; i++)
1855
0
      if (insn->detail->hppa.operands[i].type ==
1856
0
          (hppa_op_type)op_type)
1857
0
        count++;
1858
0
    break;
1859
0
  case CS_ARCH_LOONGARCH:
1860
0
    for (i = 0; i < insn->detail->loongarch.op_count; i++)
1861
0
      if (insn->detail->loongarch.operands[i].type ==
1862
0
          (loongarch_op_type)op_type)
1863
0
        count++;
1864
0
    break;
1865
0
  case CS_ARCH_ARC:
1866
0
    for (i = 0; i < insn->detail->arc.op_count; i++)
1867
0
      if (insn->detail->arc.operands[i].type ==
1868
0
          (arc_op_type)op_type)
1869
0
        count++;
1870
0
    break;
1871
0
  }
1872
1873
0
  return count;
1874
0
}
1875
1876
CAPSTONE_EXPORT
1877
int CAPSTONE_API cs_op_index(csh ud, const cs_insn *insn, unsigned int op_type,
1878
           unsigned int post)
1879
0
{
1880
0
  struct cs_struct *handle;
1881
0
  unsigned int count = 0, i;
1882
0
  if (!ud)
1883
0
    return -1;
1884
1885
0
  handle = (struct cs_struct *)(uintptr_t)ud;
1886
1887
0
  if (!handle->detail_opt) {
1888
0
    handle->errnum = CS_ERR_DETAIL;
1889
0
    return -1;
1890
0
  }
1891
1892
0
  if (!insn->id) {
1893
0
    handle->errnum = CS_ERR_SKIPDATA;
1894
0
    return -1;
1895
0
  }
1896
1897
0
  if (!insn->detail) {
1898
0
    handle->errnum = CS_ERR_DETAIL;
1899
0
    return -1;
1900
0
  }
1901
1902
0
  handle->errnum = CS_ERR_OK;
1903
1904
0
  switch (handle->arch) {
1905
0
  default:
1906
0
    handle->errnum = CS_ERR_HANDLE;
1907
0
    return -1;
1908
0
  case CS_ARCH_ARM:
1909
0
    for (i = 0; i < insn->detail->arm.op_count; i++) {
1910
0
      if (insn->detail->arm.operands[i].type ==
1911
0
          (arm_op_type)op_type)
1912
0
        count++;
1913
0
      if (count == post)
1914
0
        return i;
1915
0
    }
1916
0
    break;
1917
0
  case CS_ARCH_AARCH64:
1918
0
    for (i = 0; i < insn->detail->aarch64.op_count; i++) {
1919
0
      if (insn->detail->aarch64.operands[i].type ==
1920
0
          (aarch64_op_type)op_type)
1921
0
        count++;
1922
0
      if (count == post)
1923
0
        return i;
1924
0
    }
1925
0
    break;
1926
0
  case CS_ARCH_X86:
1927
0
    for (i = 0; i < insn->detail->x86.op_count; i++) {
1928
0
      if (insn->detail->x86.operands[i].type ==
1929
0
          (x86_op_type)op_type)
1930
0
        count++;
1931
0
      if (count == post)
1932
0
        return i;
1933
0
    }
1934
0
    break;
1935
0
  case CS_ARCH_MIPS:
1936
0
    for (i = 0; i < insn->detail->mips.op_count; i++) {
1937
0
      if (insn->detail->mips.operands[i].type ==
1938
0
          (mips_op_type)op_type)
1939
0
        count++;
1940
0
      if (count == post)
1941
0
        return i;
1942
0
    }
1943
0
    break;
1944
0
  case CS_ARCH_PPC:
1945
0
    for (i = 0; i < insn->detail->ppc.op_count; i++) {
1946
0
      if (insn->detail->ppc.operands[i].type ==
1947
0
          (ppc_op_type)op_type)
1948
0
        count++;
1949
0
      if (count == post)
1950
0
        return i;
1951
0
    }
1952
0
    break;
1953
0
  case CS_ARCH_SPARC:
1954
0
    for (i = 0; i < insn->detail->sparc.op_count; i++) {
1955
0
      if (insn->detail->sparc.operands[i].type ==
1956
0
          (sparc_op_type)op_type)
1957
0
        count++;
1958
0
      if (count == post)
1959
0
        return i;
1960
0
    }
1961
0
    break;
1962
0
  case CS_ARCH_SYSTEMZ:
1963
0
    for (i = 0; i < insn->detail->systemz.op_count; i++) {
1964
0
      if (insn->detail->systemz.operands[i].type ==
1965
0
          (systemz_op_type)op_type)
1966
0
        count++;
1967
0
      if (count == post)
1968
0
        return i;
1969
0
    }
1970
0
    break;
1971
0
  case CS_ARCH_XCORE:
1972
0
    for (i = 0; i < insn->detail->xcore.op_count; i++) {
1973
0
      if (insn->detail->xcore.operands[i].type ==
1974
0
          (xcore_op_type)op_type)
1975
0
        count++;
1976
0
      if (count == post)
1977
0
        return i;
1978
0
    }
1979
0
    break;
1980
0
  case CS_ARCH_TRICORE:
1981
0
    for (i = 0; i < insn->detail->tricore.op_count; i++) {
1982
0
      if (insn->detail->tricore.operands[i].type ==
1983
0
          (tricore_op_type)op_type)
1984
0
        count++;
1985
0
      if (count == post)
1986
0
        return i;
1987
0
    }
1988
0
    break;
1989
0
  case CS_ARCH_M68K:
1990
0
    for (i = 0; i < insn->detail->m68k.op_count; i++) {
1991
0
      if (insn->detail->m68k.operands[i].type ==
1992
0
          (m68k_op_type)op_type)
1993
0
        count++;
1994
0
      if (count == post)
1995
0
        return i;
1996
0
    }
1997
0
    break;
1998
0
  case CS_ARCH_TMS320C64X:
1999
0
    for (i = 0; i < insn->detail->tms320c64x.op_count; i++) {
2000
0
      if (insn->detail->tms320c64x.operands[i].type ==
2001
0
          (tms320c64x_op_type)op_type)
2002
0
        count++;
2003
0
      if (count == post)
2004
0
        return i;
2005
0
    }
2006
0
    break;
2007
0
  case CS_ARCH_M680X:
2008
0
    for (i = 0; i < insn->detail->m680x.op_count; i++) {
2009
0
      if (insn->detail->m680x.operands[i].type ==
2010
0
          (m680x_op_type)op_type)
2011
0
        count++;
2012
0
      if (count == post)
2013
0
        return i;
2014
0
    }
2015
0
    break;
2016
0
  case CS_ARCH_EVM:
2017
#if 0
2018
      for (i = 0; i < insn->detail->evm.op_count; i++) {
2019
        if (insn->detail->evm.operands[i].type == (evm_op_type)op_type)
2020
          count++;
2021
        if (count == post)
2022
          return i;
2023
      }
2024
#endif
2025
0
    break;
2026
0
  case CS_ARCH_MOS65XX:
2027
0
    for (i = 0; i < insn->detail->mos65xx.op_count; i++) {
2028
0
      if (insn->detail->mos65xx.operands[i].type ==
2029
0
          (mos65xx_op_type)op_type)
2030
0
        count++;
2031
0
      if (count == post)
2032
0
        return i;
2033
0
    }
2034
0
    break;
2035
0
  case CS_ARCH_WASM:
2036
0
    for (i = 0; i < insn->detail->wasm.op_count; i++) {
2037
0
      if (insn->detail->wasm.operands[i].type ==
2038
0
          (wasm_op_type)op_type)
2039
0
        count++;
2040
0
      if (count == post)
2041
0
        return i;
2042
0
    }
2043
0
    break;
2044
0
  case CS_ARCH_BPF:
2045
0
    for (i = 0; i < insn->detail->bpf.op_count; i++) {
2046
0
      if (insn->detail->bpf.operands[i].type ==
2047
0
          (bpf_op_type)op_type)
2048
0
        count++;
2049
0
      if (count == post)
2050
0
        return i;
2051
0
    }
2052
0
    break;
2053
0
  case CS_ARCH_RISCV:
2054
0
    for (i = 0; i < insn->detail->riscv.op_count; i++) {
2055
0
      if (insn->detail->riscv.operands[i].type ==
2056
0
          (riscv_op_type)op_type)
2057
0
        count++;
2058
0
      if (count == post)
2059
0
        return i;
2060
0
    }
2061
0
    break;
2062
0
  case CS_ARCH_SH:
2063
0
    for (i = 0; i < insn->detail->sh.op_count; i++) {
2064
0
      if (insn->detail->sh.operands[i].type ==
2065
0
          (sh_op_type)op_type)
2066
0
        count++;
2067
0
      if (count == post)
2068
0
        return i;
2069
0
    }
2070
0
    break;
2071
0
  case CS_ARCH_ALPHA:
2072
0
    for (i = 0; i < insn->detail->alpha.op_count; i++) {
2073
0
      if (insn->detail->alpha.operands[i].type ==
2074
0
          (alpha_op_type)op_type)
2075
0
        count++;
2076
0
      if (count == post)
2077
0
        return i;
2078
0
    }
2079
0
    break;
2080
0
  case CS_ARCH_HPPA:
2081
0
    for (i = 0; i < insn->detail->hppa.op_count; i++) {
2082
0
      if (insn->detail->hppa.operands[i].type ==
2083
0
          (hppa_op_type)op_type)
2084
0
        count++;
2085
0
      if (count == post)
2086
0
        return i;
2087
0
    }
2088
0
    break;
2089
0
  case CS_ARCH_LOONGARCH:
2090
0
    for (i = 0; i < insn->detail->loongarch.op_count; i++) {
2091
0
      if (insn->detail->loongarch.operands[i].type ==
2092
0
          (loongarch_op_type)op_type)
2093
0
        count++;
2094
0
      if (count == post)
2095
0
        return i;
2096
0
    }
2097
0
    break;
2098
0
  case CS_ARCH_ARC:
2099
0
    for (i = 0; i < insn->detail->arc.op_count; i++) {
2100
0
      if (insn->detail->arc.operands[i].type ==
2101
0
          (arc_op_type)op_type)
2102
0
        count++;
2103
0
      if (count == post)
2104
0
        return i;
2105
0
    }
2106
0
    break;
2107
0
  }
2108
2109
0
  return -1;
2110
0
}
2111
2112
CAPSTONE_EXPORT
2113
cs_err CAPSTONE_API cs_regs_access(csh ud, const cs_insn *insn,
2114
           cs_regs regs_read, uint8_t *regs_read_count,
2115
           cs_regs regs_write,
2116
           uint8_t *regs_write_count)
2117
0
{
2118
0
  struct cs_struct *handle;
2119
2120
0
  if (!ud)
2121
0
    return CS_ERR_CSH;
2122
2123
0
  handle = (struct cs_struct *)(uintptr_t)ud;
2124
2125
#ifdef CAPSTONE_DIET
2126
  // This API does not work in DIET mode
2127
  handle->errnum = CS_ERR_DIET;
2128
  return CS_ERR_DIET;
2129
#else
2130
0
  if (!handle->detail_opt) {
2131
0
    handle->errnum = CS_ERR_DETAIL;
2132
0
    return CS_ERR_DETAIL;
2133
0
  }
2134
2135
0
  if (!insn->id) {
2136
0
    handle->errnum = CS_ERR_SKIPDATA;
2137
0
    return CS_ERR_SKIPDATA;
2138
0
  }
2139
2140
0
  if (!insn->detail) {
2141
0
    handle->errnum = CS_ERR_DETAIL;
2142
0
    return CS_ERR_DETAIL;
2143
0
  }
2144
2145
0
  if (handle->reg_access) {
2146
0
    handle->reg_access(insn, regs_read, regs_read_count, regs_write,
2147
0
           regs_write_count);
2148
0
  } else {
2149
    // this arch is unsupported yet
2150
0
    handle->errnum = CS_ERR_ARCH;
2151
0
    return CS_ERR_ARCH;
2152
0
  }
2153
2154
0
  return CS_ERR_OK;
2155
0
#endif
2156
0
}