| 1 | //===-- Implementation of a64l --------------------------------------------===// |
| 2 | // |
| 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #include "src/stdlib/a64l.h" |
| 10 | #include "hdr/types/size_t.h" |
| 11 | #include "src/__support/common.h" |
| 12 | #include "src/__support/ctype_utils.h" |
| 13 | #include "src/__support/macros/config.h" |
| 14 | |
| 15 | #include <stdint.h> |
| 16 | |
| 17 | namespace LIBC_NAMESPACE_DECL { |
| 18 | |
| 19 | // I'm not sure this should go in ctype_utils since the specific ordering of |
| 20 | // base64 is so very implementation specific, and also this set is unusual. |
| 21 | // Returns -1 on any char without a specified value. |
| 22 | constexpr static int32_t b64_char_to_int(char ch) { |
| 23 | // from the standard: "The characters used to represent digits are '.' (dot) |
| 24 | // for 0, '/' for 1, '0' through '9' for [2,11], 'A' through 'Z' for [12,37], |
| 25 | // and 'a' through 'z' for [38,63]." |
| 26 | if (ch == '.') |
| 27 | return 0; |
| 28 | if (ch == '/') |
| 29 | return 1; |
| 30 | |
| 31 | // handle the case of an unspecified char. |
| 32 | if (!internal::isalnum(ch)) |
| 33 | return -1; |
| 34 | |
| 35 | bool is_lower = internal::islower(ch); |
| 36 | // add 2 to account for '.' and '/', then b36_char_to_int is case insensitive |
| 37 | // so add case sensitivity back. |
| 38 | return internal::b36_char_to_int(ch) + 2 + (is_lower ? 26 : 0); |
| 39 | } |
| 40 | |
| 41 | // This function takes a base 64 string and writes it to the low 32 bits of a |
| 42 | // long. |
| 43 | // TODO: use LIBC_ADD_NULL_CHECKS for checking if the input is a null pointer. |
| 44 | LLVM_LIBC_FUNCTION(long, a64l, (const char *s)) { |
| 45 | // the standard says to only use up to 6 characters. |
| 46 | constexpr size_t MAX_LENGTH = 6; |
| 47 | int32_t result = 0; |
| 48 | |
| 49 | for (size_t i = 0; i < MAX_LENGTH && s[i] != '\0'; ++i) { |
| 50 | int32_t cur_val = b64_char_to_int(s[i]); |
| 51 | // The standard says what happens on an unspecified character is undefined, |
| 52 | // here we treat it as the end of the string. |
| 53 | if (cur_val == -1) |
| 54 | break; |
| 55 | |
| 56 | // the first digit is the least significant, so for each subsequent digit we |
| 57 | // shift it more. 6 bits since 2^6 = 64 |
| 58 | result += (cur_val << (6 * i)); |
| 59 | } |
| 60 | |
| 61 | // standard says to sign extend from 32 bits. |
| 62 | return static_cast<long>(result); |
| 63 | } |
| 64 | |
| 65 | } // namespace LIBC_NAMESPACE_DECL |
| 66 | |