unit EncodStr;
interfaceuses
Classes;
type
TEncodedStream = class (TFileStream)
private
FKey: Char;
publicconstructor Create(const FileName: string; Mode: Word);
functionRead(var Buffer; Count: Longint): Longint; override;
functionWrite(const Buffer; Count: Longint): Longint; override;
property Key: Char read FKey write FKey default "A";
end;
implementationconstructor TEncodedStream.Create(
const FileName: string; Mode: Word);
begininherited Create (FileName, Mode);
FKey := "A";
end;
function TEncodedStream.Write(const Buffer;
Count: Longint): Longint;
var
pBuf, pEnc: PChar;
I, EncVal: Integer;
begin// allocate memory for the encoded buffer
GetMem (pEnc, Count);
try// use the buffer as an array of characters
pBuf := PChar (@Buffer);
// for every character of the bufferfor I := 0 to Count - 1 dobegin// encode the value and store it
EncVal := ( Ord (pBuf[I]) + Ord(Key) ) mod 256;
pEnc [I] := Chr (EncVal);
end;
// write the encoded buffer to the file
Result := inheritedWrite (pEnc^, Count);
finally
FreeMem (pEnc, Count);
end;
end;
function TEncodedStream.Read(var Buffer; Count: Longint): Longint;
var
pBuf, pEnc: PChar;
I, CountRead, EncVal: Integer;
begin// allocate memory for the encoded buffer
GetMem (pEnc, Count);
try// read the encoded buffer from the file
CountRead := inheritedRead (pEnc^, Count);
// use the output buffer as a string
pBuf := PChar (@Buffer);
// for every character actually readfor I := 0 to CountRead - 1 dobegin// decode the value and store it
EncVal := ( Ord (pEnc[I]) - Ord(Key) ) mod 256;
pBuf [I] := Chr (EncVal);
end;
finally
FreeMem (pEnc, Count);
end;
// return the number of characters read
Result := CountRead;
end;
end.
Если Вас заинтересовала или понравилась информация по разработке на Delph - "Кодирование файлов", Вы можете поставить закладку в социальной сети или в своём блоге на данную страницу: Так же Вы можете задать вопрос по работе этого модуля или примера через форму обратной связи, в сообщение обязательно указывайте название или ссылку на статью!