blob: f577254a8a7058e153f7b4c294e1e20f666331a4 [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{
Anton Kochkov8f9427f2013-04-07 13:20:30 +040028 char *searchpointer = (char *)haystack;
29 char *patternpointer = (char *)needle;
Stefan Reinauer6ed57e52010-04-24 01:15:17 +020030 char *endofsearch = searchpointer + haystacklen - needlelen;
31
32 if (!(haystack && needle && haystacklen && needlelen))
33 return NULL;
34
35 while (searchpointer <= endofsearch) {
36 if (*searchpointer == *patternpointer)
Anton Kochkov8f9427f2013-04-07 13:20:30 +040037 if (memcmp(searchpointer, patternpointer, needlelen) ==
38 0)
Stefan Reinauer6ed57e52010-04-24 01:15:17 +020039 return searchpointer;
Stefan Reinauer6ed57e52010-04-24 01:15:17 +020040 searchpointer++;
41 }
42
43 return NULL;
44}
45
46size_t strnlen(const char *s, size_t maxlen)
47{
48 const char *end = memchr(s, '\0', maxlen);
49 return end ? (size_t) (end - s) : maxlen;
50}
51
52char *strndup(const char *s, size_t n)
53{
54 size_t len = strnlen(s, n);
55 char *new = malloc(len + 1);
56
57 if (new == NULL)
58 return NULL;
59
60 new[len] = '\0';
61 return memcpy(new, s, len);
62}
63#endif