summaryrefslogtreecommitdiff
path: root/wsrep/wsrep_uuid.c
blob: d525808d0da06e43c7d1b4302db8d680bb3dbc74 (plain)
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
/* Copyright (C) 2009 Codership Oy <info@codersihp.com>

   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; version 2 of the License.

   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, write to the Free Software
   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335 USA
 */

/*! @file Helper functions to deal with history UUID string representations */

#include <errno.h>
#include <ctype.h>
#include <stdio.h>

#include "wsrep_api.h"

/*!
 * Read UUID from string
 * @return length of UUID string representation or -EINVAL in case of error
 */
int
wsrep_uuid_scan (const char* str, size_t str_len, wsrep_uuid_t* uuid)
{
    unsigned int uuid_len  = 0;
    unsigned int uuid_offt = 0;

    while (uuid_len + 1 < str_len) {
        /* We are skipping potential '-' after uuid_offt == 4, 6, 8, 10
         * which means
         *     (uuid_offt >> 1) == 2, 3, 4, 5,
         * which in turn means
         *     (uuid_offt >> 1) - 2 <= 3
         * since it is always >= 0, because uuid_offt is unsigned */
        if (((uuid_offt >> 1) - 2) <= 3 && str[uuid_len] == '-') {
            // skip dashes after 4th, 6th, 8th and 10th positions
            uuid_len += 1;
            continue;
        }

        if (isxdigit(str[uuid_len]) && isxdigit(str[uuid_len + 1])) {
            // got hex digit, scan another byte to uuid, increment uuid_offt
            sscanf (str + uuid_len, "%2hhx", uuid->data + uuid_offt);
            uuid_len  += 2;
            uuid_offt += 1;
            if (sizeof (uuid->data) == uuid_offt)
                return uuid_len;
        }
        else {
            break;
        }
    }

    *uuid = WSREP_UUID_UNDEFINED;
    return -EINVAL;
}

/*!
 * Write UUID to string
 * @return length of UUID string representation or -EMSGSIZE if string is too
 *         short
 */
int
wsrep_uuid_print (const wsrep_uuid_t* uuid, char* str, size_t str_len)
{
    if (str_len > 36) {
        const unsigned char* u = uuid->data;
        return snprintf(str, str_len, "%02x%02x%02x%02x-%02x%02x-%02x%02x-"
                        "%02x%02x-%02x%02x%02x%02x%02x%02x",
                        u[ 0], u[ 1], u[ 2], u[ 3], u[ 4], u[ 5], u[ 6], u[ 7],
                        u[ 8], u[ 9], u[10], u[11], u[12], u[13], u[14], u[15]);
    }
    else {
        return -EMSGSIZE;
    }
}