请帮我翻译一段50行以内的函数(LZ77算法)C++ To Delphi(100分)

  • 主题发起人 主题发起人 yuki2003
  • 开始时间 开始时间
Y

yuki2003

Unregistered / Unconfirmed
GUEST, unregistred user!
[:D][:D][:D][:D][:D][:D]
int Decompress(char *out, char *in, int len)
{
/*
Decompress buffer "in" of size "len" into buffer "out" (previously
allocated) returns the number of decompressed bytes.
*/

int x;
int outbufptr;
int mask;
int tag;
int inptr;
int outptr;
int count;
int done;
char Window[4096];
int inbufptr;

for (x = 0; x < len; x++) {
in[x] = (in[x] - x) ^ x;
}

done = FALSE;

inptr = 0;
outptr = 0;
outbufptr = 1;
mask = 1;
tag = in[inptr++];

while (!done) {
if ((mask & tag) == 0) {
out[outptr++] = in[inptr];
Window[outbufptr] = in[inptr];
outbufptr = (outbufptr + 1) & 0xFFF;
inptr++;
}
else {
count = *((unsigned short *) (in+inptr));
inptr += 2;
inbufptr = count >> 4;
if (inbufptr == 0)
return outptr;
else {
count = (count & 0x0f) + 2;
if (count >= 0) {
for (x = 0; x < count; x++) {
out[outptr++] = Window[inbufptr];
Window[outbufptr] = Window[inbufptr];
inbufptr = (inbufptr + 1) & 0xFFF;
outbufptr = (outbufptr + 1) & 0xFFF;
}
}
}
}
mask *= 2;
if (mask & 0x0100) {
mask = 1;
tag = in[inptr++];
}
}
return outptr;
}
 
这是我引用的dll的函数代码,我想编进我的程序里面,不用那个dll连接库了
因为这几天我忙糊涂了,头脑不清醒,所以就偷懒找人翻译。请大家帮帮忙。3点前我在线等后。
 
function Decompress(outB: PChar; inB: PChar; len: Integer): Inteter; // Out和In是Delphi保留关键字这里改成OutB和inB
/*
Decompress buffer "in" of size "len" into buffer "out" (previously
allocated) returns the number of decompressed bytes.
*/
var
x,
outbufptr,
mask,
tag,
inptr,
outptr,
count,
done: Integer;
Window: array [0..4095] of char;
inbufptr: Integer;
begin
for x := 0 to len-1 do
(inb+x)^ = char((byte(inb+x)^ - x) xor x); // c中的 ^ 是 XOR吧? 忘了

done := FALSE;

inptr := 0;
outptr := 0;
outbufptr := 1;
mask := 1;
tag = byte(inb+inptr)^;
inc(inptr);
while not done do
begin
if mask and tag = 0 then
begin
(outb+outptr)^ := (inb+inptr)^;
inc(outptr);
Window[outbufptr] := (inb+inptr)^;
outbufptr := (outbufptr+1) and $fff;
inc(inptr);
end
else begin
count = byte((inb+inptr)^);
inc(inptr, 2);
inbufptr := count shr 4;
if (inbufptr=0) then
result := outptr
else begin
count := (count and $f) + 2;
if (count >= 0) then begin
for x := 0 to count-1 do
begin
(outb+outptr)^ := window[inbufptr);
inc(outptr);
Window[outbufptr] := Window[inbufptr];
inbufptr := (inbufptr + 1) and $FFF;
outbufptr := (outbufptr + 1) and $FFF;
end
end
end
end;
mask :=mask * 2;
if (mask and $100) <> 0 then
begin
mask := 1;
tag := byte(inb+inptr)^;
inc(inptr);
end
end;
result := outptr;
end;
 
// c中的 ^ 是 XOR吧?
是的
for x = 0 to CompressedSize-1
data[x] = (data[x] - x) XOR x
next x

修改后运行通过
谢谢
收到分了吗
 

Similar threads

I
回复
0
查看
658
import
I
I
回复
0
查看
632
import
I
I
回复
0
查看
718
import
I
I
回复
0
查看
687
import
I
I
回复
0
查看
843
import
I
后退
顶部