1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10:
11: class Horde_Xml_Wbxml
12: {
13: 14:
15:
16:
17: const GLOBAL_TOKEN_SWITCH_PAGE = 0;
18: const GLOBAL_TOKEN_END = 1;
19: const GLOBAL_TOKEN_ENTITY = 2;
20: const GLOBAL_TOKEN_STR_I = 3;
21: const GLOBAL_TOKEN_LITERAL = 4;
22:
23: const GLOBAL_TOKEN_EXT_I_0 = 64;
24: const GLOBAL_TOKEN_EXT_I_1 = 65;
25: const GLOBAL_TOKEN_EXT_I_2 = 66;
26: const GLOBAL_TOKEN_PI = 67;
27: const GLOBAL_TOKEN_LITERAL_C = 68;
28:
29: const GLOBAL_TOKEN_EXT_T_0 = 128;
30: const GLOBAL_TOKEN_EXT_T_1 = 129;
31: const GLOBAL_TOKEN_EXT_T_2 = 130;
32: const GLOBAL_TOKEN_STR_T = 131;
33: const GLOBAL_TOKEN_LITERAL_A = 132;
34:
35: const GLOBAL_TOKEN_EXT_0 = 192;
36: const GLOBAL_TOKEN_EXT_1 = 193;
37: const GLOBAL_TOKEN_EXT_2 = 194;
38: const GLOBAL_TOKEN_OPAQUE = 195;
39: const GLOBAL_TOKEN_LITERAL_AC = 196;
40:
41:
42: const CHARSET_US_ASCII = 'US-ASCII';
43: const CHARSET_ISO_8859_1 = 'ISO-8859-1';
44: const CHARSET_UTF_8 = 'UTF-8';
45: const CHARSET_UTF_16BE = 'UTF-16BE';
46: const CHARSET_UTF_16LE = 'UTF-16LE';
47: const CHARSET_UTF_16 = 'UTF-16';
48:
49: 50: 51: 52: 53:
54: static public function MBUInt32ToInt($in, &$pos)
55: {
56: $val = 0;
57:
58: do {
59: $b = ord($in[$pos++]);
60: $val <<= 7;
61: $val += ($b & 127);
62: } while (($b & 128) != 0);
63:
64: return $val;
65: }
66:
67: 68: 69:
70: static public function intToMBUInt32(&$out, $i)
71: {
72: if ($i > 268435455) {
73: $bytes0 = 0 | Horde_Xml_Wbxml::getBits(0, $i);
74: $bytes1 = 128 | Horde_Xml_Wbxml::getBits(1, $i);
75: $bytes2 = 128 | Horde_Xml_Wbxml::getBits(2, $i);
76: $bytes3 = 128 | Horde_Xml_Wbxml::getBits(3, $i);
77: $bytes4 = 128 | Horde_Xml_Wbxml::getBits(4, $i);
78:
79: $out .= chr($bytes4) . chr($bytes3) . chr($bytes2) . chr($bytes1) . chr($bytes0);
80: } elseif ($i > 2097151) {
81: $bytes0 = 0 | Horde_Xml_Wbxml::getBits(0, $i);
82: $bytes1 = 128 | Horde_Xml_Wbxml::getBits(1, $i);
83: $bytes2 = 128 | Horde_Xml_Wbxml::getBits(2, $i);
84: $bytes3 = 128 | Horde_Xml_Wbxml::getBits(3, $i);
85:
86: $out .= chr($bytes3) . chr($bytes2) . chr($bytes1) . chr($bytes0);
87: } elseif ($i > 16383) {
88: $bytes0 = 0 | Horde_Xml_Wbxml::getBits(0, $i);
89: $bytes1 = 128 | Horde_Xml_Wbxml::getBits(1, $i);
90: $bytes2 = 128 | Horde_Xml_Wbxml::getBits(2, $i);
91:
92: $out .= chr($bytes2) . chr($bytes1) . chr($bytes0);
93: } elseif ($i > 127) {
94: $bytes0 = 0 | Horde_Xml_Wbxml::getBits(0, $i);
95: $bytes1 = 128 | Horde_Xml_Wbxml::getBits(1, $i);
96:
97: $out .= chr($bytes1) . chr($bytes0);
98: } else {
99: $bytes0 = 0 | Horde_Xml_Wbxml::getBits(0, $i);
100:
101: $out .= chr($bytes0);
102: }
103: }
104:
105: static public function getBits($num, $l)
106: {
107: switch ($num) {
108: case 0:
109: return $l & 127;
110:
111: case 1:
112: return ($l >> 7) & 127;
113:
114: case 2:
115: return ($l >> 14) & 127;
116:
117: case 3:
118: return ($l >> 21) & 127;
119:
120: case 4:
121: return ($l >> 28) & 127;
122: }
123:
124: return 0;
125: }
126:
127: static public function getDPIString($i)
128: {
129: 130: 131:
132: $DPIString = array(2 => Horde_Xml_Wbxml_Dtd::WML_1_0,
133: 3 => Horde_Xml_Wbxml_Dtd::WTA_1_0,
134: 4 => Horde_Xml_Wbxml_Dtd::WML_1_1,
135: 5 => Horde_Xml_Wbxml_Dtd::SI_1_1,
136: 6 => Horde_Xml_Wbxml_Dtd::SL_1_0,
137: 7 => Horde_Xml_Wbxml_Dtd::CO_1_0,
138: 8 => Horde_Xml_Wbxml_Dtd::CHANNEL_1_1,
139: 9 => Horde_Xml_Wbxml_Dtd::WML_1_2,
140: 10 => Horde_Xml_Wbxml_Dtd::WML_1_3,
141: 11 => Horde_Xml_Wbxml_Dtd::PROV_1_0,
142: 12 => Horde_Xml_Wbxml_Dtd::WTA_WML_1_2,
143: 13 => Horde_Xml_Wbxml_Dtd::CHANNEL_1_2,
144:
145:
146:
147:
148:
149: 4049 => Horde_Xml_Wbxml_Dtd::SYNCML_1_0,
150: 4050 => Horde_Xml_Wbxml_Dtd::DEVINF_1_0,
151: 4051 => Horde_Xml_Wbxml_Dtd::SYNCML_1_1,
152: 4052 => Horde_Xml_Wbxml_Dtd::DEVINF_1_1,
153: 4609 => Horde_Xml_Wbxml_Dtd::SYNCML_1_2,
154:
155: 4611 => Horde_Xml_Wbxml_Dtd::DEVINF_1_2
156:
157:
158:
159: );
160: return isset($DPIString[$i]) ? $DPIString[$i] : null;
161: }
162:
163: static public function getDPIInt($dpi)
164: {
165: 166: 167:
168: $DPIInt = array(Horde_Xml_Wbxml_Dtd::WML_1_0 => 2,
169: Horde_Xml_Wbxml_Dtd::WTA_1_0 => 3,
170: Horde_Xml_Wbxml_Dtd::WML_1_1 => 4,
171: Horde_Xml_Wbxml_Dtd::SI_1_1 => 5,
172: Horde_Xml_Wbxml_Dtd::SL_1_0 => 6,
173: Horde_Xml_Wbxml_Dtd::CO_1_0 => 7,
174: Horde_Xml_Wbxml_Dtd::CHANNEL_1_1 => 8,
175: Horde_Xml_Wbxml_Dtd::WML_1_2 => 9,
176: Horde_Xml_Wbxml_Dtd::WML_1_3 => 10,
177: Horde_Xml_Wbxml_Dtd::PROV_1_0 => 11,
178: Horde_Xml_Wbxml_Dtd::WTA_WML_1_2 => 12,
179: Horde_Xml_Wbxml_Dtd::CHANNEL_1_2 => 13,
180:
181:
182:
183:
184: Horde_Xml_Wbxml_Dtd::SYNCML_1_0 => 4049,
185: Horde_Xml_Wbxml_Dtd::DEVINF_1_0 => 4050,
186: Horde_Xml_Wbxml_Dtd::SYNCML_1_1 => 4051,
187: Horde_Xml_Wbxml_Dtd::DEVINF_1_1 => 4052,
188: Horde_Xml_Wbxml_Dtd::SYNCML_1_2 => 4609,
189:
190:
191:
192: Horde_Xml_Wbxml_Dtd::DEVINF_1_2 => 4611
193:
194:
195: );
196:
197: return isset($DPIInt[$dpi]) ? $DPIInt[$dpi] : 0;
198: }
199:
200: 201: 202: 203: 204: 205:
206: static public function getCharsetString($cs)
207: {
208: 209: 210:
211: $charsetString = array(3 => 'US-ASCII',
212: 4 => 'ISO-8859-1',
213: 106 => 'UTF-8',
214: 1013 => 'UTF-16BE',
215: 1014 => 'UTF-16LE',
216: 1015 => 'UTF-16');
217:
218: return isset($charsetString[$cs]) ? $charsetString[$cs] : null;
219: }
220:
221: 222: 223: 224: 225: 226: 227: 228:
229: static public function getCharsetInt($cs)
230: {
231: 232: 233:
234: $charsetInt = array('US-ASCII' => 3,
235: 'ISO-8859-1' => 4,
236: 'UTF-8' => 106,
237: 'UTF-16BE' => 1013,
238: 'UTF-16LE' => 1014,
239: 'UTF-16' => 1015);
240:
241: return isset($charsetInt[$cs]) ? $charsetInt[$cs] : null;
242: }
243: }
244: