File: uniqueid.c

package info (click to toggle)
389-ds-base 2.3.1%2Bdfsg1-1%2Bdeb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 37,536 kB
  • sloc: ansic: 306,972; python: 96,937; cpp: 10,257; perl: 2,854; makefile: 2,046; sh: 925; yacc: 806; xml: 379; lex: 366; javascript: 148; java: 50
file content (279 lines) | stat: -rw-r--r-- 9,180 bytes parent folder | download | duplicates (4)
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
/** BEGIN COPYRIGHT BLOCK
 * Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
 * Copyright (C) 2005 Red Hat, Inc.
 * All rights reserved.
 *
 * License: GPL (version 3 or any later version).
 * See LICENSE for details.
 * END COPYRIGHT BLOCK **/

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

/* uniqueid.c implementation of entryid functionality */

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>

#include "slap.h"

#define UIDSTR_SIZE 35    /* size of the string representation of the id */
#define MODULE "uniqueid" /* for logging */

static int isValidFormat(const char *buff);

/* All functions that strat with slapi_ are exposed to the plugins */

/* Function:    slapi_uniqueIDNew
   Description:    creates new Slapi_UniqueID object
   Parameters:    none
   Return:        pointer to the new uId object if successful
                NULL if memory allocation failed
 */

Slapi_UniqueID *
slapi_uniqueIDNew()
{
    Slapi_UniqueID *uId;
    uId = (Slapi_UniqueID *)slapi_ch_calloc(1, sizeof(Slapi_UniqueID));

    return uId;
}

/* Function:    slapi_uniqueIDDestroy
   Description: destroys Slapi_UniqueID objects and sets the pointer to NULL
   Parameters:  uId - id to destroy
   Return:        none
 */

void
slapi_uniqueIDDestroy(Slapi_UniqueID **uId)
{
    if (uId && *uId) {
        slapi_ch_free((void **)uId);
        *uId = NULL;
    }
}

/* Function:    slapi_uniqueIDCompare
   Description: this function lexically compares two entry ids.
                both Ids must have UUID type.
   Parameters:  uId1, uId2 - ids to compare
   Return:      -1 if uId1 <  uId2
                0  if uId2 == uId2
                1  if uId2 >  uId2
                UID_BADDATA if invalid pointer passed to the function
*/
int
slapi_uniqueIDCompare(const Slapi_UniqueID *uId1, const Slapi_UniqueID *uId2)
{
    if (uId1 == NULL || uId2 == NULL) {
        slapi_log_err(SLAPI_LOG_ERR, MODULE, "uniqueIDCompare: "
                                             "NULL argument passed to the function.\n");
        return UID_BADDATA;
    }

    return (uuid_compare(uId1, uId2));
}

/* Function:    slapi_uniqueIDCompareString
   Description: this function compares two uniqueids, represented as strings
   Parameters:  uuid1, uuid2 - ids to compare
   Return:      0  if uuid1 == uuid2
                non-zero  if uuid1 != uuid2 or uuid1 == NULL or uuid2 == NULL
*/
int
slapi_uniqueIDCompareString(const char *uuid1, const char *uuid2)
{
    int return_value = 0; /* assume not equal */
    if (NULL != uuid1) {
        if (NULL != uuid2) {
            if (strcmp(uuid1, uuid2) == 0) {
                return_value = 1;
            }
        }
    }
    return return_value;
}

/*  Function:    slapi_uniqueIDFormat
    Description: this function converts Slapi_UniqueID to its string representation.
                 The id format is HHHHHHHH-HHHHHHHH-HHHHHHHH-HHHHHHHH
                 where H is a hex digit. The data will be outputed in the
                 network byte order.
    Parameters:  uId  - entry id
                 buff - buffer in which id is returned; caller must free this
                        buffer
    Return:      UID_SUCCESS - function was successfull
                 UID_BADDATA - invalid parameter passed to the function
                 UID_MEMORY_ERROR - failed to allocate the buffer
*/
int
slapi_uniqueIDFormat(const Slapi_UniqueID *uId, char **buff)
{
    guid_t uuid_tmp;
    char *ptr;

    if (uId == NULL || buff == NULL) {
        slapi_log_err(SLAPI_LOG_ERR, MODULE, "uniqueIDFormat: "
                                             "NULL argument passed to the function.\n");
        return UID_BADDATA;
    }

    *buff = (char *)slapi_ch_malloc(UIDSTR_SIZE + 1);
    if (*buff == NULL) {
        slapi_log_err(SLAPI_LOG_ERR, MODULE, "uniqueIDFormat: "
                                             "failed to allocate buffer.\n");
        return UID_MEMORY_ERROR;
    }

    uuid_tmp = *uId;
    uuid_tmp.time_low = htonl(uuid_tmp.time_low);
    uuid_tmp.time_mid = htons(uuid_tmp.time_mid);
    uuid_tmp.time_hi_and_version = htons(uuid_tmp.time_hi_and_version);

    ptr = slapi_u8_to_hex(((uint8_t *)&uuid_tmp.time_low)[0], *buff, 0);
    ptr = slapi_u8_to_hex(((uint8_t *)&uuid_tmp.time_low)[1], ptr, 0);
    ptr = slapi_u8_to_hex(((uint8_t *)&uuid_tmp.time_low)[2], ptr, 0);
    ptr = slapi_u8_to_hex(((uint8_t *)&uuid_tmp.time_low)[3], ptr, 0);
    *ptr++ = '-';
    ptr = slapi_u8_to_hex(((uint8_t *)&uuid_tmp.time_mid)[0], ptr, 0);
    ptr = slapi_u8_to_hex(((uint8_t *)&uuid_tmp.time_mid)[1], ptr, 0);
    ptr = slapi_u8_to_hex(((uint8_t *)&uuid_tmp.time_hi_and_version)[0], ptr, 0);
    ptr = slapi_u8_to_hex(((uint8_t *)&uuid_tmp.time_hi_and_version)[1], ptr, 0);
    *ptr++ = '-';
    ptr = slapi_u8_to_hex(uuid_tmp.clock_seq_hi_and_reserved, ptr, 0);
    ptr = slapi_u8_to_hex(uuid_tmp.clock_seq_low, ptr, 0);
    ptr = slapi_u8_to_hex(uuid_tmp.node[0], ptr, 0);
    ptr = slapi_u8_to_hex(uuid_tmp.node[1], ptr, 0);
    *ptr++ = '-';
    ptr = slapi_u8_to_hex(uuid_tmp.node[2], ptr, 0);
    ptr = slapi_u8_to_hex(uuid_tmp.node[3], ptr, 0);
    ptr = slapi_u8_to_hex(uuid_tmp.node[4], ptr, 0);
    ptr = slapi_u8_to_hex(uuid_tmp.node[5], ptr, 0);
    *ptr = 0;

    return UID_SUCCESS;
}

/*  Function:    slapi_uniqueIDScan
    Description: this function converts a string buffer into uniqueID.
    Parameters:  uId  - unique id to be returned
                 buff - buffer with uniqueID in the format returned by
                        uniqueIDFormat function
    Return:      UID_SUCCESS - function was successfull
                 UID_BADDATA - null parameter(s) or bad format
*/
int
slapi_uniqueIDScan(Slapi_UniqueID *uId, const char *buff)
{
    if (uId == NULL || buff == NULL) {
        slapi_log_err(SLAPI_LOG_ERR, MODULE, "uniqueIDScan: "
                                             "NULL argument passed to the function.\n");
        return UID_BADDATA;
    }

    if (!isValidFormat(buff)) {
        slapi_log_err(SLAPI_LOG_ERR, MODULE, "uniqueIDScan: "
                                             "invalid data format.\n");
        return UID_BADDATA;
    }

    ((PRUint8 *)&uId->time_low)[0] = slapi_str_to_u8(&(buff[0]));
    ((PRUint8 *)&uId->time_low)[1] = slapi_str_to_u8(&(buff[2]));
    ((PRUint8 *)&uId->time_low)[2] = slapi_str_to_u8(&(buff[4]));
    ((PRUint8 *)&uId->time_low)[3] = slapi_str_to_u8(&(buff[6]));
    /* next field is at 9 because we skip the - */
    ((PRUint8 *)&uId->time_mid)[0] = slapi_str_to_u8(&(buff[9]));
    ((PRUint8 *)&uId->time_mid)[1] = slapi_str_to_u8(&(buff[11]));
    ((PRUint8 *)&uId->time_hi_and_version)[0] = slapi_str_to_u8(&(buff[13]));
    ((PRUint8 *)&uId->time_hi_and_version)[1] = slapi_str_to_u8(&(buff[15]));
    /* next field is at 18 because we skip the - */
    uId->clock_seq_hi_and_reserved = slapi_str_to_u8(&(buff[18]));
    uId->clock_seq_low = slapi_str_to_u8(&(buff[20]));
    uId->node[0] = slapi_str_to_u8(&(buff[22]));
    uId->node[1] = slapi_str_to_u8(&(buff[24]));
    /* next field is at 27 because we skip the - */
    uId->node[2] = slapi_str_to_u8(&(buff[27]));
    uId->node[3] = slapi_str_to_u8(&(buff[29]));
    uId->node[4] = slapi_str_to_u8(&(buff[31]));
    uId->node[5] = slapi_str_to_u8(&(buff[33]));

    uId->time_low = ntohl(uId->time_low);
    uId->time_mid = ntohs(uId->time_mid);
    uId->time_hi_and_version = ntohs(uId->time_hi_and_version);

    return UID_SUCCESS;
}

/* Function:     slapi_uniqueIDIsUUID
   Description:  tests if given entry id is in UUID format
   Parameters:   uId - id to test
   Return        0 - it is UUID
                 1 - it is not UUID
                 UID_BADDATA - invalid data passed to the function
   Note: LPXXX - This call is not used currently. Keep it ???
 */
int
slapi_uniqueIDIsUUID(const Slapi_UniqueID *uId)
{
    if (uId == NULL) {
        return UID_BADDATA;
    }
    /* Shortening Slapi_UniqueID: This call does nothing */
    return (0);
}

/* Name:        slapi_uniqueIDSize
   Description:    returns size of the string version of uniqueID in bytes
   Parameters:  none
   Return:        size of the string version of uniqueID in bytes
 */
int
slapi_uniqueIDSize()
{
    return (UIDSTR_SIZE);
}

/* Name:        slapi_uniqueIDDup
   Description:    duplicates an UniqueID object
   Parameters:    uId - id to duplicate
   Return:        duplicate of the Id
 */
Slapi_UniqueID *
slapi_uniqueIDDup(Slapi_UniqueID *uId)
{
    Slapi_UniqueID *uIdDup = slapi_uniqueIDNew();
    memcpy(uIdDup, uId, sizeof(Slapi_UniqueID));

    return uIdDup;
}

/* helper functions */

static char *format = "XXXXXXXX-XXXXXXXX-XXXXXXXX-XXXXXXXX";
static size_t format_len = 35;
/* This function verifies that buff contains data in the correct
   format (specified above). */
static int
isValidFormat(const char *buff)
{
    size_t i;

    if (strlen(buff) != strlen(format)) {
        return UID_BADDATA;
    }

    for (i = 0; i < format_len; i++) {
        if (format[i] == '-' && buff[i] != '-') {
            return 0;
        } else if (format[i] == 'X' && !isxdigit(buff[i])) {
            return 0;
        }
    }

    return 1;
}