Valhalla Legends Forums Archive | Visual Basic Programming | Help With Porting Code From C++

AuthorMessageTime
Infamous
[code]
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];
}
[/code]

Anyone mind porting/help porting this to VB?

Thanks.
October 2, 2004, 6:29 PM
CrAz3D
I'm sure there is some awesome tutorial that you can find through google, I mean google pwns my mom even!
October 2, 2004, 9:05 PM
UserLoser.
[quote author=Infamous link=topic=8979.msg82959#msg82959 date=1096741753]
Anyone mind porting/help porting this to VB?

Thanks.
[/quote]

I believe that code is old and outdated, but anyways shouldn't be hard at all
October 2, 2004, 9:45 PM
Infamous
[quote author=UserLoser link=topic=8979.msg82977#msg82977 date=1096753527]
[quote author=Infamous link=topic=8979.msg82959#msg82959 date=1096741753]
Anyone mind porting/help porting this to VB?

Thanks.
[/quote]

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

Never knew that :( , oh well. Anyone know where I can find an updated version?
October 2, 2004, 9:53 PM
UserLoser.
[quote author=Infamous link=topic=8979.msg82980#msg82980 date=1096754018]
[quote author=UserLoser link=topic=8979.msg82977#msg82977 date=1096753527]
[quote author=Infamous link=topic=8979.msg82959#msg82959 date=1096741753]
Anyone mind porting/help porting this to VB?

Thanks.
[/quote]

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

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

[/quote]

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
October 3, 2004, 12:14 AM
LivedKrad
Let me see...

[code]
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)

[/code]

Hope that helps at all =(

Edit: Changed "GamePacketSize = data[1]" to (1)
October 3, 2004, 9:13 AM
drivehappy
[code]
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
[/code]

You will probably want a shift left function instead of multiplying it by 256 because of overflow errors if the data is too large.
October 3, 2004, 5:54 PM
UserLoser.
[quote author=drivehappy link=topic=8979.msg83070#msg83070 date=1096826043]
[code]
    If data(0) < &HF Then
[/code]

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

Note: both you and LivedKrad failed to realize it's 0xF0, not 0xF.
October 3, 2004, 6:56 PM
LivedKrad
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 [code]data[0][/code] 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
October 3, 2004, 10:37 PM
Grok
[quote author=LivedKrad link=topic=8979.msg83110#msg83110 date=1096843033]
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 [code]data[0][/code] 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
[/quote]

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.
October 3, 2004, 10:51 PM
LivedKrad
Well, guess it's my fault then. Sorry.
October 3, 2004, 10:55 PM
drivehappy
[quote author=UserLoser link=topic=8979.msg83077#msg83077 date=1096829780]
[quote author=drivehappy link=topic=8979.msg83070#msg83070 date=1096826043]
[code]
    If data(0) < &HF Then
[/code]

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

Note: both you and LivedKrad failed to realize it's 0xF0, not 0xF.
[/quote]
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.
October 3, 2004, 11:08 PM
dRAgoN
a = ((data(0) And &HF)  * (2 ^ 8))

Edit: dont use 256 the way that you are 9p
October 4, 2004, 2:04 AM
drivehappy
[quote author=dRAgoN link=topic=8979.msg83138#msg83138 date=1096855466]
a = ((data(0) And &HF)  * (2 ^ 8))

Edit: dont use 256 the way that you are 9p
[/quote]
Why not? Last time I checked 2^8 = 256
October 4, 2004, 2:56 AM
dRAgoN
[quote author=drivehappy link=topic=8979.msg83149#msg83149 date=1096858564]
[quote author=dRAgoN link=topic=8979.msg83138#msg83138 date=1096855466]
a = ((data(0) And &HF)  * (2 ^ 8))

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

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
October 4, 2004, 6:47 AM
LivedKrad
Why are you using the logical "AND"? Would it not make more sense to use the appendage &?
October 4, 2004, 5:02 PM
drivehappy
@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.
October 4, 2004, 6:34 PM
dRAgoN
im kinda sure this board was for vb 6.
October 4, 2004, 7:31 PM
LivedKrad
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 []
October 4, 2004, 9:59 PM
drivehappy
@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.
October 4, 2004, 10:52 PM
Stealth
As far as I know, & serves exclusively to concatenate strings in VB6.
October 4, 2004, 11:00 PM
drivehappy
Ok, to clarify for LivedKrad:
[code]
a = (data[0] & 0xF) << 8;
[/code]

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 ].
October 5, 2004, 3:43 AM
LivedKrad
Once again my lack of knowledge in C++ syntax and operators fails me again. ;\
October 5, 2004, 9:44 PM
TheMinistered
[code]
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
[/code]

[code]
'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
[/code]
October 7, 2004, 2:55 AM

Search