Browse Source

+ added methods for calculating and verifying the omf record checksum and
properties for accessing it

git-svn-id: trunk@30345 -

nickysn 10 years ago
parent
commit
131422b594
1 changed files with 46 additions and 0 deletions
  1. 46 0
      compiler/omfbase.pas

+ 46 - 0
compiler/omfbase.pas

@@ -77,8 +77,10 @@ interface
 
     TOmfRawRecord = class
     private
+      function GetChecksumByte: Byte;
       function GetRecordLength: Word;
       function GetRecordType: Byte;
+      procedure SetChecksumByte(AValue: Byte);
       procedure SetRecordLength(AValue: Word);
       procedure SetRecordType(AValue: Byte);
     public
@@ -86,6 +88,10 @@ interface
       property RecordType: Byte read GetRecordType write SetRecordType;
       property RecordLength: Word read GetRecordLength write SetRecordLength;
 
+      procedure CalculateChecksumByte;
+      function VerifyChecksumByte: boolean;
+      property ChecksumByte: Byte read GetChecksumByte write SetChecksumByte;
+
       procedure ReadFrom(aReader: TObjectReader);
       procedure WriteTo(aWriter: TObjectWriter);
     end;
@@ -115,6 +121,46 @@ implementation
       RawData[-1]:=Byte(AValue shr 8);
     end;
 
+  function TOmfRawRecord.GetChecksumByte: Byte;
+    begin
+      if RecordLength>0 then
+        Result:=RawData[RecordLength-1]
+      else
+        Result:=0;
+    end;
+
+  procedure TOmfRawRecord.SetChecksumByte(AValue: Byte);
+    begin
+      if RecordLength>0 then
+        RawData[RecordLength-1]:=AValue;
+    end;
+
+  procedure TOmfRawRecord.CalculateChecksumByte;
+    var
+      I: Integer;
+      b: Byte;
+    begin
+      b:=0;
+      for I:=-3 to RecordLength-2 do
+        b:=byte(b+RawData[I]);
+      SetChecksumByte($100-b);
+    end;
+
+  function TOmfRawRecord.VerifyChecksumByte: boolean;
+    var
+      I: Integer;
+      b: Byte;
+    begin
+      { according to the OMF spec, some tools always write a 0 rather than
+        computing the checksum, so it should also be accepted as correct }
+      if ChecksumByte=0 then
+        exit(true);
+      b:=0;
+      for I:=-3 to RecordLength-1 do
+        b:=byte(b+RawData[I]);
+      Result:=(b=0);
+    end;
+
   procedure TOmfRawRecord.ReadFrom(aReader: TObjectReader);
     begin
       aReader.read(RawData, 3);