Ik heb geprobeerde de onderstaand C code om te zetten naar Delphi code. Dit is aardig gelukt, maar er zit (misschien) ergens nog een klein bugje in. Ik wil uitsluiten dat het in deze routine zit.
Kan iemand uitsluitsel geven dat deze code correct is omgezet, zodat ik zeker weet dat mijn bug niet in deze code zit. Alvast bedankt.
C:
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
| #define GN_BYTE_MASK ((1 << bits) - 1) int char_7bit_unpack(unsigned int offset, unsigned int in_length, unsigned int out_length, unsigned char *input, unsigned char *output) { unsigned char *out_num = output; /* Current pointer to the output buffer */ unsigned char *in_num = input; /* Current pointer to the input buffer */ unsigned char rest = 0x00; int bits; bits = offset ? offset : 7; while ((in_num - input) < in_length) { *out_num = ((*in_num & GN_BYTE_MASK) << (7 - bits)) | rest; rest = *in_num >> bits; /* If we don't start from 0th bit, we shouldn't go to the next char. Under *out_num we have now 0 and under Rest - _first_ part of the char. */ if ((in_num != input) || (bits == 7)) out_num++; in_num++; if ((out_num - output) >= out_length) break; /* After reading 7 octets we have read 7 full characters but we have 7 bits as well. This is the next character */ if (bits == 1) { *out_num = rest; out_num++; bits = 7; rest = 0x00; } else { bits--; } } return out_num - output; } |
Delphi:
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
| function Char7bitUnpack(Offset: Integer; InputLength, OutputLength: Integer; Value: String): String; var Bits: Integer; Data: String; ByteMask: Integer; InputCount: Integer; Rest: Byte; begin if Offset > 0 then Bits := Offset else Bits := 7; Data := ''; InputCount := 1; Rest := $0; while (InputCount < InputLength) do begin ByteMask := (1 shl Bits) - 1; Data := Data + Chr(((Ord(Value[InputCount]) and ByteMask) shl (7 - Bits)) or Rest); Rest := Ord(Value[InputCount]) shr Bits; Inc(InputCount); if (Length(Data) >= OutputLength) then Break; if (Bits = 1) then begin Data := Data + Chr(Rest); Bits := 7; Rest := $0; end else Dec(Bits); end; Result := Data; end; |
Kan iemand uitsluitsel geven dat deze code correct is omgezet, zodat ik zeker weet dat mijn bug niet in deze code zit. Alvast bedankt.
"The shell stopped unexpectedly and Explorer.exe was restarted."