113498266Sopenharmony_ci/*************************************************************************** 213498266Sopenharmony_ci * _ _ ____ _ 313498266Sopenharmony_ci * Project ___| | | | _ \| | 413498266Sopenharmony_ci * / __| | | | |_) | | 513498266Sopenharmony_ci * | (__| |_| | _ <| |___ 613498266Sopenharmony_ci * \___|\___/|_| \_\_____| 713498266Sopenharmony_ci * 813498266Sopenharmony_ci * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al. 913498266Sopenharmony_ci * 1013498266Sopenharmony_ci * This software is licensed as described in the file COPYING, which 1113498266Sopenharmony_ci * you should have received as part of this distribution. The terms 1213498266Sopenharmony_ci * are also available at https://curl.se/docs/copyright.html. 1313498266Sopenharmony_ci * 1413498266Sopenharmony_ci * You may opt to use, copy, modify, merge, publish, distribute and/or sell 1513498266Sopenharmony_ci * copies of the Software, and permit persons to whom the Software is 1613498266Sopenharmony_ci * furnished to do so, under the terms of the COPYING file. 1713498266Sopenharmony_ci * 1813498266Sopenharmony_ci * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY 1913498266Sopenharmony_ci * KIND, either express or implied. 2013498266Sopenharmony_ci * 2113498266Sopenharmony_ci * SPDX-License-Identifier: curl 2213498266Sopenharmony_ci * 2313498266Sopenharmony_ci ***************************************************************************/ 2413498266Sopenharmony_ci 2513498266Sopenharmony_ci#include "curl_setup.h" 2613498266Sopenharmony_ci 2713498266Sopenharmony_ci#include <curl/curl.h> 2813498266Sopenharmony_ci 2913498266Sopenharmony_ci#include "strcase.h" 3013498266Sopenharmony_ci 3113498266Sopenharmony_ci/* Mapping table to go from lowercase to uppercase for plain ASCII.*/ 3213498266Sopenharmony_cistatic const unsigned char touppermap[256] = { 3313498266Sopenharmony_ci0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 3413498266Sopenharmony_ci22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 3513498266Sopenharmony_ci41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 3613498266Sopenharmony_ci60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 3713498266Sopenharmony_ci79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 65, 3813498266Sopenharmony_ci66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 3913498266Sopenharmony_ci85, 86, 87, 88, 89, 90, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 4013498266Sopenharmony_ci134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 4113498266Sopenharmony_ci150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164, 165, 4213498266Sopenharmony_ci166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176, 177, 178, 179, 180, 181, 4313498266Sopenharmony_ci182, 183, 184, 185, 186, 187, 188, 189, 190, 191, 192, 193, 194, 195, 196, 197, 4413498266Sopenharmony_ci198, 199, 200, 201, 202, 203, 204, 205, 206, 207, 208, 209, 210, 211, 212, 213, 4513498266Sopenharmony_ci214, 215, 216, 217, 218, 219, 220, 221, 222, 223, 224, 225, 226, 227, 228, 229, 4613498266Sopenharmony_ci230, 231, 232, 233, 234, 235, 236, 237, 238, 239, 240, 241, 242, 243, 244, 245, 4713498266Sopenharmony_ci246, 247, 248, 249, 250, 251, 252, 253, 254, 255 4813498266Sopenharmony_ci}; 4913498266Sopenharmony_ci 5013498266Sopenharmony_ci/* Mapping table to go from uppercase to lowercase for plain ASCII.*/ 5113498266Sopenharmony_cistatic const unsigned char tolowermap[256] = { 5213498266Sopenharmony_ci0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 5313498266Sopenharmony_ci22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 5413498266Sopenharmony_ci42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 5513498266Sopenharmony_ci62, 63, 64, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 5613498266Sopenharmony_ci111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 91, 92, 93, 94, 95, 5713498266Sopenharmony_ci96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 5813498266Sopenharmony_ci112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 5913498266Sopenharmony_ci128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 6013498266Sopenharmony_ci144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 6113498266Sopenharmony_ci160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 6213498266Sopenharmony_ci176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191, 6313498266Sopenharmony_ci192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205, 206, 207, 6413498266Sopenharmony_ci208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, 220, 221, 222, 223, 6513498266Sopenharmony_ci224, 225, 226, 227, 228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239, 6613498266Sopenharmony_ci240, 241, 242, 243, 244, 245, 246, 247, 248, 249, 250, 251, 252, 253, 254, 255 6713498266Sopenharmony_ci}; 6813498266Sopenharmony_ci 6913498266Sopenharmony_ci 7013498266Sopenharmony_ci/* Portable, consistent toupper. Do not use toupper() because its behavior is 7113498266Sopenharmony_ci altered by the current locale. */ 7213498266Sopenharmony_cichar Curl_raw_toupper(char in) 7313498266Sopenharmony_ci{ 7413498266Sopenharmony_ci return touppermap[(unsigned char) in]; 7513498266Sopenharmony_ci} 7613498266Sopenharmony_ci 7713498266Sopenharmony_ci 7813498266Sopenharmony_ci/* Portable, consistent tolower. Do not use tolower() because its behavior is 7913498266Sopenharmony_ci altered by the current locale. */ 8013498266Sopenharmony_cichar Curl_raw_tolower(char in) 8113498266Sopenharmony_ci{ 8213498266Sopenharmony_ci return tolowermap[(unsigned char) in]; 8313498266Sopenharmony_ci} 8413498266Sopenharmony_ci 8513498266Sopenharmony_ci/* 8613498266Sopenharmony_ci * curl_strequal() is for doing "raw" case insensitive strings. This is meant 8713498266Sopenharmony_ci * to be locale independent and only compare strings we know are safe for 8813498266Sopenharmony_ci * this. See https://daniel.haxx.se/blog/2008/10/15/strcasecmp-in-turkish/ for 8913498266Sopenharmony_ci * further explanations as to why this function is necessary. 9013498266Sopenharmony_ci */ 9113498266Sopenharmony_ci 9213498266Sopenharmony_cistatic int casecompare(const char *first, const char *second) 9313498266Sopenharmony_ci{ 9413498266Sopenharmony_ci while(*first && *second) { 9513498266Sopenharmony_ci if(Curl_raw_toupper(*first) != Curl_raw_toupper(*second)) 9613498266Sopenharmony_ci /* get out of the loop as soon as they don't match */ 9713498266Sopenharmony_ci return 0; 9813498266Sopenharmony_ci first++; 9913498266Sopenharmony_ci second++; 10013498266Sopenharmony_ci } 10113498266Sopenharmony_ci /* If we're here either the strings are the same or the length is different. 10213498266Sopenharmony_ci We can just test if the "current" character is non-zero for one and zero 10313498266Sopenharmony_ci for the other. Note that the characters may not be exactly the same even 10413498266Sopenharmony_ci if they match, we only want to compare zero-ness. */ 10513498266Sopenharmony_ci return !*first == !*second; 10613498266Sopenharmony_ci} 10713498266Sopenharmony_ci 10813498266Sopenharmony_ci/* --- public function --- */ 10913498266Sopenharmony_ciint curl_strequal(const char *first, const char *second) 11013498266Sopenharmony_ci{ 11113498266Sopenharmony_ci if(first && second) 11213498266Sopenharmony_ci /* both pointers point to something then compare them */ 11313498266Sopenharmony_ci return casecompare(first, second); 11413498266Sopenharmony_ci 11513498266Sopenharmony_ci /* if both pointers are NULL then treat them as equal */ 11613498266Sopenharmony_ci return (NULL == first && NULL == second); 11713498266Sopenharmony_ci} 11813498266Sopenharmony_ci 11913498266Sopenharmony_cistatic int ncasecompare(const char *first, const char *second, size_t max) 12013498266Sopenharmony_ci{ 12113498266Sopenharmony_ci while(*first && *second && max) { 12213498266Sopenharmony_ci if(Curl_raw_toupper(*first) != Curl_raw_toupper(*second)) 12313498266Sopenharmony_ci return 0; 12413498266Sopenharmony_ci max--; 12513498266Sopenharmony_ci first++; 12613498266Sopenharmony_ci second++; 12713498266Sopenharmony_ci } 12813498266Sopenharmony_ci if(0 == max) 12913498266Sopenharmony_ci return 1; /* they are equal this far */ 13013498266Sopenharmony_ci 13113498266Sopenharmony_ci return Curl_raw_toupper(*first) == Curl_raw_toupper(*second); 13213498266Sopenharmony_ci} 13313498266Sopenharmony_ci 13413498266Sopenharmony_ci/* --- public function --- */ 13513498266Sopenharmony_ciint curl_strnequal(const char *first, const char *second, size_t max) 13613498266Sopenharmony_ci{ 13713498266Sopenharmony_ci if(first && second) 13813498266Sopenharmony_ci /* both pointers point to something then compare them */ 13913498266Sopenharmony_ci return ncasecompare(first, second, max); 14013498266Sopenharmony_ci 14113498266Sopenharmony_ci /* if both pointers are NULL then treat them as equal if max is non-zero */ 14213498266Sopenharmony_ci return (NULL == first && NULL == second && max); 14313498266Sopenharmony_ci} 14413498266Sopenharmony_ci/* Copy an upper case version of the string from src to dest. The 14513498266Sopenharmony_ci * strings may overlap. No more than n characters of the string are copied 14613498266Sopenharmony_ci * (including any NUL) and the destination string will NOT be 14713498266Sopenharmony_ci * NUL-terminated if that limit is reached. 14813498266Sopenharmony_ci */ 14913498266Sopenharmony_civoid Curl_strntoupper(char *dest, const char *src, size_t n) 15013498266Sopenharmony_ci{ 15113498266Sopenharmony_ci if(n < 1) 15213498266Sopenharmony_ci return; 15313498266Sopenharmony_ci 15413498266Sopenharmony_ci do { 15513498266Sopenharmony_ci *dest++ = Curl_raw_toupper(*src); 15613498266Sopenharmony_ci } while(*src++ && --n); 15713498266Sopenharmony_ci} 15813498266Sopenharmony_ci 15913498266Sopenharmony_ci/* Copy a lower case version of the string from src to dest. The 16013498266Sopenharmony_ci * strings may overlap. No more than n characters of the string are copied 16113498266Sopenharmony_ci * (including any NUL) and the destination string will NOT be 16213498266Sopenharmony_ci * NUL-terminated if that limit is reached. 16313498266Sopenharmony_ci */ 16413498266Sopenharmony_civoid Curl_strntolower(char *dest, const char *src, size_t n) 16513498266Sopenharmony_ci{ 16613498266Sopenharmony_ci if(n < 1) 16713498266Sopenharmony_ci return; 16813498266Sopenharmony_ci 16913498266Sopenharmony_ci do { 17013498266Sopenharmony_ci *dest++ = Curl_raw_tolower(*src); 17113498266Sopenharmony_ci } while(*src++ && --n); 17213498266Sopenharmony_ci} 17313498266Sopenharmony_ci 17413498266Sopenharmony_ci/* Compare case-sensitive NUL-terminated strings, taking care of possible 17513498266Sopenharmony_ci * null pointers. Return true if arguments match. 17613498266Sopenharmony_ci */ 17713498266Sopenharmony_cibool Curl_safecmp(char *a, char *b) 17813498266Sopenharmony_ci{ 17913498266Sopenharmony_ci if(a && b) 18013498266Sopenharmony_ci return !strcmp(a, b); 18113498266Sopenharmony_ci return !a && !b; 18213498266Sopenharmony_ci} 18313498266Sopenharmony_ci 18413498266Sopenharmony_ci/* 18513498266Sopenharmony_ci * Curl_timestrcmp() returns 0 if the two strings are identical. The time this 18613498266Sopenharmony_ci * function spends is a function of the shortest string, not of the contents. 18713498266Sopenharmony_ci */ 18813498266Sopenharmony_ciint Curl_timestrcmp(const char *a, const char *b) 18913498266Sopenharmony_ci{ 19013498266Sopenharmony_ci int match = 0; 19113498266Sopenharmony_ci int i = 0; 19213498266Sopenharmony_ci 19313498266Sopenharmony_ci if(a && b) { 19413498266Sopenharmony_ci while(1) { 19513498266Sopenharmony_ci match |= a[i]^b[i]; 19613498266Sopenharmony_ci if(!a[i] || !b[i]) 19713498266Sopenharmony_ci break; 19813498266Sopenharmony_ci i++; 19913498266Sopenharmony_ci } 20013498266Sopenharmony_ci } 20113498266Sopenharmony_ci else 20213498266Sopenharmony_ci return a || b; 20313498266Sopenharmony_ci return match; 20413498266Sopenharmony_ci} 205