Example #1
0
/* create a UUID (v1 only) */
void uuid_dce_create(uuid_dce_t *uuid_dce, int *status)
{
    ossp_uuid_t *uuid;
    size_t len;
    void *vp;

    /* initialize status */
    if (status != NULL)
        *status = uuid_s_error;

    /* sanity check argument(s) */
    if (uuid_dce == NULL)
        return;

    /* create UUID and export as binary representation */
    if (uuid_create(&uuid) != UUID_RC_OK)
        return;
    if (uuid_make(uuid, UUID_MAKE_V1) != UUID_RC_OK) {
        uuid_destroy(uuid);
        return;
    }
    vp  = uuid_dce;
    len = UUID_LEN_BIN;
    if (uuid_export(uuid, UUID_FMT_BIN, &vp, &len) != UUID_RC_OK) {
        uuid_destroy(uuid);
        return;
    }
    uuid_destroy(uuid);

    /* return successfully */
    if (status != NULL)
        *status = uuid_s_ok;
    return;
}
Example #2
0
static int generate35( lua_State *L, uuid_fmt_t fmt, int ver, const char *ns, 
                       const char *name )
{
    uuid_rc_t rc = 0;
    uuid_t *uid = NULL;
    uuid_t *uid_ns = NULL;
    
    // create context
    if( ( rc = uuid_create( &uid ) ) == UUID_RC_OK && 
        ( rc = uuid_create( &uid_ns ) ) == UUID_RC_OK && 
        ( rc = uuid_load( uid_ns, ns ) ) == UUID_RC_OK && 
        ( rc = uuid_make( uid, ver, uid_ns, name ) ) == UUID_RC_OK ){
        uuid_export2lua( &rc, L );
        uuid_destroy( uid );
        uuid_destroy( uid_ns );
    }
    else if( uid )
    {
        uuid_destroy( uid );
        if( uid_ns ){
            uuid_destroy( uid_ns );
        }
    }
    
    return ( rc == UUID_RC_OK ) ? 1 : luaL_error( L, "%s", uuid_error( rc ) );
}
Example #3
0
static String make_uuid( unsigned int mode )
{
    String result;

    uuid_t * u = 0;

    if ( UUID_RC_OK == uuid_create( & u ) )
    {
        if ( UUID_RC_OK == uuid_make( u , mode ) )
        {
            char buffer[ UUID_LEN_STR + 1 ];

            size_t len = UUID_LEN_STR + 1;

            void * up = & buffer[0];

            if ( UUID_RC_OK == uuid_export( u , UUID_FMT_STR , & up , & len ) )
            {
                result = buffer;
            }
        }

        uuid_destroy( u );
    }

    return result;
}
Example #4
0
File: util.c Project: tonyg/hop
int gen_uuid(unsigned char *uuid_buf) {
  uuid_rc_t result;
  uuid_t *uuid;
  unsigned char temp_buf[UUID_LEN_STR + 1];
  unsigned char *temp_buf_ptr = &temp_buf[0]; /* odd API */
  size_t uuid_buf_len = UUID_LEN_STR + 1;

  assert(CMSG_UUID_BUF_SIZE == UUID_LEN_STR);

  result = uuid_create(&uuid);
  UUID_CHECK("uuid_create");

  result = uuid_make(uuid, UUID_MAKE_V4);
  UUID_CHECK("uuid_make");

  result = uuid_export(uuid, UUID_FMT_STR, &temp_buf_ptr, &uuid_buf_len);
  UUID_CHECK("uuid_export");
  assert(uuid_buf_len == (UUID_LEN_STR + 1));

  memcpy(uuid_buf, temp_buf, CMSG_UUID_BUF_SIZE);

  result = uuid_destroy(uuid);
  UUID_CHECK("uuid_destroy");

  return UUID_RC_OK;
}
ngx_int_t ngx_x_rid_header_get_variable(ngx_http_request_t *r, ngx_http_variable_value_t *v, uintptr_t data) {
  u_char            *p         = NULL;
  ngx_table_elt_t   *header;
  ngx_str_t          hv;
  size_t             hlen      = 22;

  header = search_headers_in(r, ngx_x_rid_header_name.data, ngx_x_rid_header_name.len);

  if (header != NULL) {
      hv = header->value;

      if (hv.len >= 20 && hv.len <= 50) {
          // Reuse existing header
          hlen = hv.len;
          p = hv.data;
      }
  }

  if (p == NULL) {
      // Prepare 22 bytes to store the base58 string
      p = ngx_pnalloc(r->pool, 22);
      if (p == NULL) {
          return NGX_ERROR;
      }

#if (NGX_FREEBSD)
#error FreeBSD is not supported yet, sorry.
#elif (NGX_LINUX)
      uuid_t* uuid;

      // return of uuid_s_ok = 0
      if ( uuid_create(&uuid) ) {
        return -1;
      }
      if ( uuid_make(uuid, UUID_MAKE_V4) ) {
        uuid_destroy(uuid);
        return -1;
      }

      // at this point we have 16 bytes in "uuid", ready for conversion
      uuid_fmt22(uuid, p);
      uuid_destroy(uuid);
#elif (NGX_SOLARIS)
#error Solaris is not supported yet, sorry.
#elif (NGX_DARWIN)
      uuid_t uuid;
      uuid_generate(uuid);
      uuid_fmt22(uuid, p);
#endif
  }

  v->len = hlen;
  v->valid = 1;
  v->no_cacheable = 0;
  v->not_found = 0;
  v->data = p;

  return NGX_OK;
}
Example #6
0
char 
*uuid_v1(void) {
    uuid_t *uuid;
    char *str;

    uuid_create(&uuid);
    uuid_make(uuid, UUID_MAKE_V1);
    str = NULL;
    uuid_export(uuid, UUID_FMT_STR, &str, NULL);
    uuid_destroy(uuid);
    //debug("uuid: %s", str);
    return(str);
}
Example #7
0
char* guac_generate_id(char prefix) {

    char* buffer;
    char* identifier;
    size_t identifier_length;

    uuid_t* uuid;

    /* Attempt to create UUID object */
    if (uuid_create(&uuid) != UUID_RC_OK) {
        guac_error = GUAC_STATUS_NO_MEMORY;
        guac_error_message = "Could not allocate memory for UUID";
        return NULL;
    }

    /* Generate random UUID */
    if (uuid_make(uuid, UUID_MAKE_V4) != UUID_RC_OK) {
        uuid_destroy(uuid);
        guac_error = GUAC_STATUS_NO_MEMORY;
        guac_error_message = "UUID generation failed";
        return NULL;
    }

    /* Allocate buffer for future formatted ID */
    buffer = malloc(UUID_LEN_STR + 2);
    if (buffer == NULL) {
        uuid_destroy(uuid);
        guac_error = GUAC_STATUS_NO_MEMORY;
        guac_error_message = "Could not allocate memory for connection ID";
        return NULL;
    }

    identifier = &(buffer[1]);
    identifier_length = UUID_LEN_STR + 1;

    /* Build connection ID from UUID */
    if (uuid_export(uuid, UUID_FMT_STR, &identifier, &identifier_length) != UUID_RC_OK) {
        free(buffer);
        uuid_destroy(uuid);
        guac_error = GUAC_STATUS_INTERNAL_ERROR;
        guac_error_message = "Conversion of UUID to connection ID failed";
        return NULL;
    }

    uuid_destroy(uuid);

    buffer[0] = prefix;
    buffer[UUID_LEN_STR + 1] = '\0';
    return buffer;

}
Example #8
0
static int gen_uuid(char *uuid_str, unsigned int mode)
{
	char *str;
	uuid_t *uuid;

	uuid_create(&uuid);
	uuid_make(uuid, mode);
	str = NULL;
	uuid_export(uuid, UUID_FMT_STR, (void **)&str, NULL);
	uuid_destroy(uuid);
	memcpy_s(uuid_str, UUID_MAX_LEN, str, UUID_MAX_LEN);

	return 0;
}
Example #9
0
/* len is unused with OSSP, but we want to have the same number of args */
static Datum
uuid_generate_internal(int mode, const uuid_t *ns, const char *name, int len)
{
	uuid_t	   *uuid = get_cached_uuid_t(0);
	char	   *str;
	uuid_rc_t	rc;

	rc = uuid_make(uuid, mode, ns, name);
	if (rc != UUID_RC_OK)
		pguuid_complain(rc);
	str = uuid_to_string(uuid);

	return DirectFunctionCall1(uuid_in, CStringGetDatum(str));
}
Example #10
0
static int generate14( lua_State *L, uuid_fmt_t fmt, int ver )
{
    uuid_t *uid = NULL;
    uuid_rc_t rc = 0;
    
    if( ( rc = uuid_create( &uid ) ) == UUID_RC_OK &&
        ( rc = uuid_make( uid, ver ) ) == UUID_RC_OK ){
        uuid_export2lua( &rc, L );
        uuid_destroy( uid );
    }
    else if( uid ){
        uuid_destroy( uid );
    }

    return ( rc == UUID_RC_OK ) ? 1 : luaL_error( L, "%s", uuid_error( rc ) );
}
Example #11
0
/* Set everything up for using the NI compiler. Called from the main thread. */
static void
prepare_ni_compiler(CompilerData *data)
{
    I7_STORY_USE_PRIVATE(data->story, priv);
    GError *err = NULL;

    /* Clear the previous compile output */
    gtk_text_buffer_set_text(priv->progress, "", -1);
    html_load_blank(WEBKIT_WEB_VIEW(data->story->panel[LEFT]->results_tabs[I7_RESULTS_TAB_REPORT]));
    html_load_blank(WEBKIT_WEB_VIEW(data->story->panel[RIGHT]->results_tabs[I7_RESULTS_TAB_REPORT]));

    /* Create the UUID file if needed */
    GFile *uuid_file = g_file_get_child(data->input_file, "uuid.txt");
    if(!g_file_query_exists(uuid_file, NULL)) {
#ifdef E2FS_UUID /* code for e2fsprogs uuid */
        uuid_t uuid;
        gchar uuid_string[37];

        uuid_generate_time(uuid);
        uuid_unparse(uuid, uuid_string);
#else /* code for OSSP UUID */
        gchar *uuid_string = NULL; /* a new buffer is allocated if NULL */
        uuid_t *uuid;

        if(!((uuid_create(&uuid) == UUID_RC_OK)
                && (uuid_make(uuid, UUID_MAKE_V1) == UUID_RC_OK)
                && (uuid_export(uuid, UUID_FMT_STR, (void **)&uuid_string, NULL) == UUID_RC_OK)
                && (uuid_destroy(uuid) == UUID_RC_OK))) {
            error_dialog(GTK_WINDOW(data->story), NULL, _("Error creating UUID."));
            g_object_unref(uuid_file);
            return;
        }
#endif /* UUID conditional */
        if(!g_file_replace_contents(uuid_file, uuid_string, strlen(uuid_string), NULL, FALSE, G_FILE_CREATE_NONE, NULL, NULL, &err)) {
            IO_ERROR_DIALOG(GTK_WINDOW(data->story), uuid_file, err, _("creating UUID file"));
            g_object_unref(uuid_file);
            return;
        }
#ifndef E2FS_UUID /* Only OSSP UUID */
        free(uuid_string);
#endif /* !OSSP_UUID */
    }
    g_object_unref(uuid_file);

    /* Display status message */
    i7_document_display_status_message(I7_DOCUMENT(data->story), _("Compiling Inform 7 to Inform 6"), COMPILE_OPERATIONS);
}
Example #12
0
static void efi_generate_uuid(efi_guid_t *ent_uuid)
{
#ifdef HAVE_UUID_GENERATE
  uuid_generate((unsigned char*)ent_uuid);
#elif defined HAVE_UUIDGEN
  uuidgen((struct uuid*)ent_uuid,1);
#elif defined HAVE_UUID_CREATE
  uuid_t *uuid;
  char *data_ptr=(char*)&ent_uuid;
  size_t data_len=sizeof(ent_uuid);;
  uuid_create(&uuid);
  uuid_make(uuid, UUID_MAKE_V1);
  uuid_export(uuid, UUID_FMT_BIN, (void **)&data_ptr, &data_len);
  uuid_destroy(uuid);
#else
#warning You need a uuid_generate, uuidgen or uuid_create function
#endif
  swap_uuid_and_efi_guid(ent_uuid);
}
ngx_int_t ngx_x_rid_header_get_variable(ngx_http_request_t *r, ngx_http_variable_value_t *v, uintptr_t data) {
  u_char *p;

  // Prepare 22 bytes to store the base58 string
  p = ngx_pnalloc(r->pool, 22);
  if (p == NULL) {
      return NGX_ERROR;
  }

#if (NGX_FREEBSD)
#error FreeBSD is not supported yet, sorry.
#elif (NGX_LINUX)
  uuid_t* uuid;

  // return of uuid_s_ok = 0
  if ( uuid_create(&uuid) ) {
    return -1;
  }
  if ( uuid_make(uuid, UUID_MAKE_V4) ) {
    uuid_destroy(uuid);
    return -1;
  }

  // at this point we have 16 bytes in "uuid", ready for conversion
  uuid_fmt22(uuid, p);
  uuid_destroy(uuid);
#elif (NGX_SOLARIS)
#error Solaris is not supported yet, sorry.
#elif (NGX_DARWIN)
  uuid_t uuid;
  uuid_generate(uuid);
  uuid_fmt22(uuid, p);
#endif

  v->len = 22;
  v->valid = 1;
  v->no_cacheable = 0;
  v->not_found = 0;
  v->data = p;

  return NGX_OK;
}
ngx_int_t ngx_x_rid_header_get_variable(ngx_http_request_t *r, ngx_http_variable_value_t *v, uintptr_t data) {
  u_char *p;

  p = ngx_pnalloc(r->pool, 37);
  if (p == NULL) {
      return NGX_ERROR;
  }

#if (NGX_FREEBSD)
#error FreeBSD is not supported yet, sorry.
#elif (NGX_LINUX)
  uuid_t* uuid;
  if ( uuid_create(&uuid) ) {
    return -1;
  }
  if ( uuid_make(uuid, UUID_MAKE_V4) ) {
    uuid_destroy(uuid);
    return -1;
  }
  size_t data_len = 37;
  if ( uuid_export(uuid, UUID_FMT_STR, &p, &data_len) ) {
    uuid_destroy(uuid);
    return -1;
  }
  uuid_destroy(uuid);
#elif (NGX_SOLARIS)
#error Solaris is not supported yet, sorry.
#elif (NGX_DARWIN)
  uuid_t uuid;
  uuid_generate(uuid);
  uuid_unparse_lower(uuid, (char*)p);
#endif

  v->len = 36;
  v->valid = 1;
  v->no_cacheable = 0;
  v->not_found = 0;
  v->data = p;

  return NGX_OK;
}
Example #15
0
File: DBoid.c Project: high/PRiDe
dboid_t dboidCreate( char *name )
{
	uuid_t 	*uuid;
	uuid_t 	*uuid_ns;
	char 	*str;
	
	uuid_create( &uuid );
	uuid_create( &uuid_ns );
	
	uuid_load( uuid_ns, "ns:OID" );
	uuid_make( uuid, UUID_MAKE_V3, uuid_ns, name );
	
	str = NULL;
	
	uuid_export( uuid, UUID_FMT_STR, &str, NULL );
	uuid_destroy( uuid_ns );
	uuid_destroy( uuid );
	
	return str;
	
}
Example #16
0
ib_status_t ib_uuid_create_v4(ib_uuid_t *uuid)
{
    uuid_rc_t uuid_rc;
    size_t uuid_len = UUID_LEN_BIN;
    ib_status_t rc = IB_OK;

    rc = ib_lock_lock(&g_uuid_lock);
    if (rc != IB_OK) {
        return rc;
    }

    uuid_rc = uuid_make(g_ossp_uuid, UUID_MAKE_V4);
    if (uuid_rc == UUID_RC_MEM) {
        rc = IB_EALLOC;
        goto finish;
    }
    else if (uuid_rc != UUID_RC_OK) {
        rc = IB_EOTHER;
        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;
}
Example #17
0
ib_status_t ib_uuid_create_v4(ib_uuid_t *uuid)
{
    IB_FTRACE_INIT();

    uuid_rc_t uuid_rc;
    size_t uuid_len = UUID_LEN_BIN;
    ib_status_t rc = IB_OK;

    rc = ib_lock_init(&g_uuid_lock);
    if (rc != IB_OK) {
        IB_FTRACE_RET_STATUS(rc);
    }

    uuid_rc = uuid_make(g_ossp_uuid, UUID_MAKE_V4);
    if (uuid_rc == UUID_RC_MEM) {
        rc = IB_EALLOC;
        goto finish;
    }
    else if (uuid_rc != UUID_RC_OK) {
        rc = IB_EOTHER;
        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) {
        IB_FTRACE_RET_STATUS(IB_EOTHER);
    }

    IB_FTRACE_RET_STATUS(rc);
}
Example #18
0
void setup_metadata(kbconfig *config)
{
    LOG(1, "Writing metadata.\n");

    fs_metadata *md = fs_metadata_open(config->name);
    fs_metadata_clear(md);
    fs_metadata_set(md, FS_MD_NAME, config->name);
    fs_metadata_set(md, FS_MD_HASHFUNC, FS_HASH);
    fs_metadata_set(md, FS_MD_STORE, "native");
    fs_metadata_set(md, FS_MD_MODEL_DATA, "true");
    if (config->model_files) {
        fs_metadata_set(md, FS_MD_MODEL_FILES, "true");
    } else {
        fs_metadata_set(md, FS_MD_MODEL_FILES, "false");
    }
    fs_metadata_set(md, FS_MD_CODE_VERSION, GIT_REV);
    for (int seg = 0; seg < config->segments; seg++) {
        if (primary_segment(config, seg))
	    fs_metadata_add_int(md, FS_MD_SEGMENT_P, seg);
        if (mirror_segment(config, seg))
	    fs_metadata_add_int(md, FS_MD_SEGMENT_M, seg);
    }

    /* Generate store UUID for skolemisation */
#if defined(USE_LINUX_UUID)
    uuid_t uu;
    uuid_string_t uus;
    uuid_generate(uu);
    uuid_unparse(uu, uus);
#elif defined(USE_BSD_UUID)
    uuid_t uu;
    char *uus = NULL;
    int status = -1;
    uuid_create(&uu, &status);
    if (status) { fs_error(LOG_ERR, "bad return from uuid_create"); exit(1); }
    uuid_to_string(&uu, &uus, &status);
    if (status || uus == NULL) { fs_error(LOG_ERR, "bad return from uuid_to_string"); exit(1); }
#elif defined(USE_OSSP_UUID)
    uuid_t *uu = NULL;
    char *uus = NULL;
    if (uuid_create(&uu)) { fs_error(LOG_ERR, "bad return from uuid_create"); exit(1); }
    if (uuid_make(uu, UUID_MAKE_V1)) { fs_error(LOG_ERR, "bad return from uuid_make"); exit(1); }
    if (uuid_export(uu, UUID_FMT_STR, &uus, NULL) || uus == NULL) { fs_error(LOG_ERR, "bad return from uuid_export"); exit(1); }
#endif
    fs_metadata_add(md, FS_MD_UUID, uus);
#if defined(USE_OSSP_UUID)
    uuid_destroy(uu);
#endif

    unsigned char stage1[20], stage2[16];
    char hash[33] = "none";
    int now = 0;
    if (config->password) {
        md5_state_t md5;
        char *pw = g_strdup_printf("%s:%s", config->name, config->password);

        /* stage1 will contain the 4 byte Unix time_t value as a salt ... */
        now = time(NULL);
        memcpy(stage1, &now, sizeof(now));

        /* ... followed by the on-wire 16 byte MD5 auth string */
	md5_init(&md5);
        md5_append(&md5, (md5_byte_t *) pw, strlen(pw));
        md5_finish(&md5, stage1 + 4);

        /* now use MD5 on all 20 bytes and store both the salt and the hash */
	md5_init(&md5);
        md5_append(&md5, stage1, sizeof(stage1));
        md5_finish(&md5, stage2);

        sprintf(hash, "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
                stage2[0], stage2[1], stage2[2], stage2[3], stage2[4], stage2[5],
	        stage2[6], stage2[7], stage2[8], stage2[9], stage2[10], stage2[11],
                stage2[12], stage2[13], stage2[14], stage2[15]);

        g_free(pw);
    }

    fs_metadata_add_int(md, FS_MD_VERSION, FS_CURRENT_TABLE_VERSION);
    fs_metadata_add_int(md, FS_MD_SEGMENTS, config->segments);
    fs_metadata_add_int(md, FS_MD_SALT, now);
    fs_metadata_add_int(md, FS_MD_BNODE, 1);
    fs_metadata_add(md, FS_MD_HASH, hash);
    fs_metadata_flush(md);
    fs_metadata_close(md);

    fs_error(LOG_INFO, "created RDF metadata for KB %s", config->name);
}
Example #19
0
static foreign_t
pl_uuid(term_t UUID, term_t options)
{ unsigned int mode = UUID_MAKE_V1;
  atom_t format = ATOM_atom;
  uuid_t *uuid;
  char *ns = NULL;
  char *str = NULL;
  int rc;
  uuid_rc_t urc;

  if ( !PL_get_nil(options) )
  { term_t tail = PL_copy_term_ref(options);
    term_t head = PL_new_term_ref();
    term_t arg  = PL_new_term_ref();

    while( PL_get_list(tail, head, tail) )
    { atom_t name;
      size_t arity;

      if ( !PL_get_name_arity(head, &name, &arity) || arity != 1 )
	return PL_type_error("option", head);
      _PL_get_arg(1, head, arg);

      if ( name == ATOM_version )
      { int v;

	if ( !PL_get_integer_ex(arg, &v) )
	  return FALSE;
	switch(v)
	{ case 1: mode = UUID_MAKE_V1; break;
	  case 2: mode = UUID_MAKE_MC; break;
	  case 3: mode = UUID_MAKE_V3; break;
	  case 4: mode = UUID_MAKE_V4; break;
	  case 5: mode = UUID_MAKE_V5; break;
          default: return PL_domain_error("uuid_version", arg);
	}
      } else if ( name == ATOM_format )
      { if ( !PL_get_atom_ex(arg, &format) )
	  return FALSE;
	if ( format != ATOM_atom && format != ATOM_integer )
	  return PL_domain_error("uuid_format", arg);
      } else
      { char *newns = NULL;

	if ( name == ATOM_dns )
	{ newns = "ns:DNS";
	} else if ( name == ATOM_url )
	{ newns = "ns:URL";
	} else if ( name == ATOM_oid )
	{ newns = "ns:OID";
	} else if ( name == ATOM_x500 )
	{ newns = "ns:X500";
	}

	if ( newns )
	{ ns = newns;
	  if ( !PL_get_chars(arg, &str, CVT_ATOM|CVT_EXCEPTION) )
	    return FALSE;
	  if ( mode == UUID_MAKE_V1 )
	    mode = UUID_MAKE_V3;
	}
      }
    }
    if ( !PL_get_nil_ex(tail) )
      return FALSE;
  }

  switch(mode)
  { case UUID_MAKE_V1:
    case UUID_MAKE_MC:
    case UUID_MAKE_V4:
      uuid_create(&uuid);
      if ( (urc=uuid_make(uuid, mode)) != UUID_RC_OK )
	return PL_warning("UUID: make: %s\n", uuid_error(urc));
      break;
    case UUID_MAKE_V3:
    case UUID_MAKE_V5:
    { uuid_t *uuid_ns;

      if ( !ns )
	return PL_existence_error("uuid_context", options);

      uuid_create(&uuid);
      uuid_create(&uuid_ns);
      uuid_load(uuid_ns, ns);
      if ( (urc=uuid_make(uuid, mode, uuid_ns, str)) != UUID_RC_OK )
	return PL_warning("UUID: make: %s\n", uuid_error(urc));
      uuid_destroy(uuid_ns);
      break;
    }
    default:
      assert(0);
      return FALSE;
  }

  if ( format == ATOM_atom )
  { char buf[UUID_LEN_STR+1];
    void *ptr = buf;
    size_t datalen = sizeof(buf);

    if ( (urc=uuid_export(uuid, UUID_FMT_STR, &ptr, &datalen)) != UUID_RC_OK )
      return PL_warning("UUID: export: %s\n", uuid_error(urc));
    rc = PL_unify_chars(UUID, PL_ATOM|REP_ISO_LATIN_1, (size_t)-1, buf);
  } else if ( format == ATOM_integer )
  { char buf[UUID_LEN_SIV+1];
    void *ptr = buf;
    size_t datalen = sizeof(buf);
    term_t tmp = PL_new_term_ref();

    if ( (urc=uuid_export(uuid, UUID_FMT_SIV, &ptr, &datalen)) != UUID_RC_OK )
      return PL_warning("UUID: export: %s\n", uuid_error(urc));
    rc = ( PL_chars_to_term(buf, tmp) &&
	   PL_unify(UUID, tmp)
	 );
  } else
  { assert(0);
    return FALSE;
  }

  uuid_destroy(uuid);

  return rc;
}
Example #20
0
CID CID::Generate() {
  uuid_t *uuid;
  uuid_create(&uuid);
  uuid_make(uuid, UUID_MAKE_V4);
  return CID(uuid);
}
Example #21
0
/* 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;
}
Example #22
0
/* 
 * rasqal_expression_evaluate_uuid:
 * @e: The expression to evaluate.
 * @eval_context: Evaluation context
 * @want_uri: non-0 to return URI otherwise string
 *
 * INTERNAL - Evaluate SPARQL 1.1 RASQAL_EXPR_UUID, RASQAL_EXPR_STRUUID
 *
 * Return value: A #rasqal_literal URI / string value or NULL on failure.
 */
static rasqal_literal*
rasqal_expression_evaluate_uuid(rasqal_expression *e,
                                rasqal_evaluation_context *eval_context,
                                int *error_p,
                                int want_uri)
{
#ifdef RASQAL_UUID_NONE
  return NULL;

#else

  rasqal_world* world = eval_context->world;
#if defined(RASQAL_UUID_OSSP)
  uuid_t* data;
#else
  uuid_t data; /* static */
  int i;
#endif
  size_t output_len = RASQAL_UUID_STRING_LEN;
  unsigned char* output;
  unsigned char* p;

#if defined(RASQAL_UUID_LIBUUID) || defined(RASQAL_UUID_LIBC)
  uuid_generate(data);
#endif
#if defined(RASQAL_UUID_OSSP)
  uuid_create(&data);
  uuid_make(data, UUID_MAKE_V1);
#endif
#ifdef RASQAL_UUID_INTERNAL
  rasqal_uuid_generate(eval_context, data);
#endif

  if(want_uri)
    output_len += RASQAL_UUID_URI_PREFIX_LEN;

  output = RASQAL_MALLOC(unsigned char*, output_len + 1);
  if(!output) {
#if defined(RASQAL_UUID_OSSP)
    uuid_destroy(data);
#endif
    return NULL;
  }

  p = output;
  if(want_uri) {
    memcpy(p, RASQAL_UUID_URI_PREFIX, RASQAL_UUID_URI_PREFIX_LEN);
    p += RASQAL_UUID_URI_PREFIX_LEN;
  }

#if defined(RASQAL_UUID_OSSP)
  uuid_export(data, UUID_FMT_STR, p, /* data_len */ NULL);
  uuid_destroy(data);
#else
  for(i = 0; i < RASQAL_UUID_LEN; i++) {
    unsigned short hex;
    unsigned char c = data[i];

    hex = (c & 0xf0) >> 4;
    *p++ = (hex < 10) ? ('0' + hex) : ('a' + hex - 10);
    hex = (c & 0x0f);
    *p++ = (hex < 10) ? ('0' + hex) : ('a' + hex - 10);
    if(i == 3 || i == 5 || i == 7 || i == 9)
      *p++ = '-';
  }
  *p = '\0';
#endif /* end if !RASQAL_UUID_OSSP */

  /* after this output becomes owned by result */
  if(want_uri) {
    raptor_uri* u;
    rasqal_literal* l = NULL;

    u = raptor_new_uri(world->raptor_world_ptr, output);
    if(u)
      l = rasqal_new_uri_literal(world, u);

    RASQAL_FREE(char*, output);
    return l;
  } else {
    return rasqal_new_string_literal(world, output, NULL, NULL, NULL);
  }
#endif
}