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
);
60 case DATA_TYPE_UINT64
:
63 "warning: expected size of 8, got %zu\n",
68 printf("%llu\n", (unsigned long long)*(uint64_t *)data
);
71 for (size_t i
= 0; i
< data_size
; ++i
) {
78 case DATA_TYPE_UNICODE
:
79 char *chars
= to_chars((const CHAR16
*)data
, data_size
);
80 printf("%s\n", chars
);
84 fwrite(data
, 1, data_size
, stdout
);
89 static uint64_t parse_uint(const char source
[],
91 unsigned long long max
,
95 unsigned long long uint
= strtoull(source
, &end
, /*base=*/0);
97 fprintf(stderr
, "Trailing characters in \"%s\": %s\n",
103 fprintf(stderr
, "Invalid %s value: %llu\n", type
, uint
);
112 void *make_data(const char source
[], size_t *data_size
, enum data_type type
)
121 if (str_eq(source
, "true")) {
123 } else if (str_eq(source
, "false")) {
126 fprintf(stderr
, "Invalid boolean value: \"%s\"\n",
132 data
= xmalloc(*data_size
);
133 *(uint8_t *)data
= boolean
;
135 case DATA_TYPE_UINT8
:
136 uint
= parse_uint(source
, "uint8", UINT8_MAX
, &failed
);
141 data
= xmalloc(*data_size
);
142 *(uint8_t *)data
= uint
;
144 case DATA_TYPE_UINT16
:
145 uint
= parse_uint(source
, "uint16", UINT16_MAX
, &failed
);
150 data
= xmalloc(*data_size
);
151 *(uint16_t *)data
= uint
;
153 case DATA_TYPE_UINT32
:
154 uint
= parse_uint(source
, "uint32", UINT32_MAX
, &failed
);
159 data
= xmalloc(*data_size
);
160 *(uint32_t *)data
= uint
;
162 case DATA_TYPE_UINT64
:
163 uint
= parse_uint(source
, "uint64", UINT64_MAX
, &failed
);
168 data
= xmalloc(*data_size
);
169 *(uint64_t *)data
= uint
;
171 case DATA_TYPE_ASCII
:
172 *data_size
= strlen(source
) + 1;
173 return strdup(source
);
174 case DATA_TYPE_UNICODE
:
175 return to_uchars(source
, data_size
);
177 fprintf(stderr
, "Raw data type is output only\n");
184 bool parse_data_type(const char str
[], enum data_type
*type
)
186 if (str_eq(str
, "bool"))
187 *type
= DATA_TYPE_BOOL
;
188 else if (str_eq(str
, "uint8"))
189 *type
= DATA_TYPE_UINT8
;
190 else if (str_eq(str
, "uint16"))
191 *type
= DATA_TYPE_UINT16
;
192 else if (str_eq(str
, "uint32"))
193 *type
= DATA_TYPE_UINT32
;
194 else if (str_eq(str
, "uint64"))
195 *type
= DATA_TYPE_UINT64
;
196 else if (str_eq(str
, "ascii"))
197 *type
= DATA_TYPE_ASCII
;
198 else if (str_eq(str
, "unicode"))
199 *type
= DATA_TYPE_UNICODE
;
200 else if (str_eq(str
, "raw"))
201 *type
= DATA_TYPE_RAW
;