int main(void) { struct String *str; str = string_to_struct("Holberton"); print_string_struct(str); str = string_to_struct("C is fun"); print_string_struct(str); return (0); }
/* * Name: uuid_compare * * Description: Compares 2 uuid strings * * Returns: -1 if u1 < u2, 1 if u1 > u2 and 0 if both are equal */ int uuid_compare(uuid_t uu1, uuid_t uu2) { struct uuid uuid1, uuid2; string_to_struct(&uuid1, uu1); string_to_struct(&uuid2, uu2); UUCMP(uuid1.time_low, uuid2.time_low); UUCMP(uuid1.time_mid, uuid2.time_mid); UUCMP(uuid1.time_hi_and_version, uuid2.time_hi_and_version); UUCMP(uuid1.clock_seq_hi_and_reserved, uuid2.clock_seq_hi_and_reserved); UUCMP(uuid1.clock_seq_low, uuid2.clock_seq_low); return (memcmp(uuid1.node_addr, uuid2.node_addr, 6)); }
/* * uuid_time extracts the time at which the supplied UUID uu * was created. This function can only extract the creation * time for UUIDs created with the uuid_generate_time function. * The time at which the UUID was created, in seconds and * microseconds since the epoch is stored in the location * pointed to by ret_tv. */ time_t uuid_time(uuid_t uu, struct timeval *ret_tv) { struct uuid uuid; uint_t high; struct timeval tv; u_longlong_t clock_reg; uint_t tmp; uint8_t clk; string_to_struct(&uuid, uu); tmp = (uuid.time_hi_and_version & 0xF000) >> 12; clk = uuid.clock_seq_hi_and_reserved; /* check if uu is NULL, Version = 1 of DCE and Variant = 0b10x */ if ((uu == NULL) || ((tmp & 0x01) != 0x01) || ((clk & 0x80) != 0x80)) { return (-1); } high = uuid.time_mid | ((uuid.time_hi_and_version & 0xFFF) << 16); clock_reg = uuid.time_low | ((u_longlong_t)high << 32); clock_reg -= (((u_longlong_t)0x01B21DD2) << 32) + 0x13814000; tv.tv_sec = clock_reg / 10000000; tv.tv_usec = (clock_reg % 10000000) / 10; if (ret_tv) { *ret_tv = tv; } return (tv.tv_sec); }
/* * This function converts the supplied UUID uu from the internal * binary format into a 36-byte string (plus trailing null char) * and stores this value in the character string pointed to by out. */ static void uuid_unparse_common(uuid_t uu, char *out, boolean_t upper) { struct uuid uuid; uint16_t clock_seq; char etheraddr[13]; int index = 0, i; /* basic sanity checking */ if (uu == NULL) { return; } string_to_struct(&uuid, uu); clock_seq = uuid.clock_seq_hi_and_reserved; clock_seq = (clock_seq << 8) | uuid.clock_seq_low; for (i = 0; i < 6; i++) { (void) sprintf(ðeraddr[index++], upper ? "%.2X" : "%.2x", uuid.node_addr[i]); index++; } etheraddr[index] = '\0'; (void) snprintf(out, 25, upper ? "%08X-%04X-%04X-%04X-" : "%08x-%04x-%04x-%04x-", uuid.time_low, uuid.time_mid, uuid.time_hi_and_version, clock_seq); (void) strlcat(out, etheraddr, UUID_PRINTABLE_STRING_LENGTH); }
/* * Generates UUID based on DCE Version 4 */ void uuid_generate_random(uuid_t uu) { struct uuid uuid; if (uu == NULL) return; (void) memset(uu, 0, sizeof (uuid_t)); (void) memset(&uuid, 0, sizeof (struct uuid)); arc4random_buf(uu, sizeof (uuid_t)); string_to_struct(&uuid, uu); /* * This is version 4, so say so in the UUID version field (4 bits) */ uuid.time_hi_and_version |= (1 << 14); /* * we don't want the bit 1 to be set also which is for version 1 */ uuid.time_hi_and_version &= VER1_MASK; /* * The variant for this format is the 2 high bits set to 10, * so here it is */ uuid.clock_seq_hi_and_reserved |= 0x80; /* * Set MSB of Ethernet address to 1 to indicate that it was generated * randomly */ uuid.node_addr[0] |= 0x80; struct_to_string(uu, &uuid); }
/* * This function converts the supplied UUID uu from the internal * binary format into a 36-byte string (plus trailing null char) * and stores this value in the character string pointed to by out. */ void uuid_unparse(uuid_t uu, char *out) { struct uuid uuid; uint16_t clock_seq; char etheraddr[13]; int index = 0, i; /* basic sanity checking */ if (uu == NULL) { return; } /* XXX user should have allocated enough memory */ /* * if (strlen(out) < UUID_PRINTABLE_STRING_LENGTH) { * return; * } */ string_to_struct(&uuid, uu); clock_seq = uuid.clock_seq_hi_and_reserved; clock_seq = (clock_seq << 8) | uuid.clock_seq_low; for (i = 0; i < 6; i++) { (void) sprintf(ðeraddr[index++], "%.2x", uuid.node_addr[i]); index++; } etheraddr[index] = '\0'; (void) snprintf(out, 25, "%08x-%04x-%04x-%04x-", uuid.time_low, uuid.time_mid, uuid.time_hi_and_version, clock_seq); (void) strlcat(out, etheraddr, UUID_PRINTABLE_STRING_LENGTH); }