Checking File Preambles and Watermarks
I'm constantly having to check the preambles of files for some kind of signature (or "watermark") to check or validate the file's format. I've re-invented the wheel many times over while doing this. Time, I thought, for a little helper routine or two. First, a little routine to check the next N bytes from a stream for a given sequence of bytes (the "watermark"). function StreamHasWatermark ( const Stm : TStream ; const Watermark : array of Byte ) : Boolean ; var StmPos : Int64 ; Buf : array of Byte ; I : Integer ; begin Assert ( Length ( Watermark ) > 0 , 'No "watermark" specified' ) ; Result := False ; StmPos := Stm . Position ; try if Stm . Size - StmPos < Length ( Watermark ) then Exit ; SetLength ( Buf , Length ( Watermark ) ) ; Stm . ReadBuffer ( Pointer ( Buf ) ^ , Length ( Buf ) ) ; for I := Low ( Buf ) to High ( Buf ) do if Buf [ I ] <> ...