Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

114 lines
3.5 KiB

  1. /***
  2. *mbsdec.c - Move MBCS string pointer backward one charcter.
  3. *
  4. * Copyright (c) 1985-2001, Microsoft Corporation. All rights reserved.
  5. *
  6. *Purpose:
  7. * Move MBCS string pointer backward one character.
  8. *
  9. *Revision History:
  10. * 11-19-92 KRS Ported from 16-bit sources.
  11. * 08-03-93 KRS Fix prototypes.
  12. * 08-20-93 CFW Remove test for NULL string, use new function parameters.
  13. * 10-05-93 GJF Replaced _CRTAPI1 with __cdecl.
  14. * 04-15-93 CFW Add _MB_CP_LOCK.
  15. * 05-09-94 CFW Optimize for SBCS.
  16. * 05-13-94 CFW Fix SBCS optimization.
  17. * 05-19-94 CFW Enable non-Win32.
  18. * 09-11-97 GJF Replaced __mbcodepage == 0 with _ISNOTMBCP.
  19. * 04-07-98 GJF Revised multithread support based on threadmbcinfo
  20. * structs
  21. *
  22. *******************************************************************************/
  23. #ifdef _MBCS
  24. #include <mtdll.h>
  25. #include <cruntime.h>
  26. #include <mbdata.h>
  27. #include <mbstring.h>
  28. #include <mbctype.h>
  29. #include <stddef.h>
  30. /***
  31. *_mbsdec - Move MBCS string pointer backward one charcter.
  32. *
  33. *Purpose:
  34. * Move the supplied string pointer backwards by one
  35. * character. MBCS characters are handled correctly.
  36. *
  37. *Entry:
  38. * const unsigned char *string = pointer to beginning of string
  39. * const unsigned char *current = current char pointer (legal MBCS boundary)
  40. *
  41. *Exit:
  42. * Returns pointer after moving it.
  43. * Returns NULL if string >= current.
  44. *
  45. *Exceptions:
  46. *
  47. *******************************************************************************/
  48. unsigned char * __cdecl _mbsdec(
  49. const unsigned char *string,
  50. const unsigned char *current
  51. )
  52. {
  53. const unsigned char *temp;
  54. #ifdef _MT
  55. pthreadmbcinfo ptmbci = _getptd()->ptmbcinfo;
  56. if ( ptmbci != __ptmbcinfo )
  57. ptmbci = __updatetmbcinfo();
  58. #endif
  59. if (string >= current)
  60. return(NULL);
  61. #ifdef _MT
  62. if ( _ISNOTMBCP_MT(ptmbci) )
  63. #else
  64. if ( _ISNOTMBCP )
  65. #endif
  66. return (unsigned char *)--current;
  67. temp = current - 1;
  68. /*
  69. * If (current-1) returns true from _ISLEADBTYE, it is a trail byte, because
  70. * it is not a legal single byte MBCS character. Therefore, is so, return
  71. * (current-2) because it is the trailbyte's lead.
  72. */
  73. #ifdef _MT
  74. if ( __ismbblead_mt(ptmbci, *temp) )
  75. #else
  76. if ( _ismbblead(*temp) )
  77. #endif
  78. return (unsigned char *)(temp - 1);
  79. /*
  80. * It is unknown whether (current - 1) is a single byte character or a
  81. * trail. Now decrement temp until
  82. * a) The beginning of the string is reached, or
  83. * b) A non-lead byte (either single or trail) is found.
  84. * The difference between (current-1) and temp is the number of non-single
  85. * byte characters preceding (current-1). There are two cases for this:
  86. * a) (current - temp) is odd, and
  87. * b) (current - temp) is even.
  88. * If odd, then there are an odd number of "lead bytes" preceding the
  89. * single/trail byte (current - 1), indicating that it is a trail byte.
  90. * If even, then there are an even number of "lead bytes" preceding the
  91. * single/trail byte (current - 1), indicating a single byte character.
  92. */
  93. #ifdef _MT
  94. while ( (string <= --temp) && (__ismbblead_mt(ptmbci, *temp)) )
  95. #else
  96. while ( (string <= --temp) && (_ismbblead(*temp)) )
  97. #endif
  98. ;
  99. return (unsigned char *)(current - 1 - ((current - temp) & 0x01) );
  100. }
  101. #endif /* _MBCS */