From 035a2609a8ee30d580026ba0c8b451e781c6e555 Mon Sep 17 00:00:00 2001 From: David Date: Fri, 18 Feb 2011 00:18:21 +0000 Subject: david - working php gats writer/parser, except doesn't do floats yet --- php/int.php | 108 ------------- php/phpgats.php | 468 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 468 insertions(+), 108 deletions(-) delete mode 100644 php/int.php create mode 100644 php/phpgats.php diff --git a/php/int.php b/php/int.php deleted file mode 100644 index 478af73..0000000 --- a/php/int.php +++ /dev/null @@ -1,108 +0,0 @@ - $b ) - $b |= 0x80 | 0x40; - else - $b |= 0x40; - } - else - { - $b = ($iIn&0x3f); - if( $iIn > $b ) - $b |= 0x80; - } - - $ret .= chr( $b ); - $iIn = $iIn >> 6; - - while( $iIn > 0 ) - { - $b = ($iIn&0x7f); - if( $iIn > $b ) - $b |= 0x80; - $ret .= chr( $b ); - $iIn = $iIn >> 7; - } - - return $ret; -} */ - -function writeInt( $iIn ) -{ - $ret = ""; - if( gmp_cmp( $iIn, 0 ) < 0 ) - { - $iIn = gmp_mul( $iIn, -1 ); - $b = gmp_intval( gmp_and( $iIn, 0x3f ) ); - if( gmp_cmp( $iIn, $b ) > 0 ) - $b |= 0x80 | 0x40; - else - $b |= 0x40; - } - else - { - $b = gmp_intval( gmp_and( $iIn, 0x3f ) ); - if( gmp_cmp( $iIn, $b ) > 0 ) - $b |= 0x80; - } - - $ret .= chr( $b ); - $iIn = gmp_div( $iIn, 64 ); - - while( gmp_cmp( $iIn, 0 ) > 0 ) - { - $b = gmp_intval( gmp_and( $iIn, 0x7f ) ); - if( gmp_cmp( $iIn, $b ) > 0 ) - $b |= 0x80; - $ret .= chr( $b ); - $iIn = gmp_div( $iIn, 128 ); - } - - return $ret; -} - -function readInt( $sIn, &$pos ) -{ - $neg = false; - - $b = ord($sIn[$pos++]); - if( ($b&0x40) == 0x40 ) - $neg = true; - $iOut = gmp_init( $b&0x3f ); - $mult = gmp_init( 64 ); - while( ($b&0x80) ) - { - $b = ord($sIn[$pos++]); - $iOut = gmp_or( $iOut, gmp_mul( $b&0x7f, $mult ) ); - $mult = gmp_mul( $mult, 128 ); - } - if( $neg == true ) - $iOut = gmp_mul( $iOut, -1 ); - - return $iOut; -} - -function teststr( $str ) -{ - $pos = 0; - $enc = writeInt( $str ); - $out = gmp_strval( readInt( $enc, $pos ) ); - print( $str . " => " . $out ); - if( $out == $str ) - print(", correct.\n"); - else - print(", incorrect.\n"); -} -//print( bin2hex( writeInt("3898993999921") ) . "\n" ); -teststr("-7"); - - -?> diff --git a/php/phpgats.php b/php/phpgats.php new file mode 100644 index 0000000..20e5ef2 --- /dev/null +++ b/php/phpgats.php @@ -0,0 +1,468 @@ + 0 ) + $b |= 0x80 | 0x40; + else + $b |= 0x40; + } + else + { + $b = gmp_intval( gmp_and( $iIn, 0x3f ) ); + if( gmp_cmp( $iIn, $b ) > 0 ) + $b |= 0x80; + } + + $ret .= chr( $b ); + $iIn = gmp_div( $iIn, 64 ); + + while( gmp_cmp( $iIn, 0 ) > 0 ) + { + $b = gmp_intval( gmp_and( $iIn, 0x7f ) ); + if( gmp_cmp( $iIn, $b ) > 0 ) + $b |= 0x80; + $ret .= chr( $b ); + $iIn = gmp_div( $iIn, 128 ); + } + + return $ret; +} + +function phpgats_readInt( $sIn, &$pos ) +{ + $neg = false; + + $b = ord($sIn[$pos++]); + if( ($b&0x40) == 0x40 ) + $neg = true; + $iOut = gmp_init( $b&0x3f ); + $mult = gmp_init( 64 ); + while( ($b&0x80) ) + { + $b = ord($sIn[$pos++]); + $iOut = gmp_or( $iOut, gmp_mul( $b&0x7f, $mult ) ); + $mult = gmp_mul( $mult, 128 ); + } + if( $neg == true ) + $iOut = gmp_mul( $iOut, -1 ); + + return $iOut; +} + +class phpgats_Boolean extends phpgats_Element +{ + public $elem = false; + + function __construct( $_elem ) + { + $this->elem = ($_elem==true)?true:false; + } + + function set( $_elem ) + { + $this->elem = ($_elem==true)?true:false; + } + + function get() + { + return $this->elem; + } + + function encode() + { + return ($this->elem==true)?"1":"0"; + } + + function getStr() + { + return ($this->elem==true)?"true":"false"; + } + + function getGatsType() + { + return "boolean"; + } +} + +class phpgats_String extends phpgats_Element +{ + public $elem = ""; + + function __construct( $_elem ) + { + $this->elem = $_elem . ""; + } + + function set( $_elem ) + { + $this->elem = $_elem . ""; + } + + function get() + { + return $this->elem; + } + + function encode() + { + return 's' . phpgats_writeInt(strlen($this->elem)) . $this->elem; + } + + function getStr() + { + return $this->get(); + } + + function getGatsType() + { + return "string"; + } +} + +class phpgats_Integer extends phpgats_Element +{ + public $elem = 0; + + function __construct( $_elem ) + { + if( getType($_elem) != "resource" ) + $this->elem = gmp_init($_elem); + } + + function set( $_elem ) + { + $this->elem = gmp_init($_elem); + } + + function get() + { + return $this->elem; + } + + function encode() + { + return "i" . phpgats_writeInt($this->elem); + } + + function getStr() + { + return gmp_strval($this->elem); + } + + function getGatsType() + { + return "integer"; + } +} + +class phpgats_Float extends phpgats_Element +{ + public $elem = 0; + + function __construct( $_elem ) + { + $this->elem = $_elem+0.0; + } + + function set( $_elem ) + { + $this->elem = $_elem+0.0; + } + + function get() + { + return $this->elem; + } + + function encode() + { + throw "Encoding Floats in phpgats doesn't yet work..."; + } + + function getStr() + { + return $this->elem+""; + } + + function getGatsType() + { + return "float"; + } +} + +class phpgats_List extends phpgats_Element +{ + public $elems = array(); + + function append( $_elem ) + { + if( !is_subclass_of( $_elem, "phpgats_Element" ) ) + throw new Exception( + "can only call phpgats_List::append with phpgats_Element." ); + array_push( $this->elems, $_elem ); + } + + function get() + { + return $this->elems; + } + + function size() + { + return count($this->elems); + } + + function encode() + { + $s_out = "l"; + foreach( $this->elems as $val ) + { + $s_out .= $val->encode(); + } + $s_out .= "e"; + return $s_out; + } + + function getGatsType() + { + return "list"; + } +} + +class phpgats_Dictionary extends phpgats_Element +{ + public $elems = array(); + + function append( $_name, $_val ) + { + if( !is_subclass_of( $_val, "phpgats_Element" ) || + gettype($_name)!="string") + throw new Exception( + "can only call phpgats_Dictionary::append with str,phpgats_Element." ); + $this->elems[$_name] = $_val; + } + + function get() + { + return $this->elems; + } + + function size() + { + return count($this->elems); + } + + function encode() + { + $s_out = "d"; + foreach( $this->elems as $key => $val ) + { + $n = new phpgats_String( $key ); + $s_out .= $n->encode(); + $s_out .= $val->encode(); + } + $s_out .= "e"; + return $s_out; + } + + function getGatsType() + { + return "dictionary"; + } +} + +function phpgats_pC( $str_data, $offset ) +{ + if($offset>strlen($str_data)) + throw new Exception("Not enough data"); +} + +function phpgats_pS( $str_data, &$offset, $dbg ) +{ + $str_tmp = ""; + $gmpSize = phpgats_readInt( $str_data, $offset ); + if( gmp_cmp( $gmpSize, 2147483647 ) > 0 ) + { + throw new Exception( + "size (" . gmp_strval($gmpSize) . ") > phpgats can handle\n"); + } + $iSize = gmp_intval($gmpSize); + $i=0; + $str_tmp = ""; + while( $i<$iSize ) + { + phpgats_pC( $str_data, $offset+1 ); + $str_tmp .= $str_data[$offset++]; + ++$i; + } + return new phpgats_String($str_tmp); +} + +function phpgats_pI( $str_data, &$offset, $dbg ) +{ + return new phpgats_Integer(phpgats_readInt($str_data, $offset)); +} + +function phpgats_pF( $str_data, &$offset, $dbg ) +{ + throw "The phpgats Float parsing code doesn't work yet..."; +} + +function phpgats_pL( $str_data, &$offset, $dbg ) +{ + phpgats_pC( $str_data, $offset ); + $c = $str_data[$offset]; + $l_out = new phpgats_List(); + while( $c != "e" ) + { + $obj = phpgats_pM( $str_data, $offset, $dbg ); + $l_out->append( $obj ); + phpgats_pC( $str_data, $offset ); + $c = $str_data[$offset]; + } + $offset++; + return $l_out; +} + +function phpgats_pD( $str_data, &$offset, $dbg ) +{ + phpgats_pC( $str_data, $offset ); + $c = $str_data[$offset]; + $d_out = new phpgats_Dictionary(); + while( $c != "e" ) + { + $obj1 = phpgats_pM( $str_data, $offset, $dbg ); + $obj2 = phpgats_pM( $str_data, $offset, $dbg ); + $d_out->append( $obj1->get(), $obj2 ); + phpgats_pC( $str_data, $offset ); + $c = $str_data[$offset]; + } + $offset++; + return $d_out; +} + +function phpgats_pM( $str_data, &$offset, $dbg=0 ) +{ + phpgats_pC( $str_data, $offset ); + $c = $str_data[$offset++]; + //for( $meme=0; $meme<$dbg; $meme++ ) + // echo " "; + switch( $c ) + { + case 'i': + echo "int:"; + $obj = phpgats_pI( $str_data, $offset, $dbg ); + echo gmp_strval($obj->get()) . "\n"; + return $obj; + break; + case 'l': + echo "list:\n"; + $obj = phpgats_pL( $str_data, $offset, $dbg+1 ); + return $obj; + break; + case 'd': + echo "dic:\n"; + $obj = phpgats_pD( $str_data, $offset, $dbg+1 ); + return $obj; + break; + case 'f': + echo "float:\n"; + $obj = phpgats_pF( $str_data, $offset, $dbg ); + return $obj; + break; + case '1': + echo "true\n"; + return new phpgats_Boolean( true ); + break; + case '0': + echo "false\n"; + return new phpgats_Boolean( false ); + break; + default: + echo "str:"; + $obj = phpgats_pS( $str_data, $offset, $dbg ); + echo $obj->get() . "\n"; + return $obj; + break; + } +} + +function phpgats_parseGats( $str_data ) +{ + print "parsing\n"; + $offset = 0; + $data_size = strlen( $str_data ); + if( $data_size < 5 ) + { + print "invalid size (< 5)\n"; + return false; + } + if( ord($str_data) != 1 ) //version + return false; + $size = "" . $str_data[1] . $str_data[2] . $str_data[3] . $str_data[4]; + $size = unpack( "Nsize", $size ); + $size = $size["size"]; + if( $data_size < $size ) + { + print "invalid size (needed: " . $size . ", was: " . $data_size . ")\n"; + return false; + } + $offset+=5; + return phpgats_pM( $str_data, $offset ); +} + +function phpgats_writeGats( $elem ) +{ + $str_out = $elem->encode(); + $str_out = "\x01" . pack( "N", strlen($str_out)+5 ) . $str_out; + return $str_out; +} + +/*$l = new phpgats_List(); +$l->append( new phpgats_Integer("27") ); +$l->append( new phpgats_Boolean(true) ); +$l->append( new phpgats_String("testing your monkey") ); + +$d = new phpgats_Dictionary(); +$d->append( "theList", $l ); +$d->append( "theFalse", new phpgats_Boolean(false) ); + +$f = fopen("testinggats.gats", "w"); +$theStr = phpgats_writeGats($d); +fwrite( $f, $theStr ); +fclose( $f ); + +$obj = phpgats_parseGats( $theStr );*/ + +?> + -- cgit v1.2.3