From 623ea066c74e741b5dcb37b91b9ddbcecaa53416 Mon Sep 17 00:00:00 2001
From: nickysn <nickysn@gmail.com>
Date: Sun, 19 Apr 2015 11:25:25 +0000
Subject: [PATCH] + added class for encoding/decoding an omf library end record

git-svn-id: trunk@30660 -
---
 compiler/omfbase.pas | 43 +++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 43 insertions(+)

diff --git a/compiler/omfbase.pas b/compiler/omfbase.pas
index 5a2bb0f8a8..de39ec914e 100644
--- a/compiler/omfbase.pas
+++ b/compiler/omfbase.pas
@@ -72,6 +72,7 @@ interface
     RT_VERNUM    = $CC;  { OMF Version Number Record }
     RT_VENDEXT   = $CE;  { Vendor-specific OMF Extension Record }
     RT_LIBHEAD   = $F0;  { Library Header Record }
+    RT_LIBEND    = $F1;  { Library End Record (marks end of objects and beginning of dictionary) }
 
     { OMF comment class }
     CC_Translator               = $00; { language translator (compiler or assembler) name }
@@ -486,6 +487,19 @@ interface
       property CaseSensitive: Boolean read IsCaseSensitive write SetCaseSensitive;
     end;
 
+    { TOmfRecord_LIBEND }
+
+    TOmfRecord_LIBEND = class(TOmfParsedRecord)
+    private
+      FPaddingBytes: Word;
+    public
+      procedure DecodeFrom(RawRecord: TOmfRawRecord);override;
+      procedure EncodeTo(RawRecord: TOmfRawRecord);override;
+
+      procedure CalculatePaddingBytes(RecordStartOffset: DWord);
+      property PaddingBytes: Word read FPaddingBytes write FPaddingBytes;
+    end;
+
     TOmfLibHash = record
       block_x: Integer;
       block_d: Integer;
@@ -1494,6 +1508,35 @@ implementation
       FFlags:=(FFlags and $FE) or Ord(AValue);
     end;
 
+  { TOmfRecord_LIBEND }
+
+  procedure TOmfRecord_LIBEND.DecodeFrom(RawRecord: TOmfRawRecord);
+    begin
+      if RawRecord.RecordType<>RT_LIBEND then
+        internalerror(2015040301);
+      FPaddingBytes:=RawRecord.RecordLength;
+    end;
+
+  procedure TOmfRecord_LIBEND.EncodeTo(RawRecord: TOmfRawRecord);
+    begin
+      { make sure the LIBEND record is padded with zeros at the end }
+      FillChar(RawRecord.RawData,SizeOf(RawRecord.RawData),0);
+      RawRecord.RecordType:=RT_LIBEND;
+      RawRecord.RecordLength:=FPaddingBytes;
+      { the LIBEND record contains no checksum byte, so no need to call
+        RawRecord.CalculateChecksumByte }
+    end;
+
+  procedure TOmfRecord_LIBEND.CalculatePaddingBytes(RecordStartOffset: DWord);
+    var
+      DictionaryStartOffset: Integer;
+    begin
+      { padding must be calculated, so that the dictionary begins on a 512-byte boundary }
+      Inc(RecordStartOffset,3);  // padding begins _after_ the record header (3 bytes)
+      DictionaryStartOffset:=(RecordStartOffset+511) and $fffffe00;
+      PaddingBytes:=DictionaryStartOffset-RecordStartOffset;
+    end;
+
   function compute_omf_lib_hash(const name: string; blocks: Integer): TOmfLibHash;
     const
       blank=$20;  // ASCII blank