• Welcome to Valhalla Legends Archive.
 

Need help for ported C code to Java

Started by mime, November 30, 2009, 03:31 AM

Previous topic - Next topic

mime

Hello who can help ported  this http://forum.valhallalegends.com/index.php?topic=585.msg4318#msg4318 code on Java ?

I will be very grateful for any help.

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.

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.
Quote from: Hazard on August 07, 2003, 03:15 PM
Highlight your entire code. Press the delete key. Start over again using Cuphead's CSB tutorial and work your way from their rather than raping code from downloaded sources meant purely for learning purposes. If this does not fix the problem, uninstall Visual Basic and get a new hobby. I suggest Cricket.

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!

Camel

#4
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

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.

Camel

#6
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.

mime

I create the buffer with the size which function getpacketsize () returns and I transfer it the buffer in gamepacketdecode.

http://pastebin.com/m5410ca3a

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: byte[] gamePacketDecode(byte[] in), and throwing an Exception if there's a decoding error.

mime

#9
Camel, I have made as you have told, move out all in one gamePacketDecode method.

Method gamePacketDecode and main method:
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]) + " ");
}
   }


Method output:

Length:6
Offset:1
Packet:
2f 2f 2f 2f 0 0


I followed an example of a packet from here: http://forum.valhallalegends.com/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?

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.

mime

If will change ">>" on ">>>" the same result :(

Camel

Quote from: Camel on December 01, 2009, 08:11 PM
run it in a debugger and compare what the C code does to the Java code