about summary refs log tree commit diff
path: root/frida_mode/src/util.c
blob: 2b0f7be694e726b5053fa1f153ce78f8a444d6fb (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include "util.h"

guint64 util_read_address(char *key) {

  char *value_str = getenv(key);

  if (value_str == NULL) { return 0; }

  if (!g_str_has_prefix(value_str, "0x")) {

    FATAL("Invalid address should have 0x prefix: %s=%s\n", key, value_str);

  }

  char *value_str2 = &value_str[2];

  for (char *c = value_str2; *c != '\0'; c++) {

    if (!g_ascii_isxdigit(*c)) {

      FATAL("Invalid address not formed of hex digits: %s=%s ('%c')\n", key,
            value_str, *c);

    }

  }

  guint64 value = g_ascii_strtoull(value_str2, NULL, 16);
  if (value == 0) {

    FATAL("Invalid address failed hex conversion: %s=%s\n", key, value_str2);

  }

  return value;

}

guint64 util_read_num(char *key) {

  char *value_str = getenv(key);

  if (value_str == NULL) { return 0; }

  for (char *c = value_str; *c != '\0'; c++) {

    if (!g_ascii_isdigit(*c)) {

      FATAL("Invalid address not formed of decimal digits: %s=%s\n", key,
            value_str);

    }

  }

  guint64 value = g_ascii_strtoull(value_str, NULL, 10);
  if (value == 0) {

    FATAL("Invalid address failed numeric conversion: %s=%s\n", key, value_str);

  }

  return value;

}

gboolean util_output_enabled(void) {

  static gboolean initialized = FALSE;
  static gboolean enabled = TRUE;

  if (!initialized) {

    initialized = TRUE;
    if (getenv("AFL_DEBUG_CHILD") == NULL) { enabled = FALSE; }

  }

  return enabled;

}