Commit f4996089 authored by Jelte Jansen's avatar Jelte Jansen

int type for wireformat in python, ruby and C


git-svn-id: svn://bind10.isc.org/svn/bind10/branches/parkinglot@317 e5f2f494-b856-4b98-b285-d166d9295462
parent 78dabb44
......@@ -19,6 +19,7 @@ const unsigned char ITEM_HASH = 0x02;
const unsigned char ITEM_LIST = 0x03;
const unsigned char ITEM_NULL = 0x04;
const unsigned char ITEM_BOOL = 0x05;
const unsigned char ITEM_INT = 0x06;
const unsigned char ITEM_UTF8 = 0x08;
const unsigned char ITEM_MASK = 0x0f;
......@@ -566,6 +567,12 @@ decode_bool(std::stringstream& in, int& item_length)
}
}
ElementPtr
decode_int(std::stringstream& in, int& item_length)
{
return from_stringstream_int_or_double(in);
}
ElementPtr
decode_blob(std::stringstream& in, int& item_length)
{
......@@ -668,6 +675,9 @@ decode_element(std::stringstream& in, int& in_length)
case ITEM_BOOL:
element = decode_bool(in, item_length);
break;
case ITEM_INT:
element = decode_int(in, item_length);
break;
case ITEM_BLOB:
element = decode_blob(in, item_length);
break;
......@@ -765,7 +775,7 @@ IntElement::to_wire(int omit_length)
text << str();
int length = text.str().length();
ss << encode_length(length, ITEM_UTF8) << text.str();
ss << encode_length(length, ITEM_INT) << text.str();
return ss.str();
}
......
......@@ -28,6 +28,7 @@ _ITEM_HASH = 0x02
_ITEM_LIST = 0x03
_ITEM_NULL = 0x04
_ITEM_BOOL = 0x05
_ITEM_INT = 0x06
_ITEM_UTF8 = 0x08
_ITEM_MASK = 0x0f
......@@ -73,6 +74,10 @@ def _pack_bool(item):
"""Pack a bool and its type/length prefix."""
return (_encode_length_and_type(_encode_bool(item), _ITEM_BOOL))
def _pack_int(item):
"""Pack an integer and its type/length prefix."""
return (_encode_length_and_type(bytes(str(item), 'utf-8'), _ITEM_INT))
def _pack_array(item):
"""Pack a list (array) and its type/length prefix."""
return (_encode_length_and_type(_encode_array(item), _ITEM_LIST))
......@@ -100,6 +105,8 @@ def _encode_item(item):
return (_pack_nil())
elif type(item) == bool:
return (_pack_bool(item))
elif type(item) == int:
return (_pack_int(item))
elif type(item) == dict:
return (_pack_hash(item))
elif type(item) == list:
......@@ -186,6 +193,8 @@ def _decode_item(data):
value = item
elif item_type == _ITEM_BOOL:
value = _decode_bool(item)
elif item_type == _ITEM_INT:
value = _decode_int(item)
elif item_type == _ITEM_UTF8:
value = str(item, 'utf-8')
elif item_type == _ITEM_HASH:
......@@ -202,6 +211,9 @@ def _decode_item(data):
def _decode_bool(data):
return data == b'0x01'
def _decode_int(data):
return int(str(data, 'utf-8'))
def _decode_hash(data):
ret = {}
while len(data) > 0:
......
......@@ -33,7 +33,7 @@ class TestCCWireEncoding(unittest.TestCase):
def test_to_wire_of_hash(self):
wire = ISC.CC.Message.to_wire({ "simple" : { "string" : 1 }})
self.assertEqual(wire, b'Skan\x06simple"\n\x06string(\x011')
self.assertEqual(wire, b'Skan\x06simple"\n\x06string&\x011')
def test_from_wire_of_hash(self):
wire = b'Skan\x06simple"\n\x06string(\x011'
......@@ -94,5 +94,14 @@ class TestCCWireEncoding(unittest.TestCase):
decoded = ISC.CC.Message.from_wire(wire)
self.assertEqual(decoded["bool"], False)
def test_to_wire_of_int(self):
wire = ISC.CC.Message.to_wire({ "number": 123 })
self.assertEqual(wire, b'Skan\x06number&\x03123')
def test_from_wire_of_int(self):
wire = b'Skan\x06number&\x03123'
decoded = ISC.CC.Message.from_wire(wire)
self.assertEqual(decoded["number"], 123)
if __name__ == '__main__':
unittest.main()
......@@ -26,6 +26,7 @@ class Message
ITEM_LIST = 0x03
ITEM_NULL = 0x04
ITEM_BOOL = 0x05
ITEM_INT = 0x06
ITEM_UTF8 = 0x08
ITEM_MASK = 0x0f
......@@ -112,6 +113,10 @@ class Message
encode_length_and_type(encode_bool(bool), ITEM_BOOL)
end
def self.pack_int(int)
encode_length_and_type(encode_int(int), ITEM_INT)
end
def self.pack_blob(str)
encode_length_and_type(str.to_s, ITEM_BLOB)
end
......@@ -154,6 +159,8 @@ class Message
ret = pack_bool(item)
when TrueClass
ret = pack_bool(item)
when Integer
ret = pack_int(item)
else
ret = pack_blob(item.to_s)
end
......@@ -183,7 +190,10 @@ class Message
[0x00].pack("C")
end
end
def self.encode_int(int)
int.to_s.encode('binary')
end
def self.encode_array(msg)
unless msg.is_a?Array
......@@ -249,6 +259,8 @@ class Message
value = item.encode('utf-8')
when ITEM_BOOL
value = decode_bool(item)
when ITEM_INT
value = decode_int(item)
when ITEM_HASH
value = decode_hash(item)
when ITEM_LIST
......@@ -265,7 +277,11 @@ class Message
def self.decode_bool(msg)
return msg == [0x01].pack("C")
end
def self.decode_int(msg)
return Integer(msg.encode('utf-8'))
end
def self.decode_hash(msg)
ret = {}
while msg.length > 0
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment