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.

106 lines
2.7 KiB

  1. /***
  2. *mbsnbcpy.c - Copy one string to another, n bytes only (MBCS)
  3. *
  4. * Copyright (c) 1985-2001, Microsoft Corporation. All rights reserved.
  5. *
  6. *Purpose:
  7. * Copy one string to another, n bytes only (MBCS)
  8. *
  9. *Revision History:
  10. * 05-19-93 KRS Created from mbsncpy.
  11. * 08-03-93 KRS Fix logic bug.
  12. * 10-05-93 GJF Replaced _CRTAPI1 with __cdecl.
  13. * 04-15-93 CFW Add _MB_CP_LOCK.
  14. * 05-09-94 CFW Optimize for SBCS.
  15. * 05-19-94 CFW Enable non-Win32.
  16. * 09-11-97 GJF Replaced __mbcodepage == 0 with _ISNOTMBCP.
  17. * 04-13-98 GJF Revised multithread support based on threadmbcinfo
  18. * structs
  19. *
  20. *******************************************************************************/
  21. #ifdef _MBCS
  22. #include <mtdll.h>
  23. #include <cruntime.h>
  24. #include <string.h>
  25. #include <mbdata.h>
  26. #include <mbctype.h>
  27. #include <mbstring.h>
  28. /***
  29. * _mbsnbcpy - Copy one string to another, n bytes only (MBCS)
  30. *
  31. *Purpose:
  32. * Copies exactly cnt bytes from src to dst. If strlen(src) < cnt, the
  33. * remaining character are padded with null bytes. If strlen >= cnt, no
  34. * terminating null byte is added. 2-byte MBCS characters are handled
  35. * correctly.
  36. *
  37. *Entry:
  38. * unsigned char *dst = destination for copy
  39. * unsigned char *src = source for copy
  40. * int cnt = number of characters to copy
  41. *
  42. *Exit:
  43. * returns dst = destination of copy
  44. *
  45. *Exceptions:
  46. *
  47. *******************************************************************************/
  48. unsigned char * __cdecl _mbsnbcpy(
  49. unsigned char *dst,
  50. const unsigned char *src,
  51. size_t cnt
  52. )
  53. {
  54. unsigned char *start = dst;
  55. #ifdef _MT
  56. pthreadmbcinfo ptmbci = _getptd()->ptmbcinfo;
  57. if ( ptmbci != __ptmbcinfo )
  58. ptmbci = __updatetmbcinfo();
  59. if ( _ISNOTMBCP_MT(ptmbci) )
  60. #else
  61. if ( _ISNOTMBCP )
  62. #endif
  63. return strncpy(dst, src, cnt);
  64. while (cnt) {
  65. cnt--;
  66. #ifdef _MT
  67. if ( __ismbblead_mt(ptmbci, *src) ) {
  68. #else
  69. if ( _ismbblead(*src) ) {
  70. #endif
  71. *dst++ = *src++;
  72. if (!cnt) {
  73. dst[-1] = '\0';
  74. break;
  75. }
  76. cnt--;
  77. if ((*dst++ = *src++) == '\0') {
  78. dst[-2] = '\0';
  79. break;
  80. }
  81. }
  82. else
  83. if ((*dst++ = *src++) == '\0')
  84. break;
  85. }
  86. /* pad with nulls as needed */
  87. while (cnt--)
  88. *dst++ = '\0';
  89. return start;
  90. }
  91. #endif /* _MBCS */