source: roaraudio/libroar/uuid.c @ 5666:f8d48d28e43e

Last change on this file since 5666:f8d48d28e43e was 5666:f8d48d28e43e, checked in by phi, 12 years ago

cleanup

File size: 4.0 KB
Line 
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
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
87int roar_uuid_gen(roar_uuid_t uuid, enum roar_uuid_type type, const roar_uuid_t ns, const void * argp, ssize_t arglen) {
88 unsigned char digest[20];
89
90 if ( uuid == NULL ) {
91  roar_err_set(ROAR_ERROR_FAULT);
92  return -1;
93 }
94
95 switch (type) {
96  case ROAR_UUID_TYPE_NULL:
97    memset(uuid, 0, sizeof(roar_uuid_t));
98    return 0;
99   break;
100  case ROAR_UUID_TYPE_RANDOM:
101    roar_random_gen_nonce(uuid, sizeof(roar_uuid_t));
102    uuid[6] = (uuid[8] & 0x0F) | 0x40; // Version
103    uuid[8] = (uuid[8] & 0x3F) | 0x80; // Variant
104    return 0;
105   break;
106  case ROAR_UUID_TYPE_MD5:
107  case ROAR_UUID_TYPE_SHA1:
108    if ( ns == NULL || argp == NULL ) {
109     roar_err_set(ROAR_ERROR_FAULT);
110     return -1;
111    }
112
113    if ( arglen < 1 ) {
114     roar_err_set(ROAR_ERROR_INVAL);
115     return -1;
116    }
117
118    if ( roar_hash_salted_buffer(digest, ns, sizeof(roar_uuid_t),
119                                 type == ROAR_UUID_TYPE_MD5 ? ROAR_HT_MD5 : ROAR_HT_SHA1,
120                                 argp, arglen) == -1 )
121     return -1;
122
123    memcpy(uuid, digest, sizeof(roar_uuid_t));
124    uuid[6] = (uuid[8] & 0x0F) | (type << 4); // Version
125    uuid[8] = (uuid[8] & 0x3F) | 0x80; // Variant
126
127    return 0;
128   break;
129  case ROAR_UUID_TYPE_DCE_SECURITY:
130  case ROAR_UUID_TYPE_TIME:
131    roar_err_set(ROAR_ERROR_NOTSUP);
132    return -1;
133   break;
134 }
135
136 roar_err_set(ROAR_ERROR_BADRQC);
137 return -1;
138}
139
140//ll
Note: See TracBrowser for help on using the repository browser.