Valhalla Legends Archive

Programming => General Programming => Visual Basic Programming => Topic started by: teK on October 02, 2004, 01:29 PM

Title: Help With Porting Code From C++
Post by: teK on October 02, 2004, 01:29 PM

unsigned char *GamePacketSize(unsigned char *data, unsigned int *size,
                            unsigned int *offset)
{
  unsigned int a;

  if (data[0] < 0xF0) {
      *size = data[0] - 1;
      *offset = 1;
      return &data[1];
  }

  a = (data[0] & 0xF) << 8;
  *size = a + data[1] - 2;
  *offset = 2;
  return &data[2];
}


Anyone mind porting/help porting this to VB?

Thanks.
Title: Re: Help With Porting Code From C++
Post by: CrAz3D on October 02, 2004, 04:05 PM
I'm sure there is some awesome tutorial that you can find through google, I mean google pwns my mom even!
Title: Re: Help With Porting Code From C++
Post by: UserLoser. on October 02, 2004, 04:45 PM
Quote from: Infamous on October 02, 2004, 01:29 PM
Anyone mind porting/help porting this to VB?

Thanks.

I believe that code is old and outdated, but anyways shouldn't be hard at all
Title: Re: Help With Porting Code From C++
Post by: teK on October 02, 2004, 04:53 PM
Quote from: UserLoser on October 02, 2004, 04:45 PM
Quote from: Infamous on October 02, 2004, 01:29 PM
Anyone mind porting/help porting this to VB?

Thanks.

I believe that code is old and outdated, but anyways shouldn't be hard at all

Never knew that :( , oh well. Anyone know where I can find an updated version?
Title: Re: Help With Porting Code From C++
Post by: UserLoser. on October 02, 2004, 07:14 PM
Quote from: Infamous on October 02, 2004, 04:53 PM
Quote from: UserLoser on October 02, 2004, 04:45 PM
Quote from: Infamous on October 02, 2004, 01:29 PM
Anyone mind porting/help porting this to VB?

Thanks.

I believe that code is old and outdated, but anyways shouldn't be hard at all

Never knew that :( , oh well. Anyone know where I can find an updated version?


Since you havn't told anyone what it's for, I will.  This function is used to calculate the size of a D2GS compressed packet, from the server before it's actually decompressed.  I'm not exactly sure if that's working or not, but IIRC, the code someone posted here a while back (which is that) didn't work properly.  Also, you can find it in D2Net.dll
Title: Re: Help With Porting Code From C++
Post by: LivedKrad on October 03, 2004, 04:13 AM
Let me see...


Private/Public Function GamePacketSize (ByRef data() As Integer?, ByVal size as Integer,ByVal offset As Integer) As Integer?

Dim a As Integer

If data(0) < &HF Then
size = data(0) - 1
offset = 1
GamePacketSize = data(1)
End If

((((a = (data[0] & 0xF) << 8;))) Some sort of shift operation?
size = a + data(1) - 2
offset = 2
GamePacketSize = data(2)



Hope that helps at all =(

Edit: Changed "GamePacketSize = data[1]" to (1)
Title: Re: Help With Porting Code From C++
Post by: drivehappy on October 03, 2004, 12:54 PM

Private Function GamePacketSize (ByRef data() As Byte, ByRef size As Integer, ByRef offset As Integer) As Byte
   Dim a As Integer

    If data(0) < &HF0 Then
        size = data(0) - 1
        offset = 1
        GamePacketSize = data(1)
    Else
         a = ((data(0) And &HF) * 256)
         size = a + data(1) - 2
         offset = 2
         GamePacketSize = data(2)
    End If

End Function


You will probably want a shift left function instead of multiplying it by 256 because of overflow errors if the data is too large.
Title: Re: Help With Porting Code From C++
Post by: UserLoser. on October 03, 2004, 01:56 PM
Quote from: drivehappy on October 03, 2004, 12:54 PM

    If data(0) < &HF Then


You will probably want a shift left function instead of multiplying it by 256 because of overflow errors if the data is too large.

Note: both you and LivedKrad failed to realize it's 0xF0, not 0xF.
Title: Re: Help With Porting Code From C++
Post by: LivedKrad on October 03, 2004, 05:37 PM
Hmm, then it would seem Grok's explanation in Op [vL] was inaccurate. I believe he explained about creating a new string with the data located at data[0] plus 1111. According to this however, 1111 is wrong. I believe 0xF0 is 240, not 15. Sorry for the confusion [original poster]. I don't know C++ so if there were some inconsistencies within the code I'm sorry. And, before UserLoser says it, I'm not saying mistaking 0xF and 0xF0 was concerning knowledge of C++. That was my mistake, :D
Title: Re: Help With Porting Code From C++
Post by: Grok on October 03, 2004, 05:51 PM
Quote from: LivedKrad on October 03, 2004, 05:37 PM
Hmm, then it would seem Grok's explanation in Op [vL] was inaccurate. I believe he explained about creating a new string with the data located at data[0] plus 1111. According to this however, 1111 is wrong. I believe 0xF0 is 240, not 15. Sorry for the confusion [original poster]. I don't know C++ so if there were some inconsistencies within the code I'm sorry. And, before UserLoser says it, I'm not saying mistaking 0xF and 0xF0 was concerning knowledge of C++. That was my mistake, :D

Wow, blaming me?

[13:07:40] <LivedKrad> still makes no sense what that operation is: a = (data[0] & 0xF) << 8;

I responded in the channel the code you posted in the channel.
Title: Re: Help With Porting Code From C++
Post by: LivedKrad on October 03, 2004, 05:55 PM
Well, guess it's my fault then. Sorry.
Title: Re: Help With Porting Code From C++
Post by: drivehappy on October 03, 2004, 06:08 PM
Quote from: UserLoser on October 03, 2004, 01:56 PM
Quote from: drivehappy on October 03, 2004, 12:54 PM

    If data(0) < &HF Then


You will probably want a shift left function instead of multiplying it by 256 because of overflow errors if the data is too large.

Note: both you and LivedKrad failed to realize it's 0xF0, not 0xF.
Fixed. I was trying to save typing and copied both LivedKrad's and the original C++ code into the box. Goes to show shortcutting doesn't pay, plus the post message textbox is too small vertical wise so I can't see all of the code.
Title: Re: Help With Porting Code From C++
Post by: l)ragon on October 03, 2004, 09:04 PM
a = ((data(0) And &HF)  * (2 ^ 8))

Edit: dont use 256 the way that you are 9p
Title: Re: Help With Porting Code From C++
Post by: drivehappy on October 03, 2004, 09:56 PM
Quote from: dRAgoN on October 03, 2004, 09:04 PM
a = ((data(0) And &HF)  * (2 ^ 8))

Edit: dont use 256 the way that you are 9p
Why not? Last time I checked 2^8 = 256
Title: Re: Help With Porting Code From C++
Post by: l)ragon on October 04, 2004, 01:47 AM
Quote from: drivehappy on October 03, 2004, 09:56 PM
Quote from: dRAgoN on October 03, 2004, 09:04 PM
a = ((data(0) And &HF)  * (2 ^ 8))

Edit: dont use 256 the way that you are 9p
Why not? Last time I checked 2^8 = 256

test which one breaks then lol bottom one or the top one

dim x as long, x2 as long
x = 255 * 256
x2 = 255 * (2 ^ 8)

edit: typo
edit2: yes 2^8 = 256
Title: Re: Help With Porting Code From C++
Post by: LivedKrad on October 04, 2004, 12:02 PM
Why are you using the logical "AND"? Would it not make more sense to use the appendage &?
Title: Re: Help With Porting Code From C++
Post by: drivehappy on October 04, 2004, 01:34 PM
@LivedKrad:
I don't believe VB uses the '&' for bitwise operations. The And in this case acts as a bitwise AND, not logical.

@dRAgoN:
I don't have VB6 installed, tried it on VB.NET and it works fine - I have no idea why it should fail.
Title: Re: Help With Porting Code From C++
Post by: l)ragon on October 04, 2004, 02:31 PM
im kinda sure this board was for vb 6.
Title: Re: Help With Porting Code From C++
Post by: LivedKrad on October 04, 2004, 04:59 PM
I [wasn't] saying to use & as a bitwise operator, seeing as how this isn't a bitwise operation (the string appending). All you're doing, (on the left operand of the shift operation), is appending &HF0 to the original string. In which case, you would use the appending &.

Edit in []
Title: Re: Help With Porting Code From C++
Post by: drivehappy on October 04, 2004, 05:52 PM
@LivedKrad:
No, the & in C++ is a bitwise AND operation - and it's &HF, not &HF0.

@dRAgoN:
Seeing as how I explicitly mentioned I did not have VB6 installed and do not see what the problem is, I suggest you stop running around in circles and tell me flat out what the issue is.
Title: Re: Help With Porting Code From C++
Post by: Stealth on October 04, 2004, 06:00 PM
As far as I know, & serves exclusively to concatenate strings in VB6.
Title: Re: Help With Porting Code From C++
Post by: drivehappy on October 04, 2004, 10:43 PM
Ok, to clarify for LivedKrad:

a = (data[0] & 0xF) << 8;


Means: AND (bitwise) 0xF with data[ 0 ], store in data[ 0 ], then shift data[ 0 ] left 8 bits.
Since we're originally working in C notation there is no appending of data.

EDIT: Reformatted data[ 0 ].
Title: Re: Help With Porting Code From C++
Post by: LivedKrad on October 05, 2004, 04:44 PM
Once again my lack of knowledge in C++ syntax and operators fails me again. ;\
Title: Re: Help With Porting Code From C++
Post by: TheMinistered on October 06, 2004, 09:55 PM

Public Function GamePacketSize(ByRef bytData() As Byte, ByRef lngSize As Long, ByRef lngOffset As Long) As Long
    Dim objBitwiseOperator As clsBitwiseOperator
   
    Set objBitwiseOperator = New clsBitwiseOperator
   
    If (bytData(0) < &HF0) Then
        lngSize = (bytData(0) - 1)
        lngOffset = 1
        GamePacketSize = VarPtr(bytData(1))
        Exit Function
    End If
   
    lngSize = objBitwiseOperator.ShiftLeft((bytData(0) And &HF), 8) + bytData(1) - 2
    lngOffset = 2
    GamePacketSize = VarPtr(bytData(2))
End Function



'clsBitwiseOperator

Option Explicit

Private Declare Function GlobalAlloc Lib "kernel32" (ByVal wFlags As Long, ByVal dwBytes As Long) As Long
Private Declare Function GlobalFree Lib "kernel32" (ByVal hMem As Long) As Long
Private Declare Function GlobalLock Lib "kernel32" (ByVal hMem As Long) As Long
Private Declare Function GlobalUnlock Lib "kernel32" (ByVal hMem As Long) As Long

Private Declare Sub RtlMoveMemory Lib "kernel32" (Destination As Any, Source As Any, ByVal Length As Long)

Private Type tPD
    hMem                As Long
    PtrToOldCode        As Long
End Type
Private ProcDetails()   As tPD

Private VTIndex         As Long
Private Code            As Byte
Private CodeSize        As Long
Private PtrToNewCode    As Long
Private PtrToMyself     As Long
Private i               As Long

Private Sub Class_Initialize()
    VTIndex = -1    'initialize index into Virtual Table
    ShiftLeft 0, 0  'this sets up m/c code and modifies the VT
    ShiftRight 0, 0  'this sets up m/c code and modifies the VT
    RotateLeft 0, 0  'this sets up m/c code and modifies the VT
    RotateRight 0, 0  'this sets up m/c code and modifies the VT
End Sub

Public Function ShiftLeft(ByVal lngValue As Long, ByVal lngShift As Long) As Long

  'this is in fact only called once during class initialize
  'subsequent calls are diverted (via the VT) to the m/c code

    DivertTo "8B442408 8B4C240C 8B542410 D3E0 8902 31C0 C21000"

End Function

Public Function ShiftRight(ByVal lngValue As Long, ByVal lngShift As Long) As Long

  'this is in fact only called once during class initialize
  'subsequent calls are diverted (via the VT) to the m/c code

    DivertTo "8B442408 8B4C240C 8B542410 D3E8 8902 31C0 C21000"

End Function

Public Function RotateLeft(ByVal lngValue As Long, ByVal lngRotate As Long) As Long

  'this is in fact only called once during class initialize
  'subsequent calls are diverted (via the VT) to the m/c code

    DivertTo "8B442408 8B4C240C 8B542410 D3C0 8902 31C0 C21000"

End Function

Public Function RotateRight(ByVal lngValue As Long, ByVal lngRotate As Long) As Long

  'this is in fact only called once during class initialize
  'subsequent calls are diverted (via the VT) to the m/c code

    DivertTo "8B442408 8B4C240C 8B542410 D3C8 8902 31C0 C21000"

End Function

Private Sub DivertTo(ByVal HexCode As String)

    VTIndex = VTIndex + 1 'inc index into VT
    ReDim Preserve ProcDetails(0 To VTIndex) 'adjust array size
   
    HexCode = Replace$(HexCode, " ", "") 'remove spaces from hex code
    CodeSize = Len(HexCode) / 2 'length of the resulting binary code (2 hex chars per byte of code)

    With ProcDetails(VTIndex)
        .hMem = GlobalAlloc(0, CodeSize) 'get memory for m/c code and save handle
        PtrToNewCode = GlobalLock(.hMem) 'get far pointer to allocated memory

        For i = 0 To CodeSize - 1
            Code = Val("&H" & Mid$(HexCode, i + i + 1, 2)) 'convert hex to binary m/c code
            RtlMoveMemory ByVal PtrToNewCode + i, Code, 1 'store it in allocated memory
        Next i

        .PtrToOldCode = VirtualTableEntry 'save old VT entry; VTIndex determines which entry
        VirtualTableEntry = PtrToNewCode 'overwrite VT entry; VTIndex determines which entry
        GlobalUnlock .hMem 'unlock memory
    End With 'PROCDETAILS(VTINDEX)

End Sub

Private Property Let VirtualTableEntry(ByVal FarPointer As Long)

    RtlMoveMemory PtrToMyself, ByVal ObjPtr(Me), 4 'get pointer to object (Me)
    RtlMoveMemory ByVal PtrToMyself + &H1C + VTIndex * 4, FarPointer, 4 'put VT entry

End Property

Private Property Get VirtualTableEntry() As Long

    RtlMoveMemory PtrToMyself, ByVal ObjPtr(Me), 4 'get pointer to object (Me)
    RtlMoveMemory VirtualTableEntry, ByVal PtrToMyself + &H1C + VTIndex * 4, 4 'get VT entry

End Property

Private Sub Class_Terminate()

    For VTIndex = VTIndex To 0 Step -1 'VTIndex still points to the last VT entry overwritten
        With ProcDetails(VTIndex)
            VirtualTableEntry = .PtrToOldCode 'restore VT entry; VTIndex determines which entry
            GlobalFree .hMem 'release memory used for m/c code
        End With 'PROCDETAILS(VTINDEX)
    Next VTIndex

End Sub