{-------------------------------------------------------------------------- DATA ENCODING: The magic values are $EA, $EB, $EC, $ED. $EA + char + byte = insert [byte] [char]:s to output. $EB + char + lobyte + hibyte = insert [address of( lobyte + hibyte )] [char]:s to output ( if # of chars is greater than 255 ) $EC = eof $ED + [ $EA, $EB, $EC, $ED ] = insert command char to output. --------------------------------------------------------------------------} program rllencode; var infile: file of byte; outfile: file of byte; b, b1 : byte; bytecnt: LongInt; f: Text; function HowManyLikeBytesInARow( bByte: Byte ): LongInt; var fp : longint; b : Byte; counter : longint; begin counter := 0; b := bByte; fp := filesize( infile ); while ( b = bByte ) and ( FileSize( infile ) <> FilePos( infile ) ) do begin fp := FilePos( infile ); Read( infile, b ); inc( counter ); end; { if ( b <> bByte ) then} Seek( infile, fp ); if counter < 1 then counter := 1; if counter > 2 then writeln( f, counter ); HowManyLikeBytesInARow := counter; end; begin if ParamCount <> 2 then halt; Assign( infile, ParamStr( 1 ) ); Assign( outfile, ParamStr( 2 ) ); Assign( f, 'len2.txt' ); reset( infile ); rewrite( outfile ); rewrite( f ); while ( filepos( infile ) <> filesize( infile ) ) do begin Read( infile, b ); bytecnt := HowManyLikeBytesInARow( b ); if ( bytecnt = 1 ) then begin if ( b = $ea ) or ( b = $eb ) or ( b = $ec ) or ( b = $ed ) then begin b1 := $ed; write( outfile, b1 ); write( outfile, b ); end else begin Write( outfile, b ); end; end; if bytecnt = 2 then begin if ( ( b <> $ea ) and ( b <> $eb ) and ( b <> $ec ) and ( b <> $ed ) ) then begin write( outfile, b ); write( outfile, b ); end; if ( ( b = $ea ) or ( b = $eb ) or ( b = $ec ) or ( b = $ed ) ) then begin b1 := $ea; write( outfile, b1 ); write( outfile, b ); b1 := 2; write( outfile, b1 ); end end; if bytecnt > 2 then begin if ( bytecnt < 256 ) then begin b1 := $ea; write( outfile, b1 ); write( outfile, b ); b1 := bytecnt; write( outfile, b1 ); end else begin b1 := $eb; write( outfile, b1 ); write( outfile, b ); b1 := bytecnt mod 256; write( outfile, b1 ); b1 := Trunc( bytecnt / 256); write( outfile, b1 ); end; end; end; {while} close( infile ); b1 := $ec; write( outfile, b1 ); close( outfile ); close( f ); end.