Print this page
4118 libuuid should provide uuid_unparse_{upper,lower} functions
Reviewed by: Serghei Samsi <sscdvp@gmail.com>
Reviewed by: Josef 'Jeff' Sipek <jeffpc@josefsipek.net>
Reviewed by: Robert Mustacchi <rm@joyent.com>
Reviewed by: Garrett D'Amore <garrett@damore.org>


   6  * You may not use this file except in compliance with the License.
   7  *
   8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
   9  * or http://www.opensolaris.org/os/licensing.
  10  * See the License for the specific language governing permissions
  11  * and limitations under the License.
  12  *
  13  * When distributing Covered Code, include this CDDL HEADER in each
  14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
  15  * If applicable, add the following below this CDDL HEADER, with the
  16  * fields enclosed by brackets "[]" replaced with your own identifying
  17  * information: Portions Copyright [yyyy] [name of copyright owner]
  18  *
  19  * CDDL HEADER END
  20  */
  21 /*
  22  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
  23  * Use is subject to license terms.
  24  * Copyright 2012 Milan Jurik. All rights reserved.
  25  * Copyright 2013 Joyent, Inc. All rights reserved.

  26  */
  27 
  28 /*
  29  * The copyright in this file is taken from the original Leach & Salz
  30  * UUID specification, from which this implementation is derived.
  31  */
  32 
  33 /*
  34  * Copyright (c) 1990- 1993, 1996 Open Software Foundation, Inc.
  35  * Copyright (c) 1989 by Hewlett-Packard Company, Palo Alto, Ca. &
  36  * Digital Equipment Corporation, Maynard, Mass.  Copyright (c) 1998
  37  * Microsoft.  To anyone who acknowledges that this file is provided
  38  * "AS IS" without any express or implied warranty: permission to use,
  39  * copy, modify, and distribute this file for any purpose is hereby
  40  * granted without fee, provided that the above copyright notices and
  41  * this notice appears in all source code copies, and that none of the
  42  * names of Open Software Foundation, Inc., Hewlett-Packard Company,
  43  * or Digital Equipment Corporation be used in advertising or
  44  * publicity pertaining to distribution of the software without
  45  * specific, written prior permission.  Neither Open Software


 561 void
 562 uuid_copy(uuid_t dst, uuid_t src)
 563 {
 564         (void) memcpy(dst, src, UUID_LEN);
 565 }
 566 
 567 /*
 568  * Sets the value of the supplied uuid variable uu, to the NULL value.
 569  */
 570 void
 571 uuid_clear(uuid_t uu)
 572 {
 573         (void) memset(uu, 0, UUID_LEN);
 574 }
 575 
 576 /*
 577  * This function converts the supplied UUID uu from the internal
 578  * binary format into a 36-byte string (plus trailing null char)
 579  * and stores this value in the character string pointed to by out.
 580  */
 581 void
 582 uuid_unparse(uuid_t uu, char *out)
 583 {
 584         struct uuid     uuid;
 585         uint16_t        clock_seq;
 586         char            etheraddr[13];
 587         int             index = 0, i;
 588 
 589         /* basic sanity checking */
 590         if (uu == NULL) {
 591                 return;
 592         }
 593 
 594         /* XXX user should have allocated enough memory */
 595         /*
 596          * if (strlen(out) < UUID_PRINTABLE_STRING_LENGTH) {
 597          * return;
 598          * }
 599          */
 600         string_to_struct(&uuid, uu);
 601         clock_seq = uuid.clock_seq_hi_and_reserved;
 602         clock_seq = (clock_seq  << 8) | uuid.clock_seq_low;
 603         for (i = 0; i < 6; i++) {
 604                 (void) sprintf(&etheraddr[index++], "%.2x", uuid.node_addr[i]);

 605                 index++;
 606         }
 607         etheraddr[index] = '\0';
 608 
 609         (void) snprintf(out, 25, "%08x-%04x-%04x-%04x-",

 610             uuid.time_low, uuid.time_mid, uuid.time_hi_and_version, clock_seq);
 611         (void) strlcat(out, etheraddr, UUID_PRINTABLE_STRING_LENGTH);
 612 }
 613 






















 614 /*
 615  * The uuid_is_null function compares the value of the supplied
 616  * UUID variable uu to the NULL value. If the value is equal
 617  * to the NULL UUID, 1 is returned, otherwise 0 is returned.
 618  */
 619 int
 620 uuid_is_null(uuid_t uu)
 621 {
 622         int             i;
 623         uuid_t          null_uu;
 624 
 625         (void) memset(null_uu, 0, sizeof (uuid_t));
 626         i = memcmp(uu, null_uu, sizeof (uuid_t));
 627         if (i == 0) {
 628                 /* uu is NULL uuid */
 629                 return (1);
 630         } else {
 631                 return (0);
 632         }
 633 }




   6  * You may not use this file except in compliance with the License.
   7  *
   8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
   9  * or http://www.opensolaris.org/os/licensing.
  10  * See the License for the specific language governing permissions
  11  * and limitations under the License.
  12  *
  13  * When distributing Covered Code, include this CDDL HEADER in each
  14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
  15  * If applicable, add the following below this CDDL HEADER, with the
  16  * fields enclosed by brackets "[]" replaced with your own identifying
  17  * information: Portions Copyright [yyyy] [name of copyright owner]
  18  *
  19  * CDDL HEADER END
  20  */
  21 /*
  22  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
  23  * Use is subject to license terms.
  24  * Copyright 2012 Milan Jurik. All rights reserved.
  25  * Copyright 2013 Joyent, Inc. All rights reserved.
  26  * Copyright 2014 Andrew Stormont.
  27  */
  28 
  29 /*
  30  * The copyright in this file is taken from the original Leach & Salz
  31  * UUID specification, from which this implementation is derived.
  32  */
  33 
  34 /*
  35  * Copyright (c) 1990- 1993, 1996 Open Software Foundation, Inc.
  36  * Copyright (c) 1989 by Hewlett-Packard Company, Palo Alto, Ca. &
  37  * Digital Equipment Corporation, Maynard, Mass.  Copyright (c) 1998
  38  * Microsoft.  To anyone who acknowledges that this file is provided
  39  * "AS IS" without any express or implied warranty: permission to use,
  40  * copy, modify, and distribute this file for any purpose is hereby
  41  * granted without fee, provided that the above copyright notices and
  42  * this notice appears in all source code copies, and that none of the
  43  * names of Open Software Foundation, Inc., Hewlett-Packard Company,
  44  * or Digital Equipment Corporation be used in advertising or
  45  * publicity pertaining to distribution of the software without
  46  * specific, written prior permission.  Neither Open Software


 562 void
 563 uuid_copy(uuid_t dst, uuid_t src)
 564 {
 565         (void) memcpy(dst, src, UUID_LEN);
 566 }
 567 
 568 /*
 569  * Sets the value of the supplied uuid variable uu, to the NULL value.
 570  */
 571 void
 572 uuid_clear(uuid_t uu)
 573 {
 574         (void) memset(uu, 0, UUID_LEN);
 575 }
 576 
 577 /*
 578  * This function converts the supplied UUID uu from the internal
 579  * binary format into a 36-byte string (plus trailing null char)
 580  * and stores this value in the character string pointed to by out.
 581  */
 582 static void
 583 uuid_unparse_common(uuid_t uu, char *out, boolean_t upper)
 584 {
 585         struct uuid     uuid;
 586         uint16_t        clock_seq;
 587         char            etheraddr[13];
 588         int             index = 0, i;
 589 
 590         /* basic sanity checking */
 591         if (uu == NULL) {
 592                 return;
 593         }
 594 






 595         string_to_struct(&uuid, uu);
 596         clock_seq = uuid.clock_seq_hi_and_reserved;
 597         clock_seq = (clock_seq  << 8) | uuid.clock_seq_low;
 598         for (i = 0; i < 6; i++) {
 599                 (void) sprintf(&etheraddr[index++], upper ? "%.2X" : "%.2x",
 600                     uuid.node_addr[i]);
 601                 index++;
 602         }
 603         etheraddr[index] = '\0';
 604 
 605         (void) snprintf(out, 25,
 606             upper ? "%08X-%04X-%04X-%04X-" : "%08x-%04x-%04x-%04x-",
 607             uuid.time_low, uuid.time_mid, uuid.time_hi_and_version, clock_seq);
 608         (void) strlcat(out, etheraddr, UUID_PRINTABLE_STRING_LENGTH);
 609 }
 610 
 611 void
 612 uuid_unparse_upper(uuid_t uu, char *out)
 613 {
 614         uuid_unparse_common(uu, out, B_TRUE);
 615 }
 616 
 617 void
 618 uuid_unparse_lower(uuid_t uu, char *out)
 619 {
 620         uuid_unparse_common(uu, out, B_FALSE);
 621 }
 622 
 623 void
 624 uuid_unparse(uuid_t uu, char *out)
 625 {
 626         /*
 627          * Historically uuid_unparse on Solaris returns lower case,
 628          * for compatibility we preserve this behaviour.
 629          */
 630         uuid_unparse_common(uu, out, B_FALSE);
 631 }
 632 
 633 /*
 634  * The uuid_is_null function compares the value of the supplied
 635  * UUID variable uu to the NULL value. If the value is equal
 636  * to the NULL UUID, 1 is returned, otherwise 0 is returned.
 637  */
 638 int
 639 uuid_is_null(uuid_t uu)
 640 {
 641         int             i;
 642         uuid_t          null_uu;
 643 
 644         (void) memset(null_uu, 0, sizeof (uuid_t));
 645         i = memcmp(uu, null_uu, sizeof (uuid_t));
 646         if (i == 0) {
 647                 /* uu is NULL uuid */
 648                 return (1);
 649         } else {
 650                 return (0);
 651         }
 652 }