blob: 8b05ed551cd56231a9986db0314aec0113c0b947 [file] [log] [blame]
Stefan Reinauer6ed57e52010-04-24 01:15:17 +02001/*
2 * Decompression utility for AMI BIOSes.
3 *
4 * Copyright (C) 2009-2010 coresystems GmbH
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2, or (at your option)
9 * any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; see the file COPYING. If not, write to
18 * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20
21#include <stdlib.h>
22#include <string.h>
23
24#ifdef __APPLE__
25void *memmem(const void *haystack, size_t haystacklen, const void *needle,
26 size_t needlelen)
27{
28 char *searchpointer = (char *) haystack;
29 char *patternpointer = (char *) needle;
30 char *endofsearch = searchpointer + haystacklen - needlelen;
31
32 if (!(haystack && needle && haystacklen && needlelen))
33 return NULL;
34
35 while (searchpointer <= endofsearch) {
36 if (*searchpointer == *patternpointer)
37 if (memcmp
38 (searchpointer, patternpointer,
39 needlelen) == 0)
40 return searchpointer;
41
42 searchpointer++;
43 }
44
45 return NULL;
46}
47
48size_t strnlen(const char *s, size_t maxlen)
49{
50 const char *end = memchr(s, '\0', maxlen);
51 return end ? (size_t) (end - s) : maxlen;
52}
53
54char *strndup(const char *s, size_t n)
55{
56 size_t len = strnlen(s, n);
57 char *new = malloc(len + 1);
58
59 if (new == NULL)
60 return NULL;
61
62 new[len] = '\0';
63 return memcpy(new, s, len);
64}
65#endif