Coverage Report

Created: 2026-11-12 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
25.4M
{
23
25.4M
  assert(ss);
24
25.4M
  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
25.4M
  return SSTREAM_BUF_LEN - ss->index - 1;
30
25.4M
}
31
32
static void SStream_concat_raw(SStream *ss, const char *s, size_t len)
33
13.7M
{
34
13.7M
  size_t remaining = SStream_remaining(ss);
35
13.7M
  if (len > remaining) {
36
0
    len = remaining;
37
0
  }
38
13.7M
  if (len > 0) {
39
13.7M
    memcpy(ss->buffer + ss->index, s, len);
40
13.7M
    ss->index += len;
41
13.7M
  }
42
13.7M
  ss->buffer[ss->index] = '\0';
43
13.7M
}
44
45
static void SStream_concat1_raw(SStream *ss, char c)
46
2.70M
{
47
2.70M
  if (SStream_remaining(ss) == 0) {
48
0
    return;
49
0
  }
50
2.70M
  ss->buffer[ss->index] = c;
51
2.70M
  ss->index++;
52
2.70M
  ss->buffer[ss->index] = '\0';
53
2.70M
}
54
55
static void SStream_close_markup(SStream *ss)
56
25.4M
{
57
25.4M
  if (ss->markup_stream && ss->prefixed_by_markup) {
58
0
    SStream_concat1_raw(ss, '>');
59
0
  }
60
25.4M
}
61
62
void SStream_Init(SStream *ss)
63
3.41M
{
64
3.41M
  assert(ss);
65
3.41M
  ss->index = 0;
66
3.41M
  memset(ss->buffer, 0, sizeof(ss->buffer));
67
3.41M
  ss->is_closed = false;
68
3.41M
  ss->markup_stream = false;
69
3.41M
  ss->prefixed_by_markup = false;
70
3.41M
  ss->unsigned_num = false;
71
3.41M
}
72
73
void SStream_opt_unum(SStream *ss, bool print_unsigned_numbers)
74
3.27M
{
75
3.27M
  assert(ss);
76
3.27M
  ss->unsigned_num = print_unsigned_numbers;
77
3.27M
}
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
181k
{
83
181k
  assert(ss);
84
181k
  return ss->buffer;
85
181k
}
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
142k
{
94
142k
  assert(ss);
95
142k
  char *found = strchr(ss->buffer, elem);
96
142k
  if (!found || found == ss->buffer + (SSTREAM_BUF_LEN - 1)) {
97
142k
    return NULL;
98
142k
  }
99
0
  *found = repl;
100
0
  found++;
101
0
  return found;
102
142k
}
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
44.4k
{
108
44.4k
  assert(ss && rstr);
109
44.4k
  char *found = strchr(ss->buffer, chr);
110
44.4k
  if (!found || found == ss->buffer + (SSTREAM_BUF_LEN - 1)) {
111
0
    return;
112
0
  }
113
44.4k
  size_t post_len = strlen(found + 1);
114
44.4k
  size_t buf_str_len = strlen(ss->buffer);
115
44.4k
  size_t repl_len = strlen(rstr);
116
44.4k
  if (repl_len - 1 + buf_str_len >= SSTREAM_BUF_LEN) {
117
0
    return;
118
0
  }
119
44.4k
  memmove(found + repl_len, found + 1, post_len);
120
44.4k
  memcpy(found, rstr, repl_len);
121
44.4k
  ss->index = strlen(ss->buffer);
122
44.4k
}
123
124
/// Removes the space characters '\t' and ' ' from the beginning of the stream buffer.
125
void SStream_trimls(SStream *ss)
126
3.42M
{
127
3.42M
  assert(ss);
128
3.42M
  size_t buf_off = 0;
129
  /// Remove leading spaces
130
3.50M
  while (ss->buffer[buf_off] == ' ' || ss->buffer[buf_off] == '\t') {
131
85.2k
    buf_off++;
132
85.2k
  }
133
3.42M
  if (buf_off > 0) {
134
85.2k
    memmove(ss->buffer, ss->buffer + buf_off,
135
85.2k
      SSTREAM_BUF_LEN - buf_off);
136
85.2k
    ss->index -= buf_off;
137
85.2k
  }
138
3.42M
}
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.27M
{
147
3.27M
  assert(ss && mnem_buf && mnem_buf_size > 0 && op_str_buf &&
148
3.27M
         op_str_buf_size > 0);
149
3.27M
  size_t off = 0;
150
  // Copy all non space chars to as mnemonic.
151
19.0M
  while (ss->buffer[off] && ss->buffer[off] != ' ' &&
152
18.2M
         ss->buffer[off] != '\t') {
153
15.7M
    if (off < mnem_buf_size - 1) {
154
      // Only copy if there is space left.
155
15.7M
      mnem_buf[off] = ss->buffer[off];
156
15.7M
    }
157
15.7M
    off++;
158
15.7M
  }
159
3.27M
  if (!ss->buffer[off]) {
160
123k
    return;
161
123k
  }
162
163
  // Iterate until next non space char.
164
3.15M
  do {
165
3.15M
    off++;
166
3.15M
  } while (ss->buffer[off] &&
167
3.10M
     (ss->buffer[off] == ' ' || ss->buffer[off] == '\t'));
168
169
3.15M
  if (!ss->buffer[off]) {
170
47.9k
    return;
171
47.9k
  }
172
173
  // Copy all follow up characters as op_str
174
3.10M
  const char *ss_op_str = ss->buffer + off;
175
3.10M
  off = 0;
176
46.8M
  while (ss_op_str[off] && off < op_str_buf_size - 1) {
177
43.7M
    op_str_buf[off] = ss_op_str[off];
178
43.7M
    off++;
179
43.7M
  }
180
3.10M
}
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
45.6k
{
186
45.6k
  assert(ss);
187
45.6k
  if (file) {
188
0
    fprintf(file, "%s\n", ss->buffer);
189
0
  }
190
45.6k
  SStream_Init(ss);
191
45.6k
}
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
20.0M
{
216
20.0M
#ifndef CAPSTONE_DIET
217
20.0M
  assert(ss && s);
218
20.0M
  SSTREAM_RETURN_IF_CLOSED(ss);
219
20.0M
  if (s[0] == '\0')
220
6.29M
    return;
221
222
13.7M
  SStream_concat_raw(ss, s, strlen(s));
223
13.7M
  SStream_close_markup(ss);
224
#else
225
  ss->buffer[ss->index] = '\0';
226
#endif
227
13.7M
}
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.74M
{
234
2.74M
#ifndef CAPSTONE_DIET
235
2.74M
  assert(ss);
236
2.74M
  SSTREAM_RETURN_IF_CLOSED(ss);
237
2.74M
  if (c == '\0')
238
38.0k
    return;
239
240
2.70M
  SStream_concat1_raw(ss, c);
241
2.70M
  SStream_close_markup(ss);
242
#else
243
  ss->buffer[ss->index] = '\0';
244
#endif
245
2.70M
}
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.94M
{
252
8.94M
#ifndef CAPSTONE_DIET
253
8.94M
  assert(ss && fmt);
254
8.94M
  SSTREAM_RETURN_IF_CLOSED(ss);
255
8.94M
  va_list ap;
256
8.94M
  int ret;
257
8.94M
  size_t remaining = SStream_remaining(ss);
258
8.94M
  if (remaining == 0) {
259
0
    return;
260
0
  }
261
262
8.94M
  va_start(ap, fmt);
263
8.94M
  ret = cs_vsnprintf(ss->buffer + ss->index, remaining + 1, fmt, ap);
264
8.94M
  va_end(ap);
265
8.94M
  if (ret < 0) {
266
0
    ss->buffer[ss->index] = '\0';
267
0
    return;
268
0
  }
269
8.94M
  if ((size_t)ret > remaining) {
270
0
    ss->index += remaining;
271
8.94M
  } else {
272
8.94M
    ss->index += (size_t)ret;
273
8.94M
  }
274
8.94M
  SStream_close_markup(ss);
275
#else
276
  ss->buffer[ss->index] = '\0';
277
#endif
278
8.94M
}
279
280
// print number with prefix #
281
void printInt64Bang(SStream *ss, int64_t val)
282
110k
{
283
110k
  assert(ss);
284
110k
  if (ss->unsigned_num) {
285
0
    printUInt64Bang(ss, val);
286
0
    return;
287
0
  }
288
110k
  SSTREAM_RETURN_IF_CLOSED(ss);
289
110k
  SStream_concat1(ss, '#');
290
110k
  printInt64(ss, val);
291
110k
}
292
293
void printUInt64Bang(SStream *ss, uint64_t val)
294
19.8k
{
295
19.8k
  assert(ss);
296
19.8k
  SSTREAM_RETURN_IF_CLOSED(ss);
297
19.8k
  SStream_concat1(ss, '#');
298
19.8k
  printUInt64(ss, val);
299
19.8k
}
300
301
// print number
302
void printInt64(SStream *ss, int64_t val)
303
826k
{
304
826k
  assert(ss);
305
826k
  if (ss->unsigned_num) {
306
0
    printUInt64(ss, val);
307
0
    return;
308
0
  }
309
826k
  SSTREAM_RETURN_IF_CLOSED(ss);
310
826k
  if (val >= 0) {
311
754k
    if (val > HEX_THRESHOLD)
312
477k
      SStream_concat(ss, "0x%" PRIx64, val);
313
277k
    else
314
277k
      SStream_concat(ss, "%" PRIu64, val);
315
754k
  } else {
316
71.7k
    if (val < -HEX_THRESHOLD) {
317
64.8k
      if (val == INT64_MIN)
318
205
        SStream_concat(ss, "-0x%" PRIx64,
319
205
                 (uint64_t)INT64_MAX + 1);
320
64.6k
      else
321
64.6k
        SStream_concat(ss, "-0x%" PRIx64,
322
64.6k
                 (uint64_t)-val);
323
64.8k
    } else
324
6.94k
      SStream_concat(ss, "-%" PRIu64, -val);
325
71.7k
  }
326
826k
}
327
328
void printUInt64(SStream *ss, uint64_t val)
329
268k
{
330
268k
  assert(ss);
331
268k
  SSTREAM_RETURN_IF_CLOSED(ss);
332
268k
  if (val > HEX_THRESHOLD)
333
154k
    SStream_concat(ss, "0x%" PRIx64, val);
334
114k
  else
335
114k
    SStream_concat(ss, "%" PRIu64, val);
336
268k
}
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
262k
{
355
262k
  assert(ss);
356
262k
  if (ss->unsigned_num) {
357
0
    printUInt32Bang(ss, val);
358
0
    return;
359
0
  }
360
262k
  SSTREAM_RETURN_IF_CLOSED(ss);
361
262k
  SStream_concat1(ss, '#');
362
262k
  printInt32(ss, val);
363
262k
}
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
4.69k
{
385
4.69k
  assert(ss);
386
4.69k
  if (ss->unsigned_num) {
387
0
    printUInt8(ss, val);
388
0
    return;
389
0
  }
390
4.69k
  SSTREAM_RETURN_IF_CLOSED(ss);
391
4.69k
  if (val >= 0) {
392
2.63k
    if (val > HEX_THRESHOLD)
393
1.86k
      SStream_concat(ss, "0x%" PRIx8, val);
394
770
    else
395
770
      SStream_concat(ss, "%" PRId8, val);
396
2.63k
  } else {
397
2.06k
    if (val < -HEX_THRESHOLD) {
398
1.20k
      if (val == INT8_MIN)
399
428
        SStream_concat(ss, "-0x%" PRIx8,
400
428
                 (uint8_t)INT8_MAX + 1);
401
772
      else
402
772
        SStream_concat(ss, "-0x%" PRIx8, (int8_t)-val);
403
1.20k
    } else
404
866
      SStream_concat(ss, "-%" PRIu8, -val);
405
2.06k
  }
406
4.69k
}
407
408
void printInt16(SStream *ss, int16_t val)
409
5.45k
{
410
5.45k
  assert(ss);
411
5.45k
  if (ss->unsigned_num) {
412
0
    printUInt16(ss, val);
413
0
    return;
414
0
  }
415
5.45k
  SSTREAM_RETURN_IF_CLOSED(ss);
416
5.45k
  if (val >= 0) {
417
3.80k
    if (val > HEX_THRESHOLD)
418
2.99k
      SStream_concat(ss, "0x%" PRIx16, val);
419
809
    else
420
809
      SStream_concat(ss, "%" PRId16, val);
421
3.80k
  } else {
422
1.65k
    if (val < -HEX_THRESHOLD) {
423
1.41k
      if (val == INT16_MIN)
424
34
        SStream_concat(ss, "-0x%" PRIx16,
425
34
                 (uint16_t)INT16_MAX + 1);
426
1.37k
      else
427
1.37k
        SStream_concat(ss, "-0x%" PRIx16,
428
1.37k
                 (int16_t)-val);
429
1.41k
    } else
430
245
      SStream_concat(ss, "-%" PRIu16, -val);
431
1.65k
  }
432
5.45k
}
433
434
void printInt16HexOffset(SStream *ss, int16_t val)
435
7.42k
{
436
7.42k
  assert(ss);
437
7.42k
  if (ss->unsigned_num) {
438
0
    printUInt16(ss, val);
439
0
    return;
440
0
  }
441
7.42k
  SSTREAM_RETURN_IF_CLOSED(ss);
442
7.42k
  if (val >= 0) {
443
5.36k
    SStream_concat(ss, "+0x%" PRIx16, val);
444
5.36k
  } else {
445
2.06k
    if (val == INT16_MIN)
446
115
      SStream_concat(ss, "-0x%" PRIx16,
447
115
               (uint16_t)INT16_MAX + 1);
448
1.94k
    else
449
1.94k
      SStream_concat(ss, "-0x%" PRIx16, (int16_t)-val);
450
2.06k
  }
451
7.42k
}
452
453
void printInt32(SStream *ss, int32_t val)
454
336k
{
455
336k
  assert(ss);
456
336k
  if (ss->unsigned_num) {
457
0
    printUInt32(ss, val);
458
0
    return;
459
0
  }
460
336k
  SSTREAM_RETURN_IF_CLOSED(ss);
461
336k
  if (val >= 0) {
462
246k
    if (val > HEX_THRESHOLD)
463
135k
      SStream_concat(ss, "0x%" PRIx32, val);
464
110k
    else
465
110k
      SStream_concat(ss, "%" PRId32, val);
466
246k
  } else {
467
90.4k
    if (val < -HEX_THRESHOLD) {
468
84.4k
      if (val == INT32_MIN)
469
249
        SStream_concat(ss, "-0x%" PRIx32,
470
249
                 (uint32_t)INT32_MAX + 1);
471
84.1k
      else
472
84.1k
        SStream_concat(ss, "-0x%" PRIx32,
473
84.1k
                 (int32_t)-val);
474
84.4k
    } else {
475
6.08k
      SStream_concat(ss, "-%" PRIu32, (uint32_t)-val);
476
6.08k
    }
477
90.4k
  }
478
336k
}
479
480
void printInt32HexOffset(SStream *ss, int32_t val)
481
1.08k
{
482
1.08k
  assert(ss);
483
1.08k
  if (ss->unsigned_num) {
484
0
    printUInt32(ss, val);
485
0
    return;
486
0
  }
487
1.08k
  SSTREAM_RETURN_IF_CLOSED(ss);
488
1.08k
  if (val >= 0) {
489
749
    SStream_concat(ss, "+0x%" PRIx32, val);
490
749
  } else {
491
340
    if (val == INT32_MIN)
492
73
      SStream_concat(ss, "-0x%" PRIx32,
493
73
               (uint32_t)INT32_MAX + 1);
494
267
    else
495
267
      SStream_concat(ss, "-0x%" PRIx32, (int32_t)-val);
496
340
  }
497
1.08k
}
498
499
void printInt32Hex(SStream *ss, int32_t val)
500
4.87k
{
501
4.87k
  assert(ss);
502
4.87k
  SSTREAM_RETURN_IF_CLOSED(ss);
503
4.87k
  if (val >= 0) {
504
2.82k
    SStream_concat(ss, "0x%" PRIx32, val);
505
2.82k
  } else {
506
2.05k
    if (val == INT32_MIN)
507
46
      SStream_concat(ss, "-0x%" PRIx32,
508
46
               (uint32_t)INT32_MAX + 1);
509
2.01k
    else
510
2.01k
      SStream_concat(ss, "-0x%" PRIx32, (int32_t)-val);
511
2.05k
  }
512
4.87k
}
513
514
void printUInt32Bang(SStream *ss, uint32_t val)
515
168k
{
516
168k
  assert(ss);
517
168k
  SSTREAM_RETURN_IF_CLOSED(ss);
518
168k
  SStream_concat1(ss, '#');
519
168k
  printUInt32(ss, val);
520
168k
}
521
522
void printUInt32(SStream *ss, uint32_t val)
523
288k
{
524
288k
  assert(ss);
525
288k
  SSTREAM_RETURN_IF_CLOSED(ss);
526
288k
  if (val > HEX_THRESHOLD)
527
202k
    SStream_concat(ss, "0x%x", val);
528
86.4k
  else
529
86.4k
    SStream_concat(ss, "%u", val);
530
288k
}
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
1.16k
{
541
1.16k
  assert(ss);
542
1.16k
  SSTREAM_RETURN_IF_CLOSED(ss);
543
1.16k
  SStream_concat(ss, fmt, val);
544
1.16k
}
545
546
void printFloatBang(SStream *ss, float val)
547
1.58k
{
548
1.58k
  assert(ss);
549
1.58k
  SSTREAM_RETURN_IF_CLOSED(ss);
550
1.58k
  SStream_concat(ss, "#%e", val);
551
1.58k
}
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
1.08M
{
562
1.08M
  assert(OS);
563
564
1.08M
  if (OS->is_closed || !OS->markup_stream) {
565
1.08M
    return OS;
566
1.08M
  }
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
}