コード例 #1
0
int decode_block(const char* code_in, const int length_in, char* plaintext_out,
                 decodestate* state_in)
{
    const char* codechar = code_in;
    char* plainchar = plaintext_out;
    char fragment;

    *plainchar = state_in->plainchar;

    switch (state_in->step) {
        while (1) {
            case step_a:
                do {
                    if (codechar == code_in + length_in) {
                        state_in->step = step_a;
                        state_in->plainchar = *plainchar;
                        return plainchar - plaintext_out;
                    }
                    fragment = (char)decode_value(*codechar++);
                } while (fragment < 0);
                *plainchar = (fragment & 0x03f) << 2;
            case step_b:
                do {
                    if (codechar == code_in + length_in) {
                        state_in->step = step_b;
                        state_in->plainchar = *plainchar;
                        return plainchar - plaintext_out;
                    }
                    fragment = (char)decode_value(*codechar++);
                } while (fragment < 0);
                *plainchar++ |= (fragment & 0x030) >> 4;
                *plainchar = (fragment & 0x00f) << 4;
            case step_c:
                do {
                    if (codechar == code_in + length_in) {
                        state_in->step = step_c;
                        state_in->plainchar = *plainchar;
                        return plainchar - plaintext_out;
                    }
                    fragment = (char)decode_value(*codechar++);
                } while (fragment < 0);
                *plainchar++ |= (fragment & 0x03c) >> 2;
                *plainchar = (fragment & 0x003) << 6;
            case step_d:
                do {
                    if (codechar == code_in + length_in) {
                        state_in->step = step_d;
                        state_in->plainchar = *plainchar;
                        return plainchar - plaintext_out;
                    }
                    fragment = (char)decode_value(*codechar++);
                } while (fragment < 0);
                *plainchar++ |= (fragment & 0x03f);
        }
    }
    /* control should not reach here */
    return plainchar - plaintext_out;
}
コード例 #2
0
ファイル: ardb.cpp プロジェクト: kouhate/ardb
	void Ardb::PrintDB(const DBID& db)
	{
		Slice empty;
		KeyObject start(empty, KV, db);
		Iterator* iter = FindValue(start);
		while (NULL != iter && iter->Valid())
		{
			Slice tmpkey = iter->Key();
			KeyObject* kk = decode_key(tmpkey, NULL);
			if (kk->db != db)
			{
				DELETE(kk);
				break;
			}
			ValueObject v;
			Buffer readbuf(const_cast<char*>(iter->Value().data()), 0, iter->Value().size());
			decode_value(readbuf, v, false);
			if (NULL != kk)
			{
				std::string str;
				DEBUG_LOG("[%d]Key=%s, Value=%s", kk->type, kk->key.data(), v.ToString(str).c_str());
			}
			DELETE(kk);
			iter->Next();
		}
		DELETE(iter);
	}
コード例 #3
0
ファイル: network_data.cpp プロジェクト: Cassie90/ClanLib
NetGameEvent NetGameNetworkData::decode_event(const DataBuffer &data)
{
	const unsigned char *d = data.get_data<unsigned char>();
	unsigned int length = data.get_size();
	if (length < 3)
		throw Exception("Invalid network data");

	unsigned int name_length = *reinterpret_cast<const unsigned short*>(d);
	if (length < 2 + name_length + 1)
		throw Exception("Invalid network data");
	std::string name = std::string(reinterpret_cast<const char*>(d + 2), name_length);

	NetGameEvent e(name);
	unsigned int pos = 2 + name_length;
	while (true)
	{
		if (pos >= length)
			throw Exception("Invalid network data");
		unsigned char type = d[pos++];
		if (type == 0)
			break;
		e.add_argument(decode_value(type, d, length, pos));
	}
	return e;
}
コード例 #4
0
ファイル: ardb.cpp プロジェクト: kouhate/ardb
	void Ardb::Walk(WalkHandler* handler)
	{
		KeyObject start(Slice(), KV, 0);
		Iterator* iter = FindValue(start);
		uint32 cursor = 0;
		while (NULL != iter && iter->Valid())
		{
			Slice tmpkey = iter->Key();
			KeyObject* kk = decode_key(tmpkey, NULL);
			if (NULL == kk)
			{
				break;
			}
			ValueObject v;
			Buffer readbuf(const_cast<char*>(iter->Value().data()), 0, iter->Value().size());
			decode_value(readbuf, v, false);
			int ret = handler->OnKeyValue(kk, &v, cursor++);
			DELETE(kk);
			if (ret < 0)
			{
				break;
			}
			iter->Next();
		}
		DELETE(iter);
	}
コード例 #5
0
bool TupleIterator::next() {
  if (next_ == end_) {
    return false;
  }
  current_ = next_++;
  position_ = decode_value(position_);
  return true;
}
コード例 #6
0
bool CollectionIterator::next() {
  if (index_ + 1 >= count_) {
    return false;
  }
  ++index_;
  position_ = decode_value(position_);
  return true;
}
コード例 #7
0
ファイル: ardb.cpp プロジェクト: ericcapricorn/ardb
	void Ardb::Walk(KeyObject& key, bool reverse, WalkHandler* handler)
	{
		bool isFirstElement = true;
		Iterator* iter = FindValue(key);
		if (NULL != iter && !iter->Valid() && reverse)
		{
			iter->SeekToLast();
			isFirstElement = false;
		}
		uint32 cursor = 0;
		while (NULL != iter && iter->Valid())
		{
			Slice tmpkey = iter->Key();
			KeyObject* kk = decode_key(tmpkey, &key);
			if (NULL == kk || kk->type != key.type
			        || kk->key.compare(key.key) != 0)
			{
				DELETE(kk);
				if (reverse && isFirstElement)
				{
					iter->Prev();
					isFirstElement = false;
					continue;
				}
				break;
			}
			ValueObject v;
			Buffer readbuf(const_cast<char*>(iter->Value().data()), 0,
			        iter->Value().size());
			decode_value(readbuf, v, false);
			int ret = handler->OnKeyValue(kk, &v, cursor++);
			DELETE(kk);
			if (ret < 0)
			{
				break;
			}
			if (reverse)
			{
				iter->Prev();
			}
			else
			{
				iter->Next();
			}
		}
		DELETE(iter);
	}
コード例 #8
0
ファイル: gd.c プロジェクト: jollywing/jolly-code-snippets
int main(int argc, char *argv[], char *env[])
{
    int text = 0;
    int background = 0;
    int height = 50;
    int width = 0;
    int x = 0, y = 0;
    int size = 30;
    int string_rectangle[8];
    double angle = 0.0;
    char value[255] = "";
    char font[256] = "/usr/share/fonts/TTF/DejaVuSansMono.ttf";
    char *err = NULL;
    gdImagePtr im_out = NULL;

    decode_value("TEXT=", value, 255);
    // call gdImageStringFT with NULL image to obtain size
    err = gdImageStringFT(NULL, &string_rectangle[0],
                          0, font, size, angle, 0, 0, value);
    x = string_rectangle[2] - string_rectangle[6] + 6;
    y = string_rectangle[3] - string_rectangle[7] + 6;

    /* width = strlen(value) * 10 + 5; */
    /* im_out = gdImageCreate(width, height); */
    im_out = gdImageCreate(x, y);
    
    background = gdImageColorAllocate(im_out, 255, 0, 255);
    text = gdImageColorAllocate(im_out, 0, 0, 255);

    /* get starting position */
    x = 3 - string_rectangle[6];
    y = 3 - string_rectangle[7];

    gdImageFilledRectangle(im_out, 0, 0, x - 1, y - 1, background);
    /* gdImageString(im_out, gdFontGetSmall(), 10, 10, value, text); */
    err = gdImageStringFT(im_out, &string_rectangle[0],
                          text, font, size, angle, x, y, value);
    
    printf("Content-type:image/gif\n\n");
    gdImageGif(im_out, stdout);
    gdImageDestroy(im_out);
    return 0;
}
コード例 #9
0
ファイル: user_main.c プロジェクト: Jeija/huhnix
void dcf2time(void) {
	if (signal_iter == 60) {
		time.seconds = 0;

		// Assume time signal is good only if all parities are correct
		if (check_parity(21, 27, 28) && check_parity(29, 34, 35) && check_parity(36, 57, 58)) {
			time.minutes = decode_value(21, 27);
			time.hours = decode_value(29, 34);
			time.date = decode_value(36, 41);
			time.dow = decode_value(42, 44);
			time.month = decode_value(45, 49);
			time.year = decode_value(50, 57);

			time_valid = true;
		}
	}
}
コード例 #10
0
ファイル: decode.c プロジェクト: geaaru/freeradius-server
/** Decode DHCP option
 *
 * @param[in] ctx context	to alloc new attributes in.
 * @param[in,out] cursor	Where to write the decoded options.
 * @param[in] dict		to lookup attributes in.
 * @param[in] data		to parse.
 * @param[in] data_len		of data to parse.
 * @param[in] decoder_ctx	Unused.
 */
ssize_t fr_dhcpv4_decode_option(TALLOC_CTX *ctx, fr_cursor_t *cursor,
			        fr_dict_t const *dict, uint8_t const *data, size_t data_len, UNUSED void *decoder_ctx)
{
	ssize_t			ret;
	uint8_t const		*p = data;
	fr_dict_attr_t const	*child;
	fr_dict_attr_t const	*parent;

	FR_PROTO_TRACE("%s called to parse %zu byte(s)", __FUNCTION__, data_len);

	if (data_len == 0) return 0;

	FR_PROTO_HEX_DUMP(data, data_len, NULL);

	parent = fr_dict_root(dict);

	/*
	 *	Padding / End of options
	 */
	if (p[0] == 0) return 1;		/* 0x00 - Padding option */
	if (p[0] == 255) {			/* 0xff - End of options signifier */
		size_t i;

		for (i = 1; i < data_len; i++) {
			if (p[i] != 0) {
				FR_PROTO_HEX_DUMP(p + i, data_len - i, "ignoring trailing junk at end of packet");
				break;
			}
		}
		return data_len;
	}

	/*
	 *	Everything else should be real options
	 */
	if ((data_len < 2) || (data[1] > data_len)) {
		fr_strerror_printf("%s: Insufficient data", __FUNCTION__);
		return -1;
	}

	child = fr_dict_attr_child_by_num(parent, p[0]);
	if (!child) {
		/*
		 *	Unknown attribute, create an octets type
		 *	attribute with the contents of the sub-option.
		 */
		child = fr_dict_unknown_afrom_fields(ctx, parent, fr_dict_vendor_num_by_da(parent), p[0]);
		if (!child) return -1;
	}
	FR_PROTO_TRACE("decode context changed %s:%s -> %s:%s",
		       fr_int2str(fr_value_box_type_table, parent->type, "<invalid>"), parent->name,
		       fr_int2str(fr_value_box_type_table, child->type, "<invalid>"), child->name);

	ret = decode_value(ctx, cursor, child, data + 2, data[1]);
	if (ret < 0) {
		fr_dict_unknown_free(&child);
		return ret;
	}
	ret += 2; /* For header */
	FR_PROTO_TRACE("decoding option complete, returning %zu byte(s)", ret);
	return ret;
}
コード例 #11
0
ファイル: decode.c プロジェクト: geaaru/freeradius-server
/** Decode DHCP suboptions
 *
 * @param[in] ctx context to alloc new attributes in.
 * @param[in,out] cursor Where to write the decoded options.
 * @param[in] parent of sub TLVs.
 * @param[in] data to parse.
 * @param[in] data_len of data parsed.
 */
static ssize_t decode_tlv(TALLOC_CTX *ctx, fr_cursor_t *cursor, fr_dict_attr_t const *parent,
			  uint8_t const *data, size_t data_len)
{
	uint8_t const		*p = data;
	uint8_t const		*end = data + data_len;
	fr_dict_attr_t const	*child;

	if (data_len < 3) return -1; /* type, length, value */

	FR_PROTO_TRACE("%s called to parse %zu byte(s)", __FUNCTION__, data_len);
	FR_PROTO_HEX_DUMP(data, data_len, NULL);

	/*
	 *	Each TLV may contain multiple children
	 */
	while (p < end) {
		ssize_t tlv_len;

		if (p[0] == 0) {
			p++;
			continue;
		}

		/*
		 *	RFC 3046 is very specific about not allowing termination
		 *	with a 255 sub-option. But it's required for decoding
		 *	option 43, and vendors will probably screw it up
		 *	anyway.
		 */
		if (p[0] == 255) {
			p++;
			return p - data;
		}

		/*
		 *	Everything else should be real options
		 */
		if ((end - p) < 2) {
			fr_strerror_printf("%s: Insufficient data: Needed at least 2 bytes, got %zu",
					   __FUNCTION__, (end - p));
			return -1;
		}

		if (p[1] > (end - p)) {
			fr_strerror_printf("%s: Suboption would overflow option.  Remaining option data %zu byte(s) "
					   "(from %zu), Suboption length %u", __FUNCTION__, (end - p), data_len, p[1]);
			return -1;
		}

		child = fr_dict_attr_child_by_num(parent, p[0]);
		if (!child) {
			fr_dict_attr_t const *unknown_child;

			FR_PROTO_TRACE("failed to find child %u of TLV %s", p[0], parent->name);

			/*
			 *	Build an unknown attr
			 */
			unknown_child = fr_dict_unknown_afrom_fields(ctx, parent,
								     fr_dict_vendor_num_by_da(parent), p[0]);
			if (!unknown_child) return -1;
			child = unknown_child;
		}
		FR_PROTO_TRACE("decode context changed %s:%s -> %s:%s",
			       fr_int2str(fr_value_box_type_table, parent->type, "<invalid>"), parent->name,
			       fr_int2str(fr_value_box_type_table, child->type, "<invalid>"), child->name);

		tlv_len = decode_value(ctx, cursor, child, p + 2, p[1]);
		if (tlv_len < 0) {
			fr_dict_unknown_free(&child);
			return tlv_len;
		}
		p += tlv_len + 2;
		FR_PROTO_TRACE("decode_value returned %zu, adding 2 (for header)", tlv_len);
		FR_PROTO_TRACE("remaining TLV data %zu byte(s)" , end - p);
	}
	FR_PROTO_TRACE("tlv parsing complete, returning %zu byte(s)", p - data);

	return p - data;
}
コード例 #12
0
ファイル: network_data.cpp プロジェクト: Cassie90/ClanLib
NetGameEventValue NetGameNetworkData::decode_value(unsigned char type, const unsigned char *d, unsigned int length, unsigned int &pos)
{
	switch (type)
	{
	case 1: // null
		return NetGameEventValue(NetGameEventValue::null);
	case 2: // uint
		{
			if (pos + 4 > length)
				throw Exception("Invalid network data");
			unsigned int v = *reinterpret_cast<const unsigned int*>(d + pos);
			pos += 4;
			return NetGameEventValue(v);
		}
	case 3: // int
		{
			if (pos + 4 > length)
				throw Exception("Invalid network data");
			int v = *reinterpret_cast<const int*>(d + pos);
			pos += 4;
			return NetGameEventValue(v);
		}
	case 4: // number
		{
			if (pos + 4 > length)
				throw Exception("Invalid network data");
			float v = *reinterpret_cast<const float*>(d + pos);
			pos += 4;
			return NetGameEventValue(v);
		}
	case 5: // false boolean
		return NetGameEventValue(false);
	case 6: // true boolean
		return NetGameEventValue(true);
	case 7: // string
		{
			if (pos + 2 > length)
				throw Exception("Invalid network data");
			unsigned short name_length = *reinterpret_cast<const unsigned short*>(d + pos);
			pos += 2;
			if (pos + name_length > length)
				throw Exception("Invalid network data");
			std::string value(reinterpret_cast<const char*>(d + pos), name_length);
			pos += name_length;
			return NetGameEventValue(value);
		}
	case 8: // complex
		{
			NetGameEventValue value(NetGameEventValue::complex);
			while (true)
			{
				if (pos >= length)
					throw Exception("Invalid network data");
				unsigned char type = d[pos++];
				if (type == 0)
					break;
				value.add_member(decode_value(type, d, length, pos));
			}
			return value;
		}
	case 9: // uchar
		{
			if (pos + 1 > length)
				throw Exception("Invalid network data");
			unsigned char v = *reinterpret_cast<const unsigned char*>(d + pos);
			pos += 1;
			return NetGameEventValue(v);
		}
	case 10: // char
		{
			if (pos + 1 > length)
				throw Exception("Invalid network data");
			char v = *reinterpret_cast<const char*>(d + pos);
			pos += 1;
			return NetGameEventValue(v);
		}
	case 11: // binary
		{
			if (pos + 2 > length)
				throw Exception("Invalid network data");
			unsigned short binary_length = *reinterpret_cast<const unsigned short*>(d + pos);
			pos += 2;
			if (pos + binary_length > length)
				throw Exception("Invalid network data");
			DataBuffer value(reinterpret_cast<const char*>(d + pos), binary_length);
			pos += binary_length;
			return NetGameEventValue(value);
		}
	default:
		throw Exception("Invalid network data");
	}
}
コード例 #13
0
ファイル: ardb_data.cpp プロジェクト: Ivasek/ardb
	KeyObject* decode_key(const Slice& key, KeyObject* expected)
	{
		Buffer buf(const_cast<char*>(key.data()), 0, key.size());
		uint32 header;
		if (!BufferHelper::ReadFixUInt32(buf, header))
		{
			return NULL;
		}
		uint8 type = header & 0xFF;
		uint32 db = header >> 8;
		if (NULL != expected)
		{
			if (type != expected->type || db != expected->db)
			{
				return NULL;
			}
		}
		Slice keystr;
		if (!BufferHelper::ReadVarSlice(buf, keystr))
		{
			return NULL;
		}
		if (NULL != expected)
		{
			if (keystr != expected->key)
			{
				return NULL;
			}
		}
		switch (type)
		{
			case HASH_FIELD:
			{
				Slice field;
				if (!BufferHelper::ReadVarSlice(buf, field))
				{
					return NULL;
				}
				return new HashKeyObject(keystr, field, db);
			}
			case LIST_ELEMENT:
			{
				float score;
				if (!BufferHelper::ReadFixFloat(buf, score))
				{
					return NULL;
				}
				return new ListKeyObject(keystr, score, db);
			}

			case SET_ELEMENT:
			{
				SetKeyObject* sk = new SetKeyObject(keystr, Slice(), db);
				if (!decode_value(buf, sk->value, false))
				{
					DELETE(sk);
					return sk;
				}
				return sk;
			}
			case ZSET_ELEMENT:
			{
				ZSetKeyObject* zsk = new ZSetKeyObject(keystr, Slice(), 0, db);
				double score;
				if (!BufferHelper::ReadFixDouble(buf, score)
				        || !decode_value(buf, zsk->value))
				{
					DELETE(zsk);
					return NULL;
				}
				zsk->score = score;
				return zsk;
			}
			case ZSET_ELEMENT_SCORE:
			{
				ZSetScoreKeyObject* zsk = new ZSetScoreKeyObject(keystr,
				        Slice(), db);
				if (!decode_value(buf, zsk->value))
				{
					DELETE(zsk);
					return NULL;
				}
				return zsk;
			}
			case TABLE_INDEX:
			{
				Slice kname;
				if (!BufferHelper::ReadVarSlice(buf, kname))
				{
					return NULL;
				}
				TableIndexKeyObject* ik = new TableIndexKeyObject(keystr, kname,
				        ValueObject(), db);
				if (!decode_value(buf, ik->colvalue))
				{
					DELETE(ik);
					return NULL;
				}
				uint32 len;
				if (!BufferHelper::ReadVarUInt32(buf, len))
				{
					DELETE(ik);
					return NULL;
				}
				for (uint32 i = 0; i < len; i++)
				{
					ValueObject v;
					if (!decode_value(buf, v))
					{
						DELETE(ik);
						return NULL;
					}
					ik->index.push_back(v);
				}
				return ik;
			}
			case TABLE_COL:
			{
				TableColKeyObject* tk = new TableColKeyObject(keystr, Slice(),
				        db);
				Slice col;
				if (!BufferHelper::ReadVarSlice(buf, col))
				{
					DELETE(tk);
					return NULL;
				}
				tk->colname = col;
				uint32 len;
				if (!BufferHelper::ReadVarUInt32(buf, len))
				{
					DELETE(tk);
					return NULL;
				}
				for (uint32 i = 0; i < len; i++)
				{
					ValueObject v;
					if (!decode_value(buf, v))
					{
						DELETE(tk);
						return NULL;
					}
					tk->index.push_back(v);
				}
				return tk;
			}
			case BITSET_ELEMENT:
			{
				uint64 index;
				if (!BufferHelper::ReadVarUInt64(buf, index))
				{
					return NULL;
				}
				return new BitSetKeyObject(keystr, index, db);
			}
			case KEY_EXPIRATION_ELEMENT:
			{
				uint64 ts;
				if (!BufferHelper::ReadVarUInt64(buf, ts))
				{
					return NULL;
				}
				return new ExpireKeyObject(keystr, ts, db);
			}
			case SET_META:
			case ZSET_META:
			case LIST_META:
			case TABLE_META:
			case TABLE_SCHEMA:
			case BITSET_META:
			case KEY_EXPIRATION_MAPPING:
			case KV:
			case SCRIPT:
			default:
			{
				return new KeyObject(keystr, (KeyType) type, db);
			}
		}
	}