Coverage Report

Created: 2026-09-01 07:20

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
23.6M
{
23
23.6M
  assert(ss);
24
23.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
23.6M
  return SSTREAM_BUF_LEN - ss->index - 1;
30
23.6M
}
31
32
static void SStream_concat_raw(SStream *ss, const char *s, size_t len)
33
13.1M
{
34
13.1M
  size_t remaining = SStream_remaining(ss);
35
13.1M
  if (len > remaining) {
36
0
    len = remaining;
37
0
  }
38
13.1M
  if (len > 0) {
39
13.1M
    memcpy(ss->buffer + ss->index, s, len);
40
13.1M
    ss->index += len;
41
13.1M
  }
42
13.1M
  ss->buffer[ss->index] = '\0';
43
13.1M
}
44
45
static void SStream_concat1_raw(SStream *ss, char c)
46
2.00M
{
47
2.00M
  if (SStream_remaining(ss) == 0) {
48
0
    return;
49
0
  }
50
2.00M
  ss->buffer[ss->index] = c;
51
2.00M
  ss->index++;
52
2.00M
  ss->buffer[ss->index] = '\0';
53
2.00M
}
54
55
static void SStream_close_markup(SStream *ss)
56
23.6M
{
57
23.6M
  if (ss->markup_stream && ss->prefixed_by_markup) {
58
0
    SStream_concat1_raw(ss, '>');
59
0
  }
60
23.6M
}
61
62
void SStream_Init(SStream *ss)
63
3.46M
{
64
3.46M
  assert(ss);
65
3.46M
  ss->index = 0;
66
3.46M
  memset(ss->buffer, 0, sizeof(ss->buffer));
67
3.46M
  ss->is_closed = false;
68
3.46M
  ss->markup_stream = false;
69
3.46M
  ss->prefixed_by_markup = false;
70
3.46M
  ss->unsigned_num = false;
71
3.46M
}
72
73
void SStream_opt_unum(SStream *ss, bool print_unsigned_numbers)
74
3.36M
{
75
3.36M
  assert(ss);
76
3.36M
  ss->unsigned_num = print_unsigned_numbers;
77
3.36M
}
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
136k
{
83
136k
  assert(ss);
84
136k
  return ss->buffer;
85
136k
}
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
123k
{
94
123k
  assert(ss);
95
123k
  char *found = strchr(ss->buffer, elem);
96
123k
  if (!found || found == ss->buffer + (SSTREAM_BUF_LEN - 1)) {
97
123k
    return NULL;
98
123k
  }
99
0
  *found = repl;
100
0
  found++;
101
0
  return found;
102
123k
}
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
33.5k
{
108
33.5k
  assert(ss && rstr);
109
33.5k
  char *found = strchr(ss->buffer, chr);
110
33.5k
  if (!found || found == ss->buffer + (SSTREAM_BUF_LEN - 1)) {
111
0
    return;
112
0
  }
113
33.5k
  size_t post_len = strlen(found + 1);
114
33.5k
  size_t buf_str_len = strlen(ss->buffer);
115
33.5k
  size_t repl_len = strlen(rstr);
116
33.5k
  if (repl_len - 1 + buf_str_len >= SSTREAM_BUF_LEN) {
117
0
    return;
118
0
  }
119
33.5k
  memmove(found + repl_len, found + 1, post_len);
120
33.5k
  memcpy(found, rstr, repl_len);
121
33.5k
  ss->index = strlen(ss->buffer);
122
33.5k
}
123
124
/// Removes the space characters '\t' and ' ' from the beginning of the stream buffer.
125
void SStream_trimls(SStream *ss)
126
3.48M
{
127
3.48M
  assert(ss);
128
3.48M
  size_t buf_off = 0;
129
  /// Remove leading spaces
130
3.54M
  while (ss->buffer[buf_off] == ' ' || ss->buffer[buf_off] == '\t') {
131
53.9k
    buf_off++;
132
53.9k
  }
133
3.48M
  if (buf_off > 0) {
134
53.9k
    memmove(ss->buffer, ss->buffer + buf_off,
135
53.9k
      SSTREAM_BUF_LEN - buf_off);
136
53.9k
    ss->index -= buf_off;
137
53.9k
  }
138
3.48M
}
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.36M
{
147
3.36M
  assert(ss && mnem_buf && mnem_buf_size > 0 && op_str_buf &&
148
3.36M
         op_str_buf_size > 0);
149
3.36M
  size_t off = 0;
150
  // Copy all non space chars to as mnemonic.
151
18.7M
  while (ss->buffer[off] && ss->buffer[off] != ' ' &&
152
17.8M
         ss->buffer[off] != '\t') {
153
15.3M
    if (off < mnem_buf_size - 1) {
154
      // Only copy if there is space left.
155
15.3M
      mnem_buf[off] = ss->buffer[off];
156
15.3M
    }
157
15.3M
    off++;
158
15.3M
  }
159
3.36M
  if (!ss->buffer[off]) {
160
165k
    return;
161
165k
  }
162
163
  // Iterate until next non space char.
164
3.19M
  do {
165
3.19M
    off++;
166
3.19M
  } while (ss->buffer[off] &&
167
3.13M
     (ss->buffer[off] == ' ' || ss->buffer[off] == '\t'));
168
169
3.19M
  if (!ss->buffer[off]) {
170
59.7k
    return;
171
59.7k
  }
172
173
  // Copy all follow up characters as op_str
174
3.13M
  const char *ss_op_str = ss->buffer + off;
175
3.13M
  off = 0;
176
43.9M
  while (ss_op_str[off] && off < op_str_buf_size - 1) {
177
40.8M
    op_str_buf[off] = ss_op_str[off];
178
40.8M
    off++;
179
40.8M
  }
180
3.13M
}
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
34.2k
{
186
34.2k
  assert(ss);
187
34.2k
  if (file) {
188
0
    fprintf(file, "%s\n", ss->buffer);
189
0
  }
190
34.2k
  SStream_Init(ss);
191
34.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
18.8M
{
216
18.8M
#ifndef CAPSTONE_DIET
217
18.8M
  assert(ss && s);
218
18.8M
  SSTREAM_RETURN_IF_CLOSED(ss);
219
18.8M
  if (s[0] == '\0')
220
5.65M
    return;
221
222
13.1M
  SStream_concat_raw(ss, s, strlen(s));
223
13.1M
  SStream_close_markup(ss);
224
#else
225
  ss->buffer[ss->index] = '\0';
226
#endif
227
13.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
2.01M
{
234
2.01M
#ifndef CAPSTONE_DIET
235
2.01M
  assert(ss);
236
2.01M
  SSTREAM_RETURN_IF_CLOSED(ss);
237
2.01M
  if (c == '\0')
238
18.7k
    return;
239
240
2.00M
  SStream_concat1_raw(ss, c);
241
2.00M
  SStream_close_markup(ss);
242
#else
243
  ss->buffer[ss->index] = '\0';
244
#endif
245
2.00M
}
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
8.42M
{
252
8.42M
#ifndef CAPSTONE_DIET
253
8.42M
  assert(ss && fmt);
254
8.42M
  SSTREAM_RETURN_IF_CLOSED(ss);
255
8.42M
  va_list ap;
256
8.42M
  int ret;
257
8.42M
  size_t remaining = SStream_remaining(ss);
258
8.42M
  if (remaining == 0) {
259
0
    return;
260
0
  }
261
262
8.42M
  va_start(ap, fmt);
263
8.42M
  ret = cs_vsnprintf(ss->buffer + ss->index, remaining + 1, fmt, ap);
264
8.42M
  va_end(ap);
265
8.42M
  if (ret < 0) {
266
0
    ss->buffer[ss->index] = '\0';
267
0
    return;
268
0
  }
269
8.42M
  if ((size_t)ret > remaining) {
270
0
    ss->index += remaining;
271
8.42M
  } else {
272
8.42M
    ss->index += (size_t)ret;
273
8.42M
  }
274
8.42M
  SStream_close_markup(ss);
275
#else
276
  ss->buffer[ss->index] = '\0';
277
#endif
278
8.42M
}
279
280
// print number with prefix #
281
void printInt64Bang(SStream *ss, int64_t val)
282
71.9k
{
283
71.9k
  assert(ss);
284
71.9k
  if (ss->unsigned_num) {
285
0
    printUInt64Bang(ss, val);
286
0
    return;
287
0
  }
288
71.9k
  SSTREAM_RETURN_IF_CLOSED(ss);
289
71.9k
  SStream_concat1(ss, '#');
290
71.9k
  printInt64(ss, val);
291
71.9k
}
292
293
void printUInt64Bang(SStream *ss, uint64_t val)
294
10.2k
{
295
10.2k
  assert(ss);
296
10.2k
  SSTREAM_RETURN_IF_CLOSED(ss);
297
10.2k
  SStream_concat1(ss, '#');
298
10.2k
  printUInt64(ss, val);
299
10.2k
}
300
301
// print number
302
void printInt64(SStream *ss, int64_t val)
303
741k
{
304
741k
  assert(ss);
305
741k
  if (ss->unsigned_num) {
306
0
    printUInt64(ss, val);
307
0
    return;
308
0
  }
309
741k
  SSTREAM_RETURN_IF_CLOSED(ss);
310
741k
  if (val >= 0) {
311
674k
    if (val > HEX_THRESHOLD)
312
433k
      SStream_concat(ss, "0x%" PRIx64, val);
313
241k
    else
314
241k
      SStream_concat(ss, "%" PRIu64, val);
315
674k
  } else {
316
66.7k
    if (val < -HEX_THRESHOLD) {
317
61.7k
      if (val == INT64_MIN)
318
17
        SStream_concat(ss, "-0x%" PRIx64,
319
17
                 (uint64_t)INT64_MAX + 1);
320
61.7k
      else
321
61.7k
        SStream_concat(ss, "-0x%" PRIx64,
322
61.7k
                 (uint64_t)-val);
323
61.7k
    } else
324
5.00k
      SStream_concat(ss, "-%" PRIu64, -val);
325
66.7k
  }
326
741k
}
327
328
void printUInt64(SStream *ss, uint64_t val)
329
199k
{
330
199k
  assert(ss);
331
199k
  SSTREAM_RETURN_IF_CLOSED(ss);
332
199k
  if (val > HEX_THRESHOLD)
333
137k
    SStream_concat(ss, "0x%" PRIx64, val);
334
62.3k
  else
335
62.3k
    SStream_concat(ss, "%" PRIu64, val);
336
199k
}
337
338
// print number in decimal mode
339
void printInt32BangDec(SStream *ss, int32_t val)
340
{
341
  assert(ss);
342
  SSTREAM_RETURN_IF_CLOSED(ss);
343
  if (val >= 0)
344
    SStream_concat(ss, "#%" PRIu32, val);
345
  else {
346
    if (val == INT32_MIN)
347
      SStream_concat(ss, "#-%" PRIu32, val);
348
    else
349
      SStream_concat(ss, "#-%" PRIu32, (uint32_t)-val);
350
  }
351
}
352
353
void printInt32Bang(SStream *ss, int32_t val)
354
179k
{
355
179k
  assert(ss);
356
179k
  if (ss->unsigned_num) {
357
0
    printUInt32Bang(ss, val);
358
0
    return;
359
0
  }
360
179k
  SSTREAM_RETURN_IF_CLOSED(ss);
361
179k
  SStream_concat1(ss, '#');
362
179k
  printInt32(ss, val);
363
179k
}
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.70k
{
385
1.70k
  assert(ss);
386
1.70k
  if (ss->unsigned_num) {
387
0
    printUInt8(ss, val);
388
0
    return;
389
0
  }
390
1.70k
  SSTREAM_RETURN_IF_CLOSED(ss);
391
1.70k
  if (val >= 0) {
392
949
    if (val > HEX_THRESHOLD)
393
433
      SStream_concat(ss, "0x%" PRIx8, val);
394
516
    else
395
516
      SStream_concat(ss, "%" PRId8, val);
396
949
  } else {
397
758
    if (val < -HEX_THRESHOLD) {
398
691
      if (val == INT8_MIN)
399
301
        SStream_concat(ss, "-0x%" PRIx8,
400
301
                 (uint8_t)INT8_MAX + 1);
401
390
      else
402
390
        SStream_concat(ss, "-0x%" PRIx8, (int8_t)-val);
403
691
    } else
404
67
      SStream_concat(ss, "-%" PRIu8, -val);
405
758
  }
406
1.70k
}
407
408
void printInt16(SStream *ss, int16_t val)
409
3.02k
{
410
3.02k
  assert(ss);
411
3.02k
  if (ss->unsigned_num) {
412
0
    printUInt16(ss, val);
413
0
    return;
414
0
  }
415
3.02k
  SSTREAM_RETURN_IF_CLOSED(ss);
416
3.02k
  if (val >= 0) {
417
1.99k
    if (val > HEX_THRESHOLD)
418
1.44k
      SStream_concat(ss, "0x%" PRIx16, val);
419
549
    else
420
549
      SStream_concat(ss, "%" PRId16, val);
421
1.99k
  } else {
422
1.03k
    if (val < -HEX_THRESHOLD) {
423
951
      if (val == INT16_MIN)
424
18
        SStream_concat(ss, "-0x%" PRIx16,
425
18
                 (uint16_t)INT16_MAX + 1);
426
933
      else
427
933
        SStream_concat(ss, "-0x%" PRIx16,
428
933
                 (int16_t)-val);
429
951
    } else
430
81
      SStream_concat(ss, "-%" PRIu16, -val);
431
1.03k
  }
432
3.02k
}
433
434
void printInt16HexOffset(SStream *ss, int16_t val)
435
6.64k
{
436
6.64k
  assert(ss);
437
6.64k
  if (ss->unsigned_num) {
438
0
    printUInt16(ss, val);
439
0
    return;
440
0
  }
441
6.64k
  SSTREAM_RETURN_IF_CLOSED(ss);
442
6.64k
  if (val >= 0) {
443
5.01k
    SStream_concat(ss, "+0x%" PRIx16, val);
444
5.01k
  } else {
445
1.62k
    if (val == INT16_MIN)
446
45
      SStream_concat(ss, "-0x%" PRIx16,
447
45
               (uint16_t)INT16_MAX + 1);
448
1.57k
    else
449
1.57k
      SStream_concat(ss, "-0x%" PRIx16, (int16_t)-val);
450
1.62k
  }
451
6.64k
}
452
453
void printInt32(SStream *ss, int32_t val)
454
254k
{
455
254k
  assert(ss);
456
254k
  if (ss->unsigned_num) {
457
0
    printUInt32(ss, val);
458
0
    return;
459
0
  }
460
254k
  SSTREAM_RETURN_IF_CLOSED(ss);
461
254k
  if (val >= 0) {
462
192k
    if (val > HEX_THRESHOLD)
463
117k
      SStream_concat(ss, "0x%" PRIx32, val);
464
75.2k
    else
465
75.2k
      SStream_concat(ss, "%" PRId32, val);
466
192k
  } else {
467
62.1k
    if (val < -HEX_THRESHOLD) {
468
59.1k
      if (val == INT32_MIN)
469
88
        SStream_concat(ss, "-0x%" PRIx32,
470
88
                 (uint32_t)INT32_MAX + 1);
471
59.0k
      else
472
59.0k
        SStream_concat(ss, "-0x%" PRIx32,
473
59.0k
                 (int32_t)-val);
474
59.1k
    } else {
475
2.96k
      SStream_concat(ss, "-%" PRIu32, (uint32_t)-val);
476
2.96k
    }
477
62.1k
  }
478
254k
}
479
480
void printInt32HexOffset(SStream *ss, int32_t val)
481
585
{
482
585
  assert(ss);
483
585
  if (ss->unsigned_num) {
484
0
    printUInt32(ss, val);
485
0
    return;
486
0
  }
487
585
  SSTREAM_RETURN_IF_CLOSED(ss);
488
585
  if (val >= 0) {
489
411
    SStream_concat(ss, "+0x%" PRIx32, val);
490
411
  } else {
491
174
    if (val == INT32_MIN)
492
38
      SStream_concat(ss, "-0x%" PRIx32,
493
38
               (uint32_t)INT32_MAX + 1);
494
136
    else
495
136
      SStream_concat(ss, "-0x%" PRIx32, (int32_t)-val);
496
174
  }
497
585
}
498
499
void printInt32Hex(SStream *ss, int32_t val)
500
4.35k
{
501
4.35k
  assert(ss);
502
4.35k
  SSTREAM_RETURN_IF_CLOSED(ss);
503
4.35k
  if (val >= 0) {
504
2.45k
    SStream_concat(ss, "0x%" PRIx32, val);
505
2.45k
  } else {
506
1.90k
    if (val == INT32_MIN)
507
6
      SStream_concat(ss, "-0x%" PRIx32,
508
6
               (uint32_t)INT32_MAX + 1);
509
1.89k
    else
510
1.89k
      SStream_concat(ss, "-0x%" PRIx32, (int32_t)-val);
511
1.90k
  }
512
4.35k
}
513
514
void printUInt32Bang(SStream *ss, uint32_t val)
515
199k
{
516
199k
  assert(ss);
517
199k
  SSTREAM_RETURN_IF_CLOSED(ss);
518
199k
  SStream_concat1(ss, '#');
519
199k
  printUInt32(ss, val);
520
199k
}
521
522
void printUInt32(SStream *ss, uint32_t val)
523
297k
{
524
297k
  assert(ss);
525
297k
  SSTREAM_RETURN_IF_CLOSED(ss);
526
297k
  if (val > HEX_THRESHOLD)
527
228k
    SStream_concat(ss, "0x%x", val);
528
68.9k
  else
529
68.9k
    SStream_concat(ss, "%u", val);
530
297k
}
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
235
{
541
235
  assert(ss);
542
235
  SSTREAM_RETURN_IF_CLOSED(ss);
543
235
  SStream_concat(ss, fmt, val);
544
235
}
545
546
void printFloatBang(SStream *ss, float val)
547
689
{
548
689
  assert(ss);
549
689
  SSTREAM_RETURN_IF_CLOSED(ss);
550
689
  SStream_concat(ss, "#%e", val);
551
689
}
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
655k
{
562
655k
  assert(OS);
563
564
655k
  if (OS->is_closed || !OS->markup_stream) {
565
655k
    return OS;
566
655k
  }
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
}