[sync] First shot at php conversion.
Anthony Mills
amills at gascard.net
Sat Nov 8 01:52:48 PST 2003
Here is the utilities file I use with WBXML. How should I post these
files? I have not tested this yet, I wanted to make sure I am doing
this correctly. Feel free to make comments.
Thanks,
Anthony
<?php
/**
*
*
* Copyright 2003 Anthony Mills <amills at pyramid6.com>
*
* See the enclosed file COPYING for license information (LGPL). If you
* did not receive this file, see http://www.fsf.org/copyleft/lgpl.html.
*/
/**
* From Binary XML Content Format Specification Version 1.3, 25 July 2001
* found at http://www.wapforum.org
*
*/
/**
* From 7.1 Global Tokens
*/
define('WBXML_GLOBAL_TOKEN_SWITCH_PAGE', 0);//0x00
define('WBXML_GLOBAL_TOKEN_END', 1);//0x01
define('WBXML_GLOBAL_TOKEN_ENTITY', 2);//0x02
define('WBXML_GLOBAL_TOKEN_STR_I', 3);//0x03
define('WBXML_GLOBAL_TOKEN_LITERAL', 4);//0x04
define('WBXML_GLOBAL_TOKEN_EXT_I_0', 64);//0x40
define('WBXML_GLOBAL_TOKEN_EXT_I_1', 65);//0x41
define('WBXML_GLOBAL_TOKEN_EXT_I_2', 66);//0x42
define('WBXML_GLOBAL_TOKEN_PI', 67);//0x43
define('WBXML_GLOBAL_TOKEN_LITERAL_C', 68);//0x44
define('WBXML_GLOBAL_TOKEN_EXT_T_0', 128);//0x80
define('WBXML_GLOBAL_TOKEN_EXT_T_1', 129);//0x81
define('WBXML_GLOBAL_TOKEN_EXT_T_2', 130);//0x82
define('WBXML_GLOBAL_TOKEN_STR_T', 131);//0x83
define('WBXML_GLOBAL_TOKEN_LITERAL_A', 132);//0x84
define('WBXML_GLOBAL_TOKEN_EXT_0', 192);//0xC0
define('WBXML_GLOBAL_TOKEN_EXT_1', 193);//0xC1
define('WBXML_GLOBAL_TOKEN_EXT_2', 194);//0xC2
define('WBXML_GLOBAL_TOKEN_OPAQUE', 195);//0xC3
define('WBXML_GLOBAL_TOKEN_LITERAL_AC', 196);//0xC4
//Internal class used by WBXML_MBUInt32ToInt
class __BitSet {
var $bitarray;
function set($i, $value) {
$bitarray[$i] = $value;
}
function get($i) {
return $bitarray[$i];
}
}
/**
* Decoding Multi-byte Integers from Section 5.1
*
* use long becuase it is unsigned
*/
function WBXML_MBUInt32ToInt($in) {
$bs = new __BitSet;
$j = 0;
$b = 0;
do {
$b = fread($in, 1);
bs.set($j++, ($b & 64) != 0);//0x40
bs.set($j++, ($b & 32) != 0);//0x20
bs.set($j++, ($b & 16) != 0);//0x10
bs.set($j++, ($b & 8) != 0);//0x08
bs.set($j++, ($b & 4) != 0);//0x04
bs.set($j++, ($b & 2) != 0);//0x02
bs.set($j++, ($b & 1) != 0);//0x01
} while (($b & 128) != 0);
$value = 0;
$multiplier = 1;
for ($i = ($j - 1); $i >= 0; $i--) {
if (bs.get($i))
$value += $multiplier;
$multiplier *= 2;
}
return $value;
}
/**
* encoding Multi-byte Integers from Section 5.1
*
*/
function WBXML_intToMBUInt32($i, $out) {
if ($i > 268435455) {
$bytes0 = 0 | __WBXML_getBits(0, $i);
$bytes1 = 128 | __WBXML_getBits(1, $i);
$bytes2 = 128 | __WBXML_getBits(2, $i);
$bytes3 = 128 | __WBXML_getBits(3, $i);
$bytes4 = 128 | __WBXML_getBits(4, $i);
fwrite($out, $bytes4);
fwrite($out, $bytes3);
fwrite($out, $bytes2);
fwrite($out, $bytes1);
fwrite($out, $bytes0);
} else if ($i > 2097151) {
$bytes0 = 0 | __WBXML_getBits(0, $i);
$bytes1 = 128 | __WBXML_getBits(1, $i);
$bytes2 = 128 | __WBXML_getBits(2, $i);
$bytes3 = 128 | __WBXML_getBits(3, $i);
fwrite($out, $bytes3);
fwrite($out, $bytes2);
fwrite($out, $bytes1);
fwrite($out, $bytes0);
} else if ($i > 16383) {
$bytes0 = 0 | _WBXML_getBits(0, $i);
$bytes1 = 128 | _WBXML_getBits(1, $i);
$bytes2 = 128 | _WBXML_getBits(2, $i);
fwrite($out, $bytes2);
fwrite($out, $bytes1);
fwrite($out, $bytes0);
} else if ($i > 127) {
$bytes0 = 0 | _WBXML_getBits(0, $i);
$bytes1 = 128 | _WBXML_getBits(1, $i);
fwrite($out, $bytes1);
fwrite($out, $bytes0);
} else {
$bytes0 = 0 | _WBXML_getBits(0, $i);
fwrite($out, $bytes0);
}
}
function __WBXML_getBits($num, $l) {
$j = 0;
switch ($num) {
case 0:
$j = $l & 127; //0x7F
break;
case 1:
$j = ($l>>7) & 127; //0x7F
break;
case 2:
$j = ($l>>14) & 127; //0x7F
break;
case 3:
$j = ($l>>21) & 127; //0x7F
break;
case 4:
$j = ($l>>28) & 127; //0x7F
break;
}
return $j;
}
function WBXML_getDPIString($i) {
switch ($i) {
case 2:
return "-//WAPFORUM//DTD WML 1.0//EN";
case 3:
return "-//WAPFORUM//DTD WTA 1.0//EN";
case 4:
return "-//WAPFORUM//DTD WML 1.1//EN";
case 5:
return "-//WAPFORUM//DTD SI 1.1//EN";
case 6:
return "-//WAPFORUM//DTD SL 1.0//EN";
case 7:
return "-//WAPFORUM//DTD CO 1.0//EN";
case 8:
return "-//WAPFORUM//DTD CHANNEL 1.1//EN";
case 9:
return "-//WAPFORUM//DTD WML 1.2//EN";
case 10:
return "-//WAPFORUM//DTD WML 1.3//EN";
case 11:
return "-//WAPFORUM//DTD PROV 1.0//EN";
case 12:
return "-//WAPFORUM//DTD WTA-WML 1.2//EN";
case 13:
return "-//WAPFORUM//DTD CHANNEL 1.2//EN";
// case 0xFD1:
// return "-//SYNCML//DTD SyncML 1.1//EN";
// case 0xFD2:
// return "-//SYNCML//DTD DevInf 1.1//EN";
default:
return null;
}
}
function WBXML_getDPIInt($dpi) {
if ($dpi == "-//WAPFORUM//DTD WML 1.0//EN")
return 2;
else if ($dpi == "-//WAPFORUM//DTD WTA 1.0//EN")
return 3;
else if ($dpi == "-//WAPFORUM//DTD WML 1.1//EN")
return 4;
else if ($dpi == "-//WAPFORUM//DTD SI 1.1//EN")
return 5;
else if ($dpi == "-//WAPFORUM//DTD SL 1.0//EN")
return 6;
else if ($dpi == "-//WAPFORUM//DTD CO 1.0//EN")
return 7;
else if ($dpi == "-//WAPFORUM//DTD CHANNEL 1.1//EN")
return 8;
else if ($dpi == "-//WAPFORUM//DTD WML 1.2//EN")
return 9;
else if ($dpi == "-//WAPFORUM//DTD WML 1.3//EN")
return 10;
else if ($dpi == "-//WAPFORUM//DTD PROV 1.0//EN")
return 11;
else if ($dpi == "-//WAPFORUM//DTD WTA-WML 1.2//EN")
return 12;
else if ($dpi == "-//WAPFORUM//DTD CHANNEL 1.2//EN")
return 13;
// else if ($dpi == "-//SYNCML//DTD SyncML 1.1//EN")
// return 0xFD1;
// else if ($dpi == "-//SYNCML//DTD DevInf 1.1//EN")
// return 0xFD2;
else
return 0;
}
/**
* returns the character encoding
* only default character encodings from J2SE are supported
* from http://www.iana.org/assignments/character-sets
* and
http://java.sun.com/j2se/1.4.2/docs/api/java/nio/charset/Charset.html
*
*/
function WBXML_getCharsetString($cs) {
switch ($cs) {
case 3:
return "US-ASCII";
case 4:
return "ISO-8859-1";
case 106:
return "UTF-8";
case 1013:
return "UTF-16BE";
case 1014:
return "UTF-16LE";
case 1015:
return "UTF-16";
//insert additional character sets here
default:
return "UTF-8";
}
}
/**
* returns the character encoding
* only default character encodings from J2SE are supported
* from http://www.iana.org/assignments/character-sets
* and
http://java.sun.com/j2se/1.4.2/docs/api/java/nio/charset/Charset.html
*
*/
function WBXML_getCharsetInt($cs) {
if (cs == "US-ASCII")
return 3;
else if (cs == "ISO-8859-1")
return 4;
else if (cs == "UTF-8")
return 106;
else if (cs == "UTF-16BE")
return 1013;
else if (cs == "UTF-16LE")
return 1014;
else if (cs == "UTF-16")
return 1015;
//insert additional character sets here
else
return 106;
}
More information about the sync
mailing list