DEADSOFTWARE

fix network on big-endian machines
[d2df-sdl.git] / src / engine / e_msg.pas
index a2ecc52e3e9a40e6a2c58dede4d26344e35847b3..f5711ec3079d6b46f7aa38d9a2b5b28b2ebe164c 100644 (file)
@@ -2,8 +2,7 @@
  *
  * This program is free software: you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
+ * the Free Software Foundation, version 3 of the License ONLY.
  *
  * This program is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
@@ -28,6 +27,7 @@ type
     CurSize: Integer;
     ReadCount: Integer;
     Bit: Integer;
+    AllocStep: Integer;
     OwnMemory: Boolean;
 
     function Init(V: Pointer; N: Integer; Full: Boolean = False): Boolean;
@@ -94,6 +94,7 @@ begin
   if P = nil then
     raise Exception.Create('TMsg.Alloc: no mem');
   Init(P, N);
+  AllocStep := N;
   OwnMemory := True;
 end;
 
@@ -141,27 +142,32 @@ begin
 end;
 
 procedure TMsg.WriteData(V: Pointer; N: Integer);
+var
+  NewSize: Integer;
 begin
   if CurSize + N > MaxSize then
   begin
-    Overflow := True;
-    raise Exception.Create('TMsg.WriteData: buffer overrun!');
-    Exit;
+    if OwnMemory then
+    begin
+      NewSize := MaxSize + ((N + AllocStep - 1) div AllocStep) * AllocStep; // round up
+      if ReAllocMem(Data, NewSize) = nil then
+        raise Exception.Create('TMsg.WriteData: out of memory on realloc');
+      MaxSize := NewSize;
+    end
+    else
+    begin
+      Overflow := True;
+      raise Exception.Create('TMsg.WriteData: buffer overrun on borrowed memory!');
+    end;
   end;
+
   Move(V^, (Data + CurSize)^, N);
   CurSize := CurSize + N;
 end;
 
 procedure TMsg.Write(V: TMsg);
 begin
-  if CurSize + V.CurSize > MaxSize then
-  begin
-    Overflow := True;
-    raise Exception.Create('TMsg.WriteData: buffer overrun!');
-    Exit;
-  end;
-  Move(V.Data^, (Data + CurSize)^, V.CurSize);
-  CurSize := CurSize + V.CurSize;
+  WriteData(V.Data, V.CurSize);
 end;
 
 procedure TMsg.Write(V: Byte); overload;
@@ -171,31 +177,37 @@ end;
 
 procedure TMsg.Write(V: Word); overload;
 begin
+  V := NtoLE(V);
   WriteData(@V, 2);
 end;
 
 procedure TMsg.Write(V: LongWord); overload;
 begin
+  V := NtoLE(V);
   WriteData(@V, 4);
 end;
 
 procedure TMsg.Write(V: ShortInt); overload;
 begin
+  V := NtoLE(V);
   WriteData(@V, 1);
 end;
 
 procedure TMsg.Write(V: SmallInt); overload;
 begin
+  V := NtoLE(V);
   WriteData(@V, 2);
 end;
 
 procedure TMsg.Write(V: LongInt); overload;
 begin
+  V := NtoLE(V);
   WriteData(@V, 4);
 end;
 
 procedure TMsg.Write(V: Int64); overload;
 begin
+  V := NtoLE(V);
   WriteData(@V, 8);
 end;
 
@@ -273,36 +285,42 @@ function TMsg.ReadWord(): Word;
 begin
   Result := 0;
   ReadData(@Result, 2);
+  Result := LEtoN(Result);
 end;
 
 function TMsg.ReadLongWord(): LongWord;
 begin
   Result := 0;
   ReadData(@Result, 4);
+  Result := LEtoN(Result);
 end;
 
 function TMsg.ReadShortInt(): ShortInt;
 begin
   Result := 0;
   ReadData(@Result, 1);
+  Result := LEtoN(Result);
 end;
 
 function TMsg.ReadSmallInt(): SmallInt;
 begin
   Result := 0;
   ReadData(@Result, 2);
+  Result := LEtoN(Result);
 end;
 
 function TMsg.ReadLongInt(): LongInt;
 begin
   Result := 0;
   ReadData(@Result, 4);
+  Result := LEtoN(Result);
 end;
 
 function TMsg.ReadInt64(): Int64;
 begin
   Result := 0;
   ReadData(@Result, 8);
+  Result := LEtoN(Result);
 end;
 
 function TMsg.ReadString(): string;