~ubuntu-branches/ubuntu/trusty/tla/trusty

« back to all changes in this revision

Viewing changes to src/hackerlab/mem/mem.c

  • Committer: Bazaar Package Importer
  • Author(s): Andrew Suffield
  • Date: 2004-05-30 20:13:29 UTC
  • Revision ID: james.westby@ubuntu.com-20040530201329-mgovd2u99mkxi0hf
Tags: upstream-1.2
ImportĀ upstreamĀ versionĀ 1.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* mem.c - array-of-byte functions
 
2
 *
 
3
 ****************************************************************
 
4
 * Copyright (C) 1998, 2000 Thomas Lord
 
5
 * 
 
6
 * See the file "COPYING" for further information about
 
7
 * the copyright and warranty status of this work.
 
8
 */
 
9
 
 
10
 
 
11
 
 
12
#include "hackerlab/mem/mem.h"
 
13
 
 
14
 
 
15
/************************************************************************
 
16
 *(h1 "Array-of-Byte Functions"
 
17
 *      :includes ("hackerlab/mem/mem.h"))
 
18
 *
 
19
 * These functions operate on regions of memory as arrays of unsigned
 
20
 * bytes with no further interpretation.
 
21
 */
 
22
 
 
23
 
 
24
/*(c mem_set)
 
25
 * void mem_set (t_uchar * mem, unsigned int c, size_t size);
 
26
 * 
 
27
 * Store `size' bytes of value `c' beginning at `mem'.
 
28
 */
 
29
void
 
30
mem_set (t_uchar * mem, unsigned int c, size_t size)
 
31
{
 
32
  while (size--)
 
33
    *(mem++) = c;
 
34
}
 
35
 
 
36
 
 
37
/*(c mem_set0)
 
38
 * void mem_set0 (t_uchar * mem, size_t n);
 
39
 * 
 
40
 * Store `n' 0 bytes beginning at `mem'.
 
41
 */
 
42
void
 
43
mem_set0 (t_uchar * mem, size_t n)
 
44
{
 
45
  while (n--)
 
46
    *(mem++) = 0;
 
47
}
 
48
 
 
49
 
 
50
/*(c mem_move)
 
51
 * void mem_move (t_uchar * to, const t_uchar * from, size_t amt);
 
52
 * 
 
53
 * Copy `amt' bytes from `from' to `to'.  The source and destination
 
54
 * regions may overlap.
 
55
 */
 
56
void
 
57
mem_move (t_uchar * to, const t_uchar * from, size_t amt)
 
58
{
 
59
  if (from == to)
 
60
    return;
 
61
  if (from > to)
 
62
    {
 
63
      while (amt--)
 
64
        *to++ = *from++;
 
65
    }
 
66
  else
 
67
    {
 
68
      to += amt - 1;
 
69
      from += amt - 1;
 
70
      while (amt--)
 
71
        *to-- = *from--;
 
72
    }
 
73
}
 
74
 
 
75
 
 
76
/*(c mem_cmp)
 
77
 * int mem_cmp (const t_uchar * m1, const t_uchar * m2, size_t amt);
 
78
 * 
 
79
 * Compare `amt' bytes starting at `m1' and `m2'.  If a difference is
 
80
 * found, immediately return -1 if the differing byte in `m1' is less
 
81
 * than the byte in `m2', 1 otherwise.  If no difference is found,
 
82
 * return 0.
 
83
 */
 
84
int
 
85
mem_cmp (const t_uchar * m1, const t_uchar * m2, size_t amt)
 
86
{
 
87
  while (amt--)
 
88
    if (*m1++ != *m2++)
 
89
      {
 
90
        --m1;
 
91
        --m2;
 
92
        return (*m1 < *m2) ? -1 : 1;
 
93
      }
 
94
  return 0;
 
95
}
 
96
 
 
97
 
 
98
/*(c mem_occurrences)
 
99
 * size_t mem_occurrences (const t_uchar * mem,
 
100
 *                         unsigned int c,
 
101
 *                         size_t size);
 
102
 * 
 
103
 * Search `size' bytes of data for occurences of byte `c' beginning at
 
104
 * `mem'.  Return the number of occurences found.
 
105
 */
 
106
size_t
 
107
mem_occurrences (const t_uchar * mem,
 
108
                 unsigned int c,
 
109
                 size_t size)
 
110
{
 
111
  size_t x;
 
112
  x = 0;
 
113
  while (size--)
 
114
    if (*mem++ == c)
 
115
      ++x;
 
116
  return x;
 
117
}