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.
To-do
Remove WORD / DWORD.
Test.
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 BNCS packets.
*
* Copyright (C) 2005 Joe LaFrance
*/
$buffer = "";
// This is where we'll construct the packet
function clear() {
$buffer = "";
}
/*
* Create packets
*/
/* Attaches the BNCS header to a string of data */
function return_bncs($packetID) {
return chr(0xFF) . chr($packetID) . make_int16(strlen($buffer)) . $data;
clear();
}
/* Insert an NTString, also just called a string */
function insert_string($data) {
$buffer = $buffer . $data . chr(0);
}
/* Appends a void, also known as NonNTString */
function insert_void($data) {
$buffer = $buffer . $data;
}
/* Appends a little endian 8-bit integer (BYTE) */
function insert_int8($data) {
$buffer = $buffer . chr($data);
}
/* Appends a little endian 16-bit integer (WORD) */
function insert_int16($data) {
$buffer = $buffer . make_int16($data);
}
/* Append a little endian 32-bit integer (DWORD) */
function insert_int32($data) {
$buffer = $buffer . make_int32($data);
}
/*
* Packet "debuffer" functions
*/
/* Set the buffer data */
function setbuffer($data) {
clear();
$buffer = $data;
}
/* Remove the header */
function remove_header() {
return substr($buffer, 0, 4);
$buffer = substr($buffer, 4);
}
/* Remove a null terminated string */
function remove_string() {
$ntpos = strpos ($buffer, chr(0));
$ret = substr($buffer, 0, $ntpos);
$buffer = substr(strstr($buffer, chr(0)), 1);
return $ret;
}
/* Remove a void of length $len */
function remove_void($len) {
$ret = substr($buffer, 0, $len);
$buffer = substr($buffer, $len+1);
return $ret;
}
/* Remove a 8-bit little endian integer (BYTE) */
function remove_int8() {
$ret = (int)substr($buffer, 0, 1);
$buffer = substr($buffer, 1);
}
/* Remove a 16-bit little endian integer (WORD) */
function remove_int16() {
// stub
}
/* Remove a 32-bit little endian integer (WORD) */
function remove_int32() {
// stub
}
/*
* Create integral data structures
*/
/* Create a little-endian 16-bit integer (WORD) */
function make_int16($data) {
$A = $data % (256 ^ 1);
$B = $data + $a;
return chr($A) . chr($B);
}
/* Create a little-endian 32-bit integer (DWORD) */
function make_int32($data) {
$A = $data % (256 ^ 3);
$B = $data % (256 ^ 2);
$C = $data % (256 ^ 1);
$D = $data - ($A + $B + $C);
return chr($A) . chr($B) . chr($C) . chr($D);
}
/* Parse a little-endian 16-bit integer (WORD) */
function get_int16() {
// stub
}
/* Parse a little-endian 32-bit integer (DWORD) */
function get_int32() {
// stub
}
?>
PHP isn't VisualBasic, there's an assignment operator for appending strings.
$v = "This is my string. ";
$v = $v . "I'm wasting keystrokes and clarity by doing this.";
$v = "I could just do this, ";
$v .= "which would make things a lot easier.";
I personally like using x = x op y instead of x op= y. It just looks so much cleaner to me :)
Quote from: iago on December 14, 2005, 07:34:05 PM
I personally like using x = x op y instead of x op= y. It just looks so much cleaner to me :)
It looks horrible in PHP, in my opinion. It really depends on the situation, but in this case, I'd definitely do x op= y.
I was wondering if its .= or += and I didn't want to take my chances. But thanks, though.
I was obviously doing int16 and int32 horribly, so, port from JavaOp2 (hope it works..):
/* Create a little-endian 16-bit integer (WORD) */
function make_int16($w) {
$ret = "";
$ret = $ret . (($w & 0x00FF) >> 0);
$ret = $ret . (($w & 0xFF00) >> 8);
return $ret;
}
/* Create a little-endian 32-bit integer (DWORD) */
function make_int32($d) {
$ret = "";
$ret = $ret . (($d & 0x000000FF) >> 0);
$ret = $ret . (($d & 0x0000FF00) >> 8);
$ret = $ret . (($d & 0x00FF0000) >> 16);
$ret = $ret . (($d & 0xFF000000) >> 24);
return $ret;
}
Quote from: Joe[e2] on December 14, 2005, 08:23:48 PM
I was wondering if its .= or += and I didn't want to take my chances. But thanks, though.
+= is for integers. .= is for strings.
Quote from: Sidoh on December 14, 2005, 08:41:21 PM
Quote from: Joe[e2] on December 14, 2005, 08:23:48 PM
I was wondering if its .= or += and I didn't want to take my chances. But thanks, though.
+= is for integers. .= is for strings.
O RLY? =p
Hm, I need to figure out how to do MakeDWORD and MakeWORD correctly.
Quote from: Joe[e2] on December 14, 2005, 10:17:36 PM
O RLY? =p
Hm, I need to figure out how to do MakeDWORD and MakeWORD correctly.
You're the one that questioned the obvious in the first place; I only answered it.