Valhalla Legends Forums Archive | Java Programming | Need help for ported C code to Java

AuthorMessageTime
mime
Hello who can help ported  this https://davnit.net/bnet/vL/index.php?topic=585.msg4318#msg4318 code on Java ?

I will be very grateful for any help.
November 30, 2009, 9:31 AM
Camel
Yes, I'm sure someone here can help. Do you have a more specific question? If you're just looking for someone to do it for you, I doubt you'll get that here.
November 30, 2009, 4:40 PM
Imperceptus
looks pretty easy to understand... What don't you know?  I mean srs you could google most of that code and find the java equivlant.
November 30, 2009, 9:54 PM
mime
Hi, I rewrote the code to Java, but it does not work. : (

Here's the code:
http://pastebin.com/m389b1923

the line 188 is throw exception java.lang.ArrayIndexOutOfBoundsException: -1

What am I doing wrong?

Help please.

Thanks!
December 1, 2009, 5:06 AM
Camel
In C/C++, there is only the need for one shift-right operator, because signedness is determined declaratively by the type. In Java, all types are signed, so there needs to be two different shift-right operators to accommodate both uses. I'm not sure why they made >> the uncommonly used one, but they did.

More on Java shifts: http://java.sun.com/docs/books/tutorial/java/nutsandbolts/op3.html
More on C shifts: http://msdn.microsoft.com/en-us/library/336xbhcz(VS.80).aspx
December 1, 2009, 4:53 PM
mime
Camel,
Yes, Java have  ">>>" not sign right-shift, but not have "<<<" ...

I am familiar with bitwise and bit shift operators, but I can not understand as to apply them to this code : (

Please prompt how to force to work the program.

I replase operator ">>" on ">>>" but the program does not work all the same ...

Sorry for silly questions.
December 1, 2009, 7:01 PM
Camel
So you're saying (b>>>0x18) comes out to -1? That's not possible, buddy. Give more info if you want help.

[edit] Okay, I've taken a closer look at this. Looks like you've just tried to do a mechanical port without any regard for how the languages differ. I suggest you run through the C code in a debugger to figure out how it works, and then try to reproduce that behavior in your Java code. The C code uses pointers and "out" parameters pretty heavily, and those concepts just don't exist in Java, so you're going to have to take the time to understand how it works in order to port it.
December 1, 2009, 8:18 PM
mime
I create the buffer with the size which function getpacketsize () returns and I transfer it the buffer in gamepacketdecode.

http://pastebin.com/m5410ca3a
December 1, 2009, 8:57 PM
Camel
You can't do a mechanical port of getPacketSize(), because you can't pass a pointer to an int for the function to write the offset to, and you can't return a pointer to the offset in the buffer. You're probably best off getting rid of that method, and switching gamePacketDecode() to the prototype: [tt]byte[] gamePacketDecode(byte[] in)[/tt], and throwing an Exception if there's a decoding error.
December 1, 2009, 9:28 PM
mime
Camel, I have made as you have told, move out all in one gamePacketDecode method.

Method gamePacketDecode and main method:
[code]public byte[] gamePacketDecode(byte[] packet) {

// get packet length and offset
int size, offset;

int x;
if (packet[0] < 0xF0) { // if length == 1 byte, offset = 1;
   size = packet[0] - 1;
   offset = 1;
} else {
   x = (packet[0] & 0xF) << 8; // if length == 2 byte, offset = 2;
   size = x + packet[1] - 2;
   offset = 2;
}

System.out.println("Length:" + size);
System.out.println("Offset:" + offset);

int a = 0, b = 0, c = 0, d = 0;

int index;
int cnt = 0x20;
int outmax = size;
int maxcnt = outmax;

byte[] outdata = new byte[size];
byte[] indata = new byte[size];
// coppy packet without size byte
System.arraycopy(packet, offset, packet, 0, size);

int inix = 0; // index of indata array
int outix = 0; // index of outdata array
while (true) {
   if (cnt >= 0x8) {
while (size > 0 && cnt >= 8) {
   cnt -= 0x8;
   size--;
   a = indata[inix++] << cnt;
   b |= a;
}
   }

   index = charIndex[b >> 0x18];
   a = charTable[index];
   d = (b >> (0x18 - a)) & bitMasks[a];
   c = charTable[index + 2 * d + 2];

   cnt += c;
   if (cnt > 0x20) {
size = outmax - maxcnt;
break;
   }

   if (maxcnt-- == 0)
break;

   a = charTable[index + 2 * d + 1];
   outdata[outix++] = (byte) a;

   b <<= (c & 0xFF);
}

return outdata;
   }

   public static void main(String[] args) {
D2GSPacketDecoder decoder = new D2GSPacketDecoder();
byte[] packet = { 0x07, 0x1f, 0x7f, (byte) 0xff, (byte) 0xff,
(byte) 0xff, (byte) 0xc0 };
byte[] decPacket = decoder.gamePacketDecode(packet);
System.out.println("Packet:");
for (int ix = 0; ix < decPacket.length; ix++) {
   System.out.print(Integer.toHexString(decPacket[ix]) + " ");
}
   }
[/code]

Method output:
[code]
Length:6
Offset:1
Packet:
2f 2f 2f 2f 0 0
[/code]

I followed an example of a packet from here: https://davnit.net/bnet/vL/index.php?topic=14507.msg153465#msg153465

compressed packet: 07 1f 7f ff ff ff c0
decompressed packet: 8F 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00

But my method return  2f 2f 2f 2f 0 0 :(

Please, prompt what to make that the program worked correctly?
December 1, 2009, 10:57 PM
Camel
You're still using the sign extending shift right operator, for one. Other than that, all I can really say is that you should run it in a debugger and compare what the C code does to the Java code, so you can pinpoint the areas where things are going wrong.
December 2, 2009, 2:11 AM
mime
If will change ">>" on ">>>" the same result :(
December 2, 2009, 3:23 AM
Camel
[quote author=Camel link=topic=18121.msg183794#msg183794 date=1259719909]
run it in a debugger and compare what the C code does to the Java code
[/quote]
December 3, 2009, 7:55 PM

Search