17c478bd9Sstevel@tonic-gate/* 27c478bd9Sstevel@tonic-gate * CDDL HEADER START 37c478bd9Sstevel@tonic-gate * 47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*9a70fc3bSMark J. Nelson * Common Development and Distribution License (the "License"). 6*9a70fc3bSMark J. Nelson * You may not use this file except in compliance with the License. 77c478bd9Sstevel@tonic-gate * 87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 117c478bd9Sstevel@tonic-gate * and limitations under the License. 127c478bd9Sstevel@tonic-gate * 137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 187c478bd9Sstevel@tonic-gate * 197c478bd9Sstevel@tonic-gate * CDDL HEADER END 207c478bd9Sstevel@tonic-gate */ 217c478bd9Sstevel@tonic-gate/* 227c478bd9Sstevel@tonic-gate * Copyright 2004 Sun Microsystems, Inc. All rights reserved. 237c478bd9Sstevel@tonic-gate * Use is subject to license terms. 247c478bd9Sstevel@tonic-gate */ 257c478bd9Sstevel@tonic-gate 26*9a70fc3bSMark J. Nelson .file "strncpy.s" 277c478bd9Sstevel@tonic-gate 287c478bd9Sstevel@tonic-gate/ 297c478bd9Sstevel@tonic-gate/ strncpy(s1, s2, n) 307c478bd9Sstevel@tonic-gate/ 317c478bd9Sstevel@tonic-gate/ Copies s2 to s1, truncating or null-padding to always copy n bytes 327c478bd9Sstevel@tonic-gate/ Returns s1 337c478bd9Sstevel@tonic-gate/ 347c478bd9Sstevel@tonic-gate/ Fast assembly language version of the following C-program strncpy 357c478bd9Sstevel@tonic-gate/ which represents the `standard' for the C-library. 367c478bd9Sstevel@tonic-gate/ 377c478bd9Sstevel@tonic-gate/ char * 387c478bd9Sstevel@tonic-gate/ strncpy(char *s1, const char *s2, size_t n) 397c478bd9Sstevel@tonic-gate/ { 407c478bd9Sstevel@tonic-gate/ char *os1 = s1; 417c478bd9Sstevel@tonic-gate/ 427c478bd9Sstevel@tonic-gate/ n++; 437c478bd9Sstevel@tonic-gate/ while ((--n != 0) && ((*s1++ = *s2++) != '\0')) 447c478bd9Sstevel@tonic-gate/ ; 457c478bd9Sstevel@tonic-gate/ if (n != 0) 467c478bd9Sstevel@tonic-gate/ while (--n != 0) 477c478bd9Sstevel@tonic-gate/ *s1++ = '\0'; 487c478bd9Sstevel@tonic-gate/ return (os1); 497c478bd9Sstevel@tonic-gate/ } 507c478bd9Sstevel@tonic-gate/ 517c478bd9Sstevel@tonic-gate/ In this assembly language version, the following expression is used 527c478bd9Sstevel@tonic-gate/ to check if a 32-bit word data contains a null byte or not: 537c478bd9Sstevel@tonic-gate/ (((A & 0x7f7f7f7f) + 0x7f7f7f7f) | A) & 0x80808080 547c478bd9Sstevel@tonic-gate/ If the above expression geneates a value other than 0x80808080, 557c478bd9Sstevel@tonic-gate/ that means the 32-bit word data contains a null byte. 567c478bd9Sstevel@tonic-gate/ 577c478bd9Sstevel@tonic-gate 587c478bd9Sstevel@tonic-gate/ Assume relatively long strings and small numbers of nulls at end. 597c478bd9Sstevel@tonic-gate 607c478bd9Sstevel@tonic-gate#include "SYS.h" 617c478bd9Sstevel@tonic-gate 627c478bd9Sstevel@tonic-gate ENTRY(strncpy) 637c478bd9Sstevel@tonic-gate pushl %edi / save register variables 647c478bd9Sstevel@tonic-gate pushl %esi 657c478bd9Sstevel@tonic-gate 667c478bd9Sstevel@tonic-gate movl 16(%esp), %eax / %eax = source string address 677c478bd9Sstevel@tonic-gate movl 12(%esp), %edi / %edi = destination string address 687c478bd9Sstevel@tonic-gate movl 20(%esp), %esi / %esi = number of bytes 697c478bd9Sstevel@tonic-gate 707c478bd9Sstevel@tonic-gate testl $3, %eax / if %eax not word aligned 717c478bd9Sstevel@tonic-gate jnz .L1 / goto .L1 727c478bd9Sstevel@tonic-gate.L8: 737c478bd9Sstevel@tonic-gate cmpl $4, %esi / if number of bytes < 4 747c478bd9Sstevel@tonic-gate jb .L4 / goto .L4 757c478bd9Sstevel@tonic-gate .align 4 767c478bd9Sstevel@tonic-gate.L2: 777c478bd9Sstevel@tonic-gate movl (%eax), %edx / move 1 word from (%eax) to %edx 787c478bd9Sstevel@tonic-gate movl $0x7f7f7f7f, %ecx 797c478bd9Sstevel@tonic-gate andl %edx, %ecx / %ecx = %edx & 0x7f7f7f7f 807c478bd9Sstevel@tonic-gate addl $4, %eax / next word 817c478bd9Sstevel@tonic-gate addl $0x7f7f7f7f, %ecx / %ecx += 0x7f7f7f7f 827c478bd9Sstevel@tonic-gate orl %edx, %ecx / %ecx |= %edx 837c478bd9Sstevel@tonic-gate andl $0x80808080, %ecx / %ecx &= 0x80808080 847c478bd9Sstevel@tonic-gate cmpl $0x80808080, %ecx / if null byte in this word 857c478bd9Sstevel@tonic-gate jne .L3 / goto .L3 867c478bd9Sstevel@tonic-gate movl %edx, (%edi) / copy this word to (%edi) 877c478bd9Sstevel@tonic-gate subl $4, %esi / decrement number of bytes by 4 887c478bd9Sstevel@tonic-gate addl $4, %edi / next word 897c478bd9Sstevel@tonic-gate cmpl $4, %esi / if number of bytes >= 4 907c478bd9Sstevel@tonic-gate jae .L2 / goto .L2 917c478bd9Sstevel@tonic-gate jmp .L4 / goto .L4 927c478bd9Sstevel@tonic-gate.L3: 937c478bd9Sstevel@tonic-gate subl $4, %eax / post-incremented 947c478bd9Sstevel@tonic-gate .align 4 957c478bd9Sstevel@tonic-gate.L4: 967c478bd9Sstevel@tonic-gate / (number of bytes < 4) or (a null byte found in the word) 977c478bd9Sstevel@tonic-gate cmpl $0, %esi / if number of bytes == 0 987c478bd9Sstevel@tonic-gate je .L7 / goto .L7 (finished) 997c478bd9Sstevel@tonic-gate movb (%eax), %dl / %dl = a byte in (%eax) 1007c478bd9Sstevel@tonic-gate decl %esi / decrement number of bytes by 1 1017c478bd9Sstevel@tonic-gate movb %dl, (%edi) / copy %dl to (%edi) 1027c478bd9Sstevel@tonic-gate incl %eax / next byte 1037c478bd9Sstevel@tonic-gate incl %edi / next byte 1047c478bd9Sstevel@tonic-gate cmpb $0, %dl / compare %dl with a null byte 1057c478bd9Sstevel@tonic-gate je .L5 / if %dl is a null, goto .L5 1067c478bd9Sstevel@tonic-gate jmp .L4 / goto .L4 1077c478bd9Sstevel@tonic-gate .align 4 1087c478bd9Sstevel@tonic-gate.L1: 1097c478bd9Sstevel@tonic-gate / %eax not aligned 1107c478bd9Sstevel@tonic-gate cmpl $0, %esi / if number of bytes == 0 1117c478bd9Sstevel@tonic-gate je .L7 / goto .L7 (finished) 1127c478bd9Sstevel@tonic-gate movb (%eax), %dl / %dl = a byte in (%eax) 1137c478bd9Sstevel@tonic-gate decl %esi / decrement number of bytes by 1 1147c478bd9Sstevel@tonic-gate movb %dl, (%edi) / copy %dl to (%edi) 1157c478bd9Sstevel@tonic-gate incl %edi / next byte 1167c478bd9Sstevel@tonic-gate incl %eax / next byte 1177c478bd9Sstevel@tonic-gate cmpb $0, %dl / compare %dl with a null byte 1187c478bd9Sstevel@tonic-gate je .L5 / if %dl is a null, goto .L5 1197c478bd9Sstevel@tonic-gate testl $3, %eax / if %eax word aligned 1207c478bd9Sstevel@tonic-gate jz .L8 / goto .L8 1217c478bd9Sstevel@tonic-gate jmp .L1 / goto .L1 (not word aligned) 1227c478bd9Sstevel@tonic-gate .align 4 1237c478bd9Sstevel@tonic-gate.L5: 1247c478bd9Sstevel@tonic-gate movl %esi, %ecx / %ecx = length to copy null bytes 1257c478bd9Sstevel@tonic-gate xorl %eax, %eax / clear %eax 1267c478bd9Sstevel@tonic-gate shrl $2, %ecx / %ecx = words to copy null bytes 1277c478bd9Sstevel@tonic-gate rep ; sstol / rep;sstol is optimal 1287c478bd9Sstevel@tonic-gate andl $3, %esi / %esi = leftover bytes 1297c478bd9Sstevel@tonic-gate.L6: 1307c478bd9Sstevel@tonic-gate cmpl $0, %esi / if number of bytes == 0 1317c478bd9Sstevel@tonic-gate jz .L7 / goto .L7 (finished) 1327c478bd9Sstevel@tonic-gate movb $0, (%edi) / move a null byte to (%edi) 1337c478bd9Sstevel@tonic-gate decl %esi / decrement number of bytes by 1 1347c478bd9Sstevel@tonic-gate incl %edi / next byte 1357c478bd9Sstevel@tonic-gate jmp .L6 / goto .L6 1367c478bd9Sstevel@tonic-gate .align 4 1377c478bd9Sstevel@tonic-gate.L7: 1387c478bd9Sstevel@tonic-gate movl 12(%esp), %eax / return the destination address 1397c478bd9Sstevel@tonic-gate popl %esi / restore register variables 1407c478bd9Sstevel@tonic-gate popl %edi 1417c478bd9Sstevel@tonic-gate ret 1427c478bd9Sstevel@tonic-gate SET_SIZE(strncpy) 143