Calculate land digest
authorunc0rr
Thu, 11 Jan 2007 20:45:59 +0000
changeset 316 57d50189ad86
parent 315 73003488240b
child 317 e95340dbfc1d
Calculate land digest
hedgewars/SDLh.pas
hedgewars/hwengine.dpr
hedgewars/uLand.pas
hedgewars/uMisc.pas
hedgewars/uSHA.pas
--- a/hedgewars/SDLh.pas	Wed Jan 10 23:24:55 2007 +0000
+++ b/hedgewars/SDLh.pas	Thu Jan 11 20:45:59 2007 +0000
@@ -1,6 +1,6 @@
 (*
  * Hedgewars, a worms-like game
- * Copyright (c) 2004, 2005, 2006 Andrey Korotaev <unC0Rr@gmail.com>
+ * Copyright (c) 2004-2007 Andrey Korotaev <unC0Rr@gmail.com>
  *
  * 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
@@ -187,7 +187,9 @@
                        end;
 
      PByteArray = ^TByteArray;
-     TByteArray = array[0..32767] of Byte;
+     TByteArray = array[0..65535] of Byte;
+     PLongWordArray = ^TLongWordArray;
+     TLongWordArray = array[0..16383] of LongWord;
 
 function  SDL_Init(flags: Longword): integer; cdecl; external SDLLibName;
 procedure SDL_Quit; cdecl; external SDLLibName;
@@ -373,10 +375,10 @@
 function SDLNet_AddSocket(_set: PSDLNet_SocketSet; sock: PTCPSocket): LongInt; cdecl; external SDL_NetLibName;
 function SDLNet_CheckSockets(_set: PSDLNet_SocketSet; timeout: LongInt): LongInt; cdecl; external SDL_NetLibName;
 
-procedure SDLNet_Write16(value: SmallInt; buf: pointer); cdecl; external SDL_NetLibName;
-procedure SDLNet_Write32(value: LongInt; buf: pointer); cdecl; external SDL_NetLibName;
-function SDLNet_Read16(buf: pointer): SmallInt; cdecl; external SDL_NetLibName;
-function SDLNet_Read32(buf: pointer): LongInt; cdecl; external SDL_NetLibName;
+procedure SDLNet_Write16(value: Word; buf: pointer); cdecl; external SDL_NetLibName;
+procedure SDLNet_Write32(value: LongWord; buf: pointer); cdecl; external SDL_NetLibName;
+function SDLNet_Read16(buf: pointer): Word; cdecl; external SDL_NetLibName;
+function SDLNet_Read32(buf: pointer): LongWord; cdecl; external SDL_NetLibName;
 
 implementation
 
--- a/hedgewars/hwengine.dpr	Wed Jan 10 23:24:55 2007 +0000
+++ b/hedgewars/hwengine.dpr	Thu Jan 11 20:45:59 2007 +0000
@@ -42,7 +42,8 @@
   uLandObjects in 'uLandObjects.pas',
   uLandGraphics in 'uLandGraphics.pas',
   uLocale in 'uLocale.pas',
-  uAmmos in 'uAmmos.pas';
+  uAmmos in 'uAmmos.pas',
+  uSHA in 'uSHA.pas';
 
 {$INCLUDE options.inc}
 
--- a/hedgewars/uLand.pas	Wed Jan 10 23:24:55 2007 +0000
+++ b/hedgewars/uLand.pas	Thu Jan 11 20:45:59 2007 +0000
@@ -32,7 +32,7 @@
 
 
 implementation
-uses uConsole, uStore, uMisc, uConsts, uRandom, uTeams, uLandObjects;
+uses uConsole, uStore, uMisc, uConsts, uRandom, uTeams, uLandObjects, uSHA, uIO;
 
 type TPixAr = record
               Count: Longword;
@@ -40,17 +40,19 @@
               end;
 
 procedure LogLandDigest;
-//var ctx: TSHA1Context;
-//    dig: TSHA1Digest;
+var ctx: TSHA1Context;
+    dig: TSHA1Digest;
+    s: shortstring;
 begin
-//SHA1Init(ctx);
-//SHA1Update(ctx, @Land, sizeof(Land));
-//dig:= SHA1Final(ctx);
-{$IFDEF DEBUGFILE}
-//AddFileLog('SHA1 Land digest: {'+inttostr(dig.LongWords[0])+':'
-//           +inttostr(dig.LongWords[1])+':'+inttostr(dig.LongWords[2])+':'
-//           +inttostr(dig.LongWords[3])+':'+inttostr(dig.LongWords[4])+'}');
-{$ENDIF}
+SHA1Init(ctx);
+SHA1Update(ctx, @Land, sizeof(Land));
+dig:= SHA1Final(ctx);
+s:= '{'+inttostr(dig[0])+':'
+       +inttostr(dig[1])+':'
+       +inttostr(dig[2])+':'
+       +inttostr(dig[3])+':'
+       +inttostr(dig[4])+'}';
+SendIPC('M' + s)
 end;
 
 procedure DrawBezierEdge(var pa: TPixAr; Color: Longword);
--- a/hedgewars/uMisc.pas	Wed Jan 10 23:24:55 2007 +0000
+++ b/hedgewars/uMisc.pas	Thu Jan 11 20:45:59 2007 +0000
@@ -90,7 +90,7 @@
 procedure OutError(Msg: String; const isFatalError: boolean=false);
 procedure TryDo(Assert: boolean; Msg: string; isFatal: boolean);
 procedure SDLTry(Assert: boolean; isFatal: boolean);
-function IntToStr(n: integer): shortstring;
+function IntToStr(n: LongInt): shortstring;
 function FloatToStr(n: Double): shortstring;
 function DxDy2Angle32(const _dY, _dX: Extended): integer;
 function DxDy2AttackAngle(const _dY, _dX: Extended): integer;
@@ -156,7 +156,7 @@
 Color:= SDL_MapRGB(PixelFormat, (Color shr 16) and $FF, (Color shr 8) and $FF, Color and $FF)
 end;
 
-function IntToStr(n: integer): shortstring;
+function IntToStr(n: LongInt): shortstring;
 begin
 str(n, Result)
 end;
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/hedgewars/uSHA.pas	Thu Jan 11 20:45:59 2007 +0000
@@ -0,0 +1,143 @@
+(*
+ * Hedgewars, a worms-like game
+ * Copyright (c) 2004-2007 Andrey Korotaev <unC0Rr@gmail.com>
+ *
+ * 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; version 2 of the License
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
+ *)
+
+unit uSHA;
+interface
+uses SDLh;
+
+type TSHA1Context = packed record
+                    H: array[0..4] of LongWord;
+                    Length, CurrLength: Int64;
+                    Buf: array[0..63] of byte;
+                    end;
+     TSHA1Digest =  array[0.. 4] of LongWord;
+
+procedure SHA1Init(var Context: TSHA1Context);
+procedure SHA1Update(var Context: TSHA1Context; Buf: PByteArray; Length: LongWord);
+function  SHA1Final(Context: TSHA1Context): TSHA1Digest;
+
+implementation
+
+function rol(x: LongWord; y: Byte): LongWord;
+begin
+  Result:= (X shl y) or (X shr (32 - y))
+end;
+
+function Ft(t, b, c, d: LongWord): LongWord;
+begin
+case t of
+      0..19: Result := (b and c) or ((not b) and d);
+     20..39: Result :=  b xor c xor d;
+     40..59: Result := (b and c) or (b and d) or (c and d);
+     else    Result :=  b xor c xor d;
+  end;
+end;
+
+function Kt(t: Byte): LongWord;
+begin
+  case t of
+     0..19: Result := $5A827999;
+    20..39: Result := $6ED9EBA1;
+    40..59: Result := $8F1BBCDC;
+  else
+    Result := $CA62C1D6
+  end;
+end;
+
+
+procedure SHA1Hash(var Context: TSHA1Context);
+var S: array[0..4 ] of LongWord;
+    W: array[0..79] of LongWord;
+    i, t: LongWord;
+begin
+move(Context.H, S, sizeof(S));
+for i:= 0 to 15 do
+    SDLNet_Write32(PLongWordArray(@Context.Buf)[i], @W[i]);
+    
+for i := 16 to 79 do
+    W[i] := rol(W[i - 3] xor W[i - 8] xor W[i - 14] xor W[i - 16], 1);
+    
+for i := 0 to 79 do
+    begin
+    t:= rol(S[0], 5) + Ft(i, S[1], S[2], S[3]) + S[4] + W[i] + Kt(i);
+    S[4]:= S[3];
+    S[3]:= S[2];
+    S[2]:= rol(S[1], 30);
+    S[1]:= S[0];
+    S[0]:= t
+    end;
+    
+for i := 0 to 4 do
+    Context.H[i]:= Context.H[i] + S[i]
+end;
+
+procedure SHA1Init(var Context: TSHA1Context);
+begin
+  with Context do
+       begin
+       Length    := 0;
+       CurrLength:= 0;
+       H[0]:= $67452301;
+       H[1]:= $EFCDAB89;
+       H[2]:= $98BADCFE;
+       H[3]:= $10325476;
+       H[4]:= $C3D2E1F0
+  end
+end;
+
+procedure SHA1Update(var Context: TSHA1Context; Buf: PByteArray; Length: LongWord);
+var i: integer;
+begin
+for i:= 0 to Pred(Length) do
+    begin
+    Context.Buf[Context.CurrLength]:= PByteArray(Buf)^[i];
+    inc(Context.CurrLength);
+    if Context.CurrLength = 64 then
+       begin
+       SHA1Hash(Context);
+       inc(Context.Length, 512);
+       Context.CurrLength:= 0
+       end
+    end
+end;
+
+function  SHA1Final(Context: TSHA1Context): TSHA1Digest;
+var i: LongWord;
+begin
+Context.Length:= Context.Length + Context.CurrLength shl 3;
+Context.Buf[Context.CurrLength]:= $80;
+inc(Context.CurrLength);
+
+if Context.CurrLength > 56 then
+   begin
+   FillChar(Context.Buf[Context.CurrLength], 64 - Context.CurrLength, 0);
+   Context.CurrLength:= 64;
+   SHA1Hash(Context);
+   Context.CurrLength:=0
+   end;
+
+FillChar(Context.Buf[Context.CurrLength], 56 - Context.CurrLength, 0);
+
+for i:= 56 to 63 do
+    Context.Buf[i] := (Context.Length shr ((63 - i) * 8)) and $FF;
+SHA1Hash(Context);
+move(Context.H, Result, sizeof(TSHA1Digest));
+FillChar(Context, sizeof(Context), 0)
+end;
+
+end.