示例#1
0
int main(int argc, char **argv)
{
  char *eout = malloc(base64_encode_length(7));
  uint8_t *dout = malloc(base64_decode_length(8));
  size_t len;

  len = base64_encoder((const uint8_t *)"foobar", 6, eout);
  fail_unless(len == 8);
  fail_unless(strcmp(eout,"Zm9vYmFy") == 0);
  len = base64_decoder(eout,0,dout);
  fail_unless(len == 6);
  fail_unless(strncmp((char*)dout,"foobar",6) == 0);

  char bfix[] = "eyJzdWIiOjEyMzQ1Njc4OTAsIm5hbWUiOiJKb2huIERvZSIsImFkbWluIjp0cnVlfQ";
  char jfix[] = "{\"sub\":1234567890,\"name\":\"John Doe\",\"admin\":true}";
  char *jtest = malloc(base64_decode_length(strlen(bfix)));
  len = base64_decoder(bfix,0,(uint8_t*)jtest);
  fail_unless(len == strlen(jfix));
  fail_unless(strcmp(jtest,jfix) == 0);
  char *btest = malloc(base64_encode_length(strlen(jfix)));
  len = base64_encoder((uint8_t*)jfix,strlen(jfix),btest);
//  printf("len %d %d %s",len,strlen(bfix),btest);
  fail_unless(len == strlen(bfix));
  fail_unless(strncmp(btest,bfix,len) == 0);

  e3x_init(NULL); // random seed
  uint8_t *rand = malloc(32);
  char *rtest = malloc(base64_encode_length(32));
  uint8_t *rand2 = malloc(32);
  char *rtest2 = malloc(base64_encode_length(32));
  int i;
  for(i = 1;i<=32;i++)
  {
    e3x_rand(rand, i);
    e3x_rand(rand2, i);
    base64_encoder(rand,i,rtest);
    base64_decoder(rtest,0,rand2);
    base64_encoder(rand2,i,rtest2);
//    printf("%s\n%s\n",rtest,rtest2);
    fail_unless(memcmp(rand,rand2,i) == 0);
  }
  
  return 0;
}
示例#2
0
static int
_common_decode(lua_State *L, DecodeFunc_t decode) {
	size_t len;
	const char *str = lua64_tolstring(L, 1, &len);
	size_t tlen = base64_decode_length(len);
	luaL_Buffer b;
	char *dst = luaL_buffinitsize(L, &b, tlen);
	size_t dlen;
	if (decode(str, len, dst, &dlen) < 0) {
		lua_pushnil(L);
	} else {
		luaL_pushresultsize(&b, dlen);
	}
	lua_pushnil(L);
	return 2;
}
示例#3
0
    ustring base64_decode(sstring const& input) {
        BIO* b64 = BIO_new(BIO_f_base64());
        sstring str = input + "\n";
        BIO* bmem = BIO_new_mem_buf((void*) str.c_str(), str.length());
        bmem = BIO_push(b64, bmem);

        unsigned char *buffer = new unsigned char[str.length()];
        BIO_read(bmem, buffer, str.length());

        BIO_free_all(bmem);

        ustring res(buffer, base64_decode_length(input));
        delete[] buffer;

        return res;
    }
示例#4
0
static int
_scheme_decode(lua_State *L) {
	struct base64_scheme *sch = lua64_touserdata(L, 1);
	size_t len;
	const char *str = lua64_tolstring(L, 2, &len);
	size_t tlen = base64_decode_length(len);
	luaL_Buffer b;
	char *dst = luaL_buffinitsize(L, &b, tlen);
	size_t dlen;
	if (base64_scheme_decode(sch, str, len, dst, &dlen) < 0) {
		lua_pushnil(L);
	} else {
		luaL_pushresultsize(&b, dlen);
	}
	lua_pushnil(L);
	return 2;
}
示例#5
0
文件: zio.c 项目: trws/flux-core
int zio_json_decode (const char *json_str, void **pp, bool *eofp)
{
    json_t *o = NULL;
    const char *s_data;
    int s_len, len = 0;
    void *data = NULL;
    int eof = 0;

    if (!json_str || !(o = json_loads (json_str, 0, NULL))
                  || json_unpack (o, "{s:b s:s}", "eof", &eof,
                                                  "data", &s_data) < 0) {
        errno = EPROTO;
        goto error;
    }
    if (pp) {
        s_len = strlen (s_data);
        len = base64_decode_length (s_len);
        data = calloc (1, len);
        if (!data) {
            errno = ENOMEM;
            goto error;
        }
        if (base64_decode_block (data, &len, s_data, s_len) < 0) {
            errno = EPROTO;
            goto error;
        }
        *pp = data;
    }
    if (eofp) {
        *eofp = eof;
    }
    json_decref (o);
    return len;
error:
    json_decref (o);
    free (data);
    return -1;
}
示例#6
0
int base64_json_decode (json_object *o, uint8_t **datp, int *lenp)
{
    const char *s;
    int dlen, len;
    void *dst;

    if (!o || json_object_get_type (o) != json_type_string) {
        errno = EINVAL;
        return -1;
    }
    s = json_object_get_string (o);
    len = strlen (s);
    dst = xzmalloc (base64_decode_length (len));
    if (base64_decode_block (dst, &dlen, s, len) < 0) {
        free (dst);
        errno = EINVAL;
        return -1;
    }

    *lenp = dlen;
    *datp = (uint8_t *) dst;
    return 0;
}