Vesa Jääskeläinen | 7e2a230 | 2020-04-05 20:11:58 +0300 | [diff] [blame^] | 1 | // SPDX-License-Identifier: BSD-2-Clause |
| 2 | /* |
| 3 | * Copyright (c) 2019, Linaro Limited |
| 4 | */ |
| 5 | |
| 6 | #include <ctype.h> |
| 7 | #include <stdint.h> |
| 8 | #include <string.h> |
| 9 | #include <tee_api_types.h> |
| 10 | #include <tee_client_api.h> |
| 11 | #include "xtest_uuid_helpers.h" |
| 12 | |
| 13 | static int hex(char c) |
| 14 | { |
| 15 | char lc = tolower(c); |
| 16 | |
| 17 | if (isdigit(lc)) |
| 18 | return lc - '0'; |
| 19 | if (isxdigit(lc)) |
| 20 | return lc - 'a' + 10; |
| 21 | return -1; |
| 22 | } |
| 23 | |
| 24 | static uint32_t parse_hex(const char *s, size_t nchars, uint32_t *res) |
| 25 | { |
| 26 | uint32_t v = 0; |
| 27 | size_t n = 0; |
| 28 | int c = 0; |
| 29 | |
| 30 | for (n = 0; n < nchars; n++) { |
| 31 | c = hex(s[n]); |
| 32 | if (c == -1) { |
| 33 | *res = TEE_ERROR_BAD_FORMAT; |
| 34 | goto out; |
| 35 | } |
| 36 | v = (v << 4) + c; |
| 37 | } |
| 38 | *res = TEE_SUCCESS; |
| 39 | out: |
| 40 | return v; |
| 41 | } |
| 42 | |
| 43 | TEEC_Result xtest_uuid_from_str(TEEC_UUID *uuid, const char *s) |
| 44 | { |
| 45 | TEEC_Result res = TEEC_SUCCESS; |
| 46 | TEEC_UUID u = { }; |
| 47 | const char *p = s; |
| 48 | size_t i = 0; |
| 49 | |
| 50 | if (!p || strnlen(p, 37) != 36) |
| 51 | return TEEC_ERROR_BAD_FORMAT; |
| 52 | if (p[8] != '-' || p[13] != '-' || p[18] != '-' || p[23] != '-') |
| 53 | return TEEC_ERROR_BAD_FORMAT; |
| 54 | |
| 55 | u.timeLow = parse_hex(p, 8, &res); |
| 56 | if (res) |
| 57 | goto out; |
| 58 | p += 9; |
| 59 | u.timeMid = parse_hex(p, 4, &res); |
| 60 | if (res) |
| 61 | goto out; |
| 62 | p += 5; |
| 63 | u.timeHiAndVersion = parse_hex(p, 4, &res); |
| 64 | if (res) |
| 65 | goto out; |
| 66 | p += 5; |
| 67 | for (i = 0; i < 8; i++) { |
| 68 | u.clockSeqAndNode[i] = parse_hex(p, 2, &res); |
| 69 | if (res) |
| 70 | goto out; |
| 71 | if (i == 1) |
| 72 | p += 3; |
| 73 | else |
| 74 | p += 2; |
| 75 | } |
| 76 | *uuid = u; |
| 77 | out: |
| 78 | return res; |
| 79 | } |