Debian Codegraph @

src/lib/data_mgr/ByteString.h

raw
1 /*
2 * Copyright (c) 2010 SURFnet bv
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
16 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
18 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
20 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
22 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
23 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
24 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27 /*****************************************************************************
28 ByteString.h
29
30 A string class for byte strings stored in securely allocated memory
31 *****************************************************************************/
32
33 #ifndef _SOFTHSM_V2_BYTESTRING_H
34 #define _SOFTHSM_V2_BYTESTRING_H
35
36 #include <cstddef>
37 #include <vector>
38 #include <string>
39 #include <stdlib.h>
40 #include <limits.h>
41 #include "config.h"
42 #include "SecureAllocator.h"
43 #include "Serialisable.h"
44
45 #ifndef SIZE_T_MAX
46 #define SIZE_T_MAX ((size_t) -1)
47 #endif // !SIZE_T_MAX
48
49 class ByteString
50 {
51 public:
52 // Constructors
53 ByteString();
54
55 ByteString(const unsigned char* bytes, const size_t bytesLen);
56
57 ByteString(const char* hexString);
58
59 ByteString(const unsigned long longValue);
60
61 ByteString(const ByteString& in);
62
63 // Destructor
64 virtual ~ByteString() { }
65
66 // Assignment
67 ByteString& operator=(const ByteString& in);
68
69 // Append data
70 ByteString& operator+=(const ByteString& append);
71 ByteString& operator+=(const unsigned char byte);
72
73 // Return a substring
74 ByteString substr(const size_t start, const size_t len = SIZE_T_MAX) const;
75
76 // Array operator
77 unsigned char& operator[](size_t pos);
78
79 // Return the byte string
80 unsigned char* byte_str();
81
82 // Return the const byte string
83 const unsigned char* const_byte_str() const;
84
85 // Return a hexadecimal character representation of the string
86 std::string hex_str() const;
87
88 // Return the long value
89 unsigned long long_val() const;
90
91 // Cut of the first part of the string and convert it to a long value
92 unsigned long firstLong();
93
94 // Split of the specified part of the string as a separate byte string
95 ByteString split(size_t len);
96
97 // Return the size in bits
98 size_t bits() const;
99
100 // Return the size in bytes
101 size_t size() const;
102
103 // Resize
104 void resize(const size_t newSize);
105
106 // Wipe
107 void wipe(const size_t newSize = 0);
108
109 // Comparison
110 bool operator==(const ByteString& compareTo) const;
111 bool operator!=(const ByteString& compareTo) const;
112
113 // XORing
114 ByteString& operator^=(const ByteString& rhs);
115
116 // Serialisation/deserialisation
117 virtual ByteString serialise() const;
118
119 static ByteString chainDeserialise(ByteString& serialised);
120
121 private:
122 std::vector<unsigned char, SecureAllocator<unsigned char> > byteString;
123 };
124
125 // Add data
126 ByteString operator+(const ByteString& lhs, const ByteString& rhs);
127 ByteString operator+(const unsigned char lhs, const ByteString& rhs);
128 ByteString operator+(const ByteString& lhs, const unsigned char rhs);
129
130 // XOR data
131 ByteString operator^(const ByteString& lhs, const ByteString& rhs);
132
133 #endif // !_SOFTHSM_V2_BYTESTRING_H
134