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.

246 lines
8.1 KiB

  1. page ,132
  2. title strncpy - copy at most n characters of string
  3. ;***
  4. ;strncpy.asm - copy at most n characters of string
  5. ;
  6. ; Copyright (c) 1985-2001, Microsoft Corporation. All rights reserved.
  7. ;
  8. ;Purpose:
  9. ; defines strncpy() - copy at most n characters of string
  10. ;
  11. ;Revision History:
  12. ; 10-25-83 RN initial version
  13. ; 05-18-88 SJM Add model-independent (large model) ifdef
  14. ; 08-04-88 SJM convert to cruntime/ add 32-bit support
  15. ; 08-23-88 JCR 386 cleanup
  16. ; 10-26-88 JCR General cleanup for 386-only code
  17. ; 10-26-88 JCR Re-arrange regs to avoid push/pop ebx
  18. ; 03-23-90 GJF Changed to _stdcall. Also, fixed the copyright.
  19. ; 05-10-91 GJF Back to _cdecl, sigh...
  20. ; 12-20-96 GJF Faster version from Intel.
  21. ; 06-12-01 PML inc->add 1, dec->sub 1 for Pentium 4 perf (vs7#267015)
  22. ;
  23. ;*******************************************************************************
  24. .xlist
  25. include cruntime.inc
  26. .list
  27. page
  28. ;***
  29. ;char *strncpy(dest, source, count) - copy at most n characters
  30. ;
  31. ;Purpose:
  32. ; Copies count characters from the source string to the
  33. ; destination. If count is less than the length of source,
  34. ; NO NULL CHARACTER is put onto the end of the copied string.
  35. ; If count is greater than the length of sources, dest is padded
  36. ; with null characters to length count.
  37. ;
  38. ; Algorithm:
  39. ; char *
  40. ; strncpy (dest, source, count)
  41. ; char *dest, *source;
  42. ; unsigned count;
  43. ; {
  44. ; char *start = dest;
  45. ;
  46. ; while (count && (*dest++ = *source++))
  47. ; count--;
  48. ; if (count)
  49. ; while (--count)
  50. ; *dest++ = '\0';
  51. ; return(start);
  52. ; }
  53. ;
  54. ;Entry:
  55. ; char *dest - pointer to spot to copy source, enough space
  56. ; is assumed.
  57. ; char *source - source string for copy
  58. ; unsigned count - characters to copy
  59. ;
  60. ;Exit:
  61. ; returns dest, with the character copied there.
  62. ;
  63. ;Uses:
  64. ;
  65. ;Exceptions:
  66. ;
  67. ;*******************************************************************************
  68. CODESEG
  69. public strncpy
  70. strncpy proc
  71. .FPO ( 0, 3, 0, 0, 0, 0 )
  72. mov ecx,[esp + 0ch] ; ecx = count
  73. push edi ; preserve edi
  74. test ecx,ecx
  75. jz finish ; leave if count is zero
  76. push esi ; preserve edi
  77. push ebx ; preserve ebx
  78. mov ebx,ecx ; store count for tail loop
  79. mov esi,[esp + 14h] ; esi -> source string
  80. test esi,3 ; test if source string is aligned on 32 bits
  81. mov edi,[esp + 10h] ; edi -> dest string
  82. jnz short src_misaligned ; (almost always source is aligned)
  83. shr ecx,2 ; convert ecx to dword count
  84. jnz main_loop_entrance
  85. jmp short copy_tail_loop ; 0 < count < 4
  86. ; simple byte loop until string is aligned
  87. src_misaligned:
  88. mov al,byte ptr [esi] ; copy a byte from source to dest
  89. add esi,1
  90. mov [edi],al
  91. add edi,1
  92. sub ecx,1
  93. jz fill_tail_end1 ; if count == 0, leave
  94. test al,al ; was last copied byte zero?
  95. jz short align_dest ; if so, go align dest and pad it out
  96. ; with zeros
  97. test esi,3 ; esi already aligned ?
  98. jne short src_misaligned
  99. mov ebx,ecx ; store count for tail loop
  100. shr ecx,2
  101. jnz short main_loop_entrance
  102. tail_loop_start:
  103. and ebx,3 ; ebx = count_before_main_loop%4
  104. jz short fill_tail_end1 ; if ebx == 0 then leave without
  105. ; appending a null byte
  106. ; while ( EOS (end-of-string) not found and count > 0 ) copy bytes
  107. copy_tail_loop:
  108. mov al,byte ptr [esi] ; load byte from source
  109. add esi,1
  110. mov [edi],al ; store byte to dest
  111. add edi,1
  112. test al,al ; EOS found?
  113. je short fill_tail_zero_bytes ; '\0' was already copied
  114. sub ebx,1
  115. jnz copy_tail_loop
  116. fill_tail_end1:
  117. mov eax,[esp + 10h] ; prepare return value
  118. pop ebx
  119. pop esi
  120. pop edi
  121. ret
  122. ; EOS found. Pad with null characters to length count
  123. align_dest:
  124. test edi,3 ; dest string aligned?
  125. jz dest_align_loop_end
  126. dest_align_loop:
  127. mov [edi],al
  128. add edi,1
  129. sub ecx,1 ; count == 0?
  130. jz fill_tail_end ; if so, finished
  131. test edi,3 ; is edi aligned ?
  132. jnz dest_align_loop
  133. dest_align_loop_end:
  134. mov ebx,ecx ; ebx > 0
  135. shr ecx,2 ; convert ecx to count of dwords
  136. jnz fill_dwords_with_EOS
  137. ; pad tail bytes
  138. finish_loop: ; 0 < ebx < 4
  139. mov [edi],al
  140. add edi,1
  141. fill_tail_zero_bytes:
  142. sub ebx,1
  143. jnz finish_loop
  144. pop ebx
  145. pop esi
  146. finish:
  147. mov eax,[esp + 8] ; return in eax pointer to dest string
  148. pop edi
  149. ret
  150. ; copy (source) string to (dest). Also look for end of (source) string
  151. main_loop: ; edx contains first dword of source string
  152. mov [edi],edx ; store one more dword
  153. add edi,4 ; kick dest pointer
  154. sub ecx,1
  155. jz tail_loop_start
  156. main_loop_entrance:
  157. mov edx,7efefeffh
  158. mov eax,dword ptr [esi] ; read 4 bytes (dword)
  159. add edx,eax
  160. xor eax,-1
  161. xor eax,edx
  162. mov edx,[esi] ; it's in cache now
  163. add esi,4 ; kick dest pointer
  164. test eax,81010100h
  165. je short main_loop
  166. ; may have found zero byte in the dword
  167. test dl,dl ; is it byte 0
  168. je short byte_0
  169. test dh,dh ; is it byte 1
  170. je short byte_1
  171. test edx,00ff0000h ; is it byte 2
  172. je short byte_2
  173. test edx,0ff000000h ; is it byte 3
  174. jne short main_loop ; taken if bits 24-30 are clear and bit
  175. ; 31 is set
  176. ; a null character was found, so dest needs to be padded out with null chars
  177. ; to count length.
  178. mov [edi],edx
  179. jmp short fill_with_EOS_dwords
  180. byte_2:
  181. and edx,0ffffh ; fill high 2 bytes with 0
  182. mov [edi],edx
  183. jmp short fill_with_EOS_dwords
  184. byte_1:
  185. and edx,0ffh ; fill high 3 bytes with 0
  186. mov [edi],edx
  187. jmp short fill_with_EOS_dwords
  188. byte_0:
  189. xor edx,edx ; fill whole dword with 0
  190. mov [edi],edx
  191. ; End of string was found. Pad out dest string with dwords of 0
  192. fill_with_EOS_dwords: ; ecx > 0 (ecx is dword counter)
  193. add edi,4
  194. xor eax,eax ; it is instead of ???????????????????
  195. sub ecx,1
  196. jz fill_tail ; we filled all dwords
  197. fill_dwords_with_EOS:
  198. xor eax,eax
  199. fill_with_EOS_loop:
  200. mov [edi],eax
  201. add edi,4
  202. sub ecx,1
  203. jnz short fill_with_EOS_loop
  204. fill_tail: ; let's pad tail bytes with zero
  205. and ebx,3 ; ebx = ebx % 4
  206. jnz finish_loop ; taken, when there are some tail bytes
  207. fill_tail_end:
  208. mov eax,[esp + 10h]
  209. pop ebx
  210. pop esi
  211. pop edi
  212. ret
  213. strncpy endp
  214. end