Back to index

glibc  2.9
memset_chk.c
Go to the documentation of this file.
00001 /* Copyright (C) 1991, 1997, 2003, 2004, 2005 Free Software Foundation, Inc.
00002    This file is part of the GNU C Library.
00003 
00004    The GNU C Library is free software; you can redistribute it and/or
00005    modify it under the terms of the GNU Lesser General Public
00006    License as published by the Free Software Foundation; either
00007    version 2.1 of the License, or (at your option) any later version.
00008 
00009    The GNU C Library is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY; without even the implied warranty of
00011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012    Lesser General Public License for more details.
00013 
00014    You should have received a copy of the GNU Lesser General Public
00015    License along with the GNU C Library; if not, write to the Free
00016    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00017    02111-1307 USA.  */
00018 
00019 #include <string.h>
00020 #include <memcopy.h>
00021 
00022 void *
00023 __memset_chk (dstpp, c, len, dstlen)
00024      void *dstpp;
00025      int c;
00026      size_t len;
00027      size_t dstlen;
00028 {
00029   if (__builtin_expect (dstlen < len, 0))
00030     __chk_fail ();
00031 
00032   long int dstp = (long int) dstpp;
00033 
00034   if (len >= 8)
00035     {
00036       size_t xlen;
00037       op_t cccc;
00038 
00039       cccc = (unsigned char) c;
00040       cccc |= cccc << 8;
00041       cccc |= cccc << 16;
00042       if (OPSIZ > 4)
00043        /* Do the shift in two steps to avoid warning if long has 32 bits.  */
00044        cccc |= (cccc << 16) << 16;
00045 
00046       /* There are at least some bytes to set.
00047         No need to test for LEN == 0 in this alignment loop.  */
00048       while (dstp % OPSIZ != 0)
00049        {
00050          ((byte *) dstp)[0] = c;
00051          dstp += 1;
00052          len -= 1;
00053        }
00054 
00055       /* Write 8 `op_t' per iteration until less than 8 `op_t' remain.  */
00056       xlen = len / (OPSIZ * 8);
00057       while (xlen > 0)
00058        {
00059          ((op_t *) dstp)[0] = cccc;
00060          ((op_t *) dstp)[1] = cccc;
00061          ((op_t *) dstp)[2] = cccc;
00062          ((op_t *) dstp)[3] = cccc;
00063          ((op_t *) dstp)[4] = cccc;
00064          ((op_t *) dstp)[5] = cccc;
00065          ((op_t *) dstp)[6] = cccc;
00066          ((op_t *) dstp)[7] = cccc;
00067          dstp += 8 * OPSIZ;
00068          xlen -= 1;
00069        }
00070       len %= OPSIZ * 8;
00071 
00072       /* Write 1 `op_t' per iteration until less than OPSIZ bytes remain.  */
00073       xlen = len / OPSIZ;
00074       while (xlen > 0)
00075        {
00076          ((op_t *) dstp)[0] = cccc;
00077          dstp += OPSIZ;
00078          xlen -= 1;
00079        }
00080       len %= OPSIZ;
00081     }
00082 
00083   /* Write the last few bytes.  */
00084   while (len > 0)
00085     {
00086       ((byte *) dstp)[0] = c;
00087       dstp += 1;
00088       len -= 1;
00089     }
00090 
00091   return dstpp;
00092 }