Proyectos de Subversion Moodle

Rev

Rev 1 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
<?php
2
namespace Aws\Crypto\Polyfill;
3
 
4
/**
5
 * Class ByteArray
6
 */
7
class ByteArray extends \SplFixedArray
8
{
9
    use NeedsTrait;
10
 
11
    /**
12
     * ByteArray constructor.
13
     *
14
     * @param int|string|int[] $size
15
     *     If you pass in an integer, it creates a ByteArray of that size.
16
     *     If you pass in a string or array, it converts it to an array of
17
     *       integers between 0 and 255.
18
     * @throws \InvalidArgumentException
19
     */
20
    public function __construct($size = 0)
21
    {
22
        $arr = null;
23
        // Integer? This behaves just like SplFixedArray.
24
        if (\is_array($size)) {
25
            // Array? We need to pass the count to parent::__construct() then populate
26
            $arr = $size;
27
            $size = \count($arr);
28
        } elseif (\is_string($size)) {
29
            // We need to avoid mbstring.func_overload
30
            if (\is_callable('\\mb_str_split')) {
31
                $tmp = \mb_str_split($size, 1, '8bit');
32
            } else {
33
                $tmp = \str_split($size, 1);
34
            }
35
            // Let's convert each character to an 8-bit integer and store in $arr
36
            $arr = [];
37
            if (!empty($tmp)) {
38
                foreach ($tmp as $t) {
39
                    if (strlen($t) < 1) {
40
                        continue;
41
                    }
42
                    $arr []= \unpack('C', $t)[1] & 0xff;
43
                }
44
            }
45
            $size = \count($arr);
46
        } elseif ($size instanceof ByteArray) {
47
            $arr = $size->toArray();
48
            $size = $size->count();
49
        } elseif (!\is_int($size)) {
50
            throw new \InvalidArgumentException(
51
                'Argument must be an integer, string, or array of integers.'
52
            );
53
        }
54
 
55
        parent::__construct($size);
56
 
57
        if (!empty($arr)) {
58
            // Populate this object with values from constructor argument
59
            foreach ($arr as $i => $v) {
60
                $this->offsetSet($i, $v);
61
            }
62
        } else {
63
            // Initialize to zero.
64
            for ($i = 0; $i < $size; ++$i) {
65
                $this->offsetSet($i, 0);
66
            }
67
        }
68
    }
69
 
70
    /**
71
     * Encode an integer into a byte array. 32-bit (unsigned), big endian byte order.
72
     *
73
     * @param int $num
74
     * @return self
75
     */
76
    public static function enc32be($num)
77
    {
78
        return new ByteArray(\pack('N', $num));
79
    }
80
 
81
    /**
82
     * @param ByteArray $other
83
     * @return bool
84
     */
85
    public function equals(ByteArray $other)
86
    {
87
        if ($this->count() !== $other->count()) {
88
            return false;
89
        }
90
        $d = 0;
91
        for ($i = $this->count() - 1; $i >= 0; --$i) {
92
            $d |= $this[$i] ^ $other[$i];
93
        }
94
        return $d === 0;
95
    }
96
 
97
    /**
98
     * @param ByteArray $array
99
     * @return ByteArray
100
     */
101
    public function exclusiveOr(ByteArray $array)
102
    {
103
        self::needs(
104
            $this->count() === $array->count(),
105
            'Both ByteArrays must be equal size for exclusiveOr()'
106
        );
107
        $out = clone $this;
108
        for ($i = 0; $i < $this->count(); ++$i) {
109
            $out[$i] = $array[$i] ^ $out[$i];
110
        }
111
        return $out;
112
    }
113
 
114
    /**
115
     * Returns a new ByteArray incremented by 1 (big endian byte order).
116
     *
117
     * @param int $increase
118
     * @return self
119
     */
120
    public function getIncremented($increase = 1)
121
    {
122
        $clone = clone $this;
123
        $index = $clone->count();
124
        while ($index > 0) {
125
            --$index;
126
            $tmp = ($clone[$index] + $increase) & PHP_INT_MAX;
127
            $clone[$index] = $tmp & 0xff;
128
            $increase = $tmp >> 8;
129
        }
130
        return $clone;
131
    }
132
 
133
    /**
134
     * Sets a value. See SplFixedArray for more.
135
     *
136
     * @param int $index
137
     * @param int $newval
138
     * @return void
139
     */
1441 ariadna 140
    #[\ReturnTypeWillChange]
1 efrain 141
    public function offsetSet($index, $newval)
142
    {
143
        parent::offsetSet($index, $newval & 0xff);
144
    }
145
 
146
    /**
147
     * Return a copy of this ByteArray, bitshifted to the right by 1.
148
     * Used in Gmac.
149
     *
150
     * @return self
151
     */
152
    public function rshift()
153
    {
154
        $out = clone $this;
155
        for ($j = $this->count() - 1; $j > 0; --$j) {
156
            $out[$j] = (($out[$j - 1] & 1) << 7) | ($out[$j] >> 1);
157
        }
158
        $out[0] >>= 1;
159
        return $out;
160
    }
161
 
162
    /**
163
     * Constant-time conditional select. This is meant to read like a ternary operator.
164
     *
165
     * $z = ByteArray::select(1, $x, $y); // $z is equal to $x
166
     * $z = ByteArray::select(0, $x, $y); // $z is equal to $y
167
     *
168
     * @param int $select
169
     * @param ByteArray $left
170
     * @param ByteArray $right
171
     * @return ByteArray
172
     */
173
    public static function select($select, ByteArray $left, ByteArray $right)
174
    {
175
        self::needs(
176
            $left->count() === $right->count(),
177
            'Both ByteArrays must be equal size for select()'
178
        );
179
        $rightLength = $right->count();
180
        $out = clone $right;
181
        $mask = (-($select & 1)) & 0xff;
182
        for ($i = 0; $i < $rightLength;  $i++) {
183
            $out[$i] = $out[$i] ^ (($left[$i] ^ $right[$i]) & $mask);
184
        }
185
        return $out;
186
    }
187
 
188
    /**
189
     * Overwrite values of this ByteArray based on a separate ByteArray, with
190
     * a given starting offset and length.
191
     *
192
     * See JavaScript's Uint8Array.set() for more information.
193
     *
194
     * @param ByteArray $input
195
     * @param int $offset
196
     * @param int|null $length
197
     * @return self
198
     */
199
    public function set(ByteArray $input, $offset = 0, $length = null)
200
    {
201
        self::needs(
202
            is_int($offset) && $offset >= 0,
203
            'Offset must be a positive integer or zero'
204
        );
205
        if (is_null($length)) {
206
            $length = $input->count();
207
        }
208
 
209
        $i = 0; $j = $offset;
210
        while ($i < $length && $j < $this->count()) {
211
            $this[$j] = $input[$i];
212
            ++$i;
213
            ++$j;
214
        }
215
        return $this;
216
    }
217
 
218
    /**
219
     * Returns a slice of this ByteArray.
220
     *
221
     * @param int $start
222
     * @param null $length
223
     * @return self
224
     */
225
    public function slice($start = 0, $length = null)
226
    {
227
        return new ByteArray(\array_slice($this->toArray(), $start, $length));
228
    }
229
 
230
    /**
231
     * Mutates the current state and sets all values to zero.
232
     *
233
     * @return void
234
     */
235
    public function zeroize()
236
    {
237
        for ($i = $this->count() - 1; $i >= 0; --$i) {
238
            $this->offsetSet($i, 0);
239
        }
240
    }
241
 
242
    /**
243
     * Converts the ByteArray to a raw binary string.
244
     *
245
     * @return string
246
     */
247
    public function toString()
248
    {
249
        $count = $this->count();
250
        if ($count === 0) {
251
            return '';
252
        }
253
        $args = $this->toArray();
254
        \array_unshift($args, \str_repeat('C', $count));
255
        // constant-time, PHP <5.6 equivalent to pack('C*', ...$args);
256
        return \call_user_func_array('\\pack', $args);
257
    }
258
}