1 /* SPDX-License-Identifier: GPL-2.0-or-later */
14 void print_data(const uint8_t data
[], size_t data_size
, enum data_type type
)
22 for (size_t i
= 0; i
< data_size
; ++i
) {
28 printf("%s\n", value
? "true" : "false");
33 "warning: expected size of 1, got %zu\n",
38 printf("%u\n", *(uint8_t *)data
);
40 case DATA_TYPE_UINT16
:
43 "warning: expected size of 2, got %zu\n",
48 printf("%u\n", *(uint16_t *)data
);
50 case DATA_TYPE_UINT32
:
53 "warning: expected size of 4, got %zu\n",
58 printf("%u\n", *(uint32_t *)data
);
61 for (size_t i
= 0; i
< data_size
; ++i
) {
68 case DATA_TYPE_UNICODE
:
69 char *chars
= to_chars((const CHAR16
*)data
, data_size
);
70 printf("%s\n", chars
);
74 fwrite(data
, 1, data_size
, stdout
);
79 static uint64_t parse_uint(const char source
[],
81 unsigned long long max
)
84 unsigned long long uint
= strtoull(source
, &end
, /*base=*/0);
86 fprintf(stderr
, "Trailing characters in \"%s\": %s\n",
91 fprintf(stderr
, "Invalid %s value: %llu\n", type
, uint
);
98 void *make_data(const char source
[], size_t *data_size
, enum data_type type
)
103 unsigned long long uint
;
106 if (str_eq(source
, "true")) {
108 } else if (str_eq(source
, "false")) {
111 fprintf(stderr
, "Invalid boolean value: \"%s\"\n",
117 data
= xmalloc(*data_size
);
118 *(uint8_t *)data
= boolean
;
120 case DATA_TYPE_UINT8
:
121 uint
= parse_uint(source
, "uint8", UINT8_MAX
);
122 if (uint
== UINT64_MAX
)
126 data
= xmalloc(*data_size
);
127 *(uint8_t *)data
= uint
;
129 case DATA_TYPE_UINT16
:
130 uint
= parse_uint(source
, "uint16", UINT16_MAX
);
131 if (uint
== UINT64_MAX
)
135 data
= xmalloc(*data_size
);
136 *(uint16_t *)data
= uint
;
138 case DATA_TYPE_UINT32
:
139 uint
= parse_uint(source
, "uint32", UINT32_MAX
);
140 if (uint
== UINT64_MAX
)
144 data
= xmalloc(*data_size
);
145 *(uint32_t *)data
= uint
;
147 case DATA_TYPE_ASCII
:
148 *data_size
= strlen(source
) + 1;
149 return strdup(source
);
150 case DATA_TYPE_UNICODE
:
151 return to_uchars(source
, data_size
);
153 fprintf(stderr
, "Raw data type is output only\n");
160 bool parse_data_type(const char str
[], enum data_type
*type
)
162 if (str_eq(str
, "bool"))
163 *type
= DATA_TYPE_BOOL
;
164 else if (str_eq(str
, "uint8"))
165 *type
= DATA_TYPE_UINT8
;
166 else if (str_eq(str
, "uint16"))
167 *type
= DATA_TYPE_UINT16
;
168 else if (str_eq(str
, "uint32"))
169 *type
= DATA_TYPE_UINT32
;
170 else if (str_eq(str
, "ascii"))
171 *type
= DATA_TYPE_ASCII
;
172 else if (str_eq(str
, "unicode"))
173 *type
= DATA_TYPE_UNICODE
;
174 else if (str_eq(str
, "raw"))
175 *type
= DATA_TYPE_RAW
;