/
uuid.h
129 lines (106 loc) · 2.73 KB
/
uuid.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/*
* TV headend - UUID generation routines
*
* Copyright (C) 2014 Adam Sutton
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef __TVH_UUID_H__
#define __TVH_UUID_H__
#include <stdint.h>
#include <string.h>
#define UUID_BIN_SIZE (16)
#define UUID_HEX_SIZE (33) // inc NUL
extern uint8_t ___uuid_empty[UUID_BIN_SIZE];
/* Structure to hold UUID */
typedef struct uuid {
uint8_t bin[UUID_BIN_SIZE];
} tvh_uuid_t;
/* Structure for the uuid set */
typedef struct uuid_set {
tvh_uuid_t *us_array;
uint32_t us_count;
uint32_t us_size;
uint32_t us_alloc_chunk;
} tvh_uuid_set_t;
/* Initialise subsystem */
void uuid_init ( void );
/* Random bytes */
void uuid_random ( uint8_t *buf, size_t bufsize );
/* Set uuid */
int uuid_set ( tvh_uuid_t *u, const char *str );
/* Get hexa uuid, str must have length at least UUID_HEX_SIZE */
char *uuid_get_hex ( const tvh_uuid_t *u, char *dst );
/**
* Copy
*/
static inline void uuid_duplicate ( tvh_uuid_t *dst, const tvh_uuid_t *src )
{
*dst = *src;
}
/**
* Compare
*/
static inline int uuid_cmp ( const tvh_uuid_t *a, const tvh_uuid_t *b )
{
return memcmp(a->bin, b->bin, UUID_BIN_SIZE);
}
/**
* Empty
*/
static inline int uuid_empty ( const tvh_uuid_t *a )
{
return memcmp(a->bin, ___uuid_empty, UUID_BIN_SIZE) == 0;
}
/**
* Validate the hexadecimal representation of uuid
*/
int uuid_hexvalid ( const char *uuid );
/**
*
*/
void uuid_set_init( tvh_uuid_set_t *us, uint32_t alloc_chunk );
/**
*
*/
tvh_uuid_t *uuid_set_add( tvh_uuid_set_t *us, const tvh_uuid_t *u );
/**
*
*/
void uuid_set_free( tvh_uuid_set_t *us );
/**
*
*/
void uuid_set_destroy( tvh_uuid_set_t *us );
/**
*
*/
static inline int uuid_set_empty( tvh_uuid_set_t *us )
{ return us->us_count == 0; }
/**
*
*/
#define UUID_SET_FOREACH(u, us, u32) \
if ((us)->us_count > 0) \
for ((u32) = 0, (u) = (us)->us_array; \
(u32) < (us)->us_count; (u32)++, (u)++)
/**
* Hex string to binary
*/
int hex2bin ( uint8_t *buf, size_t buflen, const char *hex );
/**
* Binary to hex string
*/
char *bin2hex ( char *dst, size_t dstlen, const uint8_t *src, size_t srclen );
#endif /* __TVH_UUID_H__ */