Tag - Length - Value parse in qt
-
Hi,
I try understand below class:
https://codereview.stackexchange.com/questions/115395/tlv-implementation-in-c
to deserialize (from bytes array to TLV (tag - length - value) object).I run this class in my qt project in this way:
QByteArray buff = "045002124354"; std::vector<uint8_t> output(buff.constData(), buff.constData() + buff.size()); TLVObject obj1; obj1.Deserialize(output); // Reconstruct TLV from byte array
And doesn't work.... anyway.. How this class know which one is Tag and wchich one is value ?
How can I access this data from theobj1
object? -
@jsulm You are right.
But I do not still know why my app doesn't work.
When I try run my appQByteArray buff = "045002124354"; std::vector<uint8_t> output(buff.constData(), buff.constData() + buff.size()); TLVObject obj1; obj1.Deserialize(output); // Reconstruct TLV from byte array
I get error:
terminate called after throwing an instance of 'std::out_of_range' what(): vector::_M_range_check: __n (which is 12) >= this->size() (which is 12)
@Damian7546
How do you/we know that045002124354
is a valid serialization for TLV to deserialize?? For example, are you sure these bytes are supposed to be decimal character digits, not actual binary values...?? Glancing --- and I do mean only glancing --- at that code I am unconvinced they are what they are supposed to be. -
Hi,
I try understand below class:
https://codereview.stackexchange.com/questions/115395/tlv-implementation-in-c
to deserialize (from bytes array to TLV (tag - length - value) object).I run this class in my qt project in this way:
QByteArray buff = "045002124354"; std::vector<uint8_t> output(buff.constData(), buff.constData() + buff.size()); TLVObject obj1; obj1.Deserialize(output); // Reconstruct TLV from byte array
And doesn't work.... anyway.. How this class know which one is Tag and wchich one is value ?
How can I access this data from theobj1
object?@Damian7546 said in Tag - Length - Value parse in qt:
How this class know which one is Tag and wchich one is value ?
The code is there, why don't you read it?
void TLVObject::Deserialize(vector<uint8_t> value) { // Get type TLV_TYPE tlvType = static_cast<TLV_TYPE>(value.at(0)); // Get tag name size. vector<uint8_t> tmp; tmp.push_back(value.at(1)); tmp.push_back(value.at(2)); uint16_t tagLength = LEToUINT16(tmp); if(tagLength > MAXTAGLENGTH) throw exception("Tag name length exceeded"); // Now get tag name std::string tagName; for(int i = 0; i<tagLength; i++) tagName += value.at(3 + i); // Use an offset. ...
-
@Damian7546 said in Tag - Length - Value parse in qt:
How this class know which one is Tag and wchich one is value ?
The code is there, why don't you read it?
void TLVObject::Deserialize(vector<uint8_t> value) { // Get type TLV_TYPE tlvType = static_cast<TLV_TYPE>(value.at(0)); // Get tag name size. vector<uint8_t> tmp; tmp.push_back(value.at(1)); tmp.push_back(value.at(2)); uint16_t tagLength = LEToUINT16(tmp); if(tagLength > MAXTAGLENGTH) throw exception("Tag name length exceeded"); // Now get tag name std::string tagName; for(int i = 0; i<tagLength; i++) tagName += value.at(3 + i); // Use an offset. ...
@jsulm You are right.
But I do not still know why my app doesn't work.
When I try run my appQByteArray buff = "045002124354"; std::vector<uint8_t> output(buff.constData(), buff.constData() + buff.size()); TLVObject obj1; obj1.Deserialize(output); // Reconstruct TLV from byte array
I get error:
terminate called after throwing an instance of 'std::out_of_range' what(): vector::_M_range_check: __n (which is 12) >= this->size() (which is 12)
-
@Damian7546 said in Tag - Length - Value parse in qt:
How this class know which one is Tag and wchich one is value ?
The code is there, why don't you read it?
void TLVObject::Deserialize(vector<uint8_t> value) { // Get type TLV_TYPE tlvType = static_cast<TLV_TYPE>(value.at(0)); // Get tag name size. vector<uint8_t> tmp; tmp.push_back(value.at(1)); tmp.push_back(value.at(2)); uint16_t tagLength = LEToUINT16(tmp); if(tagLength > MAXTAGLENGTH) throw exception("Tag name length exceeded"); // Now get tag name std::string tagName; for(int i = 0; i<tagLength; i++) tagName += value.at(3 + i); // Use an offset. ...
@jsulm for the help of this class is possible Parse data to object TLV form QbyteArray?
Do you know any library to can do this ? -
@jsulm You are right.
But I do not still know why my app doesn't work.
When I try run my appQByteArray buff = "045002124354"; std::vector<uint8_t> output(buff.constData(), buff.constData() + buff.size()); TLVObject obj1; obj1.Deserialize(output); // Reconstruct TLV from byte array
I get error:
terminate called after throwing an instance of 'std::out_of_range' what(): vector::_M_range_check: __n (which is 12) >= this->size() (which is 12)
@Damian7546
How do you/we know that045002124354
is a valid serialization for TLV to deserialize?? For example, are you sure these bytes are supposed to be decimal character digits, not actual binary values...?? Glancing --- and I do mean only glancing --- at that code I am unconvinced they are what they are supposed to be. -
JonB is completely right. You initialize the QByteArray with "numerical garbage" and so the de-serialization fails. Details: If you initialize QByteArray with a const char * you will get the ASCII codes as values. A "0" will thus yield a value of 48, which is definitely not a valid enum TLV_TYPE. And even if it would be a numerical zero, it would decode to TLV_TYPE::UNDEFINED, which doesn't sound reasonable. Please use data which was generated from a serialization.
-