From 20d9ff5d3c4ac09f1f489eef4b0a4297983d9abd Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Thu, 17 Feb 2011 23:13:29 +0000 Subject: Ok, php can read and write gats style integers now. --- php/int.php | 108 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/tests/int.cpp | 30 +++++++++++++-- 2 files changed, 134 insertions(+), 4 deletions(-) create mode 100644 php/int.php diff --git a/php/int.php b/php/int.php new file mode 100644 index 0000000..478af73 --- /dev/null +++ b/php/int.php @@ -0,0 +1,108 @@ + $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/src/tests/int.cpp b/src/tests/int.cpp index 348e179..c19df9c 100644 --- a/src/tests/int.cpp +++ b/src/tests/int.cpp @@ -2,14 +2,36 @@ #include #include +#include using namespace Bu; -int main() +void hexdump( char *dat, int iSize ) { - MemBuf mb; - int64_t i = -6; + static const char *hex="0123456789ABCDEF"; + printf("----\n"); + for( int j = 0; j < iSize; j += 8 ) + { + for( int k = j; /*k < iSize &&*/ k < j+8; k++ ) + printf((k>4)&0x0F], hex[dat[k]&0x0F] ); + printf("| "); + for( int k = j; k < iSize && k < j+8; k++ ) + printf("%c ", (dat[k]>13&&dat[k]<127)?(dat[k]):('.') ); + printf("\n"); + } + printf("----\n"); +} +int main( int argc, char *argv[] ) +{ + for( int j = 1; j < argc; j++ ) + { + int64_t i = strtoll( argv[j], NULL, 10 ); + MemBuf mb; + Gats::Integer::writePackedInt( mb, i ); + hexdump( mb.getString().getStr(), mb.getString().getSize() ); + } +/* sio << "Before: " << i << sio.nl; Gats::Integer::writePackedInt( mb, i ); mb.write("aaa", 3 ); @@ -19,7 +41,7 @@ int main() char buf[4]; buf[mb.read( buf, 3 )] = '\0'; sio << "Extra: \"" << buf << "\"" << sio.nl; - +*/ return 0; } -- cgit v1.2.3