@@ -767,6 +767,10 @@ bool CDLTFileWrapper::CSubFilesHandler::CDLTFileItem::updateIndex()
767767
768768 QByteArray buf;
769769 qint64 pos = 0 ;
770+ quint16 last_message_length = 0 ;
771+ quint8 version=1 ;
772+ qint64 lengthOffset=2 ;
773+ qint64 storageLength=0 ;
770774
771775 /* open file */
772776 if (false == mInfile .isOpen ())
@@ -784,7 +788,39 @@ bool CDLTFileWrapper::CSubFilesHandler::CDLTFileItem::updateIndex()
784788 {
785789 /* move behind last found position */
786790 const QVector<qint64>* const_indexAll = &(mIndexAll );
787- pos = (*const_indexAll)[mIndexAll .size ()-1 ] + 4 ;
791+ pos = (*const_indexAll)[mIndexAll .size ()-1 ];
792+
793+ // first move to beginnng of last found message
794+ mInfile .seek (pos);
795+
796+ // read and get file storage length
797+ buf = mInfile .read (14 );
798+ if (((unsigned char )buf.at (3 ))==2 )
799+ {
800+ storageLength = 14 + ((unsigned char )buf.at (13 ));
801+ }
802+ else
803+ {
804+ storageLength = 16 ;
805+ }
806+
807+ // read and get last message length
808+ mInfile .seek (pos + storageLength);
809+ buf = mInfile .read (7 );
810+ version = (((unsigned char )buf.at (0 ))&0xe0 )>>5 ;
811+ if (version==2 )
812+ {
813+ lengthOffset = 5 ;
814+ }
815+ else
816+ {
817+ lengthOffset = 2 ; // default
818+ }
819+ last_message_length = (unsigned char )buf.at (lengthOffset); // was 0
820+ last_message_length = (last_message_length<<8 | ((unsigned char )buf.at (lengthOffset+1 ))) + storageLength; // was 1
821+
822+ // move just behind the next expected message
823+ pos += (last_message_length - 1 );
788824 mInfile .seek (pos);
789825 }
790826 else {
@@ -807,6 +843,7 @@ bool CDLTFileWrapper::CSubFilesHandler::CDLTFileItem::updateIndex()
807843
808844 while (true )
809845 {
846+
810847 /* read buffer from file */
811848 buf = mInfile .read (READ_BUF_SZ);
812849 if (buf.isEmpty ())
@@ -822,16 +859,37 @@ bool CDLTFileWrapper::CSubFilesHandler::CDLTFileItem::updateIndex()
822859 if (counter_header>0 )
823860 {
824861 counter_header++;
825- if (counter_header==16 )
862+ if (storageLength==13 && counter_header==13 )
863+ {
864+ storageLength += ((unsigned char )cbuf[num]) + 1 ;
865+ }
866+ else if (counter_header==storageLength)
867+ {
868+ // Read DLT protocol version
869+ version = (((unsigned char )cbuf[num])&0xe0 )>>5 ;
870+ if (version==1 )
871+ {
872+ lengthOffset = 2 ;
873+ }
874+ else if (version==2 )
875+ {
876+ lengthOffset = 5 ;
877+ }
878+ else
879+ {
880+ lengthOffset = 2 ; // default
881+ }
882+ }
883+ else if (counter_header==(storageLength+lengthOffset)) // was 16
826884 {
827885 // Read low byte of message length
828- message_length = static_cast < unsigned char >( cbuf[num]) ;
886+ message_length = ( unsigned char ) cbuf[num];
829887 }
830- else if (counter_header==17 )
888+ else if (counter_header==(storageLength+ 1 +lengthOffset)) // was 17
831889 {
832890 // Read high byte of message length
833891 counter_header = 0 ;
834- message_length = static_cast <quint16>(( message_length<<8 | (static_cast < unsigned char >( cbuf[num]))) + 16 ) ;
892+ message_length = ( message_length<<8 | (( unsigned char ) cbuf[num])) + storageLength ;
835893 next_message_pos = current_message_pos + message_length;
836894 if (next_message_pos==file_size)
837895 {
@@ -840,11 +898,11 @@ bool CDLTFileWrapper::CSubFilesHandler::CDLTFileItem::updateIndex()
840898 break ;
841899 }
842900 // speed up move directly to next message, if inside current buffer
843- if ((message_length > 20 ))
901+ if ((message_length > storageLength+ 2 +lengthOffset)) // was 20
844902 {
845- if ((num+message_length-20 <cbuf_sz))
903+ if ((num+message_length-(storageLength+ 2 +lengthOffset) <cbuf_sz)) // was 20
846904 {
847- num+=message_length-20 ;
905+ num+=message_length-(storageLength+ 2 +lengthOffset); // was 20
848906 }
849907 }
850908 }
@@ -861,36 +919,44 @@ bool CDLTFileWrapper::CSubFilesHandler::CDLTFileItem::updateIndex()
861919 {
862920 lastFound = ' T' ;
863921 }
864- else if (lastFound == ' T' && cbuf[num] == 0x01 )
922+ else if (lastFound == ' T' && ( cbuf[num] == 0x01 || cbuf[num] == 0x02 ) )
865923 {
866924 if (next_message_pos == 0 )
867925 {
868926 // first message detected or first message after error
869927 current_message_pos = pos+num-3 ;
870- counter_header = 1 ;
928+ counter_header = 3 ;
929+ if (cbuf[num] == 0x01 )
930+ storageLength = 16 ;
931+ else
932+ storageLength = 13 ;
871933 if (current_message_pos!=0 )
872934 {
873935 // first messages not at beginning or error occured before
874936 errors_in_file++;
875937 }
876938 // speed up move directly to message length, if inside current buffer
877- if (num+14 <cbuf_sz)
939+ if (num+9 <cbuf_sz)
878940 {
879- num+=14 ;
880- counter_header+=14 ;
941+ num+=9 ;
942+ counter_header+=9 ;
881943 }
882944 }
883945 else if ( next_message_pos == (pos+num-3 ) )
884946 {
885947 // Add message only when it is in the correct position in relationship to the last message
886948 mIndexAll .append (current_message_pos);
887949 current_message_pos = pos+num-3 ;
888- counter_header = 1 ;
950+ counter_header = 3 ;
951+ if (cbuf[num] == 0x01 )
952+ storageLength = 16 ;
953+ else
954+ storageLength = 13 ;
889955 // speed up move directly to message length, if inside current buffer
890- if (num+14 <cbuf_sz)
956+ if (num+9 <cbuf_sz)
891957 {
892- num+=14 ;
893- counter_header+=14 ;
958+ num+=9 ;
959+ counter_header+=9 ;
894960 }
895961 }
896962 else if (next_message_pos > (pos+num-3 ))
@@ -920,7 +986,6 @@ bool CDLTFileWrapper::CSubFilesHandler::CDLTFileItem::updateIndex()
920986 }
921987
922988 mInfile .close ();
923-
924989 bResult = true ;
925990 }
926991 }
0 commit comments