Path: blob/main/system/lib/llvm-libc/src/stdlib/a64l.cpp
6175 views
//===-- Implementation of a64l --------------------------------------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//78#include "src/stdlib/a64l.h"9#include "hdr/types/size_t.h"10#include "src/__support/common.h"11#include "src/__support/ctype_utils.h"12#include "src/__support/macros/config.h"1314#include <stdint.h>1516namespace LIBC_NAMESPACE_DECL {1718// I'm not sure this should go in ctype_utils since the specific ordering of19// base64 is so very implementation specific, and also this set is unusual.20// Returns -1 on any char without a specified value.21constexpr static int32_t b64_char_to_int(char ch) {22// from the standard: "The characters used to represent digits are '.' (dot)23// for 0, '/' for 1, '0' through '9' for [2,11], 'A' through 'Z' for [12,37],24// and 'a' through 'z' for [38,63]."25if (ch == '.')26return 0;27if (ch == '/')28return 1;2930// handle the case of an unspecified char.31if (!internal::isalnum(ch))32return -1;3334bool is_lower = internal::islower(ch);35// add 2 to account for '.' and '/', then b36_char_to_int is case insensitive36// so add case sensitivity back.37return internal::b36_char_to_int(ch) + 2 + (is_lower ? 26 : 0);38}3940// This function takes a base 64 string and writes it to the low 32 bits of a41// long.42// TODO: use LIBC_ADD_NULL_CHECKS for checking if the input is a null pointer.43LLVM_LIBC_FUNCTION(long, a64l, (const char *s)) {44// the standard says to only use up to 6 characters.45constexpr size_t MAX_LENGTH = 6;46int32_t result = 0;4748for (size_t i = 0; i < MAX_LENGTH && s[i] != '\0'; ++i) {49int32_t cur_val = b64_char_to_int(s[i]);50// The standard says what happens on an unspecified character is undefined,51// here we treat it as the end of the string.52if (cur_val == -1)53break;5455// the first digit is the least significant, so for each subsequent digit we56// shift it more. 6 bits since 2^6 = 6457result += (cur_val << (6 * i));58}5960// standard says to sign extend from 32 bits.61return static_cast<long>(result);62}6364} // namespace LIBC_NAMESPACE_DECL656667