greg38bj
Messages postés83Date d'inscriptionjeudi 11 mars 2004StatutMembreDernière intervention17 novembre 2008
-
29 févr. 2008 à 18:08
math467
Messages postés2Date d'inscriptionvendredi 12 octobre 2007StatutMembreDernière intervention11 décembre 2008
-
11 déc. 2008 à 10:40
Bonjour à tous
je viens de recupérer un code dont j'ai besoin pour faire un p'tit soft, le seul problème, est que c'est programmé en C++,
et que moi je develloppe en vb2005 express.
Y aurait_il sur le forum une bonne âme suffisemment calé en c++ et vb pour me faire une traduction approximative ?
je vous joins un bout du code :
procedure Generate_CRC_Table(Seed:longword);
var x,i:word;
a:longword;
begin
x:=0;
while (x<256) do
begin
a:=x shl 24;
for i:= 0 to 7 do
begin
if (a and $80000000)= $80000000 then
begin
a:= a + a;
a:= a xor Seed;
end
else
begin
a:= a shl 1;
end;
end;
CRC_Table[x]:=a;
inc(x);
end;
end;
cs_casy
Messages postés7741Date d'inscriptionmercredi 1 septembre 2004StatutMembreDernière intervention24 septembre 201440 29 févr. 2008 à 19:57
Déjà, apparement ce n'est pas du C++ mais plutot du Delphi.
Ensuite convertie en VB2005 ça devrait donner, je pense quelque chose du genre :
Public Sub Generate_CRC_table(ByVal Seed As UInt32)
Dim x, i As UInt16
Dim a As UInt32
x = 0
While x < 256
a = x << 24
For i = 0 To 7
If (a And &H80000000) = &H80000000 Then
a = a + a
a = a Xor Seed
Else
a = a << 1
End If
Next
CRC_Table(x) = a
x += 1
End While
End Sub
Par contre, on ne connais pas la définition de CRC_Table[x]. En VB2005, il devra etre déclaré de la sorte :
Dim CRC_Table(UInt16.MaxValue - 1) As UInt32
cs_casy
Messages postés7741Date d'inscriptionmercredi 1 septembre 2004StatutMembreDernière intervention24 septembre 201440 3 mars 2008 à 11:47
légé abus de ta part tout de même. Mais bon, maintenant avec es 2 exemples, tu doir etre capable de traduire le reste du code, s'il t'en reste.
Dim CRC_Table(255) As Integer
Dim Buffer(2097151) As Byte '
Max size of a flash
Dim BinSize As Integer
Private Sub cmdFileOpen_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdFileOpen.Click
Dim F As System.IO.FileStream
Dim s As String
Dim ofd As New OpenFileDialog
If ofd.ShowDialog = Windows.Forms.DialogResult.OK Then
F = New System.IO.FileStream(ofd.FileName,
IO.FileMode.Open)
F.Read(Buffer, 0, F.Length)
BinSize = F.Length
F.Close()
cmdFileOpen.text = System.IO.Path.GetFileName(ofd.FileName)
memo.Clear()
s = "File size = " & BinSize.ToString & " bytes (
" & (BinSize / 1024).ToString + " kB )"
If BinSize <= 2031616 Then
s = s & " - User Data
Saved"
Else
s = s & " - User Data
Overwitten"
End If
memo.Text = memo.Text & s & Environment.NewLine
FixCRC()
Else
cmdFileOpen.Text = "Click to select a
file..."
End If
End Sub
Function Get4Bytes(ByVal adr As Integer) As Integer
Return CInt(Buffer(adr)) << 24 + CInt(Buffer(adr + 1)) << 16 + CInt(Buffer(adr + 2)) << 8 + CInt(Buffer(adr + 3))
End Function
Function BufferGetString(ByVal adr As Integer) As String
Dim i As Integer
Dim result As String
i = 0
Result = ""
While (i < 16) And (Buffer(adr + i) <> 0)
result = result & Chr(Buffer(adr + i))
i += 1
End While
Return result
End Function
Function CalculateCRC(ByVal Start As Integer, ByVal iStop As Integer) As Integer
Dim CRC, i As Integer
i = Start
CRC = &HFFFFFFFF
While (i <= iStop)
CRC = (CRC << 8) Xor (CRC_Table(Buffer(i) Xor (CRC >> 24)))
i += 1
End While
Return CRC
End Function
Private Sub FixCRC()
Dim BlockID, offset, len, crc, newCRC, ptr As
Integer
Dim s As String
Generate_CRC_Table(FidMergerSeed);
memo.Text = memo.Text & "CRC Table
Calculated!" & Environment.NewLine
ptr = 0
BlockID = Get4Bytes(ptr)
memo.Text = memo.Text & "BlockID =
" & Hex(BlockID).PadLeft(8, "0"c) & Environment.NewLine
If BlockID = &HE3000010 Then
memo.Text = memo.Text & "Boot
loader -> NO CRC" & Environment.NewLine
Buffer(12) = Asc("N")
Buffer(13) = Asc("C")
Buffer(14) = Asc("R")
Buffer(15) = Asc("C")
End If
offset = Get4Bytes(ptr + 8)
ptr = offset
' check for rest
While (ptr + 16 < BinSize)
BlockID = Get4Bytes(ptr)
len = Get4Bytes(ptr + 4)
offset = Get4Bytes(ptr + 8)
CRC =Get4Bytes(ptr+12);
s = "BlockID = " + Hex(BlockID).PadLeft(8, "0"c)
s = s + " SIZE = " + Hex(len).PadLeft(8, "0"c)
s = s + " OFFSET = " + Hex(offset).PadLeft(8, "0"c)
s = s + " Actual CRC = " + Hex(crc).PadLeft(8, "0"c)
memo.Text = memo.Text & s & Environment.NewLine
s = "Bloc Name = " + BufferGetString(ptr + 16)
s = s + " Version = " + BufferGetString(ptr + 32)
s = s + " Date = " + BufferGetString(ptr + 48)
memo.Text = memo.Text & s & Environment.NewLine
newCRC = CalculateCRC(ptr + 16, ptr + 16
+ len - 1)
s = "Calculate CRC = " + Hex(newCRC).PadLeft(8, "0"c)
If newCRC = crc Then
s = s & " - CRC
OK!"
Else
s = s & " - CRC
FAIL!"
End If
memo.Text = memo.Text & s & Environment.NewLine
' Fix CRC
If offset = 0 Then
ptr = ptr + len
Else
ptr = ptr + offset
End If
End While
End Sub
Attention, c'est de la traduction ligne à ligne. Aucune garantie de fonctionnement, c'est à toi de débugger tout ça maintenant.
greg38bj
Messages postés83Date d'inscriptionjeudi 11 mars 2004StatutMembreDernière intervention17 novembre 2008 29 févr. 2008 à 21:06
salut
voici ce qui précedait le code :
var CRC_Table : array[0..255] of longword;
var BinSize:longword;
et voici une partie de la suite :
var Buffer : array[0..2097151] of byte; // Max size of a flash
procedure TFormMain.cmdFileOpenClick(Sender: TObject);
var F:TFileStream;
s:string;
begin
if OpenDialogBin.Execute then
begin
F:=TFileStream.Create(OpenDialogBin.FileName,fmOpenRead);
F.Read(Buffer,F.Size);
BinSize:=F.Size;
F.Free;
cmdFileOpen.Caption:=ExtractFileName(OpenDialogBin.FileName);
memo.Clear;
s:='File size = '+ inttostr(BinSize)+ ' bytes ( '+inttostr(round(BinSize/1024))+' kB )';
if BinSize <= 2031616 then
s:=s+' - User Data Saved'
else
s:=s+' - User Data Overwitten';
memo.Lines.Add(s);
FixCRC;
end
else
cmdFileOpen.Caption:='Click to select a file...';
end;
function Get4Bytes(adr:longword):longword;
begin
Result:=Buffer[adr] shl 24 + Buffer[adr+1] shl 16 + Buffer[adr+2] shl 8 + Buffer[adr+3];
end;
function BufferGetString(adr:longword):string;
var i:integer;
begin
i:=0;
Result:='';
while (i<16) and (buffer[adr+i]<>0) do
begin
Result:=Result+chr(buffer[adr+i]);
inc(i);
end;
end;
function TFormMain.CalculateCRC(Start,Stop:longword):longword;
var CRC : longword;
i:integer;
begin
i:=Start;
CRC := $FFFFFFFF;
while (i <= Stop) do
begin
CRC := (CRC shl 8) xor (CRC_table[buffer[i] xor (CRC shr 24)]);
inc(i);
end;
Result := CRC;
end;
procedure TFormMain.FixCRC;
var BlockID,offset,len,crc,newCRC:longword;
ptr:longword;
s:string;
begin
Generate_CRC_Table(FidMergerSeed);
Memo.Lines.Add('CRC Table Calculated!');
ptr:=0;
BlockID:=Get4Bytes(ptr);
Memo.Lines.Add('BlockID = '+inttohex(BlockID,8));
if BlockID = $E3000010 then
begin
Memo.Lines.Add('Boot loader -> NO CRC');
Buffer[12]:=ord('N');
Buffer[13]:=ord('C');
Buffer[14]:=ord('R');
Buffer[15]:=ord('C');
end;
offset:=Get4Bytes(ptr+8);
ptr:=offset;
// check for rest
while (PTR+16 < BinSize) do
begin
BlockID:=Get4Bytes(ptr);
len:=Get4Bytes(ptr+4);
offset:=Get4Bytes(ptr+8);
CRC:=Get4Bytes(ptr+12);
s:='BlockID = '+inttohex(BlockID,8);
s:=s+' SIZE = '+inttohex(len,8);
s:=s+' OFFSET = '+inttohex(offset,8);
s:=s+' Actual CRC = '+inttohex(crc,8);
Memo.Lines.Add(s);
s:='Bloc Name = '+BufferGetString(ptr+16);
s:=s+' Version = '+BufferGetString(ptr+32);
s:=s+' Date = '+BufferGetString(ptr+48);
Memo.Lines.Add(s);
newcrc:=CalculateCRC(ptr+16,ptr+16+len-1);
s:='Calculate CRC = ' + inttohex(newcrc,8);
if newCRC = CRC then
s:=s+' - CRC OK!'
else
s:=s+' - CRC FAIL!';
memo.Lines.Add(s);
// Fix CRC
if offset=0 then
ptr:=ptr+len
else
ptr:=ptr+offset;
end;
cet algo permet le decryptage du crc de plusieurs fichiers sur lesquels je bosse en ce moment
merci encore
greg38bj
Messages postés83Date d'inscriptionjeudi 11 mars 2004StatutMembreDernière intervention17 novembre 2008 3 mars 2008 à 19:13
salut
et merci Casy
en fait, j'avais avancé de mon côté et traduit ces lignes de code, j'étais presque arrivé au même résultat ...
Si j'ai insisté et abusé, c'est parce que ça ne marche pas du tout
je me trouve avec des depassement de capacité, des index hors du tableau etc ...
Après avoir navigué sur la toile, je me suis rendu compte que c'est un problème récurrent en vb.
j'essaye donc maintenant de contourner ces problèmes ...
Le code que je t'ai envoyé ci-dessus en deux fois est complet pour cette partie du soft.
Donc encore une fois, merci de ta patience et pour la solution apportée et désolé d'avoir abusé
@+
greg38bj
Vous n’avez pas trouvé la réponse que vous recherchez ?
math467
Messages postés2Date d'inscriptionvendredi 12 octobre 2007StatutMembreDernière intervention11 décembre 2008 11 déc. 2008 à 10:39
est-ce que quelqu'un serait assez sympathique pour me traduire cette expression en vb.net :
var
i:byte;
begin
listbox1.Sorted:=false;
listbox1.Sorted:=true;
i:=0;
while i<(listbox1.Items.Count-1)-i do
begin
listbox1.Items.Exchange(i,(listbox1.Items.Count-1)-i);
inc(i);
end;
end;