1*0Sstevel@tonic-gate/* 2*0Sstevel@tonic-gate * CDDL HEADER START 3*0Sstevel@tonic-gate * 4*0Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*0Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 6*0Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 7*0Sstevel@tonic-gate * with the License. 8*0Sstevel@tonic-gate * 9*0Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10*0Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 11*0Sstevel@tonic-gate * See the License for the specific language governing permissions 12*0Sstevel@tonic-gate * and limitations under the License. 13*0Sstevel@tonic-gate * 14*0Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 15*0Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16*0Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 17*0Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 18*0Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 19*0Sstevel@tonic-gate * 20*0Sstevel@tonic-gate * CDDL HEADER END 21*0Sstevel@tonic-gate */ 22*0Sstevel@tonic-gate/* 23*0Sstevel@tonic-gate * Copyright 2003 Sun Microsystems, Inc. All rights reserved. 24*0Sstevel@tonic-gate * Use is subject to license terms. 25*0Sstevel@tonic-gate */ 26*0Sstevel@tonic-gate 27*0Sstevel@tonic-gate 28*0Sstevel@tonic-gate.ident "%Z%%M% %I% %E% SMI" 29*0Sstevel@tonic-gate 30*0Sstevel@tonic-gate .file "%M%" 31*0Sstevel@tonic-gate 32*0Sstevel@tonic-gate/* 33*0Sstevel@tonic-gate * strncpy(s1, s2) 34*0Sstevel@tonic-gate * 35*0Sstevel@tonic-gate * Copy string s2 to s1, truncating or null-padding to always copy n bytes 36*0Sstevel@tonic-gate * return s1. 37*0Sstevel@tonic-gate * 38*0Sstevel@tonic-gate * Fast assembler language version of the following C-program for strncpy 39*0Sstevel@tonic-gate * which represents the `standard' for the C-library. 40*0Sstevel@tonic-gate * 41*0Sstevel@tonic-gate * char * 42*0Sstevel@tonic-gate * strncpy(char *s1, const char *s2, size_t n) 43*0Sstevel@tonic-gate * { 44*0Sstevel@tonic-gate * char *os1 = s1; 45*0Sstevel@tonic-gate * 46*0Sstevel@tonic-gate * n++; 47*0Sstevel@tonic-gate * while ((--n != 0) && ((*s1++ = *s2++) != '\0')) 48*0Sstevel@tonic-gate * ; 49*0Sstevel@tonic-gate * if (n != 0) 50*0Sstevel@tonic-gate * while (--n != 0) 51*0Sstevel@tonic-gate * *s1++ = '\0'; 52*0Sstevel@tonic-gate * return (os1); 53*0Sstevel@tonic-gate * } 54*0Sstevel@tonic-gate */ 55*0Sstevel@tonic-gate 56*0Sstevel@tonic-gate#include <sys/asm_linkage.h> 57*0Sstevel@tonic-gate#include "synonyms.h" 58*0Sstevel@tonic-gate 59*0Sstevel@tonic-gate ! strncpy works similarly to strcpy, except that n bytes of s2 60*0Sstevel@tonic-gate ! are copied to s1. If a null character is reached in s2 yet more 61*0Sstevel@tonic-gate ! bytes remain to be copied, strncpy will copy null bytes into 62*0Sstevel@tonic-gate ! the destination string. 63*0Sstevel@tonic-gate ! 64*0Sstevel@tonic-gate ! This implementation works by first aligning the src ptr and 65*0Sstevel@tonic-gate ! performing small copies until it is aligned. Then, the string 66*0Sstevel@tonic-gate ! is copied based upon destination alignment. (byte, half-word, 67*0Sstevel@tonic-gate ! word, etc.) 68*0Sstevel@tonic-gate 69*0Sstevel@tonic-gate ENTRY(strncpy) 70*0Sstevel@tonic-gate 71*0Sstevel@tonic-gate .align 32 72*0Sstevel@tonic-gate subcc %g0, %o2, %o4 ! n = -n 73*0Sstevel@tonic-gate bz .doneshort ! if n == 0, done 74*0Sstevel@tonic-gate cmp %o2, 7 ! n < 7 ? 75*0Sstevel@tonic-gate add %o1, %o2, %o3 ! src = src + n 76*0Sstevel@tonic-gate blu .shortcpy ! n < 7, use byte-wise copy 77*0Sstevel@tonic-gate add %o0, %o2, %o2 ! dst = dst + n 78*0Sstevel@tonic-gate andcc %o1, 3, %o5 ! src word aligned ? 79*0Sstevel@tonic-gate bz .wordaligned ! yup 80*0Sstevel@tonic-gate save %sp, -0x40, %sp ! create new register window 81*0Sstevel@tonic-gate sub %i5, 4, %i5 ! bytes until src aligned 82*0Sstevel@tonic-gate nop ! align loop on 16-byte boundary 83*0Sstevel@tonic-gate nop ! align loop on 16-byte boundary 84*0Sstevel@tonic-gate 85*0Sstevel@tonic-gate.alignsrc: 86*0Sstevel@tonic-gate ldub [%i3 + %i4], %i1 ! src[] 87*0Sstevel@tonic-gate stb %i1, [%i2 + %i4] ! dst[] = src[] 88*0Sstevel@tonic-gate inccc %i4 ! src++, dst++, n-- 89*0Sstevel@tonic-gate bz .done ! n == 0, done 90*0Sstevel@tonic-gate tst %i1 ! end of src reached (null byte) ? 91*0Sstevel@tonic-gate bz,a .bytepad ! yes, at least one byte to pad here 92*0Sstevel@tonic-gate add %i2, %i4, %l0 ! need single dest pointer for fill 93*0Sstevel@tonic-gate inccc %i5 ! src aligned now? 94*0Sstevel@tonic-gate bnz .alignsrc ! no, copy another byte 95*0Sstevel@tonic-gate .empty 96*0Sstevel@tonic-gate 97*0Sstevel@tonic-gate.wordaligned: 98*0Sstevel@tonic-gate add %i2, %i4, %l0 ! dst 99*0Sstevel@tonic-gate sethi %hi(0x01010101), %l1 ! Alan Mycroft's magic1 100*0Sstevel@tonic-gate sub %i2, 4, %i2 ! adjust for dest pre-incr in cpy loops 101*0Sstevel@tonic-gate or %l1, %lo(0x01010101),%l1! finish loading magic1 102*0Sstevel@tonic-gate andcc %l0, 3, %g1 ! destination word aligned ? 103*0Sstevel@tonic-gate bnz .dstnotaligned ! nope 104*0Sstevel@tonic-gate sll %l1, 7, %i5 ! create Alan Mycroft's magic2 105*0Sstevel@tonic-gate 106*0Sstevel@tonic-gate.storeword: 107*0Sstevel@tonic-gate lduw [%i3 + %i4], %i1 ! src dword 108*0Sstevel@tonic-gate addcc %i4, 4, %i4 ! n += 4, src += 4, dst += 4 109*0Sstevel@tonic-gate bcs .lastword ! if counter wraps, last word 110*0Sstevel@tonic-gate andn %i5, %i1, %g1 ! ~dword & 0x80808080 111*0Sstevel@tonic-gate sub %i1, %l1, %l0 ! dword - 0x01010101 112*0Sstevel@tonic-gate andcc %l0, %g1, %g0 ! ((dword - 0x01010101) & ~dword & 0x80808080) 113*0Sstevel@tonic-gate bz,a .storeword ! no zero byte if magic expression == 0 114*0Sstevel@tonic-gate stw %i1, [%i2 + %i4] ! store word to dst (address pre-incremented) 115*0Sstevel@tonic-gate 116*0Sstevel@tonic-gate ! n has not expired, but src is at the end. we need to push out the 117*0Sstevel@tonic-gate ! remaining src bytes and then start padding with null bytes 118*0Sstevel@tonic-gate 119*0Sstevel@tonic-gate.zerobyte: 120*0Sstevel@tonic-gate add %i2, %i4, %l0 ! pointer to dest string 121*0Sstevel@tonic-gate srl %i1, 24, %g1 ! first byte 122*0Sstevel@tonic-gate stb %g1, [%l0] ! store it 123*0Sstevel@tonic-gate sub %g1, 1, %g1 ! byte == 0 ? -1 : byte - 1 124*0Sstevel@tonic-gate sra %g1, 31, %g1 ! byte == 0 ? -1 : 0 125*0Sstevel@tonic-gate andn %i1, %g1, %i1 ! if byte == 0, start padding with null bytes 126*0Sstevel@tonic-gate srl %i1, 16, %g1 ! second byte 127*0Sstevel@tonic-gate stb %g1, [%l0 + 1] ! store it 128*0Sstevel@tonic-gate and %g1, 0xff, %g1 ! isolate byte 129*0Sstevel@tonic-gate sub %g1, 1, %g1 ! byte == 0 ? -1 : byte - 1 130*0Sstevel@tonic-gate sra %g1, 31, %g1 ! byte == 0 ? -1 : 0 131*0Sstevel@tonic-gate andn %i1, %g1, %i1 ! if byte == 0, start padding with null bytes 132*0Sstevel@tonic-gate srl %i1, 8, %g1 ! third byte 133*0Sstevel@tonic-gate stb %g1, [%l0 + 2] ! store it 134*0Sstevel@tonic-gate and %g1, 0xff, %g1 ! isolate byte 135*0Sstevel@tonic-gate sub %g1, 1, %g1 ! byte == 0 ? -1 : byte - 1 136*0Sstevel@tonic-gate sra %g1, 31, %g1 ! byte == 0 ? -1 : 0 137*0Sstevel@tonic-gate andn %i1, %g1, %i1 ! if byte == 0, start padding with null bytes 138*0Sstevel@tonic-gate stb %i1, [%l0 + 3] ! store fourth byte 139*0Sstevel@tonic-gate addcc %i4, 8, %g0 ! number of pad bytes < 8 ? 140*0Sstevel@tonic-gate bcs .bytepad ! yes, do simple byte wise fill 141*0Sstevel@tonic-gate add %l0, 4, %l0 ! dst += 4 142*0Sstevel@tonic-gate andcc %l0, 3, %l1 ! dst offset relative to word boundary 143*0Sstevel@tonic-gate bz .fillaligned ! dst already word aligned 144*0Sstevel@tonic-gate 145*0Sstevel@tonic-gate ! here there is a least one more byte to zero out: otherwise we would 146*0Sstevel@tonic-gate ! have exited through label .lastword 147*0Sstevel@tonic-gate 148*0Sstevel@tonic-gate sub %l1, 4, %l1 ! bytes to align dst to word boundary 149*0Sstevel@tonic-gate.makealigned: 150*0Sstevel@tonic-gate stb %g0, [%l0] ! dst[] = 0 151*0Sstevel@tonic-gate addcc %i4, 1, %i4 ! n-- 152*0Sstevel@tonic-gate bz .done ! n == 0, we are done 153*0Sstevel@tonic-gate addcc %l1, 1, %l1 ! any more byte needed to align 154*0Sstevel@tonic-gate bnz .makealigned ! yup, pad another byte 155*0Sstevel@tonic-gate add %l0, 1, %l0 ! dst++ 156*0Sstevel@tonic-gate nop ! pad to align copy loop below 157*0Sstevel@tonic-gate 158*0Sstevel@tonic-gate ! here we know that there at least another 4 bytes to pad, since 159*0Sstevel@tonic-gate ! we don't get here unless there were >= 8 bytes to pad to begin 160*0Sstevel@tonic-gate ! with, and we have padded at most 3 bytes suring dst aligning 161*0Sstevel@tonic-gate 162*0Sstevel@tonic-gate.fillaligned: 163*0Sstevel@tonic-gate add %i4, 3, %i2 ! round up to next word boundary 164*0Sstevel@tonic-gate and %i2, -4, %l1 ! pointer to next word boundary 165*0Sstevel@tonic-gate and %i2, 4, %i2 ! word count odd ? 4 : 0 166*0Sstevel@tonic-gate stw %g0, [%l0] ! store first word 167*0Sstevel@tonic-gate addcc %l1, %i2, %l1 ! dword count == 1 ? 168*0Sstevel@tonic-gate add %i4, %i2, %i4 ! if word count odd, n -= 4 169*0Sstevel@tonic-gate bz .bytepad ! if word count == 1, pad bytes left 170*0Sstevel@tonic-gate add %l0, %i2, %l0 ! bump dst if word count odd 171*0Sstevel@tonic-gate 172*0Sstevel@tonic-gate.fillword: 173*0Sstevel@tonic-gate addcc %l1, 8, %l1 ! count -= 8 174*0Sstevel@tonic-gate stw %g0, [%l0] ! dst[n] = 0 175*0Sstevel@tonic-gate stw %g0, [%l0 + 4] ! dst[n+4] = 0 176*0Sstevel@tonic-gate add %l0, 8, %l0 ! dst += 8 177*0Sstevel@tonic-gate bcc .fillword ! fill words until count == 0 178*0Sstevel@tonic-gate addcc %i4, 8, %i4 ! n -= 8 179*0Sstevel@tonic-gate bz .done ! if n == 0, we are done 180*0Sstevel@tonic-gate .empty 181*0Sstevel@tonic-gate 182*0Sstevel@tonic-gate.bytepad: 183*0Sstevel@tonic-gate and %i4, 1, %i2 ! byte count odd ? 1 : 0 184*0Sstevel@tonic-gate stb %g0, [%l0] ! store first byte 185*0Sstevel@tonic-gate addcc %i4, %i2, %i4 ! byte count == 1 ? 186*0Sstevel@tonic-gate bz .done ! yup, we are done 187*0Sstevel@tonic-gate add %l0, %i2, %l0 ! bump pointer if odd 188*0Sstevel@tonic-gate 189*0Sstevel@tonic-gate.fillbyte: 190*0Sstevel@tonic-gate addcc %i4, 2, %i4 ! n -= 2 191*0Sstevel@tonic-gate stb %g0, [%l0] ! dst[n] = 0 192*0Sstevel@tonic-gate stb %g0, [%l0 + 1] ! dst[n+1] = 0 193*0Sstevel@tonic-gate bnz .fillbyte ! fill until n == 0 194*0Sstevel@tonic-gate add %l0, 2, %l0 ! dst += 2 195*0Sstevel@tonic-gate 196*0Sstevel@tonic-gate.done: 197*0Sstevel@tonic-gate ret ! done 198*0Sstevel@tonic-gate restore %i0, %g0, %o0 ! restore reg window, return dst 199*0Sstevel@tonic-gate 200*0Sstevel@tonic-gate ! this is the last word. It may contain null bytes. store bytes 201*0Sstevel@tonic-gate ! until n == 0. if null byte encountered, continue 202*0Sstevel@tonic-gate 203*0Sstevel@tonic-gate.lastword: 204*0Sstevel@tonic-gate sub %i4, 4, %i4 ! undo counter pre-increment 205*0Sstevel@tonic-gate add %i2, 4, %i2 ! adjust dst for counter un-bumping 206*0Sstevel@tonic-gate 207*0Sstevel@tonic-gate srl %i1, 24, %g1 ! first byte 208*0Sstevel@tonic-gate stb %g1, [%i2 + %i4] ! store it 209*0Sstevel@tonic-gate inccc %i4 ! n-- 210*0Sstevel@tonic-gate bz .done ! if n == 0, we're done 211*0Sstevel@tonic-gate sub %g1, 1, %g1 ! byte == 0 ? -1 : byte - 1 212*0Sstevel@tonic-gate sra %g1, 31, %g1 ! byte == 0 ? -1 : 0 213*0Sstevel@tonic-gate andn %i1, %g1, %i1 ! if byte == 0, start padding with null 214*0Sstevel@tonic-gate srl %i1, 16, %g1 ! second byte 215*0Sstevel@tonic-gate stb %g1, [%i2 + %i4] ! store it 216*0Sstevel@tonic-gate inccc %i4 ! n-- 217*0Sstevel@tonic-gate bz .done ! if n == 0, we're done 218*0Sstevel@tonic-gate and %g1, 0xff, %g1 ! isolate byte 219*0Sstevel@tonic-gate sub %g1, 1, %g1 ! byte == 0 ? -1 : byte - 1 220*0Sstevel@tonic-gate sra %g1, 31, %g1 ! byte == 0 ? -1 : 0 221*0Sstevel@tonic-gate andn %i1, %g1, %i1 ! if byte == 0, start padding with null 222*0Sstevel@tonic-gate srl %i1, 8, %g1 ! third byte 223*0Sstevel@tonic-gate stb %g1, [%i2 + %i4] ! store it 224*0Sstevel@tonic-gate inccc %i4 ! n-- 225*0Sstevel@tonic-gate bz .done ! if n == 0, we're done 226*0Sstevel@tonic-gate and %g1, 0xff, %g1 ! isolate byte 227*0Sstevel@tonic-gate sub %g1, 1, %g1 ! byte == 0 ? -1 : byte - 1 228*0Sstevel@tonic-gate sra %g1, 31, %g1 ! byte == 0 ? -1 : 0 229*0Sstevel@tonic-gate andn %i1, %g1, %i1 ! if byte == 0, start padding with null 230*0Sstevel@tonic-gate ba .done ! here n must be zero, we are done 231*0Sstevel@tonic-gate stb %i1, [%i2 + %i4] ! store fourth byte 232*0Sstevel@tonic-gate 233*0Sstevel@tonic-gate.dstnotaligned: 234*0Sstevel@tonic-gate cmp %g1, 2 ! dst half word aligned? 235*0Sstevel@tonic-gate be .storehalfword2 ! yup, store half word at a time 236*0Sstevel@tonic-gate .empty 237*0Sstevel@tonic-gate.storebyte: 238*0Sstevel@tonic-gate lduw [%i3 + %i4], %i1 ! x = src[] 239*0Sstevel@tonic-gate addcc %i4, 4, %i4 ! src += 4, dst += 4, n -= 4 240*0Sstevel@tonic-gate bcs .lastword ! if counter wraps, last word 241*0Sstevel@tonic-gate andn %i5, %i1, %g1 ! ~x & 0x80808080 242*0Sstevel@tonic-gate sub %i1, %l1, %l0 ! x - 0x01010101 243*0Sstevel@tonic-gate andcc %l0, %g1, %g0 ! ((x - 0x01010101) & ~x & 0x80808080) 244*0Sstevel@tonic-gate bnz .zerobyte ! end of src found, may need to pad 245*0Sstevel@tonic-gate add %i2, %i4, %l0 ! dst (in pointer form) 246*0Sstevel@tonic-gate srl %i1, 24, %g1 ! %g1<7:0> = 1st byte; half-word aligned now 247*0Sstevel@tonic-gate stb %g1, [%l0] ! store first byte 248*0Sstevel@tonic-gate srl %i1, 8, %g1 ! %g1<15:0> = bytes 2, 3 249*0Sstevel@tonic-gate sth %g1, [%l0 + 1] ! store bytes 2, 3 250*0Sstevel@tonic-gate ba .storebyte ! next word 251*0Sstevel@tonic-gate stb %i1, [%l0 + 3] ! store fourth byte 252*0Sstevel@tonic-gate nop 253*0Sstevel@tonic-gate nop 254*0Sstevel@tonic-gate 255*0Sstevel@tonic-gate.storehalfword: 256*0Sstevel@tonic-gate lduw [%i3 + %i4], %i1 ! x = src[] 257*0Sstevel@tonic-gate.storehalfword2: 258*0Sstevel@tonic-gate addcc %i4, 4, %i4 ! src += 4, dst += 4, n -= 4 259*0Sstevel@tonic-gate bcs .lastword ! if counter wraps, last word 260*0Sstevel@tonic-gate andn %i5, %i1, %g1 ! ~x & 0x80808080 261*0Sstevel@tonic-gate sub %i1, %l1, %l0 ! x - 0x01010101 262*0Sstevel@tonic-gate andcc %l0, %g1, %g0 ! ((x -0x01010101) & ~x & 0x8080808080) 263*0Sstevel@tonic-gate bnz .zerobyte ! x has zero byte, handle end cases 264*0Sstevel@tonic-gate add %i2, %i4, %l0 ! dst (in pointer form) 265*0Sstevel@tonic-gate srl %i1, 16, %g1 ! %g1<15:0> = bytes 1, 2 266*0Sstevel@tonic-gate sth %g1, [%l0] ! store bytes 1, 2 267*0Sstevel@tonic-gate ba .storehalfword ! next dword 268*0Sstevel@tonic-gate sth %i1, [%l0 + 2] ! store bytes 3, 4 269*0Sstevel@tonic-gate 270*0Sstevel@tonic-gate.shortcpy: 271*0Sstevel@tonic-gate ldub [%o3 + %o4], %o5 ! src[] 272*0Sstevel@tonic-gate stb %o5, [%o2 + %o4] ! dst[] = src[] 273*0Sstevel@tonic-gate inccc %o4 ! src++, dst++, n-- 274*0Sstevel@tonic-gate bz .doneshort ! if n == 0, done 275*0Sstevel@tonic-gate tst %o5 ! src[] == 0 ? 276*0Sstevel@tonic-gate bnz,a .shortcpy ! nope, next byte 277*0Sstevel@tonic-gate nop ! empty delay slot 278*0Sstevel@tonic-gate 279*0Sstevel@tonic-gate.padbyte: 280*0Sstevel@tonic-gate stb %g0, [%o2 + %o4] ! dst[] = 0 281*0Sstevel@tonic-gate.padbyte2: 282*0Sstevel@tonic-gate addcc %o4, 1, %o4 ! dst++, n-- 283*0Sstevel@tonic-gate bnz,a .padbyte2 ! if n != 0, next byte 284*0Sstevel@tonic-gate stb %g0, [%o2 + %o4] ! dst[] = 0 285*0Sstevel@tonic-gate nop ! align label below to 16-byte boundary 286*0Sstevel@tonic-gate 287*0Sstevel@tonic-gate.doneshort: 288*0Sstevel@tonic-gate retl ! return from leaf 289*0Sstevel@tonic-gate nop ! empty delay slot 290*0Sstevel@tonic-gate SET_SIZE(strncpy) 291