#include #include #define hidden __attribute__((visibility("hidden"))) #define aliases __attribute__((__may_alias__)) #define byte_repeat(x) ((size_t)~0 / 0xff * (x)) #define word_has_zero(x) (((x) - byte_repeat(0x01)) & ~(x) & byte_repeat(0x80)) hidden size_t __strscpy(char *restrict d, const char *restrict s, size_t n) { const char *const d0 = d; size_t aliases *wd; const size_t aliases *ws; if (!n--) return 0; if ( n < sizeof(size_t) * 3 || ((uintptr_t)d | (uintptr_t)s) & sizeof(size_t) - 1) goto bytewise; for (; (uintptr_t)s & sizeof(size_t) - 1 && (*d = *s); d++, s++, n--); if ((uintptr_t)s & sizeof(size_t) - 1) return d - d0; wd = (void *)d; ws = (const void *)s; for (; !word_has_zero(*ws) && n >= sizeof(size_t) ; wd++, ws++, n -= sizeof(size_t)) *wd = *ws; d = (void *)wd; s = (const void *)ws; bytewise: for (; n && (*d = *s); d++, s++, n--); *d = 0; return d - d0; }