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.

143 lines
3.6 KiB

  1. page ,132
  2. title memccpy - copy bytes until character found
  3. ;***
  4. ;memccpy.asm - copy bytes until a character is found
  5. ;
  6. ; Copyright (c) 1985-2001, Microsoft Corporation. All rights reserved.
  7. ;
  8. ;Purpose:
  9. ; defines _memccpy() - copies bytes until a specifed character
  10. ; is found, or a maximum number of characters have been copied.
  11. ;
  12. ;Revision History:
  13. ; 05-16-84 RN initial version
  14. ; 05-17-88 SJM Add model-independent (large model) ifdef
  15. ; 08-04-88 SJM convert to cruntime/ add 32-bit support
  16. ; 08-23-88 JCR Minor 386 adjustments
  17. ; 10-25-88 JCR General cleanup for 386-only code
  18. ; 03-23-90 GJF Changed to _stdcall. Also, fixed the copyright.
  19. ; 01-17-91 GJF ANSI naming.
  20. ; 05-10-91 GJF Back to _cdecl, sigh...
  21. ; 10-27-92 SKS Avoid using a MASM keyword ("C") as a parameter name
  22. ; 01-19-95 GJF Faster version courtesy of Intel Israel.
  23. ; 01-24-95 GJF Added FPO directive.
  24. ; 06-12-01 PML inc->add 1, dec->sub 1 for Pentium 4 perf (vs7#267015)
  25. ;
  26. ;*******************************************************************************
  27. .xlist
  28. include cruntime.inc
  29. .list
  30. page
  31. ;***
  32. ;char *_memccpy(dest, src, _c, count) - copy bytes until character found
  33. ;
  34. ;Purpose:
  35. ; Copies bytes from src to dest until count bytes have been
  36. ; copied, or up to and including the character _c, whichever
  37. ; comes first.
  38. ;
  39. ; Algorithm:
  40. ; char *
  41. ; _memccpy (dest, src, _c, count)
  42. ; char *dest, *src, _c;
  43. ; unsigned int count;
  44. ; {
  45. ; while (count && (*dest++ = *src++) != _c)
  46. ; count--;
  47. ;
  48. ; return(count ? dest : NULL);
  49. ; }
  50. ;
  51. ;Entry:
  52. ; char *dest - pointer to memory to receive copy
  53. ; char *src - source of bytes
  54. ; char _c - character to stop copy at
  55. ; int count - max number of bytes to copy
  56. ;
  57. ;Exit:
  58. ; returns pointer to byte immediately after _c in dest;
  59. ; returns NULL if _c was never found
  60. ;
  61. ;Uses:
  62. ;
  63. ;Exceptions:
  64. ;
  65. ;*******************************************************************************
  66. CODESEG
  67. public _memccpy
  68. _memccpy proc
  69. .FPO ( 0, 4, 0, 0, 0, 0 )
  70. mov ecx,[esp + 10h] ; ecx = max byte count
  71. push ebx ; save ebx
  72. test ecx,ecx ; if it's nothing to move
  73. jz ret_zero_len ; restore ebx, and return NULL
  74. mov bh,[esp + 10h] ; bh = byte to look for
  75. push esi ; save esi
  76. test ecx,1 ; test if counter is odd or even
  77. mov eax,[esp + 0ch] ; eax = dest , don't affect flags
  78. mov esi,[esp + 10h] ; esi = source , don't affect flags
  79. ; nop
  80. jz lupe2 ; if counter is even, do double loop
  81. ; else do one iteration, and drop into double loop
  82. mov bl,[esi] ; get first byte into bl
  83. add esi,1 ; kick src (esi points to src)
  84. mov [eax],bl ; store it in dest
  85. add eax,1 ; kick dest
  86. cmp bl,bh ; see if we just moved the byte
  87. je short toend
  88. sub ecx,1 ; decrement counter
  89. jz retnull ; drop into double loop if nonzero
  90. lupe2:
  91. mov bl,[esi] ; get first byte into bl
  92. add esi,2 ; kick esi (src)
  93. cmp bl,bh ; check if we just moved the byte (from bl)
  94. je toend_mov_inc ; store bl & exit
  95. mov [eax],bl ; store first byte from bl
  96. mov bl,[esi - 1] ; get second byte into bl
  97. mov [eax + 1],bl ; store second byte from bl
  98. add eax,2 ; kick eax (dest)
  99. cmp bl,bh ; see if we just moved the byte
  100. je short toend ; end of string
  101. sub ecx,2 ; modify counter, and if nonzero continue
  102. jnz lupe2 ; else drop out & return NULL
  103. retnull:
  104. pop esi
  105. ret_zero_len:
  106. xor eax,eax ; null pointer
  107. pop ebx
  108. ret ; _cdecl return
  109. toend_mov_inc:
  110. mov [eax],bl ; store first byte from bl
  111. add eax,1 ; eax points right after the value
  112. toend: pop esi
  113. pop ebx
  114. ret ; _cdecl return
  115. _memccpy endp
  116. end