SUBROUTINE ReadBuf(Lu,Cnt) c Routine to read one record from a file. c Since file is serial w/o record delimiters, need to c read the header to find record length, then read the rest c of the record. c Returns the buffer and count in 16 bit words IMPLICIT none INCLUDE "FastCom.ftni" C Passed parameters INTEGER Lu, Cnt INTEGER*1 Buf(MaxSizeB) EQUIVALENCE (IB, Buf) ! so we can work with bytes instead of words C Local storage INTEGER*4 ios, isize, idx, CkSum, i INTEGER*1 TempByte idx = 1 ! fill location in Buf Buf(1) = 0 ! as we enter into loop 10 DO WHILE (Buf(1).NE.2) ! look for correct start byte READ(Lu,rec=RecNum_com,Err=999,IoStat=ios) Buf(1) IF(ios.LT.0) GOTO 1000 ! test for EOF RecNum_com = RecNum_com + 1 ! bump file pointer c IF (Buf(1).NE.2) WRITE(*,'("Bad Byte:"i3)') Buf(1) !asgg diag END DO C found a potential start byte, read the next for acft # READ(Lu,rec=RecNum_com,Err=999,IoStat=ios) Buf(2) IF(ios.LT.0) GOTO 1000 ! test for EOF RecNum_com = RecNum_com + 1 IF (Buf(2).NE.42 .AND. Buf(2).NE.43) THEN ! bad acft # WRITE(*,'("Bad acft:"i3)') Buf(2) Buf(1) = Buf(2) ! in case it was a '2' GOTO 10 ! keep looking ENDIF C Next two bytes give buffer size in 16 bit words READ(Lu,rec=RecNum_com,Err=999,IoStat=ios) Buf(3) IF(ios.LT.0) GOTO 1000 ! test for EOF RecNum_com = RecNum_com + 1 READ(Lu,rec=RecNum_com,Err=999,IoStat=ios) Buf(4) IF(ios.LT.0) GOTO 1000 ! test for EOF RecNum_com = RecNum_com + 1 isize = IAnd(Buf(3),255)*256 + IAnd(Buf(4),255) IF (isize.LT.105 .OR. isize.GT.MaxSize) THEN WRITE(*,'("Bad Size:"i6)') isize Buf(1) = Buf(4) ! (3) can't be a '2' or wouldn't fail GOTO 10 ENDIF C Now we know how many more words to read - do it all DO idx = 5,isize*2 ! because we're reading bytes READ(Lu,rec=RecNum_com,Err=999,IoStat=ios) Buf(idx) IF(ios.LT.0) GOTO 1000 ! test for EOF RecNum_com = RecNum_com + 1 END DO C do the checksum CkSum = 0 DO idx = 1,isize*2-3,2 ! handle 2 bytes at a time CkSum = Cksum + & IAnd(Buf(idx),255)*256 + IAnd(Buf(idx+1),255) END DO CkSum = IAnd(CkSum,65535) ! mask to ls 16 bits idx = IAnd(Buf(isize*2-1),255)*256 + IAnd(Buf(isize*2),255) ! last 16 word is cksum IF (CkSum.NE.idx) THEN WRITE(*,'("CkSum Err - calc:"i6" expect:"i6)') CkSum,idx Buf(1) = 0 ! throw it way & look for next sec GOTO 10 ENDIF C All tests passed, swap bytes for Intel format DO idx = 1,isize*2-1,2 TempByte = Buf(idx) Buf(idx) = Buf(idx+1) Buf(idx+1) = TempByte END DO Cnt = isize ! return the length in 16 bit words RETURN ! done reading & checking a record 999 WRITE(*,'("Read Err:"i6)') ios Cnt = 0 ! don't pass anything back RETURN 1000 WRITE(*,'("Found EOF")') Cnt = 0 RETURN END