| 1 |
/* |
| 2 |
* Copyright (C) 2024 Linux Studio Plugins Project <https://lsp-plug.in/> |
| 3 |
* (C) 2024 Vladimir Sadovnikov <[email protected]> |
| 4 |
* |
| 5 |
* This file is part of lsp-dsp-units |
| 6 |
* Created on: 11 мая 2024 г. |
| 7 |
* |
| 8 |
* lsp-dsp-units is free software: you can redistribute it and/or modify |
| 9 |
* it under the terms of the GNU Lesser General Public License as published by |
| 10 |
* the Free Software Foundation, either version 3 of the License, or |
| 11 |
* any later version. |
| 12 |
* |
| 13 |
* lsp-dsp-units is distributed in the hope that it will be useful, |
| 14 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 |
* GNU Lesser General Public License for more details. |
| 17 |
* |
| 18 |
* You should have received a copy of the GNU Lesser General Public License |
| 19 |
* along with lsp-dsp-units. If not, see <https://www.gnu.org/licenses/>. |
| 20 |
*/ |
| 21 |
|
| 22 |
#include <lsp-plug.in/runtime/uuid.h> |
| 23 |
#include <lsp-plug.in/stdlib/stdlib.h> |
| 24 |
|
| 25 |
namespace lsp |
| 26 |
{ |
| 27 |
static const char *hex_lower = "0123456789abcdef"; |
| 28 |
static const char *hex_upper = "0123456789ABCDEF"; |
| 29 |
static const char *base64 = |
| 30 |
"0123456789abcdef" |
| 31 |
"ghijklmnopqrstuv" |
| 32 |
"wxyzABCDEFGHIJKL" |
| 33 |
"MNOPQRSTUVWXYZ_-"; |
| 34 |
|
| 35 |
static char *fmt_bytes(char *dst, const uint8_t *ptr, const char *table, size_t count) |
| 36 |
{ |
| 37 |
for (size_t i=0; i<count; ++i, dst += 2) |
| 38 |
{ |
| 39 |
const uint8_t v = ptr[i]; |
| 40 |
dst[0] = table[v >> 4]; |
| 41 |
dst[1] = table[v & 0x0f]; |
| 42 |
} |
| 43 |
|
| 44 |
return dst; |
| 45 |
} |
| 46 |
|
| 47 |
void generate_uuid(uuid_t *uuid) |
| 48 |
{ |
| 49 |
uint16_t *u16 = uuid->u16; |
| 50 |
for (size_t i=0; i < 8; ++i) |
| 51 |
u16[i] = uint16_t(rand()); |
| 52 |
|
| 53 |
uint8_t *u8 = uuid->u8; |
| 54 |
u8[6] = (u8[6] & 0x0f) | 0x40; // Set version to 0100 |
| 55 |
u8[8] = (u8[8] & 0x3f) | 0x80; // Set bits 6-7 to 10 |
| 56 |
} |
| 57 |
|
| 58 |
char *format_uuid_hex(char *dst, const uuid_t *uuid, bool upper) |
| 59 |
{ |
| 60 |
const char *table = (upper) ? hex_upper : hex_lower; |
| 61 |
dst = fmt_bytes(dst, uuid->u8, table, 16); |
| 62 |
*dst = '\0'; |
| 63 |
|
| 64 |
return dst; |
| 65 |
} |
| 66 |
|
| 67 |
char *format_uuid_dashed(char *dst, const uuid_t *uuid, bool upper) |
| 68 |
{ |
| 69 |
const char *table = (upper) ? hex_upper : hex_lower; |
| 70 |
const uint8_t *u = uuid->u8; |
| 71 |
|
| 72 |
dst = fmt_bytes(dst, u, table, 4); |
| 73 |
*dst++ = '-'; |
| 74 |
u += 4; |
| 75 |
|
| 76 |
dst = fmt_bytes(dst, u, table, 2); |
| 77 |
*dst++ = '-'; |
| 78 |
u += 2; |
| 79 |
|
| 80 |
dst = fmt_bytes(dst, u, table, 2); |
| 81 |
*dst++ = '-'; |
| 82 |
u += 2; |
| 83 |
|
| 84 |
dst = fmt_bytes(dst, u, table, 2); |
| 85 |
*dst++ = '-'; |
| 86 |
u += 2; |
| 87 |
|
| 88 |
dst = fmt_bytes(dst, u, table, 6); |
| 89 |
*dst = '\0'; |
| 90 |
|
| 91 |
return dst; |
| 92 |
} |
| 93 |
|
| 94 |
static void fmt_triple(char *dst, const uint8_t *ptr) |
| 95 |
{ |
| 96 |
uint32_t value = |
| 97 |
uint32_t(ptr[0]) | |
| 98 |
(uint32_t(ptr[1]) << 8) | |
| 99 |
(uint32_t(ptr[2]) << 16); |
| 100 |
for (size_t i=0; i<4; ++i) |
| 101 |
{ |
| 102 |
dst[i] = base64[value & 0x3f]; |
| 103 |
value >>= 6; |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
char *format_uuid_base64(char *dst, const uuid_t *uuid) |
| 108 |
{ |
| 109 |
const uint8_t *ptr = uuid->u8; |
| 110 |
for (size_t i=0; i<5; ++i) |
| 111 |
{ |
| 112 |
fmt_triple(dst, ptr); |
| 113 |
ptr += 3; |
| 114 |
dst += 4; |
| 115 |
} |
| 116 |
|
| 117 |
uint32_t value = uint32_t(ptr[0]); |
| 118 |
for (size_t i=0; i<2; ++i) |
| 119 |
{ |
| 120 |
dst[i] = base64[value & 0x3f]; |
| 121 |
value >>= 6; |
| 122 |
} |
| 123 |
|
| 124 |
dst += 2; |
| 125 |
*dst = '\0'; |
| 126 |
return dst; |
| 127 |
} |
| 128 |
|
| 129 |
} /* namespace lsp */ |
| 130 |
|
| 131 |
|