• Welcome to Valhalla Legends Archive.
 

PacketDebuffer Class

Started by Stealth, March 06, 2005, 11:56 PM

Previous topic - Next topic

Stealth

I went to rewrite a big chunk of StealthBot code on Friday and Saturday (parts were hideous -- I actually had two entirely separate BNCS packet handler subroutines, one for BNLS-enabled connections and one for locally-hashed ones) and I remembered a little packet debuffer class written by Sorc.Polgara. I had really liked the idea at the time, but lacked motivation to install it.

This time around, however, it got done. For the benefit of others who want to use this neat little debuffer class, I'm going to post my modified version of Sorc's class. There were some problems with Sorc's class; namely, the CopyMemory calls had some IDE-crashing issues, and some stuff was being done with too much complexity, which is fixed. I also added some bounds-checking code to it, which can never hurt.

Here's the result. Enjoy, and feel free to comment.

'=================================================
'PacketDebuffer Class
'By Bethra, aka. Sorc.Polgara =)
'=================================================
'Modified/Fixed March 4-5, 2005
'  by Andy T, aka Stealth
[email protected]
'
' Changes:
'   - Added bounds checking code
'   - Removed unnecessary CopyMemory calls
'   - Fixed existing CopyMemory calls
'   - Added Advance(), DebuffRaw() and HasBytes()
'       functions
'=================================================
Option Explicit

' Uncomment this line if you don't already have CopyMemory declared
'Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (ByRef Destination As Any, ByVal Source As Any, ByVal numBytes As Long)

Public Debuffer As String      '// Debuffering string

'Sets the Debuffer string
Public Function DebuffPacket(PacketData As String)
   Debuffer = PacketData
End Function

'Resets/clears the Debuffer
Public Function Clear()
   Debuffer = vbNullString
End Function

'=======================================================
'Public Functions that debuffer a part from the Debuffer
'=======================================================

'Debuffers a DWORD from the Debuffer
Public Function DebuffDWORD() As Long
    If HasBytes(4) Then
        DebuffDWORD = GetDWORD
        RemoveDWORD
    End If
End Function

'Debuffers a WORD from the Debuffer
Public Function DebuffWORD() As Integer
    If HasBytes(2) Then
        DebuffWORD = GetWORD
        RemoveWORD
    End If
End Function

'Debuffers a BYTE from the Debuffer
Public Function DebuffBYTE() As Byte
    If HasBytes(1) Then
        DebuffBYTE = GetBYTE
        RemoveBYTE
    End If
End Function

'Debuffers a FILETIME from the Debuffer
' Edit by Andy: Returns the 8 bytes of the FILETIME struct
' The end-location will have to typecast this via CopyMemory into a FILETIME
' VB didn't like using a user-defined type as a return type or parameter type
'   in a public class object
Public Function DebuffFILETIME() As String
    If HasBytes(8) Then
        DebuffFILETIME = GetFILETIME
        RemoveFILETIME
    End If
End Function

'Debuffers a null-terminating string from the Debuffer
Public Function DebuffNTString() As String
    If HasBytes(1) Then
        DebuffNTString = GetNTString
        RemoveNTString
    Else
        DebuffNTString = ""
    End If
End Function

'Debuffers x bytes -- for those times when you want it straight-up
'(added by Andy)
Public Function DebuffRaw(ByVal nBytes As Long) As String
    If HasBytes(nBytes) Then
        DebuffRaw = Mid$(Debuffer, 1, nBytes)
        Call Advance(nBytes)
    End If
End Function

'=====================================================
'Public Functions that remove a part from the Debuffer
'=====================================================

'Removes a BYTE from the Debuffer
Public Sub RemoveBYTE()
    Debuffer = Mid$(Debuffer, 2)
End Sub

'Removes a WORD from the Debuffer
Public Sub RemoveWORD()
    Debuffer = Mid$(Debuffer, 3)
End Sub

'Removes a DWORD from the Debuffer
Public Sub RemoveDWORD()
    Debuffer = Mid$(Debuffer, 5)
End Sub

'Removes a FILETIME structure from the Debuffer
Public Sub RemoveFILETIME()
    Debuffer = Mid$(Debuffer, 9)
End Sub

'Removes a null-terminating string from the Debuffer
Public Sub RemoveNTString()
    Dim Pos As Integer
    Pos = InStr(1, Debuffer, Chr(0), vbBinaryCompare)
   
    If Len(Debuffer) > Pos Then
        Debuffer = Mid$(Debuffer, Pos + 1)
    Else
        Call Clear
    End If
End Sub

'Removes nBytes bytes from the buffer
' For those times when you just don't care what's there..
'(added by Andy)
Public Sub Advance(ByVal nBytes As Long)
    If Len(Debuffer) > nBytes Then
        Debuffer = Mid$(Debuffer, nBytes + 1)
    Else
        Call Clear
    End If
End Sub


'=======================================================
'Functions that get parts from the front of the Debuffer
'=======================================================

'Gets a BYTE from the Debuffer
Function GetBYTE() As Byte
    Dim PBYTE As Byte
    PBYTE = Asc(Mid$(Debuffer, 1, 1))
    GetBYTE = PBYTE
End Function

'Gets a WORD from the Debuffer
Function GetWORD() As Integer
    Dim WORD As Integer
    Dim sTemp As String * 2
   
    sTemp = Mid$(Debuffer, 1, 2)
    CopyMemory WORD, ByVal sTemp, 2
   
    GetWORD = WORD
End Function

'Gets a DWORD from the Debuffer
Function GetDWORD() As Long
    Dim DWORD As Long
    Dim sTemp As String * 4
   
    sTemp = Mid$(Debuffer, 1, 4)
    CopyMemory DWORD, ByVal sTemp, 4
   
    GetDWORD = DWORD
End Function

'Gets a FILETIME from the Debuffer
Function GetFILETIME() As String
    GetFILETIME = Mid$(Debuffer, 1, 8)
End Function

'Gets a null-terminating string from the Debuffer
Function GetNTString() As String
    Dim NTString As String
    Dim Pos As Integer
   
    Pos = InStr(1, Debuffer, Chr(0), vbBinaryCompare)
    NTString = Mid$(Debuffer, 1, Pos - 1)
   
    GetNTString = NTString
End Function


'Returns TRUE if the debuffer has >= X bytes in it, else FALSE
'(added by Andy)
Function HasBytes(ByVal X As Integer) As Boolean
    HasBytes = (Len(Debuffer) >= X)
End Function

'                 (Sorc.Polgara's)
'=====================Credits============================
'DarkMinion for using his PacketBuffer class as a guide,
'this is the first class I have ever made =)
'-----------------
'Bot Developement Forum members who helped me understand
'some stuff that I need to know inorder to make this =)
'=======================================================
- Stealth
Author of StealthBot

Sorc.Polgara

lmao, I totally forgot about this class I wrote!

Lost it when my harddrive screwed up!

kekekeke

QwertyMonster

Sorc.Polgara is happy!  :o


Stealth; It all seems good peice of coding! :)

And its good you have put who its by, and you thank them ect..

Now people cant come and say "Code Stealer", like most newbs would do

** No Names Mentioned **  ::) :P

Ban


Sorc.Polgara

#4
Quote from: QwertyMonster on March 08, 2005, 09:56 AM
Stealth; It all seems good peice of coding! :)

And its good you have put who its by, and you thank them ect..

Now people cant come and say "Code Stealer", like most newbs would do

** No Names Mentioned **  ::) :P

Yeah that is the right way of doing things.  The GM (Good mannar) way.

keke, it is also nice to know that Stealth is using my class for his well known bot =)

I use SB heh atm, while I program mine.  kekeke

QwertyMonster

Quote from: Sorc.Polgara on March 10, 2005, 08:02 AM
Quote from: QwertyMonster on March 08, 2005, 09:56 AM
Stealth; It all seems good peice of coding! :)

And its good you have put who its by, and you thank them ect..

Now people cant come and say "Code Stealer", like most newbs would do

** No Names Mentioned **  ::) :P

Yeah that is the right way of doing things.  The GM (Good mannar) way.

keke, it is also nice to know that Stealth is using my class for his well known bot =)

I use SB heh atm, while I program mine.  kekeke


You will only get accused of "stealing code" if you just take it without the authors permission, or without naming whos it by or anything.

Doing this is extremely lame, and it takes the fun out of programming   :-\

Mephisto

Too bad it was coded in VB...

UserLoser.


Mephisto


UserLoser.

Quote from: Mephisto on March 10, 2005, 05:39 PM
Quote from: UserLoser on March 10, 2005, 02:29 PM
Quote from: Mephisto on March 10, 2005, 01:59 PM
Too bad it was coded in VB...

...otherwise you'd copy it? :P

Why would you say/think that?

You took this the wrong way.  But anyways, because I know you don't code in VB and you said what you said with a trailing "..." meaning there's probably more to what you said ;)

Warrior

<attempt to put us back on topic>
Good job Stealth
</attempt to put us back on topic>
Quote from: effect on March 09, 2006, 11:52 PM
Islam is a steaming pile of fucking dog shit. Everything about it is flawed, anybody who believes in it is a terrorist, if you disagree with me, then im sorry your wrong.

Quote from: Rule on May 07, 2006, 01:30 PM
Why don't you stop being American and start acting like a decent human?

QwertyMonster

Quote from: Warrior on March 11, 2005, 02:27 PM
<attempt to put us back on topic>
Good job Stealth
</attempt to put us back on topic>

Attempt worked.

Nice stealth. 8)

Zakath

Booo! Down with debuffering class! Gogo pointer typecasting!

:P
Quote from: iago on February 02, 2005, 03:07 PM
Yes, you can't have everybody...contributing to the main source repository.  That would be stupid and create chaos.

Opensource projects...would be dumb.