Bonjour,
Dans le cadre d'un projet, je dois réécrire 3 fonctions assez techniques (en rapport avec des CRC). Lorsque je fais tourner le programme, j'ai justement une erreur dans les CRC (les valeurs ne sont pas correctes).
Je n'ai pas réussi à trouver l'erreur mais je suis certain qu'elle se trouve dans une des 3 fonctions suivantes. Si certains d'entre vous s'y connaissent suffisamment en VB et C++ pour me dire si j'ai bien traduit les fonctions, ce serait sympa
Première fonction
Version VB :
Version C++ :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 Public Function OutFile_CRC16ComputeOnPartOfFile(lOStart As Long, lOEnd As Long) As Long Dim lIx As Long, b As Byte, Ix As Integer Dim lmodE As Integer, ldivE As Long, lmodS As Integer, ldivS As Long Dim mskE As Byte, mskS As Byte ldivS = 1 + (lOStart \ 8) lmodS = lOStart Mod 8 mskS = &HFF For Ix = 1 To lmodS: mskS = mskS \ 2: Next Ix ldivE = 1 + (lOEnd \ 8) lmodE = lOEnd Mod 8 mskE = &H7F For Ix = 1 To lmodE: mskE = mskE \ 2: Next Ix mskE = Not mskE Call OutFile_CRC16Init If ldivS = ldivE Then Call OutFile_CRC16Init Get #fOut, ldivS, b b = (b And mskS) And mskE Call OutFile_CRC16UpdCRC(b) Else lIx = ldivS Get #fOut, lIx, b Call OutFile_CRC16UpdCRC(b And mskS) lIx = lIx + 1 While (lIx < ldivE) Get #fOut, lIx, b Call OutFile_CRC16UpdCRC(b) lIx = lIx + 1 Wend Get #fOut, lIx, b Call OutFile_CRC16UpdCRC(b And mskE) End If OutFile_CRC16ComputeOnPartOfFile = OutFile_CRC16GetCRC End Function
Deuxième fonction
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 long __fastcall ModOutFile::outFileCRC16ComputeOnPartOfFile(long lOStart, long lOEnd) { long lIx, ldivE, ldivS; unsigned char b, mskE, mskS; int lmodE, lmodS; ldivS = 1 + (lOStart / 8); lmodS = lOStart % 8; mskS = 0xFF; for(int ix = 1; ix <= lmodS; ++ix) mskS /= 2; ldivE = 1 + (lOEnd / 8); lmodE = lOEnd % 8; mskE = 0x7F; for(int ix = 1; ix <= lmodE; ++ix) mskE /= 2; mskE = ~mskE; outFileCRC16Init(); if(ldivS == ldivE) { outFileCRC16Init(); fseek(fOut, ldivS * sizeof(char), SEEK_SET); fread(&b, sizeof(char), 1, fOut); b = (b & mskS) & mskE; outFileCRC16UpdCRC(b); } else { lIx = ldivS; fseek(fOut, lIx * sizeof(char), SEEK_SET); fread(&b, sizeof(char), 1, fOut); outFileCRC16UpdCRC(b & mskS); ++lIx; while(lIx < ldivE) { fseek(fOut, lIx * sizeof(char), SEEK_SET); fread(&b, sizeof(char), 1, fOut); outFileCRC16UpdCRC(b); ++lIx; } fseek(fOut, lIx * sizeof(char), SEEK_SET); fread(&b, sizeof(char), 1, fOut); outFileCRC16UpdCRC(b & mskE); } return outFileCRC16GetCRC(); }
Version VB :
Version C++ :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 Public Sub OutFile_Rewrite32bits(lOffset As Long, lValue As Long) Dim ldiv As Long, lmod As Integer, bOldMode As Boolean, Ix As Integer bOldMode = bPutBitFixupMode Call OutFile_Purge bPutBitFixupMode = True ldiv = 1 + (lOffset \ 8) lmod = lOffset Mod 8 Get #fOut, ldiv, PutBitByte PutBitMask = &H80 For Ix = 1 To lmod: PutBitMask = PutBitMask \ 2: Next Ix PutBitOffset = lOffset PutBitRecNum = ldiv Call OutFile_PutValue(lValue, 1, 32) Call OutFile_Purge bPutBitFixupMode = bOldMode End Sub
Troisième fonction
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 void __fastcall ModOutFile::outFileRewrite32bits(long lOffset, long lValue) { bool bOldMode; long ldiv; int lmod; bOldMode = bPutBitFixupMode; outFilePurge(); bPutBitFixupMode = true; ldiv = 1 + (lOffset / 8); lmod = lOffset % 8; fseek(fOut, ldiv * sizeof(char), SEEK_SET); fread(&putBitByte, sizeof(char), 1, fOut); putBitMask = 0x80; for(int ix = 1; ix <= lmod; ++ix) putBitMask /= 2; putBitOffset = lOffset; putBitRecNum = ldiv; outFilePutValue(lValue, 1, 32); outFilePurge(); bPutBitFixupMode = bOldMode; }
Version VB :
Version C++ :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 Public Function OutFile_PutValue(ByVal sv As String, ByVal cResol As Double, ByVal NumberOfBits As Integer, Optional StringSize As Integer) As String Dim Final As String, Ix As Integer, re As Byte, v As Double Dim ForLog As String Dim iSS As Integer Dim aze As Double OutFile_PutValue = "?" If NumberOfBits <> NUMBITSSTRING Then If NumberOfBits < 1 Then Exit Function End If End If ForLog = OutFile_GetPutBitOffset If NumberOfBits = NUMBITSSTRING Then iSS = Abs(StringSize) If Len(sv) > iSS Then If bLogging Then Call AppendLog(LOGERROR, "Length of '" & sv & "' exceeds asked for length (" & iSS & ")") nErrors = nErrors + 1 End If sv = Left(sv & Space(iSS), iSS) OutFile_PutValue = sv For Ix = 1 To iSS If StringSize < 0 Then Call OutFile_PutValue("0", 1, 8) Call OutFile_PutValue("0", 1, 8) Call OutFile_PutValue("0", 1, 8) End If Call OutFile_PutValue(CStr(Asc(Mid(sv, Ix, 1))), 1, 8) Next Ix Exit Function End If On Error Resume Next v = CDbl(sv) If Err.Number <> 0 Then v = 0: Err.Clear End If If cResol <> 1 Then If cResol <> 0 Then v = CDec(v) / CDec(cResol) End If End If Final = String(NumberOfBits, "0") For Ix = 1 To NumberOfBits re = CMod(v, 2) v = Int(CDec(v) / 2) If re <> 0 Then Mid(Final, NumberOfBits - Ix + 1, 1) = "1" Next Ix For Ix = 1 To NumberOfBits If Mid(Final, Ix, 1) = "1" Then Call OutFile_PutBit(1) Else Call OutFile_PutBit(0) Next Ix OutFile_PutValue = Final If bLogging Then If OutFile_bHidden Then If bLogging Then Call AppendLog(LOGINFO, ForLog & " > " & NOTVISIBLESTRING) Else If bLogging Then Call AppendLog(LOGINFO, ForLog & " > " & Final) End If End If End Function
Je sais qu'il y a des appels à d'autres fonctions mais ces dernières sont correctes.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 String __fastcall ModOutFile::outFilePutValue(String sv, double cResol, int numberOfBits, int stringSize) { String ret, forLog, final; int iSS; double v; unsigned char re; ret = "?"; if(numberOfBits != NUMBITSSTRING) { if(numberOfBits < 1) return ret; } forLog = outFileGetPutBitOffset(); if(numberOfBits == NUMBITSSTRING) { iSS = abs(stringSize); if(sv.Length() > iSS) { if(bLogging) appendLog(LOGERROR, "Length of '" + sv + "' exceeds asked for length (" + iSS + ")"); } //construct spaces string String iSSSpaces = ""; for(int i = 0; i < iSS; ++i) iSSSpaces += " "; String svWithSpaces = sv + iSSSpaces; sv = svWithSpaces.SubString(1, iSS); ret = sv; for(int ix = 1; ix <= iSS; ++ix) { if(stringSize < 0) { outFilePutValue("0", 1, 8); outFilePutValue("0", 1, 8); outFilePutValue("0", 1, 8); } //get ASCII value here because no Asc() function in C/C++ String car = sv.SubString(ix, 1); int val = *(car.c_str()); outFilePutValue(IntToStr(val), 1, 8); } return ret; } try { v = StrToFloat(sv); } // careful if , or . catch(EConvertError &e) { v = 0; } if(cResol != 1) { if(cResol != 0) v /= cResol; } //best way i found to init string with a certain number of characters std::string finalC = std::string(numberOfBits, '0'); final = finalC.c_str(); for(int ix = 1; ix <= numberOfBits; ++ix) { re = cMod(v); v = (int) (v/2); if(re != 0) final[numberOfBits - ix + 1] = '1'; } for(int ix = 1; ix <= numberOfBits; ++ix) { if(final.SubString(ix, 1) == "1") outFilePutBit(1); else outFilePutBit(0); } ret = final; if(bLogging) { if(outFilebHidden) appendLog(LOGINFO, forLog + " > " + NOTVISIBLESTRING); else appendLog(LOGINFO, forLog + " > " + final); } return ret; }
Merci d'avance.
Partager