27 CFrameDecoder::CFrameDecoder (
28 const CTypeRegistry * pTypeRegistry,
29 unsigned char * pBuffer,
31 : CDecoder(pTypeRegistry)
41 CFrameDecoder::~CFrameDecoder (
void)
46 CFrameDecoder::decodeMessage (
void)
48 CFrameDecoderStream DecoderStream(
this);
51 pMessage = DecoderStream.getMessage();
57 CFrameDecoder::next_u8 (
void)
61 assert(m_iNext + 1u <= m_nBuffer);
63 Value = m_pBuffer[m_iNext++];
69 CFrameDecoder::next_u16 (
void)
73 assert(m_iNext + 2u <= m_nBuffer);
75 Value = m_pBuffer[m_iNext++];
77 Value |= m_pBuffer[m_iNext++];
83 CFrameDecoder::next_u32 (
void)
87 assert(m_iNext + 4u <= m_nBuffer);
89 Value = m_pBuffer[m_iNext++];
91 Value |= m_pBuffer[m_iNext++];
93 Value |= m_pBuffer[m_iNext++];
95 Value |= m_pBuffer[m_iNext++];
101 CFrameDecoder::next_u64 (
void)
105 assert(m_iNext + 8u <= m_nBuffer);
107 Value = m_pBuffer[m_iNext++];
109 Value |= m_pBuffer[m_iNext++];
111 Value |= m_pBuffer[m_iNext++];
113 Value |= m_pBuffer[m_iNext++];
115 Value |= m_pBuffer[m_iNext++];
117 Value |= m_pBuffer[m_iNext++];
119 Value |= m_pBuffer[m_iNext++];
121 Value |= m_pBuffer[m_iNext++];
127 CFrameDecoderStream::get_u8 (
128 const CFieldDescriptor * pFieldDescriptor)
132 if(checkAvailable(1u, pFieldDescriptor))
134 Value = m_pDecoder->next_u8();
145 CFrameDecoderStream::get_s8 (
146 const CFieldDescriptor * pFieldDescriptor)
150 if(checkAvailable(1u, pFieldDescriptor))
152 Value = m_pDecoder->next_u8();
163 CFrameDecoderStream::get_u8v (
164 const CFieldDescriptor * pFieldDescriptor)
169 nValue = getVarlenCount(pFieldDescriptor);
173 if(checkAvailable(1u * nValue, pFieldDescriptor))
175 Value = llrp_u8v_t(nValue);
176 if(verifyVectorAllocation(Value.m_pValue, pFieldDescriptor))
178 for(
unsigned int Ix = 0; Ix < nValue; Ix++)
180 Value.m_pValue[Ix] = m_pDecoder->next_u8();
190 CFrameDecoderStream::get_s8v (
191 const CFieldDescriptor * pFieldDescriptor)
196 nValue = getVarlenCount(pFieldDescriptor);
200 if(checkAvailable(1u * nValue, pFieldDescriptor))
202 Value = llrp_s8v_t(nValue);
203 if(verifyVectorAllocation(Value.m_pValue, pFieldDescriptor))
205 for(
unsigned int Ix = 0; Ix < nValue; Ix++)
207 Value.m_pValue[Ix] = m_pDecoder->next_u8();
217 CFrameDecoderStream::get_u16 (
218 const CFieldDescriptor * pFieldDescriptor)
222 if(checkAvailable(2u, pFieldDescriptor))
224 Value = m_pDecoder->next_u16();
235 CFrameDecoderStream::get_s16 (
236 const CFieldDescriptor * pFieldDescriptor)
240 if(checkAvailable(2u, pFieldDescriptor))
242 Value = m_pDecoder->next_u16();
253 CFrameDecoderStream::get_u16v (
254 const CFieldDescriptor * pFieldDescriptor)
259 nValue = getVarlenCount(pFieldDescriptor);
263 if(checkAvailable(2u * nValue, pFieldDescriptor))
265 Value = llrp_u16v_t(nValue);
266 if(verifyVectorAllocation(Value.m_pValue, pFieldDescriptor))
268 for(
unsigned int Ix = 0; Ix < nValue; Ix++)
270 Value.m_pValue[Ix] = m_pDecoder->next_u16();
280 CFrameDecoderStream::get_s16v (
281 const CFieldDescriptor * pFieldDescriptor)
286 nValue = getVarlenCount(pFieldDescriptor);
290 if(checkAvailable(2u * nValue, pFieldDescriptor))
292 Value = llrp_s16v_t(nValue);
293 if(verifyVectorAllocation(Value.m_pValue, pFieldDescriptor))
295 for(
unsigned int Ix = 0; Ix < nValue; Ix++)
297 Value.m_pValue[Ix] = m_pDecoder->next_u16();
309 CFrameDecoderStream::get_u32 (
310 const CFieldDescriptor * pFieldDescriptor)
314 if(checkAvailable(4u, pFieldDescriptor))
316 Value = m_pDecoder->next_u32();
327 CFrameDecoderStream::get_s32 (
328 const CFieldDescriptor * pFieldDescriptor)
332 if(checkAvailable(4u, pFieldDescriptor))
334 Value = m_pDecoder->next_u32();
345 CFrameDecoderStream::get_u32v (
346 const CFieldDescriptor * pFieldDescriptor)
351 nValue = getVarlenCount(pFieldDescriptor);
355 if(checkAvailable(4u * nValue, pFieldDescriptor))
357 Value = llrp_u32v_t(nValue);
358 if(verifyVectorAllocation(Value.m_pValue, pFieldDescriptor))
360 for(
unsigned int Ix = 0; Ix < nValue; Ix++)
362 Value.m_pValue[Ix] = m_pDecoder->next_u32();
372 CFrameDecoderStream::get_s32v (
373 const CFieldDescriptor * pFieldDescriptor)
378 nValue = getVarlenCount(pFieldDescriptor);
382 if(checkAvailable(4u * nValue, pFieldDescriptor))
384 Value = llrp_s32v_t(nValue);
385 if(verifyVectorAllocation(Value.m_pValue, pFieldDescriptor))
387 for(
unsigned int Ix = 0; Ix < nValue; Ix++)
389 Value.m_pValue[Ix] = m_pDecoder->next_u32();
400 CFrameDecoderStream::get_u64 (
401 const CFieldDescriptor * pFieldDescriptor)
405 if(checkAvailable(8u, pFieldDescriptor))
407 Value = m_pDecoder->next_u64();
418 CFrameDecoderStream::get_s64 (
419 const CFieldDescriptor * pFieldDescriptor)
423 if(checkAvailable(8u, pFieldDescriptor))
425 Value = m_pDecoder->next_u64();
436 CFrameDecoderStream::get_u64v (
437 const CFieldDescriptor * pFieldDescriptor)
442 nValue = getVarlenCount(pFieldDescriptor);
446 if(checkAvailable(8u * nValue, pFieldDescriptor))
448 Value = llrp_u64v_t(nValue);
449 if(verifyVectorAllocation(Value.m_pValue, pFieldDescriptor))
451 for(
unsigned int Ix = 0; Ix < nValue; Ix++)
453 Value.m_pValue[Ix] = m_pDecoder->next_u64();
463 CFrameDecoderStream::get_s64v (
464 const CFieldDescriptor * pFieldDescriptor)
469 nValue = getVarlenCount(pFieldDescriptor);
473 if(checkAvailable(8u * nValue, pFieldDescriptor))
475 Value = llrp_s64v_t(nValue);
476 if(verifyVectorAllocation(Value.m_pValue, pFieldDescriptor))
478 for(
unsigned int Ix = 0; Ix < nValue; Ix++)
480 Value.m_pValue[Ix] = m_pDecoder->next_u64();
490 CFrameDecoderStream::get_u1 (
491 const CFieldDescriptor * pFieldDescriptor)
495 Value = getBitField(1, pFieldDescriptor);
501 CFrameDecoderStream::get_u1v (
502 const CFieldDescriptor * pFieldDescriptor)
507 nBit = getVarlenCount(pFieldDescriptor);
511 unsigned int nByte = (nBit + 7u) / 8u;
513 if(checkAvailable(nByte, pFieldDescriptor))
515 Value = llrp_u1v_t(nBit);
516 if(verifyVectorAllocation(Value.m_pValue, pFieldDescriptor))
518 for(
unsigned int Ix = 0; Ix < nByte; Ix++)
520 Value.m_pValue[Ix] = m_pDecoder->next_u8();
530 CFrameDecoderStream::get_u2 (
531 const CFieldDescriptor * pFieldDescriptor)
535 Value = getBitField(2, pFieldDescriptor);
541 CFrameDecoderStream::get_u96 (
542 const CFieldDescriptor * pFieldDescriptor)
546 if(checkAvailable(12u, pFieldDescriptor))
548 for(
unsigned int Ix = 0; Ix < 12u; Ix++)
550 Value.m_aValue[Ix] = m_pDecoder->next_u8();
558 CFrameDecoderStream::get_utf8v (
559 const CFieldDescriptor * pFieldDescriptor)
564 nValue = getVarlenCount(pFieldDescriptor);
568 if(checkAvailable(1u * nValue, pFieldDescriptor))
570 Value = llrp_utf8v_t(nValue);
571 if(verifyVectorAllocation(Value.m_pValue, pFieldDescriptor))
573 for(
unsigned int Ix = 0; Ix < nValue; Ix++)
575 Value.m_pValue[Ix] = m_pDecoder->next_u8();
585 CFrameDecoderStream::get_bytesToEnd (
586 const CFieldDescriptor * pFieldDescriptor)
589 llrp_bytesToEnd_t Value;
591 nValue = getRemainingByteCount();
595 if(checkAvailable(1u * nValue, pFieldDescriptor))
597 Value = llrp_bytesToEnd_t(nValue);
598 if(verifyVectorAllocation(Value.m_pValue, pFieldDescriptor))
600 for(
unsigned int Ix = 0; Ix < nValue; Ix++)
602 Value.m_pValue[Ix] = m_pDecoder->next_u8();
612 CFrameDecoderStream::get_e1 (
613 const CFieldDescriptor * pFieldDescriptor)
617 eValue = (int)get_u1(pFieldDescriptor);
623 CFrameDecoderStream::get_e2 (
624 const CFieldDescriptor * pFieldDescriptor)
628 eValue = (int)get_u2(pFieldDescriptor);
634 CFrameDecoderStream::get_e8 (
635 const CFieldDescriptor * pFieldDescriptor)
639 eValue = (int)get_u8(pFieldDescriptor);
645 CFrameDecoderStream::get_e16 (
646 const CFieldDescriptor * pFieldDescriptor)
650 eValue = (int)get_u16(pFieldDescriptor);
656 CFrameDecoderStream::get_e32 (
657 const CFieldDescriptor * pFieldDescriptor)
661 eValue = (int)get_u32(pFieldDescriptor);
667 CFrameDecoderStream::get_e8v (
668 const CFieldDescriptor * pFieldDescriptor)
670 return get_u8v(pFieldDescriptor);
674 CFrameDecoderStream::get_reserved (
677 CErrorDetails * pError = &m_pDecoder->m_ErrorDetails;
679 if(
RC_OK != pError->m_eResultCode)
686 unsigned int Step = 7u & nBit;
688 if(0 != m_pDecoder->m_nBitFieldResid)
690 if(Step != m_pDecoder->m_nBitFieldResid)
693 pError->m_pWhatStr =
"unaligned reserved bits";
694 pError->m_pRefType = m_pRefType;
695 pError->m_pRefField = NULL;
696 pError->m_OtherDetail = m_pDecoder->m_iNext;
701 m_pDecoder->m_nBitFieldResid = 0;
708 pError->m_pWhatStr =
"unaligned reserved bits";
709 pError->m_pRefType = m_pRefType;
710 pError->m_pRefField = NULL;
711 pError->m_OtherDetail = m_pDecoder->m_iNext;
715 if(m_pDecoder->m_iNext >= m_iLimit)
718 pError->m_pWhatStr =
"underrun at reserved bits";
719 pError->m_pRefType = m_pRefType;
720 pError->m_pRefField = NULL;
721 pError->m_OtherDetail = m_pDecoder->m_iNext;
725 m_pDecoder->next_u8();
731 CFrameDecoderStream::CFrameDecoderStream (
732 CFrameDecoder * pDecoder)
734 m_pDecoder = pDecoder;
735 m_pEnclosingDecoderStream = NULL;
736 m_iBegin = pDecoder->m_iNext;
737 m_iLimit = pDecoder->m_nBuffer;
741 CFrameDecoderStream::CFrameDecoderStream (
742 CFrameDecoderStream * pEnclosingDecoderStream)
744 m_pDecoder = pEnclosingDecoderStream->m_pDecoder;
745 m_pEnclosingDecoderStream = pEnclosingDecoderStream;
746 m_iBegin = m_pDecoder->m_iNext;
747 m_iLimit = pEnclosingDecoderStream->m_iLimit;
752 CFrameDecoderStream::getMessage (
void)
754 CErrorDetails * pError = &m_pDecoder->m_ErrorDetails;
755 const CTypeRegistry * pRegistry = m_pDecoder->m_pRegistry;
756 const CTypeDescriptor * pTypeDescriptor = NULL;
761 llrp_u32_t MessageID;
763 if(
RC_OK != pError->m_eResultCode)
768 Type = get_u16(&g_fdMessageHeader_Type);
769 Vers = (Type >> 10) & 3;
772 if(
RC_OK != pError->m_eResultCode)
780 pError->m_pWhatStr =
"unsupported version";
781 pError->m_pRefType = NULL;
782 pError->m_pRefField = &g_fdMessageHeader_Type;
783 pError->m_OtherDetail = m_pDecoder->m_iNext;
787 nLength = get_u32(&g_fdMessageHeader_Length);
789 if(
RC_OK != pError->m_eResultCode)
797 pError->m_pWhatStr =
"message length too small";
798 pError->m_pRefType = NULL;
799 pError->m_pRefField = &g_fdMessageHeader_Length;
800 pError->m_OtherDetail = m_pDecoder->m_iNext;
804 iLimit = m_iBegin + nLength;
806 if(iLimit > m_iLimit)
809 pError->m_pWhatStr =
"message length exceeds enclosing length";
810 pError->m_pRefType = NULL;
811 pError->m_pRefField = &g_fdMessageHeader_Length;
812 pError->m_OtherDetail = m_pDecoder->m_iNext;
818 MessageID = get_u32(&g_fdMessageHeader_MessageID);
820 if(
RC_OK != pError->m_eResultCode)
828 llrp_u32_t VendorPEN;
831 VendorPEN = get_u32(&g_fdMessageHeader_VendorPEN);
832 Subtype = get_u8(&g_fdMessageHeader_Subtype);
834 if(
RC_OK != pError->m_eResultCode)
839 pTypeDescriptor = pRegistry->lookupCustomMessage(VendorPEN, Subtype);
840 if(NULL == pTypeDescriptor)
846 m_pDecoder->m_iNext -= 5;
847 pTypeDescriptor = pRegistry->lookupMessage(1023u);
852 pTypeDescriptor = pRegistry->lookupMessage(Type);
855 if(NULL == pTypeDescriptor)
858 pError->m_pWhatStr =
"unknown message type";
859 pError->m_pRefType = NULL;
860 pError->m_pRefField = &g_fdMessageHeader_Type;
861 pError->m_OtherDetail = 0;
865 m_pRefType = pTypeDescriptor;
869 pMessage = (CMessage *) pTypeDescriptor->constructElement();
874 pError->m_pWhatStr =
"message allocation failed";
875 pError->m_pRefType = pTypeDescriptor;
876 pError->m_pRefField = NULL;
877 pError->m_OtherDetail = m_pDecoder->m_iNext;
881 pMessage->setMessageID(MessageID);
883 pMessage->decodeFields(
this);
885 if(
RC_OK != pError->m_eResultCode)
894 while(0 < getRemainingByteCount() &&
895 RC_OK == pError->m_eResultCode)
897 CFrameDecoderStream NestStream(
this);
898 CParameter * pParameter;
900 pParameter = NestStream.getParameter();
902 if(NULL == pParameter)
904 if(
RC_OK == pError->m_eResultCode)
907 pError->m_pWhatStr =
"botch -- no param and no error";
908 pError->m_pRefType = pTypeDescriptor;
909 pError->m_pRefField = NULL;
910 pError->m_OtherDetail = m_pDecoder->m_iNext;
915 pParameter->m_pParent = pMessage;
916 pMessage->addSubParameterToAllList(pParameter);
920 if(
RC_OK == pError->m_eResultCode)
922 if(m_pDecoder->m_iNext != m_iLimit)
925 pError->m_pWhatStr =
"extra bytes at end of message";
926 pError->m_pRefType = pTypeDescriptor;
927 pError->m_pRefField = NULL;
928 pError->m_OtherDetail = m_pDecoder->m_iNext;
932 if(
RC_OK != pError->m_eResultCode)
938 pMessage->assimilateSubParameters(pError);
940 if(
RC_OK != pError->m_eResultCode)
950 CFrameDecoderStream::getParameter (
void)
952 CErrorDetails * pError = &m_pDecoder->m_ErrorDetails;
953 const CTypeRegistry * pRegistry = m_pDecoder->m_pRegistry;
954 const CTypeDescriptor * pTypeDescriptor = NULL;
958 if(
RC_OK != pError->m_eResultCode)
963 Type = get_u8(&g_fdParameterHeader_TVType);
965 if(
RC_OK != pError->m_eResultCode)
987 m_pDecoder->m_iNext--;
988 Type = get_u16(&g_fdParameterHeader_TLVType);
991 if(
RC_OK != pError->m_eResultCode)
998 nLength = get_u16(&g_fdParameterHeader_TLVLength);
1000 if(
RC_OK != pError->m_eResultCode)
1008 pError->m_pWhatStr =
"TLV parameter length too small";
1009 pError->m_pRefType = NULL;
1010 pError->m_pRefField = &g_fdParameterHeader_TLVLength;
1011 pError->m_OtherDetail = m_pDecoder->m_iNext;
1015 unsigned int iLimit;
1017 iLimit = m_iBegin + nLength;
1019 if(iLimit > m_iLimit)
1022 pError->m_pWhatStr =
1023 "TLV parameter length exceeds enclosing length";
1024 pError->m_pRefType = NULL;
1025 pError->m_pRefField = &g_fdParameterHeader_TLVLength;
1026 pError->m_OtherDetail = m_pDecoder->m_iNext;
1038 llrp_u32_t VendorPEN;
1041 VendorPEN = get_u32(&g_fdParameterHeader_VendorPEN);
1042 Subtype = get_u32(&g_fdParameterHeader_Subtype);
1044 if(
RC_OK != pError->m_eResultCode)
1049 pTypeDescriptor = pRegistry->lookupCustomParameter(VendorPEN, Subtype);
1050 if(NULL == pTypeDescriptor)
1056 m_pDecoder->m_iNext -= 8;
1057 pTypeDescriptor = pRegistry->lookupParameter(1023u);
1062 pTypeDescriptor = pRegistry->lookupParameter(Type);
1065 if(NULL == pTypeDescriptor)
1068 pError->m_pWhatStr =
"unknown parameter type";
1069 pError->m_pRefType = NULL;
1072 pError->m_pRefField = &g_fdParameterHeader_TVType;
1076 pError->m_pRefField = &g_fdParameterHeader_TLVType;
1078 pError->m_OtherDetail = m_pDecoder->m_iNext;
1082 m_pRefType = pTypeDescriptor;
1084 CParameter * pParameter;
1086 pParameter = (CParameter *) pTypeDescriptor->constructElement();
1088 if(NULL == pParameter)
1091 pError->m_pWhatStr =
"parameter allocation failed";
1092 pError->m_pRefType = pTypeDescriptor;
1093 pError->m_pRefField = NULL;
1094 pError->m_OtherDetail = m_pDecoder->m_iNext;
1098 pParameter->decodeFields(
this);
1100 if(
RC_OK != pError->m_eResultCode)
1111 while(0 < getRemainingByteCount() &&
1112 RC_OK == pError->m_eResultCode)
1114 CFrameDecoderStream NestStream(
this);
1115 CParameter * pSubParameter;
1117 pSubParameter = NestStream.getParameter();
1119 if(NULL == pSubParameter)
1121 if(
RC_OK == pError->m_eResultCode)
1124 pError->m_pWhatStr =
"botch -- no param and no error";
1125 pError->m_pRefType = pTypeDescriptor;
1126 pError->m_pRefField = NULL;
1127 pError->m_OtherDetail = m_pDecoder->m_iNext;
1132 pSubParameter->m_pParent = pParameter;
1133 pParameter->addSubParameterToAllList(pSubParameter);
1136 if(
RC_OK == pError->m_eResultCode)
1138 if(m_pDecoder->m_iNext != m_iLimit)
1141 pError->m_pWhatStr =
"extra bytes at end of TLV parameter";
1142 pError->m_pRefType = pTypeDescriptor;
1143 pError->m_pRefField = NULL;
1144 pError->m_OtherDetail = m_pDecoder->m_iNext;
1148 if(
RC_OK != pError->m_eResultCode)
1154 pParameter->assimilateSubParameters(pError);
1156 if(
RC_OK != pError->m_eResultCode)
1167 CFrameDecoderStream::getRemainingByteCount (
void)
1169 if(m_pDecoder->m_iNext < m_iLimit)
1171 return m_iLimit - m_pDecoder->m_iNext;
1180 CFrameDecoderStream::checkAvailable (
1182 const CFieldDescriptor * pFieldDescriptor)
1184 CErrorDetails * pError = &m_pDecoder->m_ErrorDetails;
1186 if(
RC_OK != pError->m_eResultCode)
1191 if(m_pDecoder->m_iNext + nByte > m_iLimit)
1194 pError->m_pWhatStr =
"underrun at field";
1195 pError->m_pRefType = m_pRefType;
1196 pError->m_pRefField = pFieldDescriptor;
1197 pError->m_OtherDetail = m_pDecoder->m_iNext;
1201 if(0 != m_pDecoder->m_nBitFieldResid)
1204 pError->m_pWhatStr =
"unaligned/incomplete bit field";
1205 pError->m_pRefType = m_pRefType;
1206 pError->m_pRefField = pFieldDescriptor;
1207 pError->m_OtherDetail = m_pDecoder->m_iNext;
1215 CFrameDecoderStream::getBitField (
1217 const CFieldDescriptor * pFieldDescriptor)
1219 CErrorDetails * pError = &m_pDecoder->m_ErrorDetails;
1222 if(0 == m_pDecoder->m_nBitFieldResid)
1224 if(checkAvailable(1u, pFieldDescriptor))
1226 m_pDecoder->m_BitFieldBuffer = m_pDecoder->next_u8();
1227 m_pDecoder->m_nBitFieldResid = 8u;
1235 if(m_pDecoder->m_nBitFieldResid < nBit)
1238 pError->m_pWhatStr =
"unaligned/incomplete bit field";
1239 pError->m_pRefType = m_pRefType;
1240 pError->m_pRefField = pFieldDescriptor;
1241 pError->m_OtherDetail = m_pDecoder->m_iNext;
1245 m_pDecoder->m_nBitFieldResid -= nBit;
1247 Value = m_pDecoder->m_BitFieldBuffer >> m_pDecoder->m_nBitFieldResid;
1248 Value &= (1u << nBit) - 1u;
1254 CFrameDecoderStream::getVarlenCount (
1255 const CFieldDescriptor * pFieldDescriptor)
1259 if(checkAvailable(2u, pFieldDescriptor))
1261 nValue = m_pDecoder->next_u16();
1272 CFrameDecoderStream::verifyVectorAllocation (
1273 const void * pValue,
1274 const CFieldDescriptor * pFieldDescriptor)
1278 CErrorDetails * pError = &m_pDecoder->m_ErrorDetails;
1281 pError->m_pWhatStr =
"field allocation failed";
1282 pError->m_pRefType = m_pRefType;
1283 pError->m_pRefField = pFieldDescriptor;
1284 pError->m_OtherDetail = m_pDecoder->m_iNext;
Based type descriptions for the LTKCPP library.
Classes to encode and decod LLRP binary frames.