Fix memory leak when maintaining resources. When extra resources are
[apr-util.git] / crypto / uuid.c
blob74b61db86aded94b0a452d5ec1a432b92e3428d0
1 /* Copyright 2000-2004 The Apache Software Foundation
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
16 #include <stdio.h> /* for sprintf */
18 #include "apr.h"
19 #include "apr_uuid.h"
20 #include "apr_errno.h"
21 #include "apr_lib.h"
24 APU_DECLARE(void) apr_uuid_format(char *buffer, const apr_uuid_t *uuid)
26 const unsigned char *d = uuid->data;
28 sprintf(buffer,
29 "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
30 d[0], d[1], d[2], d[3], d[4], d[5], d[6], d[7],
31 d[8], d[9], d[10], d[11], d[12], d[13], d[14], d[15]);
34 /* convert a pair of hex digits to an integer value [0,255] */
35 #if 'A' == 65
36 static unsigned char parse_hexpair(const char *s)
38 int result;
39 int temp;
41 result = s[0] - '0';
42 if (result > 48)
43 result = (result - 39) << 4;
44 else if (result > 16)
45 result = (result - 7) << 4;
46 else
47 result = result << 4;
49 temp = s[1] - '0';
50 if (temp > 48)
51 result |= temp - 39;
52 else if (temp > 16)
53 result |= temp - 7;
54 else
55 result |= temp;
57 return (unsigned char)result;
59 #else
60 static unsigned char parse_hexpair(const char *s)
62 int result;
64 if (isdigit(*s)) {
65 result = (*s - '0') << 4;
67 else {
68 if (isupper(*s)) {
69 result = (*s - 'A' + 10) << 4;
71 else {
72 result = (*s - 'a' + 10) << 4;
76 ++s;
77 if (isdigit(*s)) {
78 result |= (*s - '0');
80 else {
81 if (isupper(*s)) {
82 result |= (*s - 'A' + 10);
84 else {
85 result |= (*s - 'a' + 10);
89 return (unsigned char)result;
91 #endif
93 APU_DECLARE(apr_status_t) apr_uuid_parse(apr_uuid_t *uuid,
94 const char *uuid_str)
96 int i;
97 unsigned char *d = uuid->data;
99 for (i = 0; i < 36; ++i) {
100 char c = uuid_str[i];
101 if (!apr_isxdigit(c) &&
102 !(c == '-' && (i == 8 || i == 13 || i == 18 || i == 23)))
103 /* ### need a better value */
104 return APR_BADARG;
106 if (uuid_str[36] != '\0') {
107 /* ### need a better value */
108 return APR_BADARG;
111 d[0] = parse_hexpair(&uuid_str[0]);
112 d[1] = parse_hexpair(&uuid_str[2]);
113 d[2] = parse_hexpair(&uuid_str[4]);
114 d[3] = parse_hexpair(&uuid_str[6]);
116 d[4] = parse_hexpair(&uuid_str[9]);
117 d[5] = parse_hexpair(&uuid_str[11]);
119 d[6] = parse_hexpair(&uuid_str[14]);
120 d[7] = parse_hexpair(&uuid_str[16]);
122 d[8] = parse_hexpair(&uuid_str[19]);
123 d[9] = parse_hexpair(&uuid_str[21]);
125 for (i = 6; i--;)
126 d[10 + i] = parse_hexpair(&uuid_str[i*2+24]);
128 return APR_SUCCESS;