source: roaraudio/libroar/uuid.c @ 5889:d866fb1213d6

Last change on this file since 5889:d866fb1213d6 was 5823:f9f70dbaa376, checked in by phi, 11 years ago

updated copyright

File size: 5.2 KB
Line 
1//uuid.c:
2
3/*
4 *      Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2012-2013
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
38int 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
50int 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
69int 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
87const roar_uuid_t * roar_uuid_get_ns_real(const char * ns) {
88 static const roar_uuid_t
89  nulluuid = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
90  dns      = {0x6b, 0xa7, 0xb8, 0x10, 0x9d, 0xad, 0x11, 0xd1, 0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30, 0xc8},
91  url      = {0x6b, 0xa7, 0xb8, 0x11, 0x9d, 0xad, 0x11, 0xd1, 0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30, 0xc8},
92  oid      = {0x6b, 0xa7, 0xb8, 0x12, 0x9d, 0xad, 0x11, 0xd1, 0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30, 0xc8},
93  x500     = {0x6b, 0xa7, 0xb8, 0x14, 0x9d, 0xad, 0x11, 0xd1, 0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30, 0xc8};
94 static const void * null_ptr = NULL;
95
96 if ( ns == NULL ) {
97  roar_err_set(ROAR_ERROR_FAULT);
98  return (const roar_uuid_t *)&null_ptr;
99 }
100
101 if ( !strcasecmp(ns, "null") ) {
102  return &nulluuid;
103 } else if ( !strcasecmp(ns, "dns") ) {
104  return &dns;
105 } else if ( !strcasecmp(ns, "url") ) {
106  return &url;
107 } else if ( !strcasecmp(ns, "iso oid") || !strcasecmp(ns, "oid") ) {
108  return &oid;
109 } else if ( !strcasecmp(ns, "X.500 DN") || !strcasecmp(ns, "X500 DN") || !strcasecmp(ns, "x500") ) {
110  return &x500;
111 }
112
113 roar_err_set(ROAR_ERROR_NOENT);
114 return (const roar_uuid_t *)&null_ptr;
115}
116
117int roar_uuid_gen(roar_uuid_t uuid, enum roar_uuid_type type, const roar_uuid_t ns, const void * argp, ssize_t arglen) {
118 unsigned char digest[20];
119
120 if ( uuid == NULL ) {
121  roar_err_set(ROAR_ERROR_FAULT);
122  return -1;
123 }
124
125 switch (type) {
126  case ROAR_UUID_TYPE_NULL:
127    memset(uuid, 0, sizeof(roar_uuid_t));
128    return 0;
129   break;
130  case ROAR_UUID_TYPE_RANDOM:
131    roar_random_gen_nonce(uuid, sizeof(roar_uuid_t));
132    uuid[6] = (uuid[8] & 0x0F) | 0x40; // Version
133    uuid[8] = (uuid[8] & 0x3F) | 0x80; // Variant
134    return 0;
135   break;
136  case ROAR_UUID_TYPE_MD5:
137  case ROAR_UUID_TYPE_SHA1:
138    if ( ns == NULL || argp == NULL ) {
139     roar_err_set(ROAR_ERROR_FAULT);
140     return -1;
141    }
142
143    if ( arglen < 1 ) {
144     roar_err_set(ROAR_ERROR_INVAL);
145     return -1;
146    }
147
148    if ( roar_hash_salted_buffer(digest, ns, sizeof(roar_uuid_t),
149                                 type == ROAR_UUID_TYPE_MD5 ? ROAR_HT_MD5 : ROAR_HT_SHA1,
150                                 argp, arglen) == -1 )
151     return -1;
152
153    memcpy(uuid, digest, sizeof(roar_uuid_t));
154    uuid[6] = (uuid[8] & 0x0F) | (type << 4); // Version
155    uuid[8] = (uuid[8] & 0x3F) | 0x80; // Variant
156
157    return 0;
158   break;
159  case ROAR_UUID_TYPE_DCE_SECURITY:
160  case ROAR_UUID_TYPE_TIME:
161    roar_err_set(ROAR_ERROR_NOTSUP);
162    return -1;
163   break;
164 }
165
166 roar_err_set(ROAR_ERROR_BADRQC);
167 return -1;
168}
169
170//ll
Note: See TracBrowser for help on using the repository browser.