1 /*
2  * Copyright (C) 2021 The Android Open Sourete Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include <string.h>
18 #include <trusty/coverage/uuid.h>
19 #include <uuid.h>
20 
21 #include <stdio.h>
22 
reverse_u16(uint16_t u)23 static uint16_t reverse_u16(uint16_t u) {
24     return u << 8 | u >> 8;
25 }
26 
reverse_u32(uint32_t u)27 static uint32_t reverse_u32(uint32_t u) {
28     return reverse_u16((uint16_t)u) << 16 | reverse_u16(u >> 16);
29 }
30 
str_to_uuid(const char * str,struct uuid * uuid)31 bool str_to_uuid(const char* str, struct uuid* uuid) {
32     uuid_t uu;
33     static_assert(sizeof(uu) == sizeof(*uuid));
34 
35     if (uuid_parse(str, uu)) {
36         return false;
37     }
38 
39     memcpy(uuid, uu, sizeof(*uuid));
40     uuid->time_low = reverse_u32(uuid->time_low);
41     uuid->time_mid = reverse_u16(uuid->time_mid);
42     uuid->time_hi_and_version = reverse_u16(uuid->time_hi_and_version);
43     return true;
44 }
45