Botan 1.10.17
mem_ops.h
Go to the documentation of this file.
1/*
2* Memory Operations
3* (C) 1999-2009 Jack Lloyd
4*
5* Distributed under the terms of the Botan license
6*/
7
8#ifndef BOTAN_MEMORY_OPS_H__
9#define BOTAN_MEMORY_OPS_H__
10
11#include <botan/types.h>
12#include <cstring>
13
14namespace Botan {
15
16/**
17* Copy memory
18* @param out the destination array
19* @param in the source array
20* @param n the number of elements of in/out
21*/
22template<typename T> inline void copy_mem(T* out, const T* in, size_t n)
23 {
24 std::memmove(out, in, sizeof(T)*n);
25 }
26
27/**
28* Zeroize memory
29* @param ptr a pointer to an array
30* @param n the number of Ts pointed to by ptr
31*/
32template<typename T> inline void clear_mem(T* ptr, size_t n)
33 {
34 if(n) // avoid glibc warning if n == 0
35 std::memset(ptr, 0, sizeof(T)*n);
36 }
37
38/**
39* Set memory to a fixed value
40* @param ptr a pointer to an array
41* @param n the number of Ts pointed to by ptr
42* @param val the value to set each byte to
43*/
44template<typename T>
45inline void set_mem(T* ptr, size_t n, byte val)
46 {
47 std::memset(ptr, val, sizeof(T)*n);
48 }
49
50/**
51* Memory comparison, input insensitive
52* @param p1 a pointer to an array
53* @param p2 a pointer to another array
54* @param n the number of Ts in p1 and p2
55* @return true iff p1[i] == p2[i] forall i in [0...n)
56*/
57template<typename T> inline bool same_mem(const T* p1, const T* p2, size_t n)
58 {
59 bool is_same = true;
60
61 for(size_t i = 0; i != n; ++i)
62 is_same &= (p1[i] == p2[i]);
63
64 return is_same;
65 }
66
67}
68
69#endif
void set_mem(T *ptr, size_t n, byte val)
Definition mem_ops.h:45
void copy_mem(T *out, const T *in, size_t n)
Definition mem_ops.h:22
bool same_mem(const T *p1, const T *p2, size_t n)
Definition mem_ops.h:57
void clear_mem(T *ptr, size_t n)
Definition mem_ops.h:32