memory.c revision bb12ac9b85adae96cbd38b2220c5da9a9d80bc54
1/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <cutils/memory.h>
18
19#if !HAVE_MEMSET16
20void android_memset16(uint16_t* dst, uint16_t value, size_t size)
21{
22    size >>= 1;
23    while (size--) {
24        *dst++ = value;
25    }
26}
27#endif
28
29#if !HAVE_MEMSET32
30void android_memset32(uint32_t* dst, uint32_t value, size_t size)
31{
32    size >>= 2;
33    while (size--) {
34        *dst++ = value;
35    }
36}
37#endif
38
39#if !HAVE_STRLCPY
40/*
41 * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com>
42 *
43 * Permission to use, copy, modify, and distribute this software for any
44 * purpose with or without fee is hereby granted, provided that the above
45 * copyright notice and this permission notice appear in all copies.
46 *
47 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
48 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
49 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
50 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
51 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
52 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
53 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
54 */
55
56#include <sys/types.h>
57#include <string.h>
58
59/* Implementation of strlcpy() for platforms that don't already have it. */
60
61/*
62 * Copy src to string dst of size siz.  At most siz-1 characters
63 * will be copied.  Always NUL terminates (unless siz == 0).
64 * Returns strlen(src); if retval >= siz, truncation occurred.
65 */
66size_t
67strlcpy(char *dst, const char *src, size_t siz)
68{
69	char *d = dst;
70	const char *s = src;
71	size_t n = siz;
72
73	/* Copy as many bytes as will fit */
74	if (n != 0) {
75		while (--n != 0) {
76			if ((*d++ = *s++) == '\0')
77				break;
78		}
79  }
80
81	/* Not enough room in dst, add NUL and traverse rest of src */
82	if (n == 0) {
83		if (siz != 0)
84			*d = '\0';		/* NUL-terminate dst */
85		while (*s++)
86			;
87	}
88
89	return(s - src - 1);	/* count does not include NUL */
90}
91#endif
92