1<?php
2namespace ParagonIE\ConstantTime;
3
4/**
5 *  Copyright (c) 2016 - 2017 Paragon Initiative Enterprises.
6 *  Copyright (c) 2014 Steve "Sc00bz" Thomas (steve at tobtu dot com)
7 *
8 *  Permission is hereby granted, free of charge, to any person obtaining a copy
9 *  of this software and associated documentation files (the "Software"), to deal
10 *  in the Software without restriction, including without limitation the rights
11 *  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 *  copies of the Software, and to permit persons to whom the Software is
13 *  furnished to do so, subject to the following conditions:
14 *
15 *  The above copyright notice and this permission notice shall be included in all
16 *  copies or substantial portions of the Software.
17 *
18 *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 *  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 *  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 *  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 *  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 *  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 *  SOFTWARE.
25 */
26
27/**
28 * Class Base32Hex
29 * [0-9][A-V]
30 *
31 * @package ParagonIE\ConstantTime
32 */
33abstract class Base32Hex extends Base32
34{
35    /**
36     * Uses bitwise operators instead of table-lookups to turn 5-bit integers
37     * into 8-bit integers.
38     *
39     * @param int $src
40     * @return int
41     */
42    protected static function decode5Bits($src)
43    {
44        $ret = -1;
45
46        // if ($src > 0x30 && $src < 0x3a) ret += $src - 0x2e + 1; // -47
47        $ret += (((0x2f - $src) & ($src - 0x3a)) >> 8) & ($src - 47);
48
49        // if ($src > 0x60 && $src < 0x77) ret += $src - 0x61 + 10 + 1; // -86
50        $ret += (((0x60 - $src) & ($src - 0x77)) >> 8) & ($src - 86);
51
52        return $ret;
53    }
54
55    /**
56     * Uses bitwise operators instead of table-lookups to turn 5-bit integers
57     * into 8-bit integers.
58     *
59     * @param int $src
60     * @return int
61     */
62    protected static function decode5BitsUpper($src)
63    {
64        $ret = -1;
65
66        // if ($src > 0x30 && $src < 0x3a) ret += $src - 0x2e + 1; // -47
67        $ret += (((0x2f - $src) & ($src - 0x3a)) >> 8) & ($src - 47);
68
69        // if ($src > 0x40 && $src < 0x57) ret += $src - 0x41 + 10 + 1; // -54
70        $ret += (((0x40 - $src) & ($src - 0x57)) >> 8) & ($src - 54);
71
72        return $ret;
73    }
74
75    /**
76     * Uses bitwise operators instead of table-lookups to turn 8-bit integers
77     * into 5-bit integers.
78     *
79     * @param int $src
80     * @return string
81     */
82    protected static function encode5Bits($src)
83    {
84        $src += 0x30;
85
86        // if ($src > 0x39) $src += 0x61 - 0x3a; // 39
87        $src += ((0x39 - $src) >> 8) & 39;
88
89        return \pack('C', $src);
90    }
91
92    /**
93     * Uses bitwise operators instead of table-lookups to turn 8-bit integers
94     * into 5-bit integers.
95     *
96     * Uppercase variant.
97     *
98     * @param int $src
99     * @return string
100     */
101    protected static function encode5BitsUpper($src)
102    {
103        $src += 0x30;
104
105        // if ($src > 0x39) $src += 0x41 - 0x3a; // 7
106        $src += ((0x39 - $src) >> 8) & 7;
107
108        return \pack('C', $src);
109    }
110}