]> scripts.mit.edu Git - autoinstallsdev/mediawiki.git/blob - vendor/wikimedia/avro/lib/avro.php
MediaWiki 1.30.2
[autoinstallsdev/mediawiki.git] / vendor / wikimedia / avro / lib / avro.php
1 <?php
2 /**
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *     http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19
20 /**
21  * Avro library top-level file.
22  *
23  * This file in turn includes all files supporting the
24  * Avro implementation.
25  *
26  * @package Avro
27  */
28
29 /**
30  * General Avro exceptions.
31  * @package Avro
32  */
33 class AvroException extends Exception {}
34
35 /**
36  * Library-level class for PHP Avro port.
37  *
38  * Contains library details such as version number and platform checks.
39  *
40  * This port is an implementation of the
41  * {@link http://avro.apache.org/docs/1.3.3/spec.html Avro 1.3.3 Specification}
42  *
43  * @package Avro
44  *
45  */
46 class Avro
47 {
48   /**
49    * @var string version number of Avro specification to which
50    *             this implemenation complies
51    */
52   const SPEC_VERSION  = '1.3.3';
53
54   /**#@+
55    * Constant to enumerate endianness.
56    * @access private
57    * @var int
58    */
59   const BIG_ENDIAN    = 0x00;
60   const LITTLE_ENDIAN = 0x01;
61   /**#@-*/
62
63   /**
64    * Memoized result of self::set_endianness()
65    * @var int self::BIG_ENDIAN or self::LITTLE_ENDIAN
66    * @see self::set_endianness()
67    */
68   private static $endianness;
69
70   /**#@+
71    * Constant to enumerate biginteger handling mode.
72    * GMP is used, if available, on 32-bit platforms.
73    */
74   const PHP_BIGINTEGER_MODE = 0x00;
75   const GMP_BIGINTEGER_MODE = 0x01;
76   /**#@-*/
77
78   /**
79    * @var int
80    * Mode used to handle bigintegers. After Avro::check_64_bit() has been called,
81    * (usually via a call to Avro::check_platform(), set to
82    * self::GMP_BIGINTEGER_MODE on 32-bit platforms that have GMP available,
83    * and to self::PHP_BIGINTEGER_MODE otherwise.
84    */
85   private static $biginteger_mode;
86
87   /**
88    * Wrapper method to call each required check.
89    *
90    */
91   public static function check_platform()
92   {
93     self::check_64_bit();
94     self::check_little_endian();
95   }
96
97   /**
98    * Determines if the host platform can encode and decode long integer data.
99    *
100    * @throws AvroException if the platform cannot handle long integers.
101    */
102   private static function check_64_bit()
103   {
104     if (8 != PHP_INT_SIZE)
105       if (extension_loaded('gmp'))
106         self::$biginteger_mode = self::GMP_BIGINTEGER_MODE;
107       else
108         throw new AvroException('This platform cannot handle a 64-bit operations. '
109                                 . 'Please install the GMP PHP extension.');
110     else
111       self::$biginteger_mode = self::PHP_BIGINTEGER_MODE;
112
113   }
114
115   /**
116    * @returns boolean true if the PHP GMP extension is used and false otherwise.
117    * @internal Requires Avro::check_64_bit() (exposed via Avro::check_platform())
118    *           to have been called to set Avro::$biginteger_mode.
119    */
120   static function uses_gmp()
121   {
122     return (self::GMP_BIGINTEGER_MODE == self::$biginteger_mode);
123   }
124
125   /**
126    * Determines if the host platform is little endian,
127    * required for processing double and float data.
128    *
129    * @throws AvroException if the platform is not little endian.
130    */
131   private static function check_little_endian()
132   {
133     if (!self::is_little_endian_platform())
134       throw new AvroException('This is not a little-endian platform');
135   }
136
137   /**
138    * Determines the endianness of the host platform and memoizes
139    * the result to Avro::$endianness.
140    *
141    * Based on a similar check perfomed in http://pear.php.net/package/Math_BinaryUtils
142    *
143    * @throws AvroException if the endianness cannot be determined.
144    */
145   private static function set_endianness()
146   {
147     $packed = pack('d', 1);
148     switch ($packed)
149     {
150       case "\77\360\0\0\0\0\0\0":
151         self::$endianness = self::BIG_ENDIAN;
152         break;
153       case "\0\0\0\0\0\0\360\77":
154         self::$endianness = self::LITTLE_ENDIAN;
155         break;
156       default:
157         throw new AvroException(
158           sprintf('Error determining platform endianness: %s',
159                   AvroDebug::hex_string($packed)));
160     }
161   }
162
163   /**
164    * @returns boolean true if the host platform is big endian
165    *                  and false otherwise.
166    * @uses self::set_endianness()
167    */
168   private static function is_big_endian_platform()
169   {
170     if (is_null(self::$endianness))
171       self::set_endianness();
172
173     return (self::BIG_ENDIAN == self::$endianness);
174   }
175
176   /**
177    * @returns boolean true if the host platform is little endian,
178    *                  and false otherwise.
179    * @uses self::is_bin_endian_platform()
180    */
181   private static function is_little_endian_platform()
182   {
183     return !self::is_big_endian_platform();
184   }
185
186 }
187
188 require_once('avro/util.php');
189 require_once('avro/debug.php');
190 require_once('avro/schema.php');
191 require_once('avro/io.php');
192 require_once('avro/gmp.php');
193 require_once('avro/datum.php');
194 require_once('avro/data_file.php');
195 require_once('avro/protocol.php');