• Welcome to Valhalla Legends Archive.
 

[PHP] Packet Buffer

Started by Joe[x86], December 13, 2005, 10:21 PM

Previous topic - Next topic

Joe[x86]

Changelog
13 December 2005: Work began.
13 December 2005: Added DWORD creation.
14 December 2005: Added removal functions.
14 December 2005: Made numerous two-second bugfixes due to stupid things I forgot.
17 December 2005: Updated.

Example - Creating SID_AUTH_INFO
insert_int32(0);
insert_void("68XIRATS");
insert_int32(0xCD);
insert_int32(0);
insert_int32(0);
insert_int32(0);
insert_int32(0);
insert_int32(0);
insert_string("CAN");
insert_string("Canada");
$data = return_bncs(0x50);


The Code~
<?
  /*
   * PHP Packet Buffer
   *
   * Module:  pbuffer.php
   * Author:  Joe LaFrance
   * Purpose: Constructing packets.
   *
   * Copyright (C) 2005 Joe LaFrance
  */
 
  error_reporting(E_ALL);
 
  $buffer = "";
  // This is where we'll construct the packet
 
 
  /*
   * Create packets
  */
  /* Attaches the BNCS header to a string of data */
  function return_bncs($packetID) {
  global $buffer;
    $ret = chr(0xFF) . chr($packetID) . make_int16(strlen($buffer) + 4) . $buffer;
    $buffer = "";
    return $ret;
  }
  function return_bnls($packetID) {
  global $buffer;
    $ret = make_int16(strlen($buffer) + 3) . chr($packetID) . $buffer;
    $buffer = "";
    return $ret;
  }
  function return_raw() {
  $ret = $buffer;
  $buffer = "";
  return $ret;
  }
  /* Insert an NTString, also just called a string */
  function insert_string($data) {
  global $buffer;
  $buffer = $buffer . $data . chr(0);
  }
  /* Appends a void, also known as NonNTString */
  function insert_void($data) {
  global $buffer;
    $buffer .= $data;
  }
  /* Appends a little endian 8-bit integer (BYTE) */
  function insert_int8($data) {
  global $buffer;
    $buffer .= chr($data);
  }
  /* Appends a little endian 16-bit integer (WORD) */
  function insert_int16($data) {
  global $buffer;
    $buffer .=  make_int16($data);
  }
  /* Append a little endian 32-bit integer (DWORD) */
  function insert_int32($data) {
  global $buffer;
    $buffer .= make_int32($data);
  }
 
 
 
  /*
   * Packet "debuffer" functions
  */
  /* Set the buffer data */
  function setbuffer($data) {
  global $buffer;
    $buffer = $data;
  }
  /* Clear the buffer */
  function clearbuffer() {
  global $buffer;
  $buffer = "";
  }
  /* Remove the header */
  function remove_header() {
  global $buffer;
    $buffer = substr($buffer, 4);
  }
  /* Remove a BNLS header */
  function remove_header_bnls() {
  global $buffer;
    $buffer = substr($buffer, 3);
  }
  /* Remove a null terminated string */
  function remove_string() {
  global $buffer;
    $position = strpos($buffer, chr(0));
    $ret = substr($buffer, 0, $position);
    $buffer = substr($buffer, $position);
    return $ret;
  }
  /* Remove a void of length $len */
  function remove_void($len) {
  global $buffer;
  $ret = substr($buffer, 0, $len);
  $buffer = substr($buffer, $len);
    return $ret;
  }
  /* Remove a 8-bit little endian integer (BYTE) */
  function remove_int8() {
  global $buffer;
    $ret = (int)substr($buffer, 0, 1);
    $buffer = substr($buffer, 1);
  }
  /* Remove a 16-bit little endian integer (WORD) */
  function remove_int16() {
  global $buffer;
    $ret = get_int16(substr($buffer, 0, 2));
    $buffer = substr($buffer, 2);
    return $ret;
  }
  /* Remove a 32-bit little endian integer (WORD) */
  function remove_int32() {
  global $buffer;
    $ret = get_int32(substr($buffer, 0, 4));
    $buffer = substr($buffer, 4);
    return $ret;
  }
 



  /*
   * Create integral data structures
  */
  /* Create a little-endian 16-bit integer (WORD) */
  function make_int16($w) {
  $A = (((int)$w & 0x00FF) >> 0);
  $B = (((int)$w & 0xFF00) >> 8);
    return chr($A) . chr($B);
  }
  /* Create a little-endian 32-bit integer (DWORD) */
  function make_int32($d) {
    $A = (((int)$d & 0x000000FF) >> 0);
    $B = (((int)$d & 0x0000FF00) >> 8);
    $C = (((int)$d & 0x00FF0000) >> 16);
    $D = (((int)$d & 0xFF000000) >> 24);
    return chr($A) . chr($B) . chr($C) . chr($D);
  }
  /* Parse a little-endian 16-bit integer (WORD) */
  function get_int16($data) {
    $a = strrev(str_pad(dechex(ord(substr($data, 0, 1))), 2, '0', STR_PAD_LEFT));
    $b = strrev(str_pad(dechex(ord(substr($data, 1, 1))), 2, '0', STR_PAD_LEFT));
    return hexdec(strrev($a . $b));
  }
  /* Parse a little-endian 32-bit integer (DWORD) */
  function get_int32($data) {
    $a = strrev(str_pad(dechex(ord(substr($data, 0, 1))), 2, '0', STR_PAD_LEFT));
    $b = strrev(str_pad(dechex(ord(substr($data, 1, 1))), 2, '0', STR_PAD_LEFT));
    $c = strrev(str_pad(dechex(ord(substr($data, 2, 1))), 2, '0', STR_PAD_LEFT));
    $d = strrev(str_pad(dechex(ord(substr($data, 3, 1))), 2, '0', STR_PAD_LEFT));
    return hexdec(strrev($a . $b . $c . $d));
  }


  /*
   * Other stuff
  */
  function buffer_toString() {
  global $buffer;
    return debugOutput($buffer);
  }
  function buffer_clear() {
  global $buffer;
  $buffer = "";
  }

?>
Quote from: brew on April 25, 2007, 07:33 PM
that made me feel like a total idiot. this entire thing was useless.