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.

127 lines
3.4 KiB

  1. /***
  2. *mbsnbcat.c - concatenate string2 onto string1, max length n bytes
  3. *
  4. * Copyright (c) 1985-2001, Microsoft Corporation. All rights reserved.
  5. *
  6. *Purpose:
  7. * defines mbsnbcat() - concatenate maximum of n bytes
  8. *
  9. *Revision History:
  10. * 08-03-93 KRS Ported from 16-bit sources.
  11. * 08-20-93 CFW Update _MBCS_OS support.
  12. * 09-24-93 CFW Merge _MBCS_OS and _KANJI.
  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-19-94 CFW Enable non-Win32.
  17. * 09-11-97 GJF Replaced __mbcodepage == 0 with _ISNOTMBCP.
  18. * 04-07-98 GJF Revised multithread support based on threadmbcinfo
  19. * structs
  20. *
  21. *******************************************************************************/
  22. #ifdef _MBCS
  23. #include <mtdll.h>
  24. #include <cruntime.h>
  25. #include <string.h>
  26. #include <mbdata.h>
  27. #include <mbctype.h>
  28. #include <mbstring.h>
  29. /***
  30. * _mbsnbcat - concatenate max cnt bytes onto dst
  31. *
  32. *Purpose:
  33. * Concatenates src onto dst, with a maximum of cnt bytes copied.
  34. * Handles 2-byte MBCS characters correctly.
  35. *
  36. *Entry:
  37. * unsigned char *dst - string to concatenate onto
  38. * unsigned char *src - string to concatenate from
  39. * int cnt - number of bytes to copy
  40. *
  41. *Exit:
  42. * returns dst, with src (at least part) concatenated on
  43. *
  44. *Exceptions:
  45. *
  46. *******************************************************************************/
  47. unsigned char * __cdecl _mbsnbcat(
  48. unsigned char *dst,
  49. const unsigned char *src,
  50. size_t cnt
  51. )
  52. {
  53. unsigned char *start;
  54. #ifdef _MT
  55. pthreadmbcinfo ptmbci = _getptd()->ptmbcinfo;
  56. #endif
  57. if (!cnt)
  58. return(dst);
  59. #ifdef _MT
  60. if ( ptmbci != __ptmbcinfo )
  61. ptmbci = __updatetmbcinfo();
  62. if ( _ISNOTMBCP_MT(ptmbci) )
  63. #else
  64. if ( _ISNOTMBCP )
  65. #endif
  66. return strncat(dst, src, cnt);
  67. start = dst;
  68. while (*dst++)
  69. ;
  70. --dst; // dst now points to end of dst string
  71. /* if last char in string is a lead byte, back up pointer */
  72. #ifdef _MT
  73. if ( __mbsbtype_mt(ptmbci, start, (int) ((dst - start) - 1)) == _MBC_LEAD )
  74. #else
  75. if ( _mbsbtype(start, (int) ((dst - start) - 1)) == _MBC_LEAD )
  76. #endif
  77. --dst;
  78. /* copy over the characters */
  79. while (cnt--) {
  80. #ifdef _MT
  81. if ( __ismbblead_mt(ptmbci, *src) ) {
  82. #else
  83. if ( _ismbblead(*src) ) {
  84. #endif
  85. *dst++ = *src++;
  86. if (!cnt--) { /* write nul if cnt exhausted */
  87. dst[-1] = '\0';
  88. break;
  89. }
  90. if ((*dst++ = *src++)=='\0') { /* or if no trail byte */
  91. dst[-2] = '\0';
  92. break;
  93. }
  94. }
  95. else if ((*dst++ = *src++) == '\0')
  96. break;
  97. }
  98. /* enter final nul, if necessary */
  99. #ifdef _MT
  100. if ( __mbsbtype_mt(ptmbci, start, (int) ((dst - start) - 1)) == _MBC_LEAD )
  101. #else
  102. if ( _mbsbtype(start, (int) ((dst - start) - 1)) == _MBC_LEAD )
  103. #endif
  104. dst[-1] = '\0';
  105. else
  106. *dst = '\0';
  107. return(start);
  108. }
  109. #endif /* _MBCS */