Coverage Report

Created: 2026-05-30 06:22

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/capstonenext/SStream.c
Line
Count
Source
1
/* Capstone Disassembly Engine */
2
/* By Nguyen Anh Quynh <aquynh@gmail.com>, 2013-2019 */
3
4
#include <stdarg.h>
5
#if defined(CAPSTONE_HAS_OSXKERNEL)
6
#include <Availability.h>
7
#include <libkern/libkern.h>
8
#include <i386/limits.h>
9
#else
10
#include <stdio.h>
11
#include <limits.h>
12
#endif
13
#include <string.h>
14
15
#include <capstone/platform.h>
16
17
#include "SStream.h"
18
#include "cs_priv.h"
19
#include "utils.h"
20
21
static size_t SStream_remaining(SStream *ss)
22
21.6M
{
23
21.6M
  assert(ss);
24
21.6M
  if (ss->index >= SSTREAM_BUF_LEN) {
25
0
    ss->index = SSTREAM_BUF_LEN - 1;
26
0
    ss->buffer[ss->index] = '\0';
27
0
    return 0;
28
0
  }
29
21.6M
  return SSTREAM_BUF_LEN - ss->index - 1;
30
21.6M
}
31
32
static void SStream_concat_raw(SStream *ss, const char *s, size_t len)
33
12.1M
{
34
12.1M
  size_t remaining = SStream_remaining(ss);
35
12.1M
  if (len > remaining) {
36
0
    len = remaining;
37
0
  }
38
12.1M
  if (len > 0) {
39
12.1M
    memcpy(ss->buffer + ss->index, s, len);
40
12.1M
    ss->index += len;
41
12.1M
  }
42
12.1M
  ss->buffer[ss->index] = '\0';
43
12.1M
}
44
45
static void SStream_concat1_raw(SStream *ss, char c)
46
1.89M
{
47
1.89M
  if (SStream_remaining(ss) == 0) {
48
0
    return;
49
0
  }
50
1.89M
  ss->buffer[ss->index] = c;
51
1.89M
  ss->index++;
52
1.89M
  ss->buffer[ss->index] = '\0';
53
1.89M
}
54
55
static void SStream_close_markup(SStream *ss)
56
21.6M
{
57
21.6M
  if (ss->markup_stream && ss->prefixed_by_markup) {
58
0
    SStream_concat1_raw(ss, '>');
59
0
  }
60
21.6M
}
61
62
void SStream_Init(SStream *ss)
63
3.12M
{
64
3.12M
  assert(ss);
65
3.12M
  ss->index = 0;
66
3.12M
  memset(ss->buffer, 0, sizeof(ss->buffer));
67
3.12M
  ss->is_closed = false;
68
3.12M
  ss->markup_stream = false;
69
3.12M
  ss->prefixed_by_markup = false;
70
3.12M
  ss->unsigned_num = false;
71
3.12M
}
72
73
void SStream_opt_unum(SStream *ss, bool print_unsigned_numbers)
74
3.03M
{
75
3.03M
  assert(ss);
76
3.03M
  ss->unsigned_num = print_unsigned_numbers;
77
3.03M
}
78
79
/// Returns the a pointer to the internal string buffer of the stream.
80
/// For reading only.
81
const char *SStream_rbuf(const SStream *ss)
82
128k
{
83
128k
  assert(ss);
84
128k
  return ss->buffer;
85
128k
}
86
87
/// Searches in the stream for the first (from the left) occurrence of @elem and replaces
88
/// it with @repl. It returns the pointer *after* the replaced character
89
/// or NULL if no character was replaced.
90
///
91
/// It will never replace the final \0 byte in the stream buffer.
92
const char *SStream_replc(const SStream *ss, char elem, char repl)
93
103k
{
94
103k
  assert(ss);
95
103k
  char *found = strchr(ss->buffer, elem);
96
103k
  if (!found || found == ss->buffer + (SSTREAM_BUF_LEN - 1)) {
97
103k
    return NULL;
98
103k
  }
99
0
  *found = repl;
100
0
  found++;
101
0
  return found;
102
103k
}
103
104
/// Searches in the stream for the first (from the left) occurrence of @chr and replaces
105
/// it with @rstr.
106
void SStream_replc_str(SStream *ss, char chr, const char *rstr)
107
31.7k
{
108
31.7k
  assert(ss && rstr);
109
31.7k
  char *found = strchr(ss->buffer, chr);
110
31.7k
  if (!found || found == ss->buffer + (SSTREAM_BUF_LEN - 1)) {
111
0
    return;
112
0
  }
113
31.7k
  size_t post_len = strlen(found + 1);
114
31.7k
  size_t buf_str_len = strlen(ss->buffer);
115
31.7k
  size_t repl_len = strlen(rstr);
116
31.7k
  if (repl_len - 1 + buf_str_len >= SSTREAM_BUF_LEN) {
117
0
    return;
118
0
  }
119
31.7k
  memmove(found + repl_len, found + 1, post_len);
120
31.7k
  memcpy(found, rstr, repl_len);
121
31.7k
  ss->index = strlen(ss->buffer);
122
31.7k
}
123
124
/// Removes the space characters '\t' and ' ' from the beginning of the stream buffer.
125
void SStream_trimls(SStream *ss)
126
3.13M
{
127
3.13M
  assert(ss);
128
3.13M
  size_t buf_off = 0;
129
  /// Remove leading spaces
130
3.18M
  while (ss->buffer[buf_off] == ' ' || ss->buffer[buf_off] == '\t') {
131
51.8k
    buf_off++;
132
51.8k
  }
133
3.13M
  if (buf_off > 0) {
134
51.8k
    memmove(ss->buffer, ss->buffer + buf_off,
135
51.8k
      SSTREAM_BUF_LEN - buf_off);
136
51.8k
    ss->index -= buf_off;
137
51.8k
  }
138
3.13M
}
139
140
/// Extract the mnemonic to @mnem_buf and the operand string into @op_str_buf from the stream buffer.
141
/// The mnemonic is everything up until the first ' ' or '\t' character.
142
/// The operand string is everything after the first ' ' or '\t' sequence.
143
void SStream_extract_mnem_opstr(const SStream *ss, char *mnem_buf,
144
        size_t mnem_buf_size, char *op_str_buf,
145
        size_t op_str_buf_size)
146
3.03M
{
147
3.03M
  assert(ss && mnem_buf && mnem_buf_size > 0 && op_str_buf &&
148
3.03M
         op_str_buf_size > 0);
149
3.03M
  size_t off = 0;
150
  // Copy all non space chars to as mnemonic.
151
17.1M
  while (ss->buffer[off] && ss->buffer[off] != ' ' &&
152
16.4M
         ss->buffer[off] != '\t') {
153
14.1M
    if (off < mnem_buf_size - 1) {
154
      // Only copy if there is space left.
155
14.1M
      mnem_buf[off] = ss->buffer[off];
156
14.1M
    }
157
14.1M
    off++;
158
14.1M
  }
159
3.03M
  if (!ss->buffer[off]) {
160
175k
    return;
161
175k
  }
162
163
  // Iterate until next non space char.
164
2.87M
  do {
165
2.87M
    off++;
166
2.87M
  } while (ss->buffer[off] &&
167
2.82M
     (ss->buffer[off] == ' ' || ss->buffer[off] == '\t'));
168
169
2.85M
  if (!ss->buffer[off]) {
170
47.3k
    return;
171
47.3k
  }
172
173
  // Copy all follow up characters as op_str
174
2.80M
  const char *ss_op_str = ss->buffer + off;
175
2.80M
  off = 0;
176
40.0M
  while (ss_op_str[off] && off < op_str_buf_size - 1) {
177
37.2M
    op_str_buf[off] = ss_op_str[off];
178
37.2M
    off++;
179
37.2M
  }
180
2.80M
}
181
182
/// Empty the stream @ss to given @file (stdin/stderr).
183
/// @file can be NULL. Then the buffer content is not emitted.
184
void SStream_Flush(SStream *ss, FILE *file)
185
32.2k
{
186
32.2k
  assert(ss);
187
32.2k
  if (file) {
188
0
    fprintf(file, "%s\n", ss->buffer);
189
0
  }
190
32.2k
  SStream_Init(ss);
191
32.2k
}
192
193
/**
194
 * Open the output stream. Every write attempt is accepted again.
195
 */
196
void SStream_Open(SStream *ss)
197
0
{
198
0
  assert(ss);
199
0
  ss->is_closed = false;
200
0
}
201
202
/**
203
 * Closes the output stream. Every write attempt is ignored.
204
 */
205
void SStream_Close(SStream *ss)
206
0
{
207
0
  assert(ss);
208
0
  ss->is_closed = true;
209
0
}
210
211
/**
212
 * Copy the string \p s to the buffer of \p ss and terminate it with a '\\0' byte.
213
 */
214
void SStream_concat0(SStream *ss, const char *s)
215
17.4M
{
216
17.4M
#ifndef CAPSTONE_DIET
217
17.4M
  assert(ss && s);
218
17.4M
  SSTREAM_RETURN_IF_CLOSED(ss);
219
17.4M
  if (s[0] == '\0')
220
5.29M
    return;
221
222
12.1M
  SStream_concat_raw(ss, s, strlen(s));
223
12.1M
  SStream_close_markup(ss);
224
#else
225
  ss->buffer[ss->index] = '\0';
226
#endif
227
12.1M
}
228
229
/**
230
 * Copy the single char \p c to the buffer of \p ss.
231
 */
232
void SStream_concat1(SStream *ss, const char c)
233
1.91M
{
234
1.91M
#ifndef CAPSTONE_DIET
235
1.91M
  assert(ss);
236
1.91M
  SSTREAM_RETURN_IF_CLOSED(ss);
237
1.91M
  if (c == '\0')
238
20.7k
    return;
239
240
1.89M
  SStream_concat1_raw(ss, c);
241
1.89M
  SStream_close_markup(ss);
242
#else
243
  ss->buffer[ss->index] = '\0';
244
#endif
245
1.89M
}
246
247
/**
248
 * Copy all strings given to the buffer of \p ss according to formatting \p fmt.
249
 */
250
void SStream_concat(SStream *ss, const char *fmt, ...)
251
7.60M
{
252
7.60M
#ifndef CAPSTONE_DIET
253
7.60M
  assert(ss && fmt);
254
7.60M
  SSTREAM_RETURN_IF_CLOSED(ss);
255
7.60M
  va_list ap;
256
7.60M
  int ret;
257
7.60M
  size_t remaining = SStream_remaining(ss);
258
7.60M
  if (remaining == 0) {
259
0
    return;
260
0
  }
261
262
7.60M
  va_start(ap, fmt);
263
7.60M
  ret = cs_vsnprintf(ss->buffer + ss->index, remaining + 1, fmt, ap);
264
7.60M
  va_end(ap);
265
7.60M
  if (ret < 0) {
266
0
    ss->buffer[ss->index] = '\0';
267
0
    return;
268
0
  }
269
7.60M
  if ((size_t)ret > remaining) {
270
0
    ss->index += remaining;
271
7.60M
  } else {
272
7.60M
    ss->index += (size_t)ret;
273
7.60M
  }
274
7.60M
  SStream_close_markup(ss);
275
#else
276
  ss->buffer[ss->index] = '\0';
277
#endif
278
7.60M
}
279
280
// print number with prefix #
281
void printInt64Bang(SStream *ss, int64_t val)
282
72.7k
{
283
72.7k
  assert(ss);
284
72.7k
  if (ss->unsigned_num) {
285
0
    printUInt64Bang(ss, val);
286
0
    return;
287
0
  }
288
72.7k
  SSTREAM_RETURN_IF_CLOSED(ss);
289
72.7k
  SStream_concat1(ss, '#');
290
72.7k
  printInt64(ss, val);
291
72.7k
}
292
293
void printUInt64Bang(SStream *ss, uint64_t val)
294
8.72k
{
295
8.72k
  assert(ss);
296
8.72k
  SSTREAM_RETURN_IF_CLOSED(ss);
297
8.72k
  SStream_concat1(ss, '#');
298
8.72k
  printUInt64(ss, val);
299
8.72k
}
300
301
// print number
302
void printInt64(SStream *ss, int64_t val)
303
721k
{
304
721k
  assert(ss);
305
721k
  if (ss->unsigned_num) {
306
0
    printUInt64(ss, val);
307
0
    return;
308
0
  }
309
721k
  SSTREAM_RETURN_IF_CLOSED(ss);
310
721k
  if (val >= 0) {
311
658k
    if (val > HEX_THRESHOLD)
312
424k
      SStream_concat(ss, "0x%" PRIx64, val);
313
234k
    else
314
234k
      SStream_concat(ss, "%" PRIu64, val);
315
658k
  } else {
316
62.8k
    if (val < -HEX_THRESHOLD) {
317
55.4k
      if (val == INT64_MIN)
318
18
        SStream_concat(ss, "-0x%" PRIx64,
319
18
                 (uint64_t)INT64_MAX + 1);
320
55.4k
      else
321
55.4k
        SStream_concat(ss, "-0x%" PRIx64,
322
55.4k
                 (uint64_t)-val);
323
55.4k
    } else
324
7.34k
      SStream_concat(ss, "-%" PRIu64, -val);
325
62.8k
  }
326
721k
}
327
328
void printUInt64(SStream *ss, uint64_t val)
329
177k
{
330
177k
  assert(ss);
331
177k
  SSTREAM_RETURN_IF_CLOSED(ss);
332
177k
  if (val > HEX_THRESHOLD)
333
117k
    SStream_concat(ss, "0x%" PRIx64, val);
334
59.7k
  else
335
59.7k
    SStream_concat(ss, "%" PRIu64, val);
336
177k
}
337
338
// print number in decimal mode
339
void printInt32BangDec(SStream *ss, int32_t val)
340
0
{
341
0
  assert(ss);
342
0
  SSTREAM_RETURN_IF_CLOSED(ss);
343
0
  if (val >= 0)
344
0
    SStream_concat(ss, "#%" PRIu32, val);
345
0
  else {
346
0
    if (val == INT32_MIN)
347
0
      SStream_concat(ss, "#-%" PRIu32, val);
348
0
    else
349
0
      SStream_concat(ss, "#-%" PRIu32, (uint32_t)-val);
350
0
  }
351
0
}
352
353
void printInt32Bang(SStream *ss, int32_t val)
354
168k
{
355
168k
  assert(ss);
356
168k
  if (ss->unsigned_num) {
357
0
    printUInt32Bang(ss, val);
358
0
    return;
359
0
  }
360
168k
  SSTREAM_RETURN_IF_CLOSED(ss);
361
168k
  SStream_concat1(ss, '#');
362
168k
  printInt32(ss, val);
363
168k
}
364
365
void printUInt8(SStream *ss, uint8_t val)
366
0
{
367
0
  assert(ss);
368
0
  if (val > HEX_THRESHOLD)
369
0
    SStream_concat(ss, "0x%" PRIx8, val);
370
0
  else
371
0
    SStream_concat(ss, "%" PRIu8, val);
372
0
}
373
374
void printUInt16(SStream *ss, uint16_t val)
375
0
{
376
0
  assert(ss);
377
0
  if (val > HEX_THRESHOLD)
378
0
    SStream_concat(ss, "0x%" PRIx16, val);
379
0
  else
380
0
    SStream_concat(ss, "%" PRIu16, val);
381
0
}
382
383
void printInt8(SStream *ss, int8_t val)
384
1.45k
{
385
1.45k
  assert(ss);
386
1.45k
  if (ss->unsigned_num) {
387
0
    printUInt8(ss, val);
388
0
    return;
389
0
  }
390
1.45k
  SSTREAM_RETURN_IF_CLOSED(ss);
391
1.45k
  if (val >= 0) {
392
871
    if (val > HEX_THRESHOLD)
393
664
      SStream_concat(ss, "0x%" PRIx8, val);
394
207
    else
395
207
      SStream_concat(ss, "%" PRId8, val);
396
871
  } else {
397
588
    if (val < -HEX_THRESHOLD) {
398
386
      if (val == INT8_MIN)
399
132
        SStream_concat(ss, "-0x%" PRIx8,
400
132
                 (uint8_t)INT8_MAX + 1);
401
254
      else
402
254
        SStream_concat(ss, "-0x%" PRIx8, (int8_t)-val);
403
386
    } else
404
202
      SStream_concat(ss, "-%" PRIu8, -val);
405
588
  }
406
1.45k
}
407
408
void printInt16(SStream *ss, int16_t val)
409
2.64k
{
410
2.64k
  assert(ss);
411
2.64k
  if (ss->unsigned_num) {
412
0
    printUInt16(ss, val);
413
0
    return;
414
0
  }
415
2.64k
  SSTREAM_RETURN_IF_CLOSED(ss);
416
2.64k
  if (val >= 0) {
417
1.67k
    if (val > HEX_THRESHOLD)
418
1.33k
      SStream_concat(ss, "0x%" PRIx16, val);
419
341
    else
420
341
      SStream_concat(ss, "%" PRId16, val);
421
1.67k
  } else {
422
973
    if (val < -HEX_THRESHOLD) {
423
869
      if (val == INT16_MIN)
424
53
        SStream_concat(ss, "-0x%" PRIx16,
425
53
                 (uint16_t)INT16_MAX + 1);
426
816
      else
427
816
        SStream_concat(ss, "-0x%" PRIx16,
428
816
                 (int16_t)-val);
429
869
    } else
430
104
      SStream_concat(ss, "-%" PRIu16, -val);
431
973
  }
432
2.64k
}
433
434
void printInt16HexOffset(SStream *ss, int16_t val)
435
5.52k
{
436
5.52k
  assert(ss);
437
5.52k
  if (ss->unsigned_num) {
438
0
    printUInt16(ss, val);
439
0
    return;
440
0
  }
441
5.52k
  SSTREAM_RETURN_IF_CLOSED(ss);
442
5.52k
  if (val >= 0) {
443
4.04k
    SStream_concat(ss, "+0x%" PRIx16, val);
444
4.04k
  } else {
445
1.47k
    if (val == INT16_MIN)
446
6
      SStream_concat(ss, "-0x%" PRIx16,
447
6
               (uint16_t)INT16_MAX + 1);
448
1.47k
    else
449
1.47k
      SStream_concat(ss, "-0x%" PRIx16, (int16_t)-val);
450
1.47k
  }
451
5.52k
}
452
453
void printInt32(SStream *ss, int32_t val)
454
236k
{
455
236k
  assert(ss);
456
236k
  if (ss->unsigned_num) {
457
0
    printUInt32(ss, val);
458
0
    return;
459
0
  }
460
236k
  SSTREAM_RETURN_IF_CLOSED(ss);
461
236k
  if (val >= 0) {
462
177k
    if (val > HEX_THRESHOLD)
463
107k
      SStream_concat(ss, "0x%" PRIx32, val);
464
70.6k
    else
465
70.6k
      SStream_concat(ss, "%" PRId32, val);
466
177k
  } else {
467
58.3k
    if (val < -HEX_THRESHOLD) {
468
55.4k
      if (val == INT32_MIN)
469
117
        SStream_concat(ss, "-0x%" PRIx32,
470
117
                 (uint32_t)INT32_MAX + 1);
471
55.3k
      else
472
55.3k
        SStream_concat(ss, "-0x%" PRIx32,
473
55.3k
                 (int32_t)-val);
474
55.4k
    } else {
475
2.93k
      SStream_concat(ss, "-%" PRIu32, (uint32_t)-val);
476
2.93k
    }
477
58.3k
  }
478
236k
}
479
480
void printInt32HexOffset(SStream *ss, int32_t val)
481
995
{
482
995
  assert(ss);
483
995
  if (ss->unsigned_num) {
484
0
    printUInt32(ss, val);
485
0
    return;
486
0
  }
487
995
  SSTREAM_RETURN_IF_CLOSED(ss);
488
995
  if (val >= 0) {
489
845
    SStream_concat(ss, "+0x%" PRIx32, val);
490
845
  } else {
491
150
    if (val == INT32_MIN)
492
21
      SStream_concat(ss, "-0x%" PRIx32,
493
21
               (uint32_t)INT32_MAX + 1);
494
129
    else
495
129
      SStream_concat(ss, "-0x%" PRIx32, (int32_t)-val);
496
150
  }
497
995
}
498
499
void printInt32Hex(SStream *ss, int32_t val)
500
3.34k
{
501
3.34k
  assert(ss);
502
3.34k
  SSTREAM_RETURN_IF_CLOSED(ss);
503
3.34k
  if (val >= 0) {
504
1.87k
    SStream_concat(ss, "0x%" PRIx32, val);
505
1.87k
  } else {
506
1.46k
    if (val == INT32_MIN)
507
17
      SStream_concat(ss, "-0x%" PRIx32,
508
17
               (uint32_t)INT32_MAX + 1);
509
1.44k
    else
510
1.44k
      SStream_concat(ss, "-0x%" PRIx32, (int32_t)-val);
511
1.46k
  }
512
3.34k
}
513
514
void printUInt32Bang(SStream *ss, uint32_t val)
515
196k
{
516
196k
  assert(ss);
517
196k
  SSTREAM_RETURN_IF_CLOSED(ss);
518
196k
  SStream_concat1(ss, '#');
519
196k
  printUInt32(ss, val);
520
196k
}
521
522
void printUInt32(SStream *ss, uint32_t val)
523
281k
{
524
281k
  assert(ss);
525
281k
  SSTREAM_RETURN_IF_CLOSED(ss);
526
281k
  if (val > HEX_THRESHOLD)
527
215k
    SStream_concat(ss, "0x%x", val);
528
66.4k
  else
529
66.4k
    SStream_concat(ss, "%u", val);
530
281k
}
531
532
void printFloat(SStream *ss, float val)
533
0
{
534
0
  assert(ss);
535
0
  SSTREAM_RETURN_IF_CLOSED(ss);
536
0
  SStream_concat(ss, "%e", val);
537
0
}
538
539
void printfFloat(SStream *ss, const char *fmt, float val)
540
231
{
541
231
  assert(ss);
542
231
  SSTREAM_RETURN_IF_CLOSED(ss);
543
231
  SStream_concat(ss, fmt, val);
544
231
}
545
546
void printFloatBang(SStream *ss, float val)
547
514
{
548
514
  assert(ss);
549
514
  SSTREAM_RETURN_IF_CLOSED(ss);
550
514
  SStream_concat(ss, "#%e", val);
551
514
}
552
553
void printExpr(SStream *ss, uint64_t val)
554
0
{
555
0
  assert(ss);
556
0
  SSTREAM_RETURN_IF_CLOSED(ss);
557
0
  SStream_concat(ss, "%" PRIu64, val);
558
0
}
559
560
SStream *markup_OS(SStream *OS, SStreamMarkup style)
561
619k
{
562
619k
  assert(OS);
563
564
619k
  if (OS->is_closed || !OS->markup_stream) {
565
619k
    return OS;
566
619k
  }
567
0
  OS->markup_stream = false; // Disable temporarily.
568
0
  switch (style) {
569
0
  default:
570
0
    SStream_concat0(OS, "<UNKNOWN:");
571
0
    return OS;
572
0
  case Markup_Immediate:
573
0
    SStream_concat0(OS, "<imm:");
574
0
    break;
575
0
  case Markup_Register:
576
0
    SStream_concat0(OS, "<reg:");
577
0
    break;
578
0
  case Markup_Target:
579
0
    SStream_concat0(OS, "<tar:");
580
0
    break;
581
0
  case Markup_Memory:
582
0
    SStream_concat0(OS, "<mem:");
583
0
    break;
584
0
  }
585
0
  OS->markup_stream = true;
586
  OS->prefixed_by_markup = true;
587
0
  return OS;
588
0
}