예제 #1
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 ) );
}
예제 #2
0
static void
pguuid_complain(uuid_rc_t rc)
{
	char	   *err = uuid_error(rc);

	if (err != NULL)
		ereport(ERROR,
				(errcode(ERRCODE_EXTERNAL_ROUTINE_EXCEPTION),
				 errmsg("OSSP uuid library failure: %s", err)));
	else
		ereport(ERROR,
				(errcode(ERRCODE_EXTERNAL_ROUTINE_EXCEPTION),
				 errmsg("OSSP uuid library failure: error code %d", rc)));
}
예제 #3
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 ) );
}
예제 #4
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;
}
예제 #5
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;
}