Changeset ebb1489 in mainline for uspace/lib/c/generic/uuid.c


Ignore:
Timestamp:
2024-10-13T08:23:40Z (8 weeks ago)
Author:
GitHub <noreply@…>
Children:
0472cf17
Parents:
2a0c827c (diff), b3b79981 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.
git-author:
boba-buba <120932204+boba-buba@…> (2024-10-13 08:23:40)
git-committer:
GitHub <noreply@…> (2024-10-13 08:23:40)
Message:

Merge branch 'HelenOS:master' into topic/packet-capture

File:
1 edited

Legend:

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

    r2a0c827c rebb1489  
    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:
     
    82121        int i;
    83122
    84         for (i = 0; i < uuid_bytes; i++)
    85                 buf[i] = uuid->b[i];
     123        encode32_be(buf, uuid->time_low);
     124        encode16_be(buf + 4, uuid->time_mid);
     125        encode16_be(buf + 6, uuid->time_hi_and_version);
     126        buf[8] = uuid->clock_seq_hi_and_reserved;
     127        buf[9] = uuid->clock_seq_low;
     128
     129        for (i = 0; i < _UUID_NODE_LEN; i++)
     130                buf[10 + i] = uuid->node[i];
    86131}
    87132
     
    95140        int i;
    96141
    97         for (i = 0; i < uuid_bytes; i++)
    98                 uuid->b[i] = buf[i];
     142        uuid->time_low = decode32_be(buf);
     143        uuid->time_mid = decode16_be(buf + 4);
     144        uuid->time_hi_and_version = decode16_be(buf + 6);
     145        uuid->clock_seq_hi_and_reserved = buf[8];
     146        uuid->clock_seq_low = buf[9];
     147
     148        for (i = 0; i < _UUID_NODE_LEN; i++)
     149                uuid->node[i] = buf[10 + i];
     150}
     151
     152/** Encode UUID into little-endian (GPT) binary form.
     153 *
     154 * @param uuid UUID
     155 * @param buf 16-byte destination buffer
     156 */
     157void uuid_encode_le(uuid_t *uuid, uint8_t *buf)
     158{
     159        int i;
     160
     161        encode32_le(buf, uuid->time_low);
     162        encode16_le(buf + 4, uuid->time_mid);
     163        encode16_le(buf + 6, uuid->time_hi_and_version);
     164        buf[8] = uuid->clock_seq_hi_and_reserved;
     165        buf[9] = uuid->clock_seq_low;
     166
     167        for (i = 0; i < _UUID_NODE_LEN; i++)
     168                buf[10 + i] = uuid->node[i];
     169}
     170
     171/** Decode UUID from little-endian (GPT) binary form.
     172 *
     173 * @param buf 16-byte source buffer
     174 * @param uuid Place to store UUID
     175 */
     176void uuid_decode_le(uint8_t *buf, uuid_t *uuid)
     177{
     178        int i;
     179
     180        uuid->time_low = decode32_le(buf);
     181        uuid->time_mid = decode16_le(buf + 4);
     182        uuid->time_hi_and_version = decode16_le(buf + 6);
     183        uuid->clock_seq_hi_and_reserved = buf[8];
     184        uuid->clock_seq_low = buf[9];
     185
     186        for (i = 0; i < _UUID_NODE_LEN; i++)
     187                uuid->node[i] = buf[10 + i];
    99188}
    100189
     
    142231                return EINVAL;
    143232
    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;
     233        uuid->time_low = time_low;
     234
     235        uuid->time_mid = time_mid;
     236
     237        uuid->time_hi_and_version = time_ver;
     238
     239        uuid->clock_seq_hi_and_reserved = clock >> 8;
     240
     241        uuid->clock_seq_low = clock & 0xff;
     242
     243        for (i = 0; i < _UUID_NODE_LEN; i++)
     244                uuid->node[i] = (node >> 8 * (5 - i)) & 0xff;
    160245
    161246        if (endptr != NULL) {
     
    183268                return ENOMEM;
    184269
    185         const char *format = "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x";
     270        const char *format = "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x";
    186271        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]);
     272                format = "%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X";
     273
     274        int ret = snprintf(str, size, format, uuid->time_low, uuid->time_mid,
     275            uuid->time_hi_and_version, uuid->clock_seq_hi_and_reserved,
     276            uuid->clock_seq_low, uuid->node[0], uuid->node[1], uuid->node[2],
     277            uuid->node[3], uuid->node[4], uuid->node[5]);
    190278
    191279        if (ret != 36) {
     
    198286}
    199287
     288static void encode16_be(uint8_t *buf, uint16_t value)
     289{
     290        buf[0] = (value >> 8) & 0xff;
     291        buf[1] = value & 0xff;
     292}
     293
     294static void encode16_le(uint8_t *buf, uint16_t value)
     295{
     296        buf[0] = value & 0xff;
     297        buf[1] = (value >> 8) & 0xff;
     298}
     299
     300static void encode32_be(uint8_t *buf, uint32_t value)
     301{
     302        buf[0] = (value >> 24) & 0xff;
     303        buf[1] = (value >> 16) & 0xff;
     304        buf[2] = (value >> 8) & 0xff;
     305        buf[3] = value & 0xff;
     306}
     307
     308static void encode32_le(uint8_t *buf, uint32_t value)
     309{
     310        buf[0] = value & 0xff;
     311        buf[1] = (value >> 8) & 0xff;
     312        buf[2] = (value >> 16) & 0xff;
     313        buf[3] = (value >> 24) & 0xff;
     314}
     315
     316static uint16_t decode16_be(uint8_t *buf)
     317{
     318        return ((buf[0] << 8) | buf[1]);
     319}
     320
     321static uint16_t decode16_le(uint8_t *buf)
     322{
     323        return ((buf[1] << 8) | buf[0]);
     324}
     325
     326static uint32_t decode32_be(uint8_t *buf)
     327{
     328        return ((buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3]);
     329}
     330
     331static uint32_t decode32_le(uint8_t *buf)
     332{
     333        return ((buf[3] << 24) | (buf[2] << 16) | (buf[1] << 8) | buf[0]);
     334}
     335
    200336/** @}
    201337 */
Note: See TracChangeset for help on using the changeset viewer.