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(roar_uuid_t a, roar_uuid_t b) { |
---|
39 | if ( a == NULL || b == NULL ) { |
---|
40 | roar_err_set(ROAR_ERROR_FAULT); |
---|
41 | return -1; |
---|
42 | } |
---|
43 | |
---|
44 | if ( memcpy(a, b, sizeof(roar_uuid_t)) == 0 ) |
---|
45 | return 1; |
---|
46 | |
---|
47 | return 0; |
---|
48 | } |
---|
49 | |
---|
50 | int roar_uuid2str(char * str, 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 | int roar_uuid_gen(roar_uuid_t uuid, enum roar_uuid_type type, roar_uuid_t ns, void * argp, ssize_t arglen) { |
---|
87 | if ( uuid == NULL ) { |
---|
88 | roar_err_set(ROAR_ERROR_FAULT); |
---|
89 | return -1; |
---|
90 | } |
---|
91 | |
---|
92 | switch (type) { |
---|
93 | case ROAR_UUID_TYPE_NULL: |
---|
94 | memset(uuid, 0, sizeof(roar_uuid_t)); |
---|
95 | return 0; |
---|
96 | break; |
---|
97 | case ROAR_UUID_TYPE_RANDOM: |
---|
98 | roar_random_gen_nonce(uuid, sizeof(roar_uuid_t)); |
---|
99 | uuid[6] = (uuid[8] & 0x0F) | 0x40; // Version |
---|
100 | uuid[8] = (uuid[8] & 0x3F) | 0x80; // Variant |
---|
101 | return 0; |
---|
102 | break; |
---|
103 | case ROAR_UUID_TYPE_DCE_SECURITY: |
---|
104 | case ROAR_UUID_TYPE_MD5: |
---|
105 | case ROAR_UUID_TYPE_SHA1: |
---|
106 | roar_err_set(ROAR_ERROR_NOTSUP); |
---|
107 | return -1; |
---|
108 | break; |
---|
109 | } |
---|
110 | |
---|
111 | roar_err_set(ROAR_ERROR_BADRQC); |
---|
112 | return -1; |
---|
113 | } |
---|
114 | |
---|
115 | //ll |
---|