[Delphi & C] Klopt deze omgezette code

Pagina: 1
Acties:
  • 28 views sinds 30-01-2008

  • jelmervos
  • Registratie: Oktober 2000
  • Niet online

jelmervos

Simple user

Topicstarter
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.

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."


  • Creepy
  • Registratie: Juni 2001
  • Nu online

Creepy

Tactical Espionage Splatterer

Test het, debug het en je weet het. Testen en debuggen is toch echt iets wat je als ontwikkelaar zelf moet kunnen doen.

Zie ook P&W FAQ - Leer **** debuggen!! en P&W FAQ - De "quickstart"

Daarom sluit ik je topic.

Overigens, heb je de aanroepende code ook omgezet? Want een unsigned char *input is echt niet hetzelfde als een Integer. Een unsigned char *input is in Delphi hetzelfde als een pointer naar een byte (^byte);

Edit: moet ik hem wel echt sluiten natuurlijk.

[ Voor 5% gewijzigd door Creepy op 26-11-2004 21:17 ]

"I had a problem, I solved it with regular expressions. Now I have two problems". That's shows a lack of appreciation for regular expressions: "I know have _star_ problems" --Kevlin Henney


  • jelmervos
  • Registratie: Oktober 2000
  • Niet online

jelmervos

Simple user

Topicstarter
De Delphi code kan ik prima testen, alleen de C code niet. En ik werk nooit met C code.
Aanroepende code heb ik ook omgezet, het werkt grotendeels al. Alleen zijn er 2 kleine probleempjes.


Sluiten van dit topic vind ik niet nodig eerlijk gezegd.

[ Voor 46% gewijzigd door jelmervos op 26-11-2004 21:17 ]

"The shell stopped unexpectedly and Explorer.exe was restarted."


  • Creepy
  • Registratie: Juni 2001
  • Nu online

Creepy

Tactical Espionage Splatterer

Tjah. als je nu precies aangeeft wat voor problemen je nog hebt, waar je denkt dat het mis gaat en wat je zelf al geprobeerd hebt dan mag je wat mij betreft rustig een nieuwe topic openen.

Nu is het teveel een "dit is mijn code, debuggen jullie het eens voor me" topic.

"I had a problem, I solved it with regular expressions. Now I have two problems". That's shows a lack of appreciation for regular expressions: "I know have _star_ problems" --Kevlin Henney


Dit topic is gesloten.