Reactos
at master 109 lines 3.8 kB view raw
1/*** 2*mbsdec.c - Move MBCS string pointer backward one charcter. 3* 4* Copyright (c) Microsoft Corporation. All rights reserved. 5* 6*Purpose: 7* Move MBCS string pointer backward one character. 8* 9*******************************************************************************/ 10#ifndef _MBCS 11 #error This file should only be compiled with _MBCS defined 12#endif 13 14#include <corecrt_internal.h> 15#include <corecrt_internal_mbstring.h> 16#include <locale.h> 17#include <stddef.h> 18 19/*** 20*_mbsdec - Move MBCS string pointer backward one charcter. 21* 22*Purpose: 23* Move the supplied string pointer backwards by one 24* character. MBCS characters are handled correctly. 25* 26*Entry: 27* const unsigned char *string = pointer to beginning of string 28* const unsigned char *current = current char pointer (legal MBCS boundary) 29* 30*Exit: 31* Returns pointer after moving it. 32* Returns nullptr if string >= current. 33* 34*Exceptions: 35* Input parameters are validated. Refer to the validation section of the function. 36* 37*******************************************************************************/ 38 39extern "C" unsigned char * __cdecl _mbsdec_l( 40 const unsigned char *string, 41 const unsigned char *current, 42 _locale_t plocinfo 43 ) 44{ 45 const unsigned char *temp; 46 47 /* validation section */ 48 _VALIDATE_RETURN(string != nullptr, EINVAL, nullptr); 49 _VALIDATE_RETURN(current != nullptr, EINVAL, nullptr); 50 51 if (string >= current) 52 return(nullptr); 53 54 _LocaleUpdate _loc_update(plocinfo); 55 56 if (_loc_update.GetLocaleT()->mbcinfo->ismbcodepage == 0) 57 return (unsigned char *)--current; 58 59 temp = current - 1; 60 61/* There used to be an optimisation here: 62 * 63 * If (current-1) returns true from _ismbblead, it is a trail byte, because 64 * current is a known character start point, and so current-1 would have to be a 65 * legal single byte MBCS character, which a lead byte is not. Therefore, if so, 66 * return (current-2) because it must be the trailbyte's lead. 67 * 68 * if ( _ismbblead(*temp) ) 69 * return (unsigned char *)(temp - 1); 70 * 71 * But this is not a valid optimisation if you want to cope correctly with an 72 * MBCS string which is terminated by a leadbyte and a 0 byte, when you are passed 73 * an initial position pointing to the \0 at the end of the string. 74 * 75 * This optimisation is also invalid if you are passed a pointer to half-way 76 * through an MBCS pair. 77 * 78 * Neither of these are truly valid input conditions, but to ensure predictably 79 * correct behaviour in the presence of these conditions, we have removed 80 * the optimisation. 81 */ 82 83/* 84 * It is unknown whether (current - 1) is a single byte character or a 85 * trail. Now decrement temp until 86 * a) The beginning of the string is reached, or 87 * b) A non-lead byte (either single or trail) is found. 88 * The difference between (current-1) and temp is the number of non-single 89 * byte characters preceding (current-1). There are two cases for this: 90 * a) (current - temp) is odd, and 91 * b) (current - temp) is even. 92 * If odd, then there are an odd number of "lead bytes" preceding the 93 * single/trail byte (current - 1), indicating that it is a trail byte. 94 * If even, then there are an even number of "lead bytes" preceding the 95 * single/trail byte (current - 1), indicating a single byte character. 96 */ 97 while ( (string <= --temp) && (_ismbblead_l(*temp, _loc_update.GetLocaleT())) ) 98 ; 99 100 return (unsigned char *)(current - 1 - ((current - temp) & 0x01) ); 101} 102 103extern "C" unsigned char * (__cdecl _mbsdec)( 104 const unsigned char *string, 105 const unsigned char *current 106 ) 107{ 108 return _mbsdec_l(string, current, nullptr); 109}