-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHexString.h
More file actions
222 lines (171 loc) · 7.13 KB
/
HexString.h
File metadata and controls
222 lines (171 loc) · 7.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
#ifndef __HEX_STRING_H
#define __HEX_STRING_H
/// Bug: need to memset 0 for new byte array, when use Trim*()
#include "DS_List.h"
#include <stdio.h>
#include <stdarg.h>
#include "DS_ThreadSafeList.h"
#if defined(__GNUC__)
#define _vsnprintf vsnprintf
#endif
#pragma warning(disable:4996)
namespace DataStructures
{
/// \brief String class
/// Has the following improvements over std::string
/// Reference counting: Suitable to store in lists
/// Varidic assignment operator
/// Doesn't cause linker errors
class HexString
{
public:
/// Constructors
HexString();
HexString(char input);
HexString(unsigned char input);
HexString(const unsigned char *format, ...)
{
char text[8096];
va_list ap;
va_start(ap, format);
_vsnprintf(text, 8096, (const char*) format, ap);
va_end(ap);
text[8096-1]=0;
Assign(text);
}
HexString(const char *format, ...){
char text[8096];
va_list ap;
va_start(ap, format);
_vsnprintf(text, 8096, format, ap);
va_end(ap);
text[8096-1]=0;
Assign(text);
}
~HexString();
HexString( const HexString & rhs);
HexString( const char *source, unsigned int maxLen )
{
Assign(source);
SetLength(maxLen);
}
/// Implicit return of const char*
operator const char *() const {return sharedString->c_str;}
/// Same as std::string::c_str
const char *C_String(void) const {return sharedString->c_str;}
/// Assigment operators
HexString& operator = ( const HexString& rhs );
HexString& operator = ( const char *str );
HexString& operator = ( char *str );
/// Concatenation
HexString& operator +=( const HexString& rhs);
HexString& operator += ( const char *str );
HexString& operator += ( char *str );
/// Character index. Do not use to change the string however.
unsigned char operator[] ( const unsigned int position ) const;
/// Equality
bool operator==(const HexString &rhs) const;
bool operator==(const char *str) const;
bool operator==(char *str) const;
/// Inequality
bool operator!=(const HexString &rhs) const;
/// erase left and right spaces in string
void Trim(void);
/// erase left spaces in string
void TrimLeft(void);
/// erase right spaces in string
void TrimRight(void);
/// Change all characters to lowercase
void ToLower(void);
/// Change all characters to uppercase
void ToUpper(void);
/// Set the value of the string
void Set(const char *format, ...);
void SetLength(unsigned int len);
/// Returns if the string is empty. Also, C_String() would return ""
bool IsEmpty(void) const;
/// Returns the length of the string
size_t GetLength(void) const;
/// Replace character(s) in starting at index, for count, with c
void Replace(unsigned index, unsigned count, unsigned char c);
/// Replace character(s) in starting at index, for count, with string
void Replace(unsigned index, unsigned count, const char *string);
void Replace(char source, char c);
/// Insert character(s) in starting at index
void Insert(unsigned index, const HexString &toAdd);
/// Insert character(s) in starting at index with specific count
void Insert(unsigned index, const char toAdd, unsigned int count);
/// Erase characters out of the string at index for count
void Erase(unsigned index, unsigned count);
/// Compare strings (case sensitive)
int StrCmp(const HexString &rhs) const;
/// Compare strings (not case sensitive)
int StrICmp(const HexString &rhs) const;
/// find sub string in given source (case sensitive)
int IsSubString(const HexString &rhs) const;
/// find sub string in given source (not case sensitive)
int IsSubIString(const HexString &rhs) const;
/// create a new string started with index
HexString SubString(unsigned int index, int length = -1) const;
/// find sub string in this (case sensitive)
int FindString(const HexString &rhs) const;
/// find sub string in this (not case sensitive)
int FindIString(const HexString &rhs) const;
/// find sub string in this from the end (case sensitive)
int FindStringReverse(const HexString &rhs) const;
/// find sub string in this (case sensitive)
int FindString(const HexString &rhs, unsigned int index) const;
/// Clear the string
void Clear(void);
/// Print the string to the screen
void Printf(void);
/// Print the string to a file
void FPrintf(FILE *fp);
/// Does the given IP address match the IP address encoded into this string, accounting for wildcards?
bool IPAddressMatch(const char *IP);
/// \internal
static size_t GetSizeToAllocate(size_t bytes)
{
const size_t smallStringSize = 128-sizeof(unsigned int)-sizeof(size_t)-sizeof(char*)*2;
if (bytes<=smallStringSize)
return smallStringSize;
else
return bytes*2;
}
/// \internal
struct SharedString
{
unsigned int refCount;
size_t bytesUsed;
char *bigString;
char *c_str;
char smallString[128-sizeof(unsigned int)-sizeof(size_t)-sizeof(char*)*2];
};
/// \internal
HexString( SharedString *_sharedString );
/// \internal
SharedString *sharedString;
// static SimpleMutex poolMutex;
// static DataStructures::MemoryPool<SharedString> pool;
/// \internal
static SharedString emptyString;
//static SharedString *sharedStringFreeList;
//static unsigned int sharedStringFreeListAllocationCount;
/// \internal
/// List of free objects to reduce memory reallocations
static DataStructures::ThreadSafeList<SharedString*> freeList;
static int HexStringComp( HexString const &key, HexString const &data );
static void FreeCachedMemory();
static void EnableCaching() { needCaching = true; }
protected:
void Assign(const char *str);
void Clone(void);
void Free(void);
unsigned char ToLower(unsigned char c);
unsigned char ToUpper(unsigned char c);
void Realloc(SharedString *sharedString, size_t bytes);
static bool needCaching;
};
}
const DataStructures::HexString operator+(const DataStructures::HexString &lhs, const DataStructures::HexString &rhs);
#endif