Blame crypto/uuid.c

Packit 383869
/* Licensed to the Apache Software Foundation (ASF) under one or more
Packit 383869
 * contributor license agreements.  See the NOTICE file distributed with
Packit 383869
 * this work for additional information regarding copyright ownership.
Packit 383869
 * The ASF licenses this file to You under the Apache License, Version 2.0
Packit 383869
 * (the "License"); you may not use this file except in compliance with
Packit 383869
 * the License.  You may obtain a copy of the License at
Packit 383869
 *
Packit 383869
 *     http://www.apache.org/licenses/LICENSE-2.0
Packit 383869
 *
Packit 383869
 * Unless required by applicable law or agreed to in writing, software
Packit 383869
 * distributed under the License is distributed on an "AS IS" BASIS,
Packit 383869
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Packit 383869
 * See the License for the specific language governing permissions and
Packit 383869
 * limitations under the License.
Packit 383869
 */
Packit 383869
Packit 383869
#include <stdio.h>      /* for sprintf */
Packit 383869
Packit 383869
#include "apr.h"
Packit 383869
#include "apr_uuid.h"
Packit 383869
#include "apr_errno.h"
Packit 383869
#include "apr_lib.h"
Packit 383869
Packit 383869
Packit 383869
APU_DECLARE(void) apr_uuid_format(char *buffer, const apr_uuid_t *uuid)
Packit 383869
{
Packit 383869
    const unsigned char *d = uuid->data;
Packit 383869
Packit 383869
    sprintf(buffer,
Packit 383869
            "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
Packit 383869
            d[0], d[1], d[2], d[3], d[4], d[5], d[6], d[7],
Packit 383869
            d[8], d[9], d[10], d[11], d[12], d[13], d[14], d[15]);
Packit 383869
}
Packit 383869
Packit 383869
/* convert a pair of hex digits to an integer value [0,255] */
Packit 383869
#if 'A' == 65
Packit 383869
static unsigned char parse_hexpair(const char *s)
Packit 383869
{
Packit 383869
    int result;
Packit 383869
    int temp;
Packit 383869
Packit 383869
    result = s[0] - '0';
Packit 383869
    if (result > 48)
Packit 383869
	result = (result - 39) << 4;
Packit 383869
    else if (result > 16)
Packit 383869
	result = (result - 7) << 4;
Packit 383869
    else
Packit 383869
	result = result << 4;
Packit 383869
Packit 383869
    temp = s[1] - '0';
Packit 383869
    if (temp > 48)
Packit 383869
	result |= temp - 39;
Packit 383869
    else if (temp > 16)
Packit 383869
	result |= temp - 7;
Packit 383869
    else
Packit 383869
	result |= temp;
Packit 383869
Packit 383869
    return (unsigned char)result;
Packit 383869
}
Packit 383869
#else
Packit 383869
static unsigned char parse_hexpair(const char *s)
Packit 383869
{
Packit 383869
    int result;
Packit 383869
Packit 383869
    if (isdigit(*s)) {
Packit 383869
        result = (*s - '0') << 4;
Packit 383869
    }
Packit 383869
    else {
Packit 383869
        if (isupper(*s)) {
Packit 383869
            result = (*s - 'A' + 10) << 4;
Packit 383869
        }
Packit 383869
        else {
Packit 383869
            result = (*s - 'a' + 10) << 4;
Packit 383869
        }
Packit 383869
    }
Packit 383869
Packit 383869
    ++s;
Packit 383869
    if (isdigit(*s)) {
Packit 383869
        result |= (*s - '0');
Packit 383869
    }
Packit 383869
    else {
Packit 383869
        if (isupper(*s)) {
Packit 383869
            result |= (*s - 'A' + 10);
Packit 383869
        }
Packit 383869
        else {
Packit 383869
            result |= (*s - 'a' + 10);
Packit 383869
        }
Packit 383869
    }
Packit 383869
Packit 383869
    return (unsigned char)result;
Packit 383869
}
Packit 383869
#endif
Packit 383869
Packit 383869
APU_DECLARE(apr_status_t) apr_uuid_parse(apr_uuid_t *uuid,
Packit 383869
                                         const char *uuid_str)
Packit 383869
{
Packit 383869
    int i;
Packit 383869
    unsigned char *d = uuid->data;
Packit 383869
Packit 383869
    for (i = 0; i < 36; ++i) {
Packit 383869
	char c = uuid_str[i];
Packit 383869
	if (!apr_isxdigit(c) &&
Packit 383869
	    !(c == '-' && (i == 8 || i == 13 || i == 18 || i == 23)))
Packit 383869
            /* ### need a better value */
Packit 383869
	    return APR_BADARG;
Packit 383869
    }
Packit 383869
    if (uuid_str[36] != '\0') {
Packit 383869
        /* ### need a better value */
Packit 383869
	return APR_BADARG;
Packit 383869
    }
Packit 383869
Packit 383869
    d[0] = parse_hexpair(&uuid_str[0]);
Packit 383869
    d[1] = parse_hexpair(&uuid_str[2]);
Packit 383869
    d[2] = parse_hexpair(&uuid_str[4]);
Packit 383869
    d[3] = parse_hexpair(&uuid_str[6]);
Packit 383869
Packit 383869
    d[4] = parse_hexpair(&uuid_str[9]);
Packit 383869
    d[5] = parse_hexpair(&uuid_str[11]);
Packit 383869
Packit 383869
    d[6] = parse_hexpair(&uuid_str[14]);
Packit 383869
    d[7] = parse_hexpair(&uuid_str[16]);
Packit 383869
Packit 383869
    d[8] = parse_hexpair(&uuid_str[19]);
Packit 383869
    d[9] = parse_hexpair(&uuid_str[21]);
Packit 383869
Packit 383869
    for (i = 6; i--;)
Packit 383869
	d[10 + i] = parse_hexpair(&uuid_str[i*2+24]);
Packit 383869
Packit 383869
    return APR_SUCCESS;
Packit 383869
}