Esempio n. 1
0
int Element_FromBytes(Element & e, int type, unsigned char *data)
{
	string d = "";
	if(is_base64((unsigned char) data[0])) {
		string b64_encoded((char *) data);
		d = _base64_decode(b64_encoded);
	}
	else {
		return ELEMENT_INVALID_ARG;
	}

	if(type == ZR_t) {
		bn_read_bin(e.zr.z, (unsigned char *) d.c_str(), d.size());
	}
	else if(type == G1_t) {
		return g1_read_bin(e.g1.g, (unsigned char *) d.c_str(), d.size()); // x & y
	}
	else if(type == G2_t) {
		return g2_read_bin(e.g2.g, (unsigned char *) d.c_str(), d.size()); // x1, y1  & x2, y2
	}
	else if(type == GT_t) {
		return gt_read_bin(e.gt.g, (unsigned char *) d.c_str(), d.size()); // x1-6 && y1-6
	}

	return ELEMENT_INVALID_ARG;
}
static pd_index_line
_parse_index_line(const char *line, const char *end)
{
    pd_index_line ret = {};
    /* <name> \t <pos> \t <len> \n
     * ^      ^  ^     ^  ^     ^
     * |      |  |     |  |     |
     * |      |  pos   |  len   endlen
     * |      |        |
     * name   endname  endpos
     *
     * <pos> and <len> are base64-encoded strings
     */
    const char *name = line;
    const char *endname = line;
    while (endname < end && *endname != '\t') endname++;
    if (endname == end || endname == name)
        return ret;
    const char *pos = endname + 1;
    const char *endpos = pos;
    while (endpos < end && _is_base64_sym(*endpos)) endpos++;
    if (endpos == end || endpos == pos || *endpos != '\t')
        return ret;
    const char *len = endpos + 1;
    const char *endlen = len;
    while (endlen < end && _is_base64_sym(*endlen)) endlen++;
    if (endlen == end || endlen == len || *endlen != '\n')
        return ret;

    ret.name = line;
    ret.endname = endname;
    ret.article_offset = _base64_decode(pos);
    ret.article_length = _base64_decode(len);
    ret.nextline = endlen + 1;
    return ret;
}
Esempio n. 3
0
int Element_FromBytes(Element &e, int type, unsigned char *data)
{
	if(type == ZR_t) {
		if(is_base64((unsigned char) data[0])) {
			string b64_encoded((char *) data);
			string s = _base64_decode(b64_encoded);
			int cnt = 0;
			Big b = Big(bytesToBig(s, &cnt));
			e = Element(b);
//			cout << "Element_FromBytes: " << e << endl;
			return TRUE;
		}
	}
	else if(type == G1_t) {
		if(is_base64((unsigned char) data[0])) {
			string b64_encoded((char *) data);
			string s = _base64_decode(b64_encoded);

			int cnt = 0;
			Big x, y;
			x = bytesToBig(s, &cnt);
			s = s.substr(cnt);
			y = bytesToBig(s, &cnt);
	//		cout << "point => (" << x << ", " << y << ")" << endl;
			G1 p;
			p.g.set(x, y);
			e = Element(p);
			//cout << "Element_FromBytes: " << e << endl;
			return TRUE;
		}
	}
#if ASYMMETRIC == 1
	else if(type == G2_t) {
		if(is_base64((unsigned char) data[0])) {
			string b64_encoded((char *) data);
			string s = _base64_decode(b64_encoded);
	//		cout << "original => " << s << endl;
			int cnt = 0;
			G2 p;
#if BUILD_MNT_CURVE == 1
			ZZn a[MNT_G2_SIZE];
			for(int i = 0; i < MNT_G2_SIZE; i++) {
				Big b = bytesToBig(s, &cnt);
				a[i] = ZZn( b ); // retrieve all six coordinates
				s = s.substr(cnt);
			}
			ZZn3 x (a[0], a[1], a[2]);
			ZZn3 y (a[3], a[4], a[5]);

			p.g.set(x, y);
#elif BUILD_BN_CURVE == 1
			Big a[BN_G2_SIZE];
			for(int i = 0; i < BN_G2_SIZE; i++) {
				a[i] = bytesToBig(s, &cnt);
				s = s.substr(cnt); // advance s ptr
			}

			ZZn2 x1(a[0], a[1]); // each zzn2 has a (x, y) coordinate of type Big
			ZZn2 y1(a[2], a[3]);
			p.g.set(x1, y1);
#endif
			e = Element(p);
			//cout << "Element_FromBytes: " << e << endl;
			return TRUE;
		}
	}
#endif
	else if(type == GT_t) {
		if(is_base64((unsigned char) data[0])) {
			string b64_encoded((char *) data);
			string s = _base64_decode(b64_encoded);
	//		cout << "original => " << s << endl;
			int cnt = 0;
			GT p;
#if BUILD_MNT_CURVE == 1
			Big a[MNT_GT_SIZE];
			for(int i = 0; i < MNT_GT_SIZE; i++) {
				a[i] = bytesToBig(s, &cnt);
				s = s.substr(cnt); // advance s ptr
			}

			ZZn2 x, y, z;
			x.set(a[0], a[1]);
			y.set(a[2], a[3]);
			z.set(a[4], a[5]);
			p.g.set(x, y, z);
#elif BUILD_BN_CURVE == 1
			Big a[BN_GT_SIZE];
			for(int i = 0; i < BN_GT_SIZE; i++) {
				a[i] = bytesToBig(s, &cnt);
				s = s.substr(cnt); // advance s ptr
			}
			ZZn2 x0, x1, y0, y1, z0, z1;
			x0.set(a[0], a[1]);
			x1.set(a[2], a[3]);
			y0.set(a[4], a[5]);
			y1.set(a[6], a[7]);
			z0.set(a[8], a[9]);
			z1.set(a[10], a[11]);

			ZZn4 x(x0, x1);
			ZZn4 y(y0, y1);
			ZZn4 z(z0, z1);
			p.g.set(x, y, z);
#elif BUILD_SS_CURVE == 1
			// must be symmetric
			Big a[SS_GT_SIZE];
			for(int i = 0; i < SS_GT_SIZE; i++) {
				a[i] = bytesToBig(s, &cnt);
				s = s.substr(cnt); // advance s ptr
			}
			p.g.set(a[0], a[1]);
#endif
			e = Element(p);
			//cout << "Element_FromBytes: " << e << endl;
			return TRUE;
		}
	}
	return 0;
}