/* export UUID to string representation */ void uuid_dce_to_string(uuid_dce_t *uuid_dce, char **str, int *status) { ossp_uuid_t *uuid = NULL; size_t len; void *vp; /* initialize status */ if (status != NULL) *status = uuid_s_error; /* sanity check argument(s) */ if (str == NULL || uuid_dce == NULL) return; /* import binary representation and export string representation */ if (uuid_create(&uuid) != UUID_RC_OK) goto leave; if (uuid_import(uuid, UUID_FMT_BIN, uuid_dce, UUID_LEN_BIN) != UUID_RC_OK) goto leave; vp = str; len = UUID_LEN_STR; if (uuid_export(uuid, UUID_FMT_STR, &vp, &len) != UUID_RC_OK) goto leave; /* indicate successful operation */ if (status != NULL) *status = uuid_s_ok; /* cleanup and return */ leave: if (uuid != NULL) uuid_destroy(uuid); return; }
static void string_to_uuid(const char *str, uuid_t *uuid) { uuid_rc_t rc; rc = uuid_import(uuid, UUID_FMT_STR, str, UUID_LEN_STR + 1); if (rc != UUID_RC_OK) pguuid_complain(rc); }
ib_status_t ib_uuid_ascii_to_bin( ib_uuid_t *uuid, const char *str ) { uuid_rc_t uuid_rc; size_t uuid_len = UUID_LEN_BIN; size_t str_len; ib_status_t rc = IB_OK; if (uuid == NULL || str == NULL) { return IB_EINVAL; } str_len = strlen(str); if (str_len != UUID_LEN_STR) { return IB_EINVAL; } assert(str_len == UUID_LEN_STR); rc = ib_lock_lock(&g_uuid_lock); if (rc != IB_OK) { return rc; } uuid_rc = uuid_import(g_ossp_uuid, UUID_FMT_STR, str, str_len); if (uuid_rc == UUID_RC_MEM) { rc = IB_EALLOC; goto finish; } else if (uuid_rc != UUID_RC_OK) { rc = IB_EINVAL; goto finish; } uuid_rc = uuid_export(g_ossp_uuid, UUID_FMT_BIN, (void *)&uuid, &uuid_len ); if (uuid_rc == UUID_RC_MEM) { rc = IB_EALLOC; goto finish; } else if (uuid_rc != UUID_RC_OK || uuid_len != UUID_LEN_BIN) { rc = IB_EOTHER; goto finish; } finish: if (ib_lock_unlock(&g_uuid_lock) != IB_OK) { return IB_EOTHER; } return rc; }
/* compare two UUIDs */ int uuid_dce_compare(uuid_dce_t *uuid_dce1, uuid_dce_t *uuid_dce2, int *status) { ossp_uuid_t *uuid1 = NULL; ossp_uuid_t *uuid2 = NULL; int result = 0; /* initialize status */ if (status != NULL) *status = uuid_s_error; /* sanity check argument(s) */ if (uuid_dce1 == NULL || uuid_dce2 == NULL) return 0; /* import both UUID binary representations and compare them */ if (uuid_create(&uuid1) != UUID_RC_OK) goto leave; if (uuid_create(&uuid2) != UUID_RC_OK) goto leave; if (uuid_import(uuid1, UUID_FMT_BIN, uuid_dce1, UUID_LEN_BIN) != UUID_RC_OK) goto leave; if (uuid_import(uuid2, UUID_FMT_BIN, uuid_dce2, UUID_LEN_BIN) != UUID_RC_OK) goto leave; if (uuid_compare(uuid1, uuid2, &result) != UUID_RC_OK) goto leave; /* indicate successful operation */ if (status != NULL) *status = uuid_s_ok; /* cleanup and return */ leave: if (uuid1 != NULL) uuid_destroy(uuid1); if (uuid2 != NULL) uuid_destroy(uuid2); return result; }
ib_status_t ib_uuid_bin_to_ascii( char *str, const ib_uuid_t *uuid ) { IB_FTRACE_INIT(); uuid_rc_t uuid_rc; size_t uuid_len = UUID_LEN_STR+1; ib_status_t rc = IB_OK; if (uuid == NULL || str == NULL) { IB_FTRACE_RET_STATUS(IB_EINVAL); } rc = ib_lock_init(&g_uuid_lock); if (rc != IB_OK) { IB_FTRACE_RET_STATUS(rc); } uuid_rc = uuid_import(g_ossp_uuid, UUID_FMT_BIN, uuid, UUID_LEN_BIN); if (uuid_rc == UUID_RC_MEM) { rc = IB_EALLOC; goto finish; } else if (uuid_rc != UUID_RC_OK) { rc = IB_EINVAL; goto finish; } uuid_rc = uuid_export(g_ossp_uuid, UUID_FMT_STR, (void *)&str, &uuid_len); if (uuid_rc == UUID_RC_MEM) { rc = IB_EALLOC; goto finish; } else if (uuid_rc != UUID_RC_OK || uuid_len != UUID_LEN_STR+1) { rc = IB_EOTHER; goto finish; } finish: if (ib_lock_unlock(&g_uuid_lock) != IB_OK) { IB_FTRACE_RET_STATUS(IB_EOTHER); } IB_FTRACE_RET_STATUS(rc); }
CID CID::FromString(const std::string &cid) { uuid_t *uuid; uuid_create(&uuid); uuid_import(uuid, UUID_FMT_STR, cid.data(), cid.length()); return CID(uuid); }
CID CID::FromData(const uint8_t *data) { uuid_t *uuid; uuid_create(&uuid); uuid_import(uuid, UUID_FMT_BIN, data, CID_LENGTH); return CID(uuid); }
/* main procedure */ int main(int argc, char *argv[]) { char uuid_buf_bin[UUID_LEN_BIN]; char uuid_buf_str[UUID_LEN_STR+1]; char uuid_buf_siv[UUID_LEN_SIV+1]; uuid_t *uuid; uuid_t *uuid_ns; uuid_rc_t rc; FILE *fp; char *p; int ch; int count; int i; int iterate; uuid_fmt_t fmt; int decode; void *vp; size_t n; unsigned int version; /* command line parsing */ count = -1; /* no count yet */ fp = stdout; /* default output file */ iterate = 0; /* not one at a time */ fmt = UUID_FMT_STR; /* default is ASCII output */ decode = 0; /* default is to encode */ version = UUID_MAKE_V1; while ((ch = getopt(argc, argv, "1n:rF:dmo:v:h")) != -1) { switch (ch) { case '1': iterate = 1; break; case 'n': if (count > 0) usage("option 'n' specified multiple times"); count = strtol(optarg, &p, 10); if (*p != '\0' || count < 1) usage("invalid argument to option 'n'"); break; case 'r': fmt = UUID_FMT_BIN; break; case 'F': if (strcasecmp(optarg, "bin") == 0) fmt = UUID_FMT_BIN; else if (strcasecmp(optarg, "str") == 0) fmt = UUID_FMT_STR; else if (strcasecmp(optarg, "siv") == 0) fmt = UUID_FMT_SIV; else error(1, "invalid format \"%s\" (has to be \"bin\", \"str\" or \"siv\")", optarg); break; case 'd': decode = 1; break; case 'o': if (fp != stdout) error(1, "multiple output files are not allowed"); if ((fp = fopen(optarg, "w")) == NULL) error(1, "fopen: %s", strerror(errno)); break; case 'm': version |= UUID_MAKE_MC; break; case 'v': i = strtol(optarg, &p, 10); if (*p != '\0') usage("invalid argument to option 'v'"); switch (i) { case 1: version = UUID_MAKE_V1; break; case 3: version = UUID_MAKE_V3; break; case 4: version = UUID_MAKE_V4; break; case 5: version = UUID_MAKE_V5; break; default: usage("invalid version on option 'v'"); break; } break; case 'h': usage(NULL); break; default: usage("invalid option '%c'", optopt); } } argv += optind; argc -= optind; if (count == -1) count = 1; if (decode) { /* decoding */ if ((rc = uuid_create(&uuid)) != UUID_RC_OK) error(1, "uuid_create: %s", uuid_error(rc)); if (argc != 1) usage("invalid number of arguments"); if (strcmp(argv[0], "-") == 0) { if (fmt == UUID_FMT_BIN) { if (fread(uuid_buf_bin, UUID_LEN_BIN, 1, stdin) != 1) error(1, "fread: failed to read %d (UUID_LEN_BIN) bytes from stdin", UUID_LEN_BIN); if ((rc = uuid_import(uuid, UUID_FMT_BIN, uuid_buf_bin, UUID_LEN_BIN)) != UUID_RC_OK) error(1, "uuid_import: %s", uuid_error(rc)); } else if (fmt == UUID_FMT_STR) { if (fread(uuid_buf_str, UUID_LEN_STR, 1, stdin) != 1) error(1, "fread: failed to read %d (UUID_LEN_STR) bytes from stdin", UUID_LEN_STR); uuid_buf_str[UUID_LEN_STR] = '\0'; if ((rc = uuid_import(uuid, UUID_FMT_STR, uuid_buf_str, UUID_LEN_STR)) != UUID_RC_OK) error(1, "uuid_import: %s", uuid_error(rc)); } else if (fmt == UUID_FMT_SIV) { if (fread(uuid_buf_siv, UUID_LEN_SIV, 1, stdin) != 1) error(1, "fread: failed to read %d (UUID_LEN_SIV) bytes from stdin", UUID_LEN_SIV); uuid_buf_siv[UUID_LEN_SIV] = '\0'; if ((rc = uuid_import(uuid, UUID_FMT_SIV, uuid_buf_siv, UUID_LEN_SIV)) != UUID_RC_OK) error(1, "uuid_import: %s", uuid_error(rc)); } } else { if (fmt == UUID_FMT_BIN) { error(1, "binary input mode only possible if reading from stdin"); } else if (fmt == UUID_FMT_STR) { if ((rc = uuid_import(uuid, UUID_FMT_STR, argv[0], strlen(argv[0]))) != UUID_RC_OK) error(1, "uuid_import: %s", uuid_error(rc)); } else if (fmt == UUID_FMT_SIV) { if ((rc = uuid_import(uuid, UUID_FMT_SIV, argv[0], strlen(argv[0]))) != UUID_RC_OK) error(1, "uuid_import: %s", uuid_error(rc)); } } vp = NULL; if ((rc = uuid_export(uuid, UUID_FMT_TXT, &vp, NULL)) != UUID_RC_OK) error(1, "uuid_export: %s", uuid_error(rc)); fprintf(stdout, "%s", (char *)vp); free(vp); if ((rc = uuid_destroy(uuid)) != UUID_RC_OK) error(1, "uuid_destroy: %s", uuid_error(rc)); } else { /* encoding */ if ( (version == UUID_MAKE_V1 && argc != 0) || (version == UUID_MAKE_V3 && argc != 2) || (version == UUID_MAKE_V4 && argc != 0) || (version == UUID_MAKE_V5 && argc != 2)) usage("invalid number of arguments"); if ((rc = uuid_create(&uuid)) != UUID_RC_OK) error(1, "uuid_create: %s", uuid_error(rc)); if (argc == 1) { /* load initial UUID for setting old generator state */ if (strlen(argv[0]) != UUID_LEN_STR) error(1, "invalid length of UUID string representation"); if ((rc = uuid_import(uuid, UUID_FMT_STR, argv[0], strlen(argv[0]))) != UUID_RC_OK) error(1, "uuid_import: %s", uuid_error(rc)); } for (i = 0; i < count; i++) { if (iterate) { if ((rc = uuid_load(uuid, "nil")) != UUID_RC_OK) error(1, "uuid_load: %s", uuid_error(rc)); } if (version == UUID_MAKE_V3 || version == UUID_MAKE_V5) { if ((rc = uuid_create(&uuid_ns)) != UUID_RC_OK) error(1, "uuid_create: %s", uuid_error(rc)); if ((rc = uuid_load(uuid_ns, argv[0])) != UUID_RC_OK) { if ((rc = uuid_import(uuid_ns, UUID_FMT_STR, argv[0], strlen(argv[0]))) != UUID_RC_OK) error(1, "uuid_import: %s", uuid_error(rc)); } if ((rc = uuid_make(uuid, version, uuid_ns, argv[1])) != UUID_RC_OK) error(1, "uuid_make: %s", uuid_error(rc)); if ((rc = uuid_destroy(uuid_ns)) != UUID_RC_OK) error(1, "uuid_destroy: %s", uuid_error(rc)); } else { if ((rc = uuid_make(uuid, version)) != UUID_RC_OK) error(1, "uuid_make: %s", uuid_error(rc)); } if (fmt == UUID_FMT_BIN) { vp = NULL; if ((rc = uuid_export(uuid, UUID_FMT_BIN, &vp, &n)) != UUID_RC_OK) error(1, "uuid_export: %s", uuid_error(rc)); fwrite(vp, n, 1, fp); free(vp); } else if (fmt == UUID_FMT_STR) { vp = NULL; if ((rc = uuid_export(uuid, UUID_FMT_STR, &vp, &n)) != UUID_RC_OK) error(1, "uuid_export: %s", uuid_error(rc)); fprintf(fp, "%s\n", (char *)vp); free(vp); } else if (fmt == UUID_FMT_SIV) { vp = NULL; if ((rc = uuid_export(uuid, UUID_FMT_SIV, &vp, &n)) != UUID_RC_OK) error(1, "uuid_export: %s", uuid_error(rc)); fprintf(fp, "%s\n", (char *)vp); free(vp); } } if ((rc = uuid_destroy(uuid)) != UUID_RC_OK) error(1, "uuid_destroy: %s", uuid_error(rc)); } /* close output channel */ if (fp != stdout) fclose(fp); return 0; }