11e3472c5f37ca3686dd69b079d4d02a302f5798dTheodore Ts'o/*
21e3472c5f37ca3686dd69b079d4d02a302f5798dTheodore Ts'o * parse.c --- UUID parsing
3efc6f628e15de95bcd13e4f0ee223cb42115d520Theodore Ts'o *
419c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o * Copyright (C) 1996, 1997 Theodore Ts'o.
519c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o *
619c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o * %Begin-Header%
71bbfec624c4bbe767060a13762aa9a656536a4fdTheodore Ts'o * Redistribution and use in source and binary forms, with or without
81bbfec624c4bbe767060a13762aa9a656536a4fdTheodore Ts'o * modification, are permitted provided that the following conditions
91bbfec624c4bbe767060a13762aa9a656536a4fdTheodore Ts'o * are met:
101bbfec624c4bbe767060a13762aa9a656536a4fdTheodore Ts'o * 1. Redistributions of source code must retain the above copyright
111bbfec624c4bbe767060a13762aa9a656536a4fdTheodore Ts'o *    notice, and the entire permission notice in its entirety,
121bbfec624c4bbe767060a13762aa9a656536a4fdTheodore Ts'o *    including the disclaimer of warranties.
131bbfec624c4bbe767060a13762aa9a656536a4fdTheodore Ts'o * 2. Redistributions in binary form must reproduce the above copyright
141bbfec624c4bbe767060a13762aa9a656536a4fdTheodore Ts'o *    notice, this list of conditions and the following disclaimer in the
151bbfec624c4bbe767060a13762aa9a656536a4fdTheodore Ts'o *    documentation and/or other materials provided with the distribution.
161bbfec624c4bbe767060a13762aa9a656536a4fdTheodore Ts'o * 3. The name of the author may not be used to endorse or promote
171bbfec624c4bbe767060a13762aa9a656536a4fdTheodore Ts'o *    products derived from this software without specific prior
181bbfec624c4bbe767060a13762aa9a656536a4fdTheodore Ts'o *    written permission.
19efc6f628e15de95bcd13e4f0ee223cb42115d520Theodore Ts'o *
201bbfec624c4bbe767060a13762aa9a656536a4fdTheodore Ts'o * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
211bbfec624c4bbe767060a13762aa9a656536a4fdTheodore Ts'o * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
221bbfec624c4bbe767060a13762aa9a656536a4fdTheodore Ts'o * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ALL OF
231bbfec624c4bbe767060a13762aa9a656536a4fdTheodore Ts'o * WHICH ARE HEREBY DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE
241bbfec624c4bbe767060a13762aa9a656536a4fdTheodore Ts'o * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
251bbfec624c4bbe767060a13762aa9a656536a4fdTheodore Ts'o * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
261bbfec624c4bbe767060a13762aa9a656536a4fdTheodore Ts'o * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
271bbfec624c4bbe767060a13762aa9a656536a4fdTheodore Ts'o * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
281bbfec624c4bbe767060a13762aa9a656536a4fdTheodore Ts'o * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
291bbfec624c4bbe767060a13762aa9a656536a4fdTheodore Ts'o * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
301bbfec624c4bbe767060a13762aa9a656536a4fdTheodore Ts'o * USE OF THIS SOFTWARE, EVEN IF NOT ADVISED OF THE POSSIBILITY OF SUCH
311bbfec624c4bbe767060a13762aa9a656536a4fdTheodore Ts'o * DAMAGE.
3219c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o * %End-Header%
331e3472c5f37ca3686dd69b079d4d02a302f5798dTheodore Ts'o */
341e3472c5f37ca3686dd69b079d4d02a302f5798dTheodore Ts'o
3519c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o#include <stdlib.h>
361e3472c5f37ca3686dd69b079d4d02a302f5798dTheodore Ts'o#include <stdio.h>
3719c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o#include <ctype.h>
38b1416db3227b4b7192ee0d2d3ff6e00e92e9d3e2Theodore Ts'o#include <string.h>
391e3472c5f37ca3686dd69b079d4d02a302f5798dTheodore Ts'o
401e3472c5f37ca3686dd69b079d4d02a302f5798dTheodore Ts'o#include "uuidP.h"
411e3472c5f37ca3686dd69b079d4d02a302f5798dTheodore Ts'o
42ce2722f86de298ad1a8965f55a453b5723d2b2b9Theodore Ts'oint uuid_parse(const char *in, uuid_t uu)
431e3472c5f37ca3686dd69b079d4d02a302f5798dTheodore Ts'o{
44ce2722f86de298ad1a8965f55a453b5723d2b2b9Theodore Ts'o	struct uuid	uuid;
45ce2722f86de298ad1a8965f55a453b5723d2b2b9Theodore Ts'o	int 		i;
46ce2722f86de298ad1a8965f55a453b5723d2b2b9Theodore Ts'o	const char	*cp;
47ce2722f86de298ad1a8965f55a453b5723d2b2b9Theodore Ts'o	char		buf[3];
481e3472c5f37ca3686dd69b079d4d02a302f5798dTheodore Ts'o
491e3472c5f37ca3686dd69b079d4d02a302f5798dTheodore Ts'o	if (strlen(in) != 36)
501e3472c5f37ca3686dd69b079d4d02a302f5798dTheodore Ts'o		return -1;
511e3472c5f37ca3686dd69b079d4d02a302f5798dTheodore Ts'o	for (i=0, cp = in; i <= 36; i++,cp++) {
521e3472c5f37ca3686dd69b079d4d02a302f5798dTheodore Ts'o		if ((i == 8) || (i == 13) || (i == 18) ||
538d7f458743c2e37f15f58a38a67a85c3a8bb5f3dTheodore Ts'o		    (i == 23)) {
541e3472c5f37ca3686dd69b079d4d02a302f5798dTheodore Ts'o			if (*cp == '-')
551e3472c5f37ca3686dd69b079d4d02a302f5798dTheodore Ts'o				continue;
568d7f458743c2e37f15f58a38a67a85c3a8bb5f3dTheodore Ts'o			else
578d7f458743c2e37f15f58a38a67a85c3a8bb5f3dTheodore Ts'o				return -1;
588d7f458743c2e37f15f58a38a67a85c3a8bb5f3dTheodore Ts'o		}
591e3472c5f37ca3686dd69b079d4d02a302f5798dTheodore Ts'o		if (i== 36)
601e3472c5f37ca3686dd69b079d4d02a302f5798dTheodore Ts'o			if (*cp == 0)
611e3472c5f37ca3686dd69b079d4d02a302f5798dTheodore Ts'o				continue;
621e3472c5f37ca3686dd69b079d4d02a302f5798dTheodore Ts'o		if (!isxdigit(*cp))
631e3472c5f37ca3686dd69b079d4d02a302f5798dTheodore Ts'o			return -1;
641e3472c5f37ca3686dd69b079d4d02a302f5798dTheodore Ts'o	}
651e3472c5f37ca3686dd69b079d4d02a302f5798dTheodore Ts'o	uuid.time_low = strtoul(in, NULL, 16);
661e3472c5f37ca3686dd69b079d4d02a302f5798dTheodore Ts'o	uuid.time_mid = strtoul(in+9, NULL, 16);
671e3472c5f37ca3686dd69b079d4d02a302f5798dTheodore Ts'o	uuid.time_hi_and_version = strtoul(in+14, NULL, 16);
681e3472c5f37ca3686dd69b079d4d02a302f5798dTheodore Ts'o	uuid.clock_seq = strtoul(in+19, NULL, 16);
691e3472c5f37ca3686dd69b079d4d02a302f5798dTheodore Ts'o	cp = in+24;
701e3472c5f37ca3686dd69b079d4d02a302f5798dTheodore Ts'o	buf[2] = 0;
711e3472c5f37ca3686dd69b079d4d02a302f5798dTheodore Ts'o	for (i=0; i < 6; i++) {
721e3472c5f37ca3686dd69b079d4d02a302f5798dTheodore Ts'o		buf[0] = *cp++;
731e3472c5f37ca3686dd69b079d4d02a302f5798dTheodore Ts'o		buf[1] = *cp++;
741e3472c5f37ca3686dd69b079d4d02a302f5798dTheodore Ts'o		uuid.node[i] = strtoul(buf, NULL, 16);
751e3472c5f37ca3686dd69b079d4d02a302f5798dTheodore Ts'o	}
76efc6f628e15de95bcd13e4f0ee223cb42115d520Theodore Ts'o
771e3472c5f37ca3686dd69b079d4d02a302f5798dTheodore Ts'o	uuid_pack(&uuid, uu);
781e3472c5f37ca3686dd69b079d4d02a302f5798dTheodore Ts'o	return 0;
791e3472c5f37ca3686dd69b079d4d02a302f5798dTheodore Ts'o}
80