dietlibc中的strcpy算法浅析
2011-04-03 13:02:38

#include "stdafx.h"
#include <stdio.h>
/* fast strcpy -- Copyright (C) 2003 Thomas M. Ogrisegg <tom@hi-tek.fnord.at> */
//#include <string.h>
//#include "dietfeatures.h"
//#include "dietstring.h"
// ----following are dietstring.h content.
//#include <endian.h>
//# define MKW(x) (x|x<<8|x<<16|x<<24)
int MKW(int x)
{
x = x | x<<8 | x << 16 | x << 24;
return x;
}

//# define STRALIGN(x) (((unsigned long)x&3)?4-((unsigned long)x&3):0)
unsigned long STRALIGN(unsigned long xPtr)
{
unsigned long xRet = (unsigned long)xPtr & 3;
if (xRet)
xRet = 4 - ((unsigned long) xPtr & 3);
else
xRet = 0;
return xRet;
}

/* GFC(x)    - returns first character */
/* INCSTR(x) - moves to next character */
# define GFC(x) ((x)&0xff)
# define INCSTR(x) do { x >>= 8; } while (0)

//#define UNALIGNED(x,y) (((unsigned long)x & (sizeof (unsigned long)-1)) ^ ((unsigned long)y & (sizeof (unsigned long)-1)))
unsigned long MyUnaligned(unsigned long xPtr, unsigned long yPtr)
{
unsigned long valN1 = sizeof (unsigned long)-1;
unsigned long xVal = (unsigned long) xPtr & valN1;
unsigned long yVal = (unsigned long) yPtr & valN1;
unsigned long retVal = xVal ^ yVal;
return retVal;
}
// ----above are dietstring.h content.

char *
strcpy2 (char *s1, const char *s2)
{
char           *res = s1;
int             tmp;
unsigned long   l;

if (MyUnaligned((unsigned long)s1, (unsigned long)s2)){
while ((*s1++ = *s2++));
return (res);
}

if ((tmp = STRALIGN((unsigned long)s1))){
while (tmp-- && (*s1++ = *s2++));
if (tmp != -1) return (res);
}

while (1) {
unsigned long key1 = MKW(0x1ul);
unsigned long key2 = MKW(0x80ul);

l = *(const unsigned long *) s2;

if (((l - key1) & ~l) & key2) {
while ((*s1++ = GFC(l))) INCSTR(l);
return (res);
}

*(unsigned long *) s1 = l;
s2 += sizeof(unsigned long);
s1 += sizeof(unsigned long);
}
}

int _tmain(int argc, _TCHAR* argv[])
{
char* p = (char*)malloc(50* sizeof p);
char* str = "aaaabbbbbcccccc";
strcpy2(p, str);
free(p);
return 0;
}

``` *(unsigned long *) s1 = l;

s2 += sizeof(unsigned long);

s1 += sizeof(unsigned long);
```

OK，不还有一段代码么，在STRALIGN里面，会对目标字符串指针地址取模，然后将余数返回，比如我们运行时人为地修改s1以及s2地址将其+1。debug运行如下图，得到p以及str地址，可以看到都是对齐在unsigned long边界上的（ p & 3 一定是0）。