Overview

Packages

  • Xml
    • Wbxml

Classes

  • Horde_Xml_Wbxml
  • Horde_Xml_Wbxml_ContentHandler
  • Horde_Xml_Wbxml_Decoder
  • Horde_Xml_Wbxml_Dtd
  • Horde_Xml_Wbxml_Dtd_SyncMl
  • Horde_Xml_Wbxml_Dtd_SyncMlDevInf
  • Horde_Xml_Wbxml_Dtd_SyncMlMetInf
  • Horde_Xml_Wbxml_DtdManager
  • Horde_Xml_Wbxml_Encoder
  • Horde_Xml_Wbxml_Exception
  • Horde_Xml_Wbxml_HashTable
  • Horde_Xml_Wbxml_LifoQueue
  • Overview
  • Package
  • Class
  • Tree
  1: <?php
  2: /**
  3:  * Copyright 2003-2012 Horde LLC (http://www.horde.org/)
  4:  *
  5:  * See the enclosed file COPYING for license information (LGPL). If you
  6:  * did not receive this file, see http://www.horde.org/licenses/lgpl21.
  7:  *
  8:  * @author  Anthony Mills <amills@pyramid6.com>
  9:  * @package Xml_Wbxml
 10:  */
 11: class Horde_Xml_Wbxml
 12: {
 13:     /* Constants are from Binary XML Content Format Specification Version 1.3,
 14:      * 25 July 2001 found at http://www.wapforum.org */
 15: 
 16:     /* From 7.1 Global Tokens. */
 17:     const GLOBAL_TOKEN_SWITCH_PAGE = 0;  // 0x00
 18:     const GLOBAL_TOKEN_END = 1;          // 0x01
 19:     const GLOBAL_TOKEN_ENTITY = 2;       // 0x02
 20:     const GLOBAL_TOKEN_STR_I = 3;        // 0x03
 21:     const GLOBAL_TOKEN_LITERAL = 4;      // 0x04
 22: 
 23:     const GLOBAL_TOKEN_EXT_I_0 = 64;     // 0x40
 24:     const GLOBAL_TOKEN_EXT_I_1 = 65;     // 0x41
 25:     const GLOBAL_TOKEN_EXT_I_2 = 66;     // 0x42
 26:     const GLOBAL_TOKEN_PI = 67;          // 0x43
 27:     const GLOBAL_TOKEN_LITERAL_C = 68;   // 0x44
 28: 
 29:     const GLOBAL_TOKEN_EXT_T_0 = 128;    // 0x80
 30:     const GLOBAL_TOKEN_EXT_T_1 = 129;    // 0x81
 31:     const GLOBAL_TOKEN_EXT_T_2 = 130;    // 0x82
 32:     const GLOBAL_TOKEN_STR_T = 131;      // 0x83
 33:     const GLOBAL_TOKEN_LITERAL_A = 132;  // 0x84
 34: 
 35:     const GLOBAL_TOKEN_EXT_0 = 192;      // 0xC0
 36:     const GLOBAL_TOKEN_EXT_1 = 193;      // 0xC1
 37:     const GLOBAL_TOKEN_EXT_2 = 194;      // 0xC2
 38:     const GLOBAL_TOKEN_OPAQUE = 195;     // 0xC3
 39:     const GLOBAL_TOKEN_LITERAL_AC = 196; // 0xC4
 40: 
 41:     /* Only default character encodings from J2SE are currently supported. */
 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:      * Decoding Multi-byte Integers from Section 5.1
 51:      *
 52:      * Use long because it is unsigned.
 53:      */
 54:     static public function MBUInt32ToInt($in, &$pos)
 55:     {
 56:         $val = 0;
 57: 
 58:         do {
 59:             $b = ord($in[$pos++]);
 60:             $val <<= 7; // Bitshift left 7 bits.
 61:             $val += ($b & 127);
 62:         } while (($b & 128) != 0);
 63: 
 64:         return $val;
 65:     }
 66: 
 67:     /**
 68:      * Encoding Multi-byte Integers from Section 5.1
 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; // 0x7F
110: 
111:         case 1:
112:             return ($l >> 7) & 127; // 0x7F
113: 
114:         case 2:
115:             return ($l >> 14) & 127; // 0x7F
116: 
117:         case 3:
118:             return ($l >> 21) & 127; // 0x7F
119: 
120:         case 4:
121:             return ($l >> 28) & 127; // 0x7F
122:         }
123: 
124:         return 0;
125:     }
126: 
127:     static public function getDPIString($i)
128:     {
129:         /**
130:          * ADD CHAPTER
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:                            // Not all SyncML clients know this, so we
146:                            // should use the string table.
147:                            // 0xFD1 => Horde_Xml_Wbxml_Dtd::SYNCML_1_1,
148:                            // These codes are taken from libwbxml wbxml_tables.h:
149:                            4049 => Horde_Xml_Wbxml_Dtd::SYNCML_1_0, // 0x0fd1
150:                            4050 => Horde_Xml_Wbxml_Dtd::DEVINF_1_0, // 0x0fd2
151:                            4051 => Horde_Xml_Wbxml_Dtd::SYNCML_1_1, // 0x0fd3
152:                            4052 => Horde_Xml_Wbxml_Dtd::DEVINF_1_1, // 0x0fd4
153:                            4609 => Horde_Xml_Wbxml_Dtd::SYNCML_1_2, // 0x1201
154:                            //@todo: verify this:
155:                            4611 => Horde_Xml_Wbxml_Dtd::DEVINF_1_2  // 0x1203
156: // taken from libxml but might be wrong:
157: //                           4610 => Horde_Xml_Wbxml_Dtd::DEVINF_1_2, // 0x1202
158: //                           4611 => Horde_Xml_Wbxml_Dtd::METINF_1_2  // 0x1203
159:                            );
160:         return isset($DPIString[$i]) ? $DPIString[$i] : null;
161:     }
162: 
163:     static public function getDPIInt($dpi)
164:     {
165:         /**
166:          * ADD CHAPTER
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:                         // Not all SyncML clients know this, so maybe we
182:                         // should use the string table.
183:                            // These codes are taken from libwbxml wbxml_tables.h:
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, // 0x1201
189: //                        Horde_Xml_Wbxml_Dtd::DEVINF_1_2 => 4610, // 0x1202
190: //                        Horde_Xml_Wbxml_Dtd::METINF_1_2 => 4611  // 0x1203
191:                         //@todo: verify this
192:                         Horde_Xml_Wbxml_Dtd::DEVINF_1_2 => 4611  // 0x1203
193:                         // Horde_Xml_Wbxml_Dtd::SYNCML_1_1 => 0xFD1,
194:                         // Horde_Xml_Wbxml_Dtd::DEVINF_1_1 => 0xFD2,
195:                         );
196: 
197:         return isset($DPIInt[$dpi]) ? $DPIInt[$dpi] : 0;
198:     }
199: 
200:     /**
201:      * Returns the character encoding.
202:      * only default character encodings from J2SE are supported
203:      * from http://www.iana.org/assignments/character-sets
204:      * and http://java.sun.com/j2se/1.4.2/docs/api/java/nio/charset/Charset.html
205:      */
206:     static public function getCharsetString($cs)
207:     {
208:         /**
209:          * From http://www.iana.org/assignments/character-sets
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:      * Returns the character encoding.
223:      *
224:      * Only default character encodings from J2SE are supported.
225:      *
226:      * From http://www.iana.org/assignments/character-sets and
227:      * http://java.sun.com/j2se/1.4.2/docs/api/java/nio/charset/Charset.html
228:      */
229:     static public function getCharsetInt($cs)
230:     {
231:         /**
232:          * From http://www.iana.org/assignments/character-sets
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: 
API documentation generated by ApiGen