-
Notifications
You must be signed in to change notification settings - Fork 4
/
string.cpp
263 lines (197 loc) · 6.5 KB
/
string.cpp
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
/*
* Copyright 2010 Core Security Technologies.
*
* This file is part of turbodiff, an IDA plugin for analyzing differences
* between binary files.
* The plugin was designed and developed by Nicolas Economou, from the
* Exploit Writers team of Core Security Technologies.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* For further details, see the file COPYING distributed with turbodiff.
*/
/****************************************************************************/
/****************************************************************************/
/* string.cpp */
/****************************************************************************/
/****************************************************************************/
/* Includes */
#include <string.h>
/****************************************************************************/
/* defines */
/* Para mantener la compatibilidad */
#ifdef _IDA_HPP
#define strcpy(a,b) qstrncpy(a,b,strlen(b)+1)
#define strcat(a,b) qstrncat(a,b,strlen(b)+1)
#define fread(a,b,c,d) qfread(d,a,b)
#define fwrite(a,b,c,d) qfwrite(d,a,b)
#endif
/****************************************************************************/
/****************************************************************************/
/* Definicion de las clases */
class String
{
private:
unsigned int len;
char *string;
public:
String ();
~String ();
void Reset ( void );
const char *Get ( void );
int Set ( const char * );
unsigned int Len ( void );
int Append ( const char * );
int Truncate ( unsigned int );
/* Metodos para hacer PERSISTENCIA */
int Save ( FILE * );
int Load ( FILE * );
};
/****************************************************************************/
/****************************************************************************/
/* Metodos */
/****************************************************************************/
String::String ()
{
/* Inicializo la longitud */
this -> len = 0;
/* Inicializo el string */
this -> string = NULL;
}
/****************************************************************************/
String::~String ()
{
/* Si tengo algun string allocado */
if ( this -> string != NULL )
{
/* Libero la memoria */
free ( this -> string );
}
}
/****************************************************************************/
void String::Reset ( void )
{
/* Reinicializo el contador */
this -> len = 0;
/* Si tengo algun string allocado */
if ( this -> string != NULL )
{
/* Libero la memoria */
free ( this -> string );
}
/* Reinicializo el puntero al string */
this -> string = NULL;
}
/****************************************************************************/
const char *String::Get ( void )
{
/* Retorno el string */
return ( this -> string );
}
/****************************************************************************/
int String::Set ( const char *string )
{
unsigned int len;
int ret = FALSE;
char *dest;
/* Reinicializo el string */
this -> Reset ();
/* Obtengo la longitud del string */
len = strlen ( string );
/* Reservo la cantidad en bytes de memoria del string a guardar + 1 */
dest = ( char * ) malloc ( len + 1 );
/* Si todo salio OK */
if ( dest != NULL )
{
/* Seteo la longitud del string */
this -> len = len;
/* Seteo el puntero al string */
this -> string = dest;
/* Me hago una copia del string */
strcpy ( this -> string , string );
/* Retorno OK */
ret = TRUE;
}
return ( ret );
}
/****************************************************************************/
unsigned int String::Len ( void )
{
return ( this -> len );
}
/****************************************************************************/
int String::Append ( const char *string )
{
unsigned int len;
char *new_string;
int ret = FALSE;
/* Obtengo la longitud del string a appendear */
len = strlen ( string );
/* Intento agrandar el espacio reservado para el string */
new_string = ( char * ) realloc ( this -> string , this -> len + len + 1 );
/* Si pude agrandar el string */
if ( new_string != NULL )
{
/* Seteo el puntero al nuevo string */
this -> string = new_string;
/* Concateno el nuevo string al final del string original */
strcpy ( this -> string + this -> len , string );
/* Seteo la nueva longitud del string */
this -> len = this -> len + len;
/* Retorno OK */
ret = TRUE;
}
return ( ret );
}
/****************************************************************************/
int String::Truncate ( unsigned int pos )
{
int ret = FALSE;
/* Si la posicion es valida */
if ( pos < this -> len )
{
/* Seteo el nuevo len del string */
this -> len = pos;
/* Realloco el string */
this -> string = ( char * ) realloc ( this -> string , this -> len + 1 );
/* Cierro el string */
this -> string [ pos ] = '\0';
/* Retorno OK */
ret = TRUE;
}
return ( ret);
}
/****************************************************************************/
int String::Save ( FILE *f )
{
int ret = TRUE;
/* Guardo las propiedades del objeto */
fwrite ( this , sizeof ( String ) , 1 , f );
/* Guardo el string */
fwrite ( this -> string , this -> len + 1 , 1 , f );
return ( ret );
}
/****************************************************************************/
int String::Load ( FILE *f )
{
int ret = TRUE;
/* Levanto las propiedades del objeto */
fread ( this , sizeof ( String ) , 1 , f );
/* Alloco espacio para el string */
this -> string = ( char * ) malloc ( this -> len + 1 );
/* Levanto el string */
fread ( this -> string , this -> len + 1 , 1 , f );
return ( ret );
}
/****************************************************************************/
/****************************************************************************/