Changeset d85d4f2 in mainline


Ignore:
Timestamp:
2024-06-13T08:37:44Z (3 weeks ago)
Author:
Miroslav Cimerman <mc@…>
Children:
8c5d686
Parents:
407e251
git-author:
Miroslav Cimerman <mc@…> (2024-06-12 21:08:14)
git-committer:
Miroslav Cimerman <mc@…> (2024-06-13 08:37:44)
Message:

Change internal UUID representation to named fields

Location:
uspace/lib/c
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • uspace/lib/c/generic/uuid.c

    r407e251 rd85d4f2  
    4141#include <stdio.h>
    4242
     43static void encode16_be(uint8_t *, uint16_t);
     44static void encode16_le(uint8_t *, uint16_t);
     45static void encode32_be(uint8_t *, uint32_t);
     46static void encode32_le(uint8_t *, uint32_t);
     47static uint16_t decode16_be(uint8_t *);
     48static uint16_t decode16_le(uint8_t *);
     49static uint32_t decode32_be(uint8_t *);
     50static uint32_t decode32_le(uint8_t *);
     51
    4352/** Generate UUID.
    4453 *
     
    5665                return EIO;
    5766
    58         for (i = 0; i < uuid_bytes; i++) {
    59                 rc = rndgen_uint8(rndgen, &uuid->b[i]);
     67        rc = rndgen_uint32(rndgen, &uuid->time_low);
     68        if (rc != EOK) {
     69                rc = EIO;
     70                goto error;
     71        }
     72
     73        rc = rndgen_uint16(rndgen, &uuid->time_mid);
     74        if (rc != EOK) {
     75                rc = EIO;
     76                goto error;
     77        }
     78
     79        rc = rndgen_uint16(rndgen, &uuid->time_hi_and_version);
     80        if (rc != EOK) {
     81                rc = EIO;
     82                goto error;
     83        }
     84
     85        rc = rndgen_uint8(rndgen, &uuid->clock_seq_hi_and_reserved);
     86        if (rc != EOK) {
     87                rc = EIO;
     88                goto error;
     89        }
     90
     91        rc = rndgen_uint8(rndgen, &uuid->clock_seq_low);
     92        if (rc != EOK) {
     93                rc = EIO;
     94                goto error;
     95        }
     96
     97        for (i = 0; i < _UUID_NODE_LEN; i++) {
     98                rc = rndgen_uint8(rndgen, &uuid->node[i]);
    6099                if (rc != EOK) {
    61100                        rc = EIO;
     
    65104
    66105        /* Version 4 UUID from random or pseudo-random numbers */
    67         uuid->b[6] = (uuid->b[6] & 0x0f) | 0x40;
    68         uuid->b[8] = (uuid->b[8] & 0x3f) | 0x80;
     106        uuid->time_hi_and_version = (uuid->time_hi_and_version & 0x0fff) | 0x4000;
     107        uuid->clock_seq_hi_and_reserved = (uuid->clock_seq_hi_and_reserved & 0x3f) | 0x80;
    69108
    70109error:
     
    73112}
    74113
     114
    75115/** Encode UUID into binary form per RFC 4122.
    76116 *
     
    82122        int i;
    83123
    84         for (i = 0; i < uuid_bytes; i++)
    85                 buf[i] = uuid->b[i];
     124        encode32_be(buf, uuid->time_low);
     125        encode16_be(buf + 4, uuid->time_mid);
     126        encode16_be(buf + 6, uuid->time_hi_and_version);
     127        buf[8] = uuid->clock_seq_hi_and_reserved;
     128        buf[9] = uuid->clock_seq_low;
     129
     130        for (i = 0; i < _UUID_NODE_LEN; i++)
     131                buf[10 + i] = uuid->node[i];
    86132}
    87133
     
    95141        int i;
    96142
    97         for (i = 0; i < uuid_bytes; i++)
    98                 uuid->b[i] = buf[i];
     143        uuid->time_low = decode32_be(buf);
     144        uuid->time_mid = decode16_be(buf + 4);
     145        uuid->time_hi_and_version = decode16_be(buf + 6);
     146        uuid->clock_seq_hi_and_reserved = buf[8];
     147        uuid->clock_seq_low = buf[9];
     148
     149        for (i = 0; i < _UUID_NODE_LEN; i++)
     150                uuid->node[i] = buf[10 + i];
     151}
     152
     153/** Encode UUID into little-endian (GPT) binary form.
     154 *
     155 * @param uuid UUID
     156 * @param buf 16-byte destination buffer
     157 */
     158void uuid_encode_le(uuid_t *uuid, uint8_t *buf)
     159{
     160        int i;
     161
     162        encode32_le(buf, uuid->time_low);
     163        encode16_le(buf + 4, uuid->time_mid);
     164        encode16_le(buf + 6, uuid->time_hi_and_version);
     165        buf[8] = uuid->clock_seq_hi_and_reserved;
     166        buf[9] = uuid->clock_seq_low;
     167
     168        for (i = 0; i < _UUID_NODE_LEN; i++)
     169                buf[10 + i] = uuid->node[i];
     170}
     171
     172/** Decode UUID from little-endian (GPT) binary form.
     173 *
     174 * @param buf 16-byte source buffer
     175 * @param uuid Place to store UUID
     176 */
     177void uuid_decode_le(uint8_t *buf, uuid_t *uuid)
     178{
     179        int i;
     180
     181        uuid->time_low = decode32_le(buf);
     182        uuid->time_mid = decode16_le(buf + 4);
     183        uuid->time_hi_and_version = decode16_le(buf + 6);
     184        uuid->clock_seq_hi_and_reserved = buf[8];
     185        uuid->clock_seq_low = buf[9];
     186
     187        for (i = 0; i < _UUID_NODE_LEN; i++)
     188                uuid->node[i] = buf[10 + i];
    99189}
    100190
     
    142232                return EINVAL;
    143233
    144         uuid->b[0] = time_low >> 24;
    145         uuid->b[1] = (time_low >> 16) & 0xff;
    146         uuid->b[2] = (time_low >> 8) & 0xff;
    147         uuid->b[3] = time_low & 0xff;
    148 
    149         uuid->b[4] = time_mid >> 8;
    150         uuid->b[5] = time_mid & 0xff;
    151 
    152         uuid->b[6] = time_ver >> 8;
    153         uuid->b[7] = time_ver & 0xff;
    154 
    155         uuid->b[8] = clock >> 8;
    156         uuid->b[9] = clock & 0xff;
    157 
    158         for (i = 0; i < 6; i++)
    159                 uuid->b[10 + i] = (node >> 8 * (5 - i)) & 0xff;
     234        uuid->time_low = time_low;
     235
     236        uuid->time_mid = time_mid;
     237
     238        uuid->time_hi_and_version = time_ver;
     239
     240        uuid->clock_seq_hi_and_reserved = clock >> 8;
     241
     242        uuid->clock_seq_low = clock & 0xff;
     243
     244        for (i = 0; i < _UUID_NODE_LEN; i++)
     245                uuid->node[i] = (node >> 8 * (5 - i)) & 0xff;
    160246
    161247        if (endptr != NULL) {
     
    183269                return ENOMEM;
    184270
    185         const char *format = "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x";
     271        const char *format = "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x";
    186272        if (uppercase)
    187                 format = "%02X%02X%02X%02X-%02X%02X-%02X%02X-%02X%02X-%02X%02X%02X%02X%02X%02X";
    188 
    189         int ret = snprintf(str, size, format, uuid->b[0], uuid->b[1], uuid->b[2], uuid->b[3], uuid->b[4], uuid->b[5], uuid->b[6], uuid->b[7], uuid->b[8], uuid->b[9], uuid->b[10], uuid->b[11], uuid->b[12], uuid->b[13], uuid->b[14], uuid->b[15]);
     273                format = "%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X";
     274
     275        int ret = snprintf(str, size, format, uuid->time_low, uuid->time_mid,
     276            uuid->time_hi_and_version, uuid->clock_seq_hi_and_reserved,
     277            uuid->clock_seq_low, uuid->node[0], uuid->node[1], uuid->node[2],
     278            uuid->node[3], uuid->node[4], uuid->node[5]);
    190279
    191280        if (ret != 36) {
     
    198287}
    199288
     289static void encode16_be(uint8_t *buf, uint16_t value)
     290{
     291        buf[0] = (value >> 8) & 0xff;
     292        buf[1] = value & 0xff;
     293}
     294
     295static void encode16_le(uint8_t *buf, uint16_t value)
     296{
     297        buf[0] = value & 0xff;
     298        buf[1] = (value >> 8) & 0xff;
     299}
     300
     301static void encode32_be(uint8_t *buf, uint32_t value)
     302{
     303        buf[0] = (value >> 24) & 0xff;
     304        buf[1] = (value >> 16) & 0xff;
     305        buf[2] = (value >> 8) & 0xff;
     306        buf[3] = value & 0xff;
     307}
     308
     309static void encode32_le(uint8_t *buf, uint32_t value)
     310{
     311        buf[0] = value & 0xff;
     312        buf[1] = (value >> 8) & 0xff;
     313        buf[2] = (value >> 16) & 0xff;
     314        buf[3] = (value >> 24) & 0xff;
     315}
     316
     317static uint16_t decode16_be(uint8_t *buf)
     318{
     319        return ((buf[0] << 8) | buf[1]);
     320}
     321
     322static uint16_t decode16_le(uint8_t *buf)
     323{
     324        return ((buf[1] << 8) | buf[0]);
     325}
     326
     327static uint32_t decode32_be(uint8_t *buf)
     328{
     329        return ((buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3]);
     330}
     331
     332static uint32_t decode32_le(uint8_t *buf)
     333{
     334        return ((buf[3] << 24) | (buf[2] << 16) | (buf[1] << 8) | buf[0]);
     335}
     336
    200337/** @}
    201338 */
  • uspace/lib/c/include/types/uuid.h

    r407e251 rd85d4f2  
    3838#include <stdint.h>
    3939
    40 enum {
    41         uuid_bytes = 16
    42 };
     40#define _UUID_NODE_LEN 6
    4341
    4442/** Universally Unique Identifier */
     43
    4544typedef struct {
    46         uint8_t b[uuid_bytes];
     45        uint32_t time_low;
     46        uint16_t time_mid;
     47        uint16_t time_hi_and_version;
     48        uint8_t clock_seq_hi_and_reserved;
     49        uint8_t clock_seq_low;
     50        uint8_t node[_UUID_NODE_LEN];
    4751} uuid_t;
    4852
  • uspace/lib/c/include/uuid.h

    r407e251 rd85d4f2  
    4343extern void uuid_encode(uuid_t *, uint8_t *);
    4444extern void uuid_decode(uint8_t *, uuid_t *);
     45extern void uuid_encode_le(uuid_t *, uint8_t *);
     46extern void uuid_decode_le(uint8_t *, uuid_t *);
    4547extern errno_t uuid_parse(const char *, uuid_t *, const char **);
    4648extern errno_t uuid_format(uuid_t *, char **, bool);
  • uspace/lib/c/test/uuid.c

    r407e251 rd85d4f2  
    5050static bool uuid_valid(uuid_t uuid)
    5151{
    52         if (!(uuid.b[6] & 0x40)) {
     52        if (!((uuid.time_hi_and_version & 0xf000) & 0x4000)) {
    5353                return false;
    5454        }
    5555
    56         int f = (uuid.b[8] & 0x80) || (uuid.b[8] & 0x90);
    57         f = f || (uuid.b[8] & 0xA0) || (uuid.b[8] & 0xB0);
     56        int f = (uuid.clock_seq_hi_and_reserved & 0x80) || (uuid.clock_seq_hi_and_reserved & 0x90);
     57        f = f || (uuid.clock_seq_hi_and_reserved & 0xA0) || (uuid.clock_seq_hi_and_reserved & 0xB0);
    5858        if (!f) {
    5959                return false;
Note: See TracChangeset for help on using the changeset viewer.