1 /*** 2 *mbscmp.c - Compare MBCS strings 3 * 4 * Copyright (c) Microsoft Corporation. All rights reserved. 5 * 6 *Purpose: 7 * Compare MBCS strings 8 * 9 *******************************************************************************/ 10 #ifndef _MBCS 11 #error This file should only be compiled with _MBCS defined 12 #endif 13 14 #include <corecrt_internal_mbstring.h> 15 #include <locale.h> 16 #include <string.h> 17 18 #pragma warning(disable:__WARNING_POTENTIAL_BUFFER_OVERFLOW_NULLTERMINATED) // 26018 19 20 /*** 21 * _mbscmp - Compare MBCS strings 22 * 23 *Purpose: 24 * Compares two strings in ordinal order. 25 * Single byte strings are compared byte-by-byte 26 * Double byte strings are compared by character 27 * (basically ensuring that double byte sequences are higher than single byte) 28 * UTF-8 is compared byte-by-byte, which work since its the same as character order. 29 * 30 *Entry: 31 * char *s1, *s2 = strings to compare 32 * 33 *WARNING: 34 * No validation for improper UTF-8 strings, mixed up lead/trail byte orders are not defined. 35 * 36 *Exit: 37 * Returns <0 if s1 < s2 38 * Returns 0 if s1 == s2 39 * Returns >0 if s1 > s2 40 * Returns _NLSCMPERROR is something went wrong 41 * 42 *Exceptions: 43 * Input parameters are validated. Refer to the validation section of the function. 44 * 45 *******************************************************************************/ 46 47 extern "C" int __cdecl _mbscmp_l( 48 const unsigned char *s1, 49 const unsigned char *s2, 50 _locale_t plocinfo 51 ) 52 { 53 unsigned short c1, c2; 54 _LocaleUpdate _loc_update(plocinfo); 55 56 /* validation section */ 57 _VALIDATE_RETURN(s1 != nullptr, EINVAL, _NLSCMPERROR); 58 _VALIDATE_RETURN(s2 != nullptr, EINVAL, _NLSCMPERROR); 59 if (_loc_update.GetLocaleT()->mbcinfo->ismbcodepage == 0) 60 return strcmp((const char *)s1, (const char *)s2); 61 62 for (;;) { 63 64 c1 = *s1++; 65 if ( _ismbblead_l(c1, _loc_update.GetLocaleT()) ) 66 c1 = ( (*s1 == '\0') ? 0 : ((c1<<8) | *s1++) ); 67 68 c2 = *s2++; 69 if ( _ismbblead_l(c2, _loc_update.GetLocaleT()) ) 70 c2 = ( (*s2 == '\0') ? 0 : ((c2<<8) | *s2++) ); 71 72 if (c1 != c2) 73 return (c1 > c2) ? 1 : -1; 74 75 if (c1 == 0) 76 return 0; 77 78 } 79 } 80 81 extern "C" int (__cdecl _mbscmp)( 82 const unsigned char *s1, 83 const unsigned char *s2 84 ) 85 { 86 return _mbscmp_l(s1, s2, nullptr); 87 } 88