1 | //uuid.c: |
---|
2 | |
---|
3 | /* |
---|
4 | * Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2012 |
---|
5 | * |
---|
6 | * This file is part of libroar a part of RoarAudio, |
---|
7 | * a cross-platform sound system for both, home and professional use. |
---|
8 | * See README for details. |
---|
9 | * |
---|
10 | * This file is free software; you can redistribute it and/or modify |
---|
11 | * it under the terms of the GNU General Public License version 3 |
---|
12 | * as published by the Free Software Foundation. |
---|
13 | * |
---|
14 | * libroar is distributed in the hope that it will be useful, |
---|
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
17 | * GNU General Public License for more details. |
---|
18 | * |
---|
19 | * You should have received a copy of the GNU General Public License |
---|
20 | * along with this software; see the file COPYING. If not, write to |
---|
21 | * the Free Software Foundation, 51 Franklin Street, Fifth Floor, |
---|
22 | * Boston, MA 02110-1301, USA. |
---|
23 | * |
---|
24 | * NOTE for everyone want's to change something and send patches: |
---|
25 | * read README and HACKING! There a addition information on |
---|
26 | * the license of this document you need to read before you send |
---|
27 | * any patches. |
---|
28 | * |
---|
29 | * NOTE for uses of non-GPL (LGPL,...) software using libesd, libartsc |
---|
30 | * or libpulse*: |
---|
31 | * The libs libroaresd, libroararts and libroarpulse link this lib |
---|
32 | * and are therefore GPL. Because of this it may be illigal to use |
---|
33 | * them with any software that uses libesd, libartsc or libpulse*. |
---|
34 | */ |
---|
35 | |
---|
36 | #include "libroar.h" |
---|
37 | |
---|
38 | int roar_uuid_eq(const roar_uuid_t a, const roar_uuid_t b) { |
---|
39 | if ( a == NULL || b == NULL ) { |
---|
40 | roar_err_set(ROAR_ERROR_FAULT); |
---|
41 | return -1; |
---|
42 | } |
---|
43 | |
---|
44 | if ( memcmp(a, b, sizeof(roar_uuid_t)) == 0 ) |
---|
45 | return 1; |
---|
46 | |
---|
47 | return 0; |
---|
48 | } |
---|
49 | |
---|
50 | int roar_uuid2str(char * str, const roar_uuid_t uuid, ssize_t len) { |
---|
51 | if ( str == NULL || uuid == NULL ) { |
---|
52 | roar_err_set(ROAR_ERROR_FAULT); |
---|
53 | return -1; |
---|
54 | } |
---|
55 | |
---|
56 | if ( len < 1 ) { |
---|
57 | roar_err_set(ROAR_ERROR_NOSPC); |
---|
58 | return -1; |
---|
59 | } |
---|
60 | |
---|
61 | snprintf(str, len, "%.2x%.2x%.2x%.2x-%.2x%.2x-%.2x%.2x-%.2x%.2x-%.2x%.2x%.2x%.2x%.2x%.2x", |
---|
62 | uuid[0], uuid[1], uuid[2], uuid[3], uuid[4], uuid[5], uuid[6], uuid[7], |
---|
63 | uuid[8], uuid[9], uuid[10], uuid[11], uuid[12], uuid[13], uuid[14], uuid[15] |
---|
64 | ); |
---|
65 | |
---|
66 | return 0; |
---|
67 | } |
---|
68 | |
---|
69 | int roar_str2uuid(roar_uuid_t uuid, const char * str) { |
---|
70 | unsigned int inbuf[16]; |
---|
71 | int i; |
---|
72 | |
---|
73 | if ( sscanf(str, "%2x%2x%2x%2x-%2x%2x-%2x%2x-%2x%2x-%2x%2x%2x%2x%2x%2x", |
---|
74 | &(inbuf[0]), &(inbuf[1]), &(inbuf[2]), &(inbuf[3]), &(inbuf[4]), &(inbuf[5]), |
---|
75 | &(inbuf[6]), &(inbuf[7]), &(inbuf[8]), &(inbuf[9]), &(inbuf[10]), &(inbuf[11]), |
---|
76 | &(inbuf[12]), &(inbuf[13]), &(inbuf[14]), &(inbuf[15])) != 16 ) { |
---|
77 | roar_err_set(ROAR_ERROR_ILLSEQ); |
---|
78 | return -1; |
---|
79 | } |
---|
80 | |
---|
81 | for (i = 0; i < 16; i++) |
---|
82 | uuid[i] = inbuf[i]; |
---|
83 | |
---|
84 | return 0; |
---|
85 | } |
---|
86 | |
---|
87 | const roar_uuid_t * roar_uuid_get_ns_real(const char * ns) { |
---|
88 | static const roar_uuid_t |
---|
89 | dns = {0x6b, 0xa7, 0xb8, 0x10, 0x9d, 0xad, 0x11, 0xd1, 0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30, 0xc8}, |
---|
90 | url = {0x6b, 0xa7, 0xb8, 0x11, 0x9d, 0xad, 0x11, 0xd1, 0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30, 0xc8}, |
---|
91 | oid = {0x6b, 0xa7, 0xb8, 0x12, 0x9d, 0xad, 0x11, 0xd1, 0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30, 0xc8}, |
---|
92 | x500 = {0x6b, 0xa7, 0xb8, 0x14, 0x9d, 0xad, 0x11, 0xd1, 0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30, 0xc8}; |
---|
93 | static const void * null_ptr = NULL; |
---|
94 | |
---|
95 | if ( ns == NULL ) { |
---|
96 | roar_err_set(ROAR_ERROR_FAULT); |
---|
97 | return (const roar_uuid_t *)&null_ptr; |
---|
98 | } |
---|
99 | |
---|
100 | if ( !strcasecmp(ns, "dns") ) { |
---|
101 | return &dns; |
---|
102 | } else if ( !strcasecmp(ns, "url") ) { |
---|
103 | return &url; |
---|
104 | } else if ( !strcasecmp(ns, "iso oid") || !strcasecmp(ns, "oid") ) { |
---|
105 | return &oid; |
---|
106 | } else if ( !strcasecmp(ns, "X.500 DN") || !strcasecmp(ns, "X500 DN") || !strcasecmp(ns, "x500") ) { |
---|
107 | return &x500; |
---|
108 | } |
---|
109 | |
---|
110 | roar_err_set(ROAR_ERROR_NOENT); |
---|
111 | return (const roar_uuid_t *)&null_ptr; |
---|
112 | } |
---|
113 | |
---|
114 | int roar_uuid_gen(roar_uuid_t uuid, enum roar_uuid_type type, const roar_uuid_t ns, const void * argp, ssize_t arglen) { |
---|
115 | unsigned char digest[20]; |
---|
116 | |
---|
117 | if ( uuid == NULL ) { |
---|
118 | roar_err_set(ROAR_ERROR_FAULT); |
---|
119 | return -1; |
---|
120 | } |
---|
121 | |
---|
122 | switch (type) { |
---|
123 | case ROAR_UUID_TYPE_NULL: |
---|
124 | memset(uuid, 0, sizeof(roar_uuid_t)); |
---|
125 | return 0; |
---|
126 | break; |
---|
127 | case ROAR_UUID_TYPE_RANDOM: |
---|
128 | roar_random_gen_nonce(uuid, sizeof(roar_uuid_t)); |
---|
129 | uuid[6] = (uuid[8] & 0x0F) | 0x40; // Version |
---|
130 | uuid[8] = (uuid[8] & 0x3F) | 0x80; // Variant |
---|
131 | return 0; |
---|
132 | break; |
---|
133 | case ROAR_UUID_TYPE_MD5: |
---|
134 | case ROAR_UUID_TYPE_SHA1: |
---|
135 | if ( ns == NULL || argp == NULL ) { |
---|
136 | roar_err_set(ROAR_ERROR_FAULT); |
---|
137 | return -1; |
---|
138 | } |
---|
139 | |
---|
140 | if ( arglen < 1 ) { |
---|
141 | roar_err_set(ROAR_ERROR_INVAL); |
---|
142 | return -1; |
---|
143 | } |
---|
144 | |
---|
145 | if ( roar_hash_salted_buffer(digest, ns, sizeof(roar_uuid_t), |
---|
146 | type == ROAR_UUID_TYPE_MD5 ? ROAR_HT_MD5 : ROAR_HT_SHA1, |
---|
147 | argp, arglen) == -1 ) |
---|
148 | return -1; |
---|
149 | |
---|
150 | memcpy(uuid, digest, sizeof(roar_uuid_t)); |
---|
151 | uuid[6] = (uuid[8] & 0x0F) | (type << 4); // Version |
---|
152 | uuid[8] = (uuid[8] & 0x3F) | 0x80; // Variant |
---|
153 | |
---|
154 | return 0; |
---|
155 | break; |
---|
156 | case ROAR_UUID_TYPE_DCE_SECURITY: |
---|
157 | case ROAR_UUID_TYPE_TIME: |
---|
158 | roar_err_set(ROAR_ERROR_NOTSUP); |
---|
159 | return -1; |
---|
160 | break; |
---|
161 | } |
---|
162 | |
---|
163 | roar_err_set(ROAR_ERROR_BADRQC); |
---|
164 | return -1; |
---|
165 | } |
---|
166 | |
---|
167 | //ll |
---|