Autor Beitrag
rizla
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 417
Erhaltene Danke: 2

XP
FPC mit Lazarus
BeitragVerfasst: So 31.01.10 19:37 
Hallo,
kann jemand hier zufällig folgenden Code nach Delphi oder einfach nur in PseudoCode transpilieren?

ausblenden volle Höhe C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
#include "stdafx.h" 
 
#include <fcntl.h> 
#include <sys/types.h> 
#include <sys/stat.h> 
#include <io.h> 
#include <stdio.h> 
 
#include "test.h" 
#ifdef _DEBUG 
#define new DEBUG_NEW 
#endif 
 
 
// The one and only application object 
 
CWinApp theApp; 
 
using namespace std; 
 
unsigned long CalcChecksum( unsigned char* buffer, unsigned long len, unsigned long seed ); 
 
 
int _tmain(int argc, TCHAR* argv[], TCHAR* envp[]) 

    int nRetCode = 0
  int fh; 
  unsigned char buffer[0x8000]; 
  unsigned short len = 0x7FFC
  unsigned short count; 
  unsigned short seed = 1
  unsigned long chkSum; 
 
    // initialize MFC and print and error on failure 
    if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0)) 
    { 
        // TODO: change error code to suit your needs 
        _tprintf(_T("Fatal Error: MFC initialization failed\n")); 
        nRetCode = 1
    } 
    else 
    { 
        // TODO: code your application's behavior here. 
    if( argc != 2 ) 
    { 
      cout << "usage: CalcChecksum.exe drive:\\path\\info.dvr" << endl; 
      return 0
    } 
 
    fh = _open(argv[1], _O_RDONLY | _O_BINARY); 
    if( fh == -1 ) 
    { 
      cout << "Error opening file " << argv[1] << endl; 
    } 
    count = _read( fh, buffer, 0x8000 ); 
    if( count != 0x8000 ) 
    { 
      cout << "Error reading file " << argv[1] << endl; 
      return 0
    } 
    _close(fh); 
    chkSum = CalcChecksum( &buffer[0], len, seed ); 
    _ultoa( chkSum, (char*)&buffer[0], 16 ); 
    cout << "Checksum: 0x" << buffer << endl; 
  } 
 
    return nRetCode; 

 
 
unsigned long CalcChecksum( unsigned char* buffer, unsigned long len, unsigned long seed ) 

  unsigned char nrOfSegments; 
  unsigned long regECX = seed; 
  unsigned long regEDX = 0
  unsigned long regEDI = 0
  unsigned long retVal; 
 
 
  nrOfSegments = len / 0x15b0
 
  for( unsigned short i = 0; i < nrOfSegments; i++ ) 
  { 
    for( unsigned short j = 0; j < 0x15b0; j++ ) 
    { 
      regEDX = buffer[i*0x15b0+j]; 
      regECX += regEDX; 
      regEDI += regECX; 
    } 
    regECX = regECX % 0xFFF1
    regEDI = regEDI % 0xFFF1
  } 
 
  for( i = (nrOfSegments*0x15b0); i < len; i++ ) 
  { 
      regEDX = buffer[i]; 
      regECX += regEDX; 
      regEDI += regECX; 
  } 
  regECX = regECX % 0xFFF1
  regEDI = regEDI % 0xFFF1
 
  retVal = (regEDI << 16) | regECX; 
  return( retVal ); 
 
}


Danke für die Bemühungen..

:r:

_________________
if you have what they want - they'll find a way to take it (bruce sterling)
WOW - 10 JAHRE Mitglied beim Delphi-Forum. Wie die Zeit vergeht, Freunde.
BenBE
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 8721
Erhaltene Danke: 191

Win95, Win98SE, Win2K, WinXP
D1S, D3S, D4S, D5E, D6E, D7E, D9PE, D10E, D12P, DXEP, L0.9\FPC2.0
BeitragVerfasst: So 31.01.10 19:55 
Das ist ne Methode zum Berechnen der Adler32-Prüfsumme. Gibt's IIRC ne Implementierung für im DEC.

_________________
Anyone who is capable of being elected president should on no account be allowed to do the job.
Ich code EdgeMonkey - In dubio pro Setting.
rizla Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 417
Erhaltene Danke: 2

XP
FPC mit Lazarus
BeitragVerfasst: So 31.01.10 21:17 
Ist das so richtig? Kanns leider nicht evaluieren..

ausblenden volle Höhe Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
var 
  buffer: array[0..$7FFFof byte;
  chkSum: longint;

const 
  len: integer = $7FFC;
  seed: integer = 1;


proc main();
var f: file;
count: integer;

begin

if paramcount <> 1 then 
 begin
  writeln('usage: CalcChecksum.exe drive:\path\info.dvr');
  halt(0);
 end

(..)
if iOResult <> 0 then
 begin
  writeln('Error opening file '+paramstr(1));  
  halt;
 end;

(..)
blockread(f, buffer, count);
if (count  <> $7FFFthen 
 begin
  writeln('Error reading file '+paramstr(1));
  halt;
 end;

chkSum := CalcChecksum(buffer, len, seed);
_ultoa( chkSum, (char*)&buffer[0], 16 );           // die ersten 16 bytes des arrays in einen string konvertieren
cout << "Checksum: 0x" << buffer << endl;     // und diesen ausgeben?

}

func chkSum(..): longint; // Resultat richtig?
var
nrOfSegments: byte;   
regECX,      
regEDX,      
regEDI: longint;    

begin
 regECX := seed;
 regEDX := 0;
 regEDI := 0;

 nrOfSegments := len / $15b0// oder len div $15B0?

 for i := 0 to nrOfSegments do
  begin
   for j := 0 to ($15b0-1do
    begin
      regEDX := buffer[i*$15b0+j]; 
      regECX := regECX + regEDX; 
      regEDI := regEDI + regECX; 
     end;
    regECX := regECX MOD $FFF1
    regEDI = regEDI MOD $FFF1
   end
  
 for  i := nrOfSegments*$15b0 to len-1 do 
  begin
    regEDX = buffer[i]; 
    regECX := regECX + regEDX; 
    regEDI := regEDI + regECX; 
  end;
 
    regECX := regECX MOD $FFF1
    regEDI = regEDI MOD $FFF1

  result := regEDI shl 16 OR regECX;

end;


Also vor allem ab func ChkSum..

_________________
if you have what they want - they'll find a way to take it (bruce sterling)
WOW - 10 JAHRE Mitglied beim Delphi-Forum. Wie die Zeit vergeht, Freunde.
BenBE
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 8721
Erhaltene Danke: 191

Win95, Win98SE, Win2K, WinXP
D1S, D3S, D4S, D5E, D6E, D7E, D9PE, D10E, D12P, DXEP, L0.9\FPC2.0
BeitragVerfasst: So 31.01.10 21:37 
Also: Beim Lesen der Datei die Größenangabe stimmt nicht: Len Bytes lesen und auf <> Len beim gelesenen prüfen. Also da bitte noch mal genau schauen. Ist oft hilfreich, sich beide Sources "interleaved" hinzulegen und dann Zeilenweise zu vergleichen.

Bzgl. CalcCRC:
From 0 to NrSegs -1 ... Bounds Check stimmt nicht
Die Division in Delphi muss ein Div sein.

Sonst seh ich erstmal Nix Offensichtliches ...

_________________
Anyone who is capable of being elected president should on no account be allowed to do the job.
Ich code EdgeMonkey - In dubio pro Setting.
rizla Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 417
Erhaltene Danke: 2

XP
FPC mit Lazarus
BeitragVerfasst: So 31.01.10 21:47 
me - the human transpiler ;)

okay, #2 versteh ich, aber #1: was stimmt nicht mit len?

entspricht das denn:
ausblenden Delphi-Quelltext
1:
2:
3:
var buffer: byte;
(..)
len := buffer[$7FFC];

?

aber danke schon mal, mate!

_________________
if you have what they want - they'll find a way to take it (bruce sterling)
WOW - 10 JAHRE Mitglied beim Delphi-Forum. Wie die Zeit vergeht, Freunde.
rizla Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 417
Erhaltene Danke: 2

XP
FPC mit Lazarus
BeitragVerfasst: So 31.01.10 21:51 
user profile iconrizla hat folgendes geschrieben Zum zitierten Posting springen:
me - the human transpiler ;)

okay, #2 versteh ich, aber #1: was stimmt nicht mit len?

entspricht das denn:
ausblenden Delphi-Quelltext
1:
2:
3:
var buffer: byte;
(..)
len := buffer[$7FFC];

?

aber danke schon mal, mate!



Edit 1:
gibts ein äquivalent zu _ultoa? oder stimmt meine vermutung, einfach die ersten 16 bytes des arrays in einen string zu konvertieren?

Edit 2:
Sorry für die "Dauerkleinschreibung".
Mods - ich brauch eine Funktion, die alle Substantive automatisch groß schreibt :)

_________________
if you have what they want - they'll find a way to take it (bruce sterling)
WOW - 10 JAHRE Mitglied beim Delphi-Forum. Wie die Zeit vergeht, Freunde.
BenBE
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 8721
Erhaltene Danke: 191

Win95, Win98SE, Win2K, WinXP
D1S, D3S, D4S, D5E, D6E, D7E, D9PE, D10E, D12P, DXEP, L0.9\FPC2.0
BeitragVerfasst: So 31.01.10 21:56 
user profile iconrizla hat folgendes geschrieben Zum zitierten Posting springen:
user profile iconrizla hat folgendes geschrieben Zum zitierten Posting springen:
me - the human transpiler ;)

okay, #2 versteh ich, aber #1: was stimmt nicht mit len?

entspricht das denn:
ausblenden Delphi-Quelltext
1:
2:
3:
var buffer: byte;
(..)
len := buffer[$7FFC];

?

aber danke schon mal, mate!

Vgl. mit obigem Source in C#:
Len wird mit 0x7FFC initialisiert und ist als "nicht veränderlich" markiert.
Bei dem _read wird explizit auf die Puffer-Größe verwiesen (0x8000).

user profile iconrizla hat folgendes geschrieben Zum zitierten Posting springen:
Edit 1:
gibts ein äquivalent zu _ultoa? oder stimmt meine vermutung, einfach die ersten 16 bytes des arrays in einen string zu konvertieren?

Heißt ausgeschrieben Unsigned Long To ASCII ... Rest ergibt sich ;-)

_________________
Anyone who is capable of being elected president should on no account be allowed to do the job.
Ich code EdgeMonkey - In dubio pro Setting.
rizla Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 417
Erhaltene Danke: 2

XP
FPC mit Lazarus
BeitragVerfasst: So 31.01.10 22:15 
Heißt das nun nrOfSegments := len div $15B0; ?

Wenn
const len: integer = $77FC;,
dann wäre doch aber nrOfSegments konstant,nämlich 5.

'tschuldigung, aber ich steh da echt gerade auf'm Schlauch..

_________________
if you have what they want - they'll find a way to take it (bruce sterling)
WOW - 10 JAHRE Mitglied beim Delphi-Forum. Wie die Zeit vergeht, Freunde.
BenBE
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 8721
Erhaltene Danke: 191

Win95, Win98SE, Win2K, WinXP
D1S, D3S, D4S, D5E, D6E, D7E, D9PE, D10E, D12P, DXEP, L0.9\FPC2.0
BeitragVerfasst: So 31.01.10 22:16 
Jap. Das ist beides korrekt ;-) Und IMHO optimiert der C++-Compiler das sogar in der Form ;-)

_________________
Anyone who is capable of being elected president should on no account be allowed to do the job.
Ich code EdgeMonkey - In dubio pro Setting.
rizla Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 417
Erhaltene Danke: 2

XP
FPC mit Lazarus
BeitragVerfasst: So 31.01.10 22:19 
BenBE weiß einfach alles! :zustimm:

(Hab ich das Gefühl)

:r:

_________________
if you have what they want - they'll find a way to take it (bruce sterling)
WOW - 10 JAHRE Mitglied beim Delphi-Forum. Wie die Zeit vergeht, Freunde.