• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1<?php
2
3// Protocol Buffers - Google's data interchange format
4// Copyright 2008 Google Inc.  All rights reserved.
5//
6// Use of this source code is governed by a BSD-style
7// license that can be found in the LICENSE file or at
8// https://developers.google.com/open-source/licenses/bsd
9
10namespace Google\Protobuf\Internal;
11
12use Google\Protobuf\Duration;
13use Google\Protobuf\FieldMask;
14use Google\Protobuf\Internal\GPBType;
15use Google\Protobuf\Internal\RepeatedField;
16use Google\Protobuf\Internal\MapField;
17use function bccomp;
18
19function camel2underscore($input) {
20    preg_match_all(
21        '!([A-Z][A-Z0-9]*(?=$|[A-Z][a-z0-9])|[A-Za-z][a-z0-9]+)!',
22        $input,
23        $matches);
24    $ret = $matches[0];
25    foreach ($ret as &$match) {
26        $match = $match == strtoupper($match) ? strtolower($match) : lcfirst($match);
27    }
28    return implode('_', $ret);
29}
30
31class GPBUtil
32{
33    const NANOS_PER_MILLISECOND = 1000000;
34    const NANOS_PER_MICROSECOND = 1000;
35    const TYPE_URL_PREFIX = 'type.googleapis.com/';
36
37    public static function divideInt64ToInt32($value, &$high, &$low, $trim = false)
38    {
39        $isNeg = (bccomp($value, 0) < 0);
40        if ($isNeg) {
41            $value = bcsub(0, $value);
42        }
43
44        $high = bcdiv($value, 4294967296);
45        $low = bcmod($value, 4294967296);
46        if (bccomp($high, 2147483647) > 0) {
47            $high = (int) bcsub($high, 4294967296);
48        } else {
49            $high = (int) $high;
50        }
51        if (bccomp($low, 2147483647) > 0) {
52            $low = (int) bcsub($low, 4294967296);
53        } else {
54            $low = (int) $low;
55        }
56
57        if ($isNeg) {
58            $high = ~$high;
59            $low = ~$low;
60            $low++;
61            if (!$low) {
62                $high = (int)($high + 1);
63            }
64        }
65
66        if ($trim) {
67            $high = 0;
68        }
69    }
70
71    public static function checkString(&$var, $check_utf8)
72    {
73        if (is_array($var) || is_object($var)) {
74            throw new \InvalidArgumentException("Expect string.");
75        }
76        if (!is_string($var)) {
77            $var = strval($var);
78        }
79        if ($check_utf8 && !preg_match('//u', $var)) {
80            throw new \Exception("Expect utf-8 encoding.");
81        }
82    }
83
84    public static function checkEnum(&$var)
85    {
86      static::checkInt32($var);
87    }
88
89    public static function checkInt32(&$var)
90    {
91        if (is_numeric($var)) {
92            $var = intval($var);
93        } else {
94            throw new \Exception("Expect integer.");
95        }
96    }
97
98    public static function checkUint32(&$var)
99    {
100        if (is_numeric($var)) {
101            if (PHP_INT_SIZE === 8) {
102                $var = intval($var);
103                $var |= ((-(($var >> 31) & 0x1)) & ~0xFFFFFFFF);
104            } else {
105                if (bccomp($var, 0x7FFFFFFF) > 0) {
106                    $var = bcsub($var, "4294967296");
107                }
108                $var = (int) $var;
109            }
110        } else {
111            throw new \Exception("Expect integer.");
112        }
113    }
114
115    public static function checkInt64(&$var)
116    {
117        if (is_numeric($var)) {
118            if (PHP_INT_SIZE == 8) {
119                $var = intval($var);
120            } else {
121                if (is_float($var) ||
122                    is_integer($var) ||
123                    (is_string($var) &&
124                         bccomp($var, "9223372036854774784") < 0)) {
125                    $var = number_format($var, 0, ".", "");
126                }
127            }
128        } else {
129            throw new \Exception("Expect integer.");
130        }
131    }
132
133    public static function checkUint64(&$var)
134    {
135        if (is_numeric($var)) {
136            if (PHP_INT_SIZE == 8) {
137                $var = intval($var);
138            } else {
139                $var = number_format($var, 0, ".", "");
140            }
141        } else {
142            throw new \Exception("Expect integer.");
143        }
144    }
145
146    public static function checkFloat(&$var)
147    {
148        if (is_float($var) || is_numeric($var)) {
149            $var = unpack("f", pack("f", $var))[1];
150        } else {
151            throw new \Exception("Expect float.");
152        }
153    }
154
155    public static function checkDouble(&$var)
156    {
157        if (is_float($var) || is_numeric($var)) {
158            $var = floatval($var);
159        } else {
160            throw new \Exception("Expect float.");
161        }
162    }
163
164    public static function checkBool(&$var)
165    {
166        if (is_array($var) || is_object($var)) {
167            throw new \Exception("Expect boolean.");
168        }
169        $var = boolval($var);
170    }
171
172    public static function checkMessage(&$var, $klass, $newClass = null)
173    {
174        if (!$var instanceof $klass && !is_null($var)) {
175            throw new \Exception("Expect $klass.");
176        }
177    }
178
179    public static function checkRepeatedField(&$var, $type, $klass = null)
180    {
181        if (!$var instanceof RepeatedField && !is_array($var)) {
182            throw new \Exception("Expect array.");
183        }
184        if (is_array($var)) {
185            $tmp = new RepeatedField($type, $klass);
186            foreach ($var as $value) {
187                $tmp[] = $value;
188            }
189            return $tmp;
190        } else {
191            if ($var->getType() != $type) {
192                throw new \Exception(
193                    "Expect repeated field of different type.");
194            }
195            if ($var->getType() === GPBType::MESSAGE &&
196                $var->getClass() !== $klass &&
197                $var->getLegacyClass() !== $klass) {
198                throw new \Exception(
199                    "Expect repeated field of " . $klass . ".");
200            }
201            return $var;
202        }
203    }
204
205    public static function checkMapField(&$var, $key_type, $value_type, $klass = null)
206    {
207        if (!$var instanceof MapField && !is_array($var)) {
208            throw new \Exception("Expect dict.");
209        }
210        if (is_array($var)) {
211            $tmp = new MapField($key_type, $value_type, $klass);
212            foreach ($var as $key => $value) {
213                $tmp[$key] = $value;
214            }
215            return $tmp;
216        } else {
217            if ($var->getKeyType() != $key_type) {
218                throw new \Exception("Expect map field of key type.");
219            }
220            if ($var->getValueType() != $value_type) {
221                throw new \Exception("Expect map field of value type.");
222            }
223            if ($var->getValueType() === GPBType::MESSAGE &&
224                $var->getValueClass() !== $klass &&
225                $var->getLegacyValueClass() !== $klass) {
226                throw new \Exception(
227                    "Expect map field of " . $klass . ".");
228            }
229            return $var;
230        }
231    }
232
233    public static function Int64($value)
234    {
235        return new Int64($value);
236    }
237
238    public static function Uint64($value)
239    {
240        return new Uint64($value);
241    }
242
243    public static function getClassNamePrefix(
244        $classname,
245        $file_proto)
246    {
247        $option = $file_proto->getOptions();
248        $prefix = is_null($option) ? "" : $option->getPhpClassPrefix();
249        if ($prefix !== "") {
250            return $prefix;
251        }
252
253        $reserved_words = array(
254            "abstract"=>0, "and"=>0, "array"=>0, "as"=>0, "break"=>0,
255            "callable"=>0, "case"=>0, "catch"=>0, "class"=>0, "clone"=>0,
256            "const"=>0, "continue"=>0, "declare"=>0, "default"=>0, "die"=>0,
257            "do"=>0, "echo"=>0, "else"=>0, "elseif"=>0, "empty"=>0,
258            "enddeclare"=>0, "endfor"=>0, "endforeach"=>0, "endif"=>0,
259            "endswitch"=>0, "endwhile"=>0, "eval"=>0, "exit"=>0, "extends"=>0,
260            "final"=>0, "finally"=>0, "fn"=>0, "for"=>0, "foreach"=>0,
261            "function"=>0, "global"=>0, "goto"=>0, "if"=>0, "implements"=>0,
262            "include"=>0, "include_once"=>0, "instanceof"=>0, "insteadof"=>0,
263            "interface"=>0, "isset"=>0, "list"=>0, "match"=>0, "namespace"=>0,
264            "new"=>0, "or"=>0, "parent"=>0, "print"=>0, "private"=>0,
265            "protected"=>0,"public"=>0, "readonly" => 0,"require"=>0,
266            "require_once"=>0,"return"=>0, "self"=>0, "static"=>0, "switch"=>0,
267            "throw"=>0,"trait"=>0, "try"=>0,"unset"=>0, "use"=>0, "var"=>0,
268            "while"=>0,"xor"=>0, "yield"=>0, "int"=>0, "float"=>0, "bool"=>0,
269            "string"=>0,"true"=>0, "false"=>0, "null"=>0, "void"=>0,
270            "iterable"=>0
271        );
272
273        if (array_key_exists(strtolower($classname), $reserved_words)) {
274            if ($file_proto->getPackage() === "google.protobuf") {
275                return "GPB";
276            } else {
277                return "PB";
278            }
279        }
280
281        return "";
282    }
283
284    private static function getPreviouslyUnreservedClassNamePrefix(
285        $classname,
286        $file_proto)
287    {
288        $previously_unreserved_words = array(
289            "readonly"=>0
290        );
291
292        if (array_key_exists(strtolower($classname), $previously_unreserved_words)) {
293            $option = $file_proto->getOptions();
294            $prefix = is_null($option) ? "" : $option->getPhpClassPrefix();
295            if ($prefix !== "") {
296                return $prefix;
297            }
298
299            return "";
300        }
301
302        return self::getClassNamePrefix($classname, $file_proto);
303    }
304
305    public static function getLegacyClassNameWithoutPackage(
306        $name,
307        $file_proto)
308    {
309        $classname = implode('_', explode('.', $name));
310        return static::getClassNamePrefix($classname, $file_proto) . $classname;
311    }
312
313    public static function getClassNameWithoutPackage(
314        $name,
315        $file_proto)
316    {
317        $parts = explode('.', $name);
318        foreach ($parts as $i => $part) {
319            $parts[$i] = static::getClassNamePrefix($parts[$i], $file_proto) . $parts[$i];
320        }
321        return implode('\\', $parts);
322    }
323
324    private static function getPreviouslyUnreservedClassNameWithoutPackage(
325        $name,
326        $file_proto)
327    {
328        $parts = explode('.', $name);
329        foreach ($parts as $i => $part) {
330            $parts[$i] = static::getPreviouslyUnreservedClassNamePrefix($parts[$i], $file_proto) . $parts[$i];
331        }
332        return implode('\\', $parts);
333    }
334
335    public static function getFullClassName(
336        $proto,
337        $containing,
338        $file_proto,
339        &$message_name_without_package,
340        &$classname,
341        &$legacy_classname,
342        &$fullname,
343        &$previous_classname)
344    {
345        // Full name needs to start with '.'.
346        $message_name_without_package = $proto->getName();
347        if ($containing !== "") {
348            $message_name_without_package =
349                $containing . "." . $message_name_without_package;
350        }
351
352        $package = $file_proto->getPackage();
353        if ($package === "") {
354            $fullname = $message_name_without_package;
355        } else {
356            $fullname = $package . "." . $message_name_without_package;
357        }
358
359        $class_name_without_package =
360            static::getClassNameWithoutPackage($message_name_without_package, $file_proto);
361        $legacy_class_name_without_package =
362            static::getLegacyClassNameWithoutPackage(
363                $message_name_without_package, $file_proto);
364        $previous_class_name_without_package =
365            static::getPreviouslyUnreservedClassNameWithoutPackage(
366                $message_name_without_package, $file_proto);
367
368        $option = $file_proto->getOptions();
369        if (!is_null($option) && $option->hasPhpNamespace()) {
370            $namespace = $option->getPhpNamespace();
371            if ($namespace !== "") {
372                $classname = $namespace . "\\" . $class_name_without_package;
373                $legacy_classname =
374                    $namespace . "\\" . $legacy_class_name_without_package;
375                $previous_classname =
376                    $namespace . "\\" . $previous_class_name_without_package;
377                return;
378            } else {
379                $classname = $class_name_without_package;
380                $legacy_classname = $legacy_class_name_without_package;
381                $previous_classname = $previous_class_name_without_package;
382                return;
383            }
384        }
385
386        if ($package === "") {
387            $classname = $class_name_without_package;
388            $legacy_classname = $legacy_class_name_without_package;
389            $previous_classname = $previous_class_name_without_package;
390        } else {
391            $parts = array_map('ucwords', explode('.', $package));
392            foreach ($parts as $i => $part) {
393                $parts[$i] = self::getClassNamePrefix($part, $file_proto).$part;
394            }
395            $classname =
396                implode('\\', $parts) .
397                "\\".self::getClassNamePrefix($class_name_without_package,$file_proto).
398                $class_name_without_package;
399            $legacy_classname =
400                implode('\\', array_map('ucwords', explode('.', $package))).
401                "\\".$legacy_class_name_without_package;
402            $previous_classname =
403                implode('\\', array_map('ucwords', explode('.', $package))).
404                "\\".self::getPreviouslyUnreservedClassNamePrefix(
405                    $previous_class_name_without_package, $file_proto).
406                    $previous_class_name_without_package;
407        }
408    }
409
410    public static function combineInt32ToInt64($high, $low)
411    {
412        $isNeg = $high < 0;
413        if ($isNeg) {
414            $high = ~$high;
415            $low = ~$low;
416            $low++;
417            if (!$low) {
418                $high = (int) ($high + 1);
419            }
420        }
421        $result = bcadd(bcmul($high, 4294967296), $low);
422        if ($low < 0) {
423            $result = bcadd($result, 4294967296);
424        }
425        if ($isNeg) {
426          $result = bcsub(0, $result);
427        }
428        return $result;
429    }
430
431    public static function parseTimestamp($timestamp)
432    {
433        // prevent parsing timestamps containing with the non-existent year "0000"
434        // DateTime::createFromFormat parses without failing but as a nonsensical date
435        if (substr($timestamp, 0, 4) === "0000") {
436            throw new \Exception("Year cannot be zero.");
437        }
438        // prevent parsing timestamps ending with a lowercase z
439        if (substr($timestamp, -1, 1) === "z") {
440            throw new \Exception("Timezone cannot be a lowercase z.");
441        }
442
443        $nanoseconds = 0;
444        $periodIndex = strpos($timestamp, ".");
445        if ($periodIndex !== false) {
446            $nanosecondsLength = 0;
447            // find the next non-numeric character in the timestamp to calculate
448            // the length of the nanoseconds text
449            for ($i = $periodIndex + 1, $length = strlen($timestamp); $i < $length; $i++) {
450                if (!is_numeric($timestamp[$i])) {
451                    $nanosecondsLength = $i - ($periodIndex + 1);
452                    break;
453                }
454            }
455            if ($nanosecondsLength % 3 !== 0) {
456                throw new \Exception("Nanoseconds must be disible by 3.");
457            }
458            if ($nanosecondsLength > 9) {
459                throw new \Exception("Nanoseconds must be in the range of 0 to 999,999,999 nanoseconds.");
460            }
461            if ($nanosecondsLength > 0) {
462                $nanoseconds = substr($timestamp, $periodIndex + 1, $nanosecondsLength);
463                $nanoseconds = intval($nanoseconds);
464
465                if ($nanosecondsLength < 9) {
466                    $nanoseconds = $nanoseconds * pow(10, 9 - $nanosecondsLength);
467                }
468
469                // remove the nanoseconds and preceding period from the timestamp
470                $date = substr($timestamp, 0, $periodIndex);
471                $timezone = substr($timestamp, $periodIndex + $nanosecondsLength + 1);
472                $timestamp = $date.$timezone;
473            }
474        }
475
476        $date = \DateTime::createFromFormat(\DateTime::RFC3339, $timestamp, new \DateTimeZone("UTC"));
477        if ($date === false) {
478            throw new \Exception("Invalid RFC 3339 timestamp.");
479        }
480
481        $value = new \Google\Protobuf\Timestamp();
482        $seconds = $date->format("U");
483        $value->setSeconds($seconds);
484        $value->setNanos($nanoseconds);
485        return $value;
486    }
487
488    public static function formatTimestamp($value)
489    {
490        if (bccomp($value->getSeconds(), "253402300800") != -1) {
491          throw new GPBDecodeException("Duration number too large.");
492        }
493        if (bccomp($value->getSeconds(), "-62135596801") != 1) {
494          throw new GPBDecodeException("Duration number too small.");
495        }
496        $nanoseconds = static::getNanosecondsForTimestamp($value->getNanos());
497        if (!empty($nanoseconds)) {
498            $nanoseconds = ".".$nanoseconds;
499        }
500        $date = new \DateTime('@'.$value->getSeconds(), new \DateTimeZone("UTC"));
501        return $date->format("Y-m-d\TH:i:s".$nanoseconds."\Z");
502    }
503
504    public static function parseDuration($value)
505    {
506        if (strlen($value) < 2 || substr($value, -1) !== "s") {
507          throw new GPBDecodeException("Missing s after duration string");
508        }
509        $number = substr($value, 0, -1);
510        if (bccomp($number, "315576000001") != -1) {
511          throw new GPBDecodeException("Duration number too large.");
512        }
513        if (bccomp($number, "-315576000001") != 1) {
514          throw new GPBDecodeException("Duration number too small.");
515        }
516        $pos = strrpos($number, ".");
517        if ($pos !== false) {
518            $seconds = substr($number, 0, $pos);
519            if (bccomp($seconds, 0) < 0) {
520                $nanos = bcmul("0" . substr($number, $pos), -1000000000);
521            } else {
522                $nanos = bcmul("0" . substr($number, $pos), 1000000000);
523            }
524        } else {
525            $seconds = $number;
526            $nanos = 0;
527        }
528        $duration = new Duration();
529        $duration->setSeconds($seconds);
530        $duration->setNanos($nanos);
531        return $duration;
532    }
533
534    public static function formatDuration($value)
535    {
536        if (bccomp($value->getSeconds(), '315576000001') != -1) {
537            throw new GPBDecodeException('Duration number too large.');
538        }
539        if (bccomp($value->getSeconds(), '-315576000001') != 1) {
540            throw new GPBDecodeException('Duration number too small.');
541        }
542
543        $nanos = $value->getNanos();
544        if ($nanos === 0) {
545            return (string) $value->getSeconds();
546        }
547
548        if ($nanos % 1000000 === 0) {
549            $digits = 3;
550        } elseif ($nanos % 1000 === 0) {
551            $digits = 6;
552        } else {
553            $digits = 9;
554        }
555
556        $nanos = bcdiv($nanos, '1000000000', $digits);
557        return bcadd($value->getSeconds(), $nanos, $digits);
558    }
559
560    public static function parseFieldMask($paths_string)
561    {
562        $field_mask = new FieldMask();
563        if (strlen($paths_string) === 0) {
564            return $field_mask;
565        }
566        $path_strings = explode(",", $paths_string);
567        $paths = $field_mask->getPaths();
568        foreach($path_strings as &$path_string) {
569            $field_strings = explode(".", $path_string);
570            foreach($field_strings as &$field_string) {
571                $field_string = camel2underscore($field_string);
572            }
573            $path_string = implode(".", $field_strings);
574            $paths[] = $path_string;
575        }
576        return $field_mask;
577    }
578
579    public static function formatFieldMask($field_mask)
580    {
581        $converted_paths = [];
582        foreach($field_mask->getPaths() as $path) {
583            $fields = explode('.', $path);
584            $converted_path = [];
585            foreach ($fields as $field) {
586                $segments = explode('_', $field);
587                $start = true;
588                $converted_segments = "";
589                foreach($segments as $segment) {
590                  if (!$start) {
591                    $converted = ucfirst($segment);
592                  } else {
593                    $converted = $segment;
594                    $start = false;
595                  }
596                  $converted_segments .= $converted;
597                }
598                $converted_path []= $converted_segments;
599            }
600            $converted_path = implode(".", $converted_path);
601            $converted_paths []= $converted_path;
602        }
603        return implode(",", $converted_paths);
604    }
605
606    public static function getNanosecondsForTimestamp($nanoseconds)
607    {
608        if ($nanoseconds == 0) {
609            return '';
610        }
611        if ($nanoseconds % static::NANOS_PER_MILLISECOND == 0) {
612            return sprintf('%03d', $nanoseconds / static::NANOS_PER_MILLISECOND);
613        }
614        if ($nanoseconds % static::NANOS_PER_MICROSECOND == 0) {
615            return sprintf('%06d', $nanoseconds / static::NANOS_PER_MICROSECOND);
616        }
617        return sprintf('%09d', $nanoseconds);
618    }
619
620    public static function hasSpecialJsonMapping($msg)
621    {
622        return is_a($msg, 'Google\Protobuf\Any')         ||
623               is_a($msg, "Google\Protobuf\ListValue")   ||
624               is_a($msg, "Google\Protobuf\Struct")      ||
625               is_a($msg, "Google\Protobuf\Value")       ||
626               is_a($msg, "Google\Protobuf\Duration")    ||
627               is_a($msg, "Google\Protobuf\Timestamp")   ||
628               is_a($msg, "Google\Protobuf\FieldMask")   ||
629               static::hasJsonValue($msg);
630    }
631
632    public static function hasJsonValue($msg)
633    {
634        return is_a($msg, "Google\Protobuf\DoubleValue") ||
635               is_a($msg, "Google\Protobuf\FloatValue")  ||
636               is_a($msg, "Google\Protobuf\Int64Value")  ||
637               is_a($msg, "Google\Protobuf\UInt64Value") ||
638               is_a($msg, "Google\Protobuf\Int32Value")  ||
639               is_a($msg, "Google\Protobuf\UInt32Value") ||
640               is_a($msg, "Google\Protobuf\BoolValue")   ||
641               is_a($msg, "Google\Protobuf\StringValue") ||
642               is_a($msg, "Google\Protobuf\BytesValue");
643    }
644}
645