/*1* SPDX-License-Identifier: ISC2*3* Copyright (c) 2013-2015, 2023 Todd C. Miller <[email protected]>4*5* Permission to use, copy, modify, and distribute this software for any6* purpose with or without fee is hereby granted, provided that the above7* copyright notice and this permission notice appear in all copies.8*9* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES10* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF11* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR12* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES13* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN14* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF15* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.16*/1718#include <config.h>1920#include <sudo_compat.h>21#include <sudo_debug.h>22#include <sudo_util.h>2324/*25* Converts a two-byte hex string to decimal.26* Returns a value 0-255 on success or -1 for invalid input.27*/28int29sudo_hexchar_v1(const char s[restrict static 2])30{31unsigned char result[2];32unsigned int i;33debug_decl(sudo_hexchar, SUDO_DEBUG_UTIL);3435for (i = 0; i < 2; i++) {36switch (s[i]) {37case '0':38result[i] = 0;39break;40case '1':41result[i] = 1;42break;43case '2':44result[i] = 2;45break;46case '3':47result[i] = 3;48break;49case '4':50result[i] = 4;51break;52case '5':53result[i] = 5;54break;55case '6':56result[i] = 6;57break;58case '7':59result[i] = 7;60break;61case '8':62result[i] = 8;63break;64case '9':65result[i] = 9;66break;67case 'A':68case 'a':69result[i] = 10;70break;71case 'B':72case 'b':73result[i] = 11;74break;75case 'C':76case 'c':77result[i] = 12;78break;79case 'D':80case 'd':81result[i] = 13;82break;83case 'E':84case 'e':85result[i] = 14;86break;87case 'F':88case 'f':89result[i] = 15;90break;91default:92/* Invalid input. */93debug_return_int(-1);94}95}96debug_return_int((result[0] << 4) | result[1]);97}9899100