Page 1 of 1

Having touble converting from Java -> PureBasic

Posted: Sat Aug 18, 2012 4:07 pm
by SoFlawLess_
Hi everyone, I'm attempting to copy over a game server written in Java. I'm almost through the login decoding but this is the part that is stopping me from finishing.

Java Vesion:

Code: Select all

	public int readDWord() {
		currentOffset += 4;
		return ((buffer[currentOffset - 4] & 0xff) << 24) + ((buffer[currentOffset - 3] & 0xff) << 16) + ((buffer[currentOffset - 2] & 0xff) << 8) + (buffer[currentOffset - 1] & 0xff);
	}
	public long readQWord() {
		long l = (long)readDWord() & 0xffffffffL;
		long l1 = (long)readDWord() & 0xffffffffL;
		return (l << 32) + l1;
	}
My Attempt

Code: Select all

Procedure.l ReadDWord(*MemoryBuffer)
  dword = ((PeekB(*MemoryBuffer) & $FF) << 24) + ((PeekB(*MemoryBuffer + 1) & $FF) << 16) + ((PeekB(*MemoryBuffer + 2) & $FF) << 8) + (((PeekB(*MemoryBuffer + 3) & $FF)))
  ProcedureReturn dword
EndProcedure

Procedure.l ReadQWord(*MemoryBuffer)
		l = ReadDWord(*MemoryBuffer) & 4294967295;
		l1 = ReadDWord(*MemoryBuffer + 4) & 4294967295;
		l2 = (l << 2)
		ProcedureReturn (l2 << 30) + l1
EndProcedure
I've tried using PeekQ and PeekL but they aren't working. Maybe the problem is I can't use 32 left shift? PB is only allowing 31 as the maximum. Please help

Re: Having touble converting from Java -> PureBasic

Posted: Sat Aug 18, 2012 4:20 pm
by Demivec
SoFlawLess_ wrote:I've tried using PeekQ and PeekL but they aren't working. Maybe the problem is I can't use 32 left shift? PB is only allowing 31 as the maximum.
When you say PeekQ and PeekL aren't working, did you try it like this?

Code: Select all

Procedure.l ReadDWord(*MemoryBuffer)
  ProcedureReturn PeekL(*MemoryBuffer)
EndProcedure

Procedure.q ReadQWord(*MemoryBuffer)
  ProcedureReturn PeekQ(*MemoryBuffer)
EndProcedure
I noticed that the return value that you had for the procedure ReadQWord() was specified as a long, it should be a quad. Perhaps that was the cause of your problems.

Re: Having touble converting from Java -> PureBasic

Posted: Sat Aug 18, 2012 4:22 pm
by SoFlawLess_
Demivec wrote:
SoFlawLess_ wrote:I've tried using PeekQ and PeekL but they aren't working. Maybe the problem is I can't use 32 left shift? PB is only allowing 31 as the maximum.
When you say PeekQ and PeekL aren't working, did you try it like this?

Code: Select all

Procedure.l ReadDWord(*MemoryBuffer)
  ProcedureReturn PeekL(*MemoryBuffer)
EndProcedure

Procedure.q ReadQWord(*MemoryBuffer)
  ProcedureReturn PeekQ(*MemoryBuffer)
EndProcedure
I noticed that the return value that you had for the procedure ReadQWord() was specified as a long, it should be a quad. Perhaps that was the cause of your problems.
Thank you so much it's working now :D

Re: Having touble converting from Java -> PureBasic

Posted: Sat Aug 18, 2012 4:25 pm
by Demivec
SoFlawLess_ wrote:Thank you so much it's working now :D
Your welcome.

I noticed the original code seem to advance the buffer pointer, you may want to add that back in.