Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Path: blob/next/external/cache/sources/hcitools/lib/uuid.c
Views: 3959
/*1*2* BlueZ - Bluetooth protocol stack for Linux3*4* Copyright (C) 2011 Nokia Corporation5* Copyright (C) 2011 Marcel Holtmann <[email protected]>6*7*8* This program is free software; you can redistribute it and/or modify9* it under the terms of the GNU General Public License as published by10* the Free Software Foundation; either version 2 of the License, or11* (at your option) any later version.12*13* This program is distributed in the hope that it will be useful,14* but WITHOUT ANY WARRANTY; without even the implied warranty of15* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the16* GNU General Public License for more details.17*18* You should have received a copy of the GNU General Public License19* along with this program; if not, write to the Free Software20* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA21*22*/2324#ifdef HAVE_CONFIG_H25#include <config.h>26#endif2728#include <string.h>29#include <stdlib.h>30#include <errno.h>3132#include "uuid.h"3334#if __BYTE_ORDER == __BIG_ENDIAN35static uint128_t bluetooth_base_uuid = {36.data = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00,370x80, 0x00, 0x00, 0x80, 0x5F, 0x9B, 0x34, 0xFB }38};3940#define BASE_UUID16_OFFSET 241#define BASE_UUID32_OFFSET 04243#else44static uint128_t bluetooth_base_uuid = {45.data = { 0xFB, 0x34, 0x9B, 0x5F, 0x80, 0x00, 0x00, 0x80,460x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }47};4849#define BASE_UUID16_OFFSET 1250#define BASE_UUID32_OFFSET BASE_UUID16_OFFSET5152#endif5354static void bt_uuid16_to_uuid128(const bt_uuid_t *src, bt_uuid_t *dst)55{56dst->value.u128 = bluetooth_base_uuid;57dst->type = BT_UUID128;5859memcpy(&dst->value.u128.data[BASE_UUID16_OFFSET],60&src->value.u16, sizeof(src->value.u16));61}6263static void bt_uuid32_to_uuid128(const bt_uuid_t *src, bt_uuid_t *dst)64{65dst->value.u128 = bluetooth_base_uuid;66dst->type = BT_UUID128;6768memcpy(&dst->value.u128.data[BASE_UUID32_OFFSET],69&src->value.u32, sizeof(src->value.u32));70}7172void bt_uuid_to_uuid128(const bt_uuid_t *src, bt_uuid_t *dst)73{74switch (src->type) {75case BT_UUID128:76*dst = *src;77break;78case BT_UUID32:79bt_uuid32_to_uuid128(src, dst);80break;81case BT_UUID16:82bt_uuid16_to_uuid128(src, dst);83break;84default:85break;86}87}8889static int bt_uuid128_cmp(const bt_uuid_t *u1, const bt_uuid_t *u2)90{91return memcmp(&u1->value.u128, &u2->value.u128, sizeof(uint128_t));92}9394int bt_uuid16_create(bt_uuid_t *btuuid, uint16_t value)95{96memset(btuuid, 0, sizeof(bt_uuid_t));97btuuid->type = BT_UUID16;98btuuid->value.u16 = value;99100return 0;101}102103int bt_uuid32_create(bt_uuid_t *btuuid, uint32_t value)104{105memset(btuuid, 0, sizeof(bt_uuid_t));106btuuid->type = BT_UUID32;107btuuid->value.u32 = value;108109return 0;110}111112int bt_uuid128_create(bt_uuid_t *btuuid, uint128_t value)113{114memset(btuuid, 0, sizeof(bt_uuid_t));115btuuid->type = BT_UUID128;116btuuid->value.u128 = value;117118return 0;119}120121int bt_uuid_cmp(const bt_uuid_t *uuid1, const bt_uuid_t *uuid2)122{123bt_uuid_t u1, u2;124125bt_uuid_to_uuid128(uuid1, &u1);126bt_uuid_to_uuid128(uuid2, &u2);127128return bt_uuid128_cmp(&u1, &u2);129}130131/*132* convert the UUID to string, copying a maximum of n characters.133*/134int bt_uuid_to_string(const bt_uuid_t *uuid, char *str, size_t n)135{136if (!uuid) {137snprintf(str, n, "NULL");138return -EINVAL;139}140141switch (uuid->type) {142case BT_UUID16:143snprintf(str, n, "%.4x", uuid->value.u16);144break;145case BT_UUID32:146snprintf(str, n, "%.8x", uuid->value.u32);147break;148case BT_UUID128: {149unsigned int data0;150unsigned short data1;151unsigned short data2;152unsigned short data3;153unsigned int data4;154unsigned short data5;155156uint128_t nvalue;157const uint8_t *data = (uint8_t *) &nvalue;158159hton128(&uuid->value.u128, &nvalue);160161memcpy(&data0, &data[0], 4);162memcpy(&data1, &data[4], 2);163memcpy(&data2, &data[6], 2);164memcpy(&data3, &data[8], 2);165memcpy(&data4, &data[10], 4);166memcpy(&data5, &data[14], 2);167168snprintf(str, n, "%.8x-%.4x-%.4x-%.4x-%.8x%.4x",169ntohl(data0), ntohs(data1),170ntohs(data2), ntohs(data3),171ntohl(data4), ntohs(data5));172}173break;174default:175snprintf(str, n, "Type of UUID (%x) unknown.", uuid->type);176return -EINVAL; /* Enum type of UUID not set */177}178179return 0;180}181182static inline int is_uuid128(const char *string)183{184return (strlen(string) == 36 &&185string[8] == '-' &&186string[13] == '-' &&187string[18] == '-' &&188string[23] == '-');189}190191static inline int is_uuid32(const char *string)192{193return (strlen(string) == 8 || strlen(string) == 10);194}195196static inline int is_uuid16(const char *string)197{198return (strlen(string) == 4 || strlen(string) == 6);199}200201static int bt_string_to_uuid16(bt_uuid_t *uuid, const char *string)202{203uint16_t u16;204char *endptr = NULL;205206u16 = strtol(string, &endptr, 16);207if (endptr && *endptr == '\0') {208bt_uuid16_create(uuid, u16);209return 0;210}211212return -EINVAL;213}214215static int bt_string_to_uuid32(bt_uuid_t *uuid, const char *string)216{217uint32_t u32;218char *endptr = NULL;219220u32 = strtol(string, &endptr, 16);221if (endptr && *endptr == '\0') {222bt_uuid32_create(uuid, u32);223return 0;224}225226return -EINVAL;227}228229static int bt_string_to_uuid128(bt_uuid_t *uuid, const char *string)230{231uint32_t data0, data4;232uint16_t data1, data2, data3, data5;233uint128_t n128, u128;234uint8_t *val = (uint8_t *) &n128;235236if (sscanf(string, "%08x-%04hx-%04hx-%04hx-%08x%04hx",237&data0, &data1, &data2,238&data3, &data4, &data5) != 6)239return -EINVAL;240241data0 = htonl(data0);242data1 = htons(data1);243data2 = htons(data2);244data3 = htons(data3);245data4 = htonl(data4);246data5 = htons(data5);247248memcpy(&val[0], &data0, 4);249memcpy(&val[4], &data1, 2);250memcpy(&val[6], &data2, 2);251memcpy(&val[8], &data3, 2);252memcpy(&val[10], &data4, 4);253memcpy(&val[14], &data5, 2);254255ntoh128(&n128, &u128);256257bt_uuid128_create(uuid, u128);258259return 0;260}261262int bt_string_to_uuid(bt_uuid_t *uuid, const char *string)263{264if (is_uuid128(string))265return bt_string_to_uuid128(uuid, string);266else if (is_uuid32(string))267return bt_string_to_uuid32(uuid, string);268else if (is_uuid16(string))269return bt_string_to_uuid16(uuid, string);270271return -EINVAL;272}273274int bt_uuid_strcmp(const void *a, const void *b)275{276return strcasecmp(a, b);277}278279280