The Pedigree Project  0.1
support.cc
1 /*
2  * Copyright (c) 2008-2014, Pedigree Developers
3  *
4  * Please see the CONTRIB file in the root of the source tree for a full
5  * list of contributors.
6  *
7  * Permission to use, copy, modify, and distribute this software for any
8  * purpose with or without fee is hereby granted, provided that the above
9  * copyright notice and this permission notice appear in all copies.
10  *
11  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18  */
19 
20 #include "support.h"
21 
22 #pragma GCC optimize("no-tree-loop-distribute-patterns")
23 
24 extern "C" {
25 
26 int StringCopyN(char *dest, const char *src, int len)
27 {
28  int i = 0;
29  while (*src && len)
30  {
31  *dest++ = *src++;
32  len--;
33  i++;
34  }
35  *dest = '\0';
36  return i;
37 }
38 
39 int ByteSet(void *buf, int c, size_t len)
40 {
41  size_t count = len;
42  char *tmp = (char *) buf;
43  while (len--)
44  {
45  *tmp++ = (char) c;
46  }
47  return count;
48 }
49 
50 void MemoryCopy(void *dest, const void *src, size_t len)
51 {
52  const unsigned char *sp = (const unsigned char *) src;
53  unsigned char *dp = (unsigned char *) dest;
54  for (; len != 0; len--)
55  *dp++ = *sp++;
56 }
57 
58 int StringCompare(const char *p1, const char *p2)
59 {
60  int i = 0;
61  int failed = 0;
62  while (p1[i] != '\0' && p2[i] != '\0')
63  {
64  if (p1[i] != p2[i])
65  {
66  failed = 1;
67  break;
68  }
69  i++;
70  }
71  // why did the loop exit?
72  if ((p1[i] == '\0' && p2[i] != '\0') || (p1[i] != '\0' && p2[i] == '\0'))
73  failed = 1;
74 
75  return failed;
76 }
77 }