| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
#include <string.h> |
| 17 |
|
| 18 |
#include "error/s2n_errno.h" |
| 19 |
#include "stuffer/s2n_stuffer.h" |
| 20 |
#include "testlib/s2n_testlib.h" |
| 21 |
#include "utils/s2n_safety.h" |
| 22 |
|
| 23 |
S2N_RESULT s2n_stuffer_alloc_from_hex(struct s2n_stuffer *bytes_out, const char *hex_cstr) |
| 24 |
{ |
| 25 |
RESULT_ENSURE_REF(bytes_out); |
| 26 |
RESULT_ENSURE_REF(hex_cstr); |
| 27 |
|
| 28 |
DEFER_CLEANUP(struct s2n_stuffer hex = { 0 }, s2n_stuffer_free); |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
RESULT_GUARD_POSIX(s2n_stuffer_alloc(&hex, strlen(hex_cstr))); |
| 33 |
RESULT_GUARD_POSIX(s2n_stuffer_write_str(&hex, hex_cstr)); |
| 34 |
|
| 35 |
uint32_t bytes_size = strlen(hex_cstr) / 2; |
| 36 |
RESULT_GUARD_POSIX(s2n_stuffer_alloc(bytes_out, bytes_size)); |
| 37 |
RESULT_GUARD(s2n_stuffer_read_hex(&hex, &bytes_out->blob)); |
| 38 |
RESULT_ENSURE(s2n_stuffer_data_available(&hex) == 0, S2N_ERR_BAD_HEX); |
| 39 |
RESULT_GUARD_POSIX(s2n_stuffer_skip_write(bytes_out, bytes_size)); |
| 40 |
return S2N_RESULT_OK; |
| 41 |
} |
| 42 |
|
| 43 |
S2N_RESULT s2n_blob_alloc_from_hex_with_whitespace(struct s2n_blob *bytes_out, const char *hex_cstr) |
| 44 |
{ |
| 45 |
RESULT_ENSURE_REF(bytes_out); |
| 46 |
RESULT_ENSURE_REF(hex_cstr); |
| 47 |
|
| 48 |
DEFER_CLEANUP(struct s2n_stuffer hex_in = { 0 }, s2n_stuffer_free); |
| 49 |
RESULT_GUARD_POSIX(s2n_stuffer_alloc(&hex_in, strlen(hex_cstr))); |
| 50 |
for (size_t i = 0; i < strlen(hex_cstr); i++) { |
| 51 |
if (hex_cstr[i] == ' ') { |
| 52 |
continue; |
| 53 |
} |
| 54 |
RESULT_GUARD_POSIX(s2n_stuffer_write_uint8(&hex_in, hex_cstr[i])); |
| 55 |
} |
| 56 |
uint32_t hex_in_size = s2n_stuffer_data_available(&hex_in); |
| 57 |
|
| 58 |
RESULT_GUARD_POSIX(s2n_alloc(bytes_out, hex_in_size / 2)); |
| 59 |
RESULT_GUARD(s2n_stuffer_read_hex(&hex_in, bytes_out)); |
| 60 |
RESULT_ENSURE(s2n_stuffer_data_available(&hex_in) == 0, S2N_ERR_BAD_HEX); |
| 61 |
return S2N_RESULT_OK; |
| 62 |
} |