comparison vendor/paragonie/random_compat/lib/byte_safe_strings.php @ 16:c2387f117808

Routine composer update
author Chris Cannam
date Tue, 10 Jul 2018 15:07:59 +0100
parents 4c8ae668cc8c
children
comparison
equal deleted inserted replaced
15:e200cb7efeb3 16:c2387f117808
3 * Random_* Compatibility Library 3 * Random_* Compatibility Library
4 * for using the new PHP 7 random_* API in PHP 5 projects 4 * for using the new PHP 7 random_* API in PHP 5 projects
5 * 5 *
6 * The MIT License (MIT) 6 * The MIT License (MIT)
7 * 7 *
8 * Copyright (c) 2015 - 2017 Paragon Initiative Enterprises 8 * Copyright (c) 2015 - 2018 Paragon Initiative Enterprises
9 * 9 *
10 * Permission is hereby granted, free of charge, to any person obtaining a copy 10 * Permission is hereby granted, free of charge, to any person obtaining a copy
11 * of this software and associated documentation files (the "Software"), to deal 11 * of this software and associated documentation files (the "Software"), to deal
12 * in the Software without restriction, including without limitation the rights 12 * in the Software without restriction, including without limitation the rights
13 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 13 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
26 * SOFTWARE. 26 * SOFTWARE.
27 */ 27 */
28 28
29 if (!is_callable('RandomCompat_strlen')) { 29 if (!is_callable('RandomCompat_strlen')) {
30 if ( 30 if (
31 defined('MB_OVERLOAD_STRING') && 31 defined('MB_OVERLOAD_STRING')
32 ini_get('mbstring.func_overload') & MB_OVERLOAD_STRING 32 &&
33 ((int) ini_get('mbstring.func_overload')) & MB_OVERLOAD_STRING
33 ) { 34 ) {
34 /** 35 /**
35 * strlen() implementation that isn't brittle to mbstring.func_overload 36 * strlen() implementation that isn't brittle to mbstring.func_overload
36 * 37 *
37 * This version uses mb_strlen() in '8bit' mode to treat strings as raw 38 * This version uses mb_strlen() in '8bit' mode to treat strings as raw
80 81
81 if (!is_callable('RandomCompat_substr')) { 82 if (!is_callable('RandomCompat_substr')) {
82 83
83 if ( 84 if (
84 defined('MB_OVERLOAD_STRING') 85 defined('MB_OVERLOAD_STRING')
85 && 86 &&
86 ini_get('mbstring.func_overload') & MB_OVERLOAD_STRING 87 ((int) ini_get('mbstring.func_overload')) & MB_OVERLOAD_STRING
87 ) { 88 ) {
88 /** 89 /**
89 * substr() implementation that isn't brittle to mbstring.func_overload 90 * substr() implementation that isn't brittle to mbstring.func_overload
90 * 91 *
91 * This version uses mb_substr() in '8bit' mode to treat strings as raw 92 * This version uses mb_substr() in '8bit' mode to treat strings as raw
92 * binary rather than UTF-8, ISO-8859-1, etc 93 * binary rather than UTF-8, ISO-8859-1, etc
93 * 94 *
94 * @param string $binary_string 95 * @param string $binary_string
95 * @param int $start 96 * @param int $start
96 * @param int $length (optional) 97 * @param int|null $length (optional)
97 * 98 *
98 * @throws TypeError 99 * @throws TypeError
99 * 100 *
100 * @return string 101 * @return string
101 */ 102 */
116 if ($length === null) { 117 if ($length === null) {
117 /** 118 /**
118 * mb_substr($str, 0, NULL, '8bit') returns an empty string on 119 * mb_substr($str, 0, NULL, '8bit') returns an empty string on
119 * PHP 5.3, so we have to find the length ourselves. 120 * PHP 5.3, so we have to find the length ourselves.
120 */ 121 */
122 /** @var int $length */
121 $length = RandomCompat_strlen($binary_string) - $start; 123 $length = RandomCompat_strlen($binary_string) - $start;
122 } elseif (!is_int($length)) { 124 } elseif (!is_int($length)) {
123 throw new TypeError( 125 throw new TypeError(
124 'RandomCompat_substr(): Third argument should be an integer, or omitted' 126 'RandomCompat_substr(): Third argument should be an integer, or omitted'
125 ); 127 );
131 } 133 }
132 if ($start > RandomCompat_strlen($binary_string)) { 134 if ($start > RandomCompat_strlen($binary_string)) {
133 return ''; 135 return '';
134 } 136 }
135 137
136 return (string) mb_substr($binary_string, $start, $length, '8bit'); 138 return (string) mb_substr(
139 (string) $binary_string,
140 (int) $start,
141 (int) $length,
142 '8bit'
143 );
137 } 144 }
138 145
139 } else { 146 } else {
140 147
141 /** 148 /**
143 * 150 *
144 * This version just uses the default substr() 151 * This version just uses the default substr()
145 * 152 *
146 * @param string $binary_string 153 * @param string $binary_string
147 * @param int $start 154 * @param int $start
148 * @param int $length (optional) 155 * @param int|null $length (optional)
149 * 156 *
150 * @throws TypeError 157 * @throws TypeError
151 * 158 *
152 * @return string 159 * @return string
153 */ 160 */
170 throw new TypeError( 177 throw new TypeError(
171 'RandomCompat_substr(): Third argument should be an integer, or omitted' 178 'RandomCompat_substr(): Third argument should be an integer, or omitted'
172 ); 179 );
173 } 180 }
174 181
175 return (string) substr($binary_string, $start, $length); 182 return (string) substr(
183 (string )$binary_string,
184 (int) $start,
185 (int) $length
186 );
176 } 187 }
177 188
178 return (string) substr($binary_string, $start); 189 return (string) substr(
190 (string) $binary_string,
191 (int) $start
192 );
179 } 193 }
180 } 194 }
181 } 195 }