-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathVar.cpp
More file actions
549 lines (419 loc) · 12.5 KB
/
Var.cpp
File metadata and controls
549 lines (419 loc) · 12.5 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
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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
/*******************************************************************
// Copyright (c) 2000, Robert Umbehant
// mailto:rumbehant@wheresjames.com
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later
// version.
//
// This library 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 Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free
// Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
// MA 02111-1307 USA
//
*******************************************************************/
// Var.cpp: implementation of the CVar class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CVar::CVar()
{_STTEX();
}
CVar::~CVar()
{_STTEX();
Destroy();
}
void CVar::DeleteObject(void *node)
{_STTEX();
LPVAR pv = (LPVAR)node;
if ( pv == NULL ) return;
// Release memory
if ( pv->size > 0 && pv->val != NULL )
delete [] pv->val;
CLList::DeleteObject( node );
}
LPVAR CVar::AddVar(LPCTSTR pVar, DWORD dwType, LPVOID pVal, DWORD dwSize)
{_STTEX();
// Sanity checks
if ( pVar == NULL ) return NULL;
// Does the variable already exist?
BOOL add = FALSE;
LPVAR node = FindVar( pVar );
if ( node != NULL )
{
// Delete variable if needed
if ( node->size != 0 && node->val != NULL )
{
delete [] node->val;
node->val = NULL;
} // end if
} // end if
else
{
// Allocate memory
node = (LPVAR)New();
if ( node == NULL ) return NULL;
} // end if
// Save variable type
node->type = dwType;
// Copy variable name
strcpy_sz( node->name, pVar );
// Copy variable size
node->size = dwSize;
// Copy the data
if ( dwSize == 0 ) node->val = pVal;
else
{
// Allocate variable value memory plus one for NULL char
node->val = new LPBYTE[ dwSize + 1 ];
if ( node->val == NULL ) { Delete( node ); return NULL; }
// Check for init data
if ( pVal == NULL ) ZeroMemory( node->val, dwSize );
// Copy the value
else memcpy( node->val, pVal, dwSize );
// NULL terminate for good measure
( (LPBYTE)node->val )[ dwSize ] = 0;
} // end else
return node;
}
LPVAR CVar::FindVar(LPCTSTR pVar)
{_STTEX();
// Sanity check
if ( pVar == NULL ) return FALSE;
// Find the variable
LPVAR pv = NULL;
while ( ( pv = (LPVAR)GetNext( pv ) ) != NULL )
if ( !strcmpi( pv->name, pVar ) )
return pv;
return NULL;
}
BOOL CVar::RemoveVar(LPCTSTR pVar)
{_STTEX();
LPVAR node = FindVar( pVar );
if ( node == NULL ) return FALSE;
Delete( node );
return TRUE;
}
BOOL CVar::ReadMIME(LPBYTE buf, DWORD size)
{_STTEX();
DWORD i = 0;
BOOL endvars = FALSE;
char token[ CWF_STRSIZE ];
while ( !endvars && i < size )
{
// Forgive one CRLF
if ( i < size && buf[ i ] == 0x0d ) i++;
if ( i < size && buf[ i ] == 0x0a ) i++;
// Skip white space
while ( i < size && ( buf[ i ] <= ' ' || buf[ i ] > '~' ) &&
buf[ i ] != 0x0d && buf[ i ] != 0x0a )
i++;
// Are there any variables?
if ( buf[ i ] != 0x0d && buf[ i ] != 0x0a )
{
// Punt if end of data
if ( i >= size ) { return TRUE; }
// Copy token
DWORD x = 0;
while ( i < size &&
buf[ i ] != ':' &&
buf[ i ] > ' ' &&
buf[ i ] <= '~' &&
x < sizeof( token ) - 1 )
token[ x++ ] = buf[ i++ ];
token[ x ] = NULL;
// Did we get the separator?
if ( buf[ i++ ] == ':' )
{
// Skip white space
while ( ( buf[ i ] <= ' ' || buf[ i ] > '~' ) &&
buf[ i ] != 0x0d && buf[ i ] != 0x0a ) i++;
DWORD e = i, skip;
// Find the end
BOOL end = FALSE;
while ( !end && !endvars )
{
// Find the end
while ( buf[ e ] != 0x0d && buf[ e ] != 0x0a && e < size ) e++;
if( buf[ e ] != 0x0d && buf[ e ] != 0x0a ) return FALSE;
if ( buf[ ++e ] == 0x0a ) e++;
if ( buf[ e ] > ' ' || buf[ e ] == 0x0d || buf[ e ] == 0x0a )
end = TRUE;
// Check for end of variables
if ( buf[ e ] == 0x0d || buf[ e ] == 0x0a ) endvars = TRUE;
} // end while
// Remember where the end is
skip = e;
// Lose trailing white space
e--; while ( e > i && ( buf[ e ] <= ' ' || buf[ e ] > '~' ) ) e--;
if ( e > i )
{
// Add this variable
AddVar( token, VAR_STR, (LPSTR)&buf[ i ], e - i + 1 );
} // end if
// Forge ahead
i = skip;
} // end if
// Skip to next line
else while ( i < size && buf[ i ] != 0x0d && buf[ i ] != 0x0a ) i++;
} // end if
else endvars = TRUE;
} // end while
return TRUE;
}
BOOL CVar::ReadInline(LPBYTE buf, DWORD size, char sep, BOOL bDeCanonicalize)
{_STTEX();
// Sanity checks
if ( buf == NULL || size == 0 ) return FALSE;
DWORD i = 0;
char token[ CWF_STRSIZE ];
while ( i < size && buf[ i ] != 0 )
{
DWORD t = 0;
// Skip white space
while ( i < size && ( buf[ i ] <= ' ' || buf[ i ] > '~' ) && buf[ i ] != 0 ) i++;
if ( i >= size || buf[ i ] == 0 ) return TRUE;
// Read in first token
while ( i < size &&
buf[ i ] >= ' ' && buf[ i ] <= '~' &&
buf[ i ] != '=' )
token[ t++ ] = buf[ i++ ];
token[ t ] = 0;
// Check for '='
if ( buf[ i ] == '=' )
{ i++;
// Skip starting spaces
while ( i < size && buf[ i ] == ' ' ) i++;
DWORD s = i;
BOOL quoted = FALSE;
// Find the end of the data
while ( i < size &&
// ( ( quoted && buf[ i ] >= ' ' ) || buf[ i ] > ' ' ) &&
( buf[ i ] >= ' ' ) &&
buf[ i ] <= '~' && buf[ i ] != sep )
{
// Toggle quote mode
if ( buf[ i ] == '"' ) quoted = !quoted;
// Next char
i++;
} // end if
if ( i > s )
{
if ( bDeCanonicalize )
{
// DeCanonicalize data
DWORD dwMin = CCfgFile::GetMinDeCanonicalizeBufferSize( i - s );
TMem< BYTE > mem;
if ( mem.allocate( dwMin + 1 ) )
{
// Do it
DWORD dwSize = 0;
if ( CCfgFile::DeCanonicalizeBuffer( (LPCTSTR)&buf[ s ], (LPBYTE)mem.ptr(), i - s, &dwSize ) )
// Add variable
AddVar( token, VAR_STR, mem.ptr(), dwSize );
} // end if
} // end if
// Add variable if any
else AddVar( token, VAR_STR, &buf[ s ], i - s );
} // end if
// Skip separator
if ( buf[ i ] == sep ) i++;
} // end if
// Add null
else AddVar( token, VAR_VOID, NULL, 0 );
} // end while
return TRUE;
}
BOOL CVar::Replace(CPipe *out, LPDWORD op, LPCTSTR in, DWORD dwin, LPCTSTR pBegin, LPCTSTR pEnd, LPSTR pBreak, CVar *params, LPDWORD pdwBreak, char sep)
{_STTEX();
// Sanity check
if ( out == NULL || in == NULL ) return FALSE;
// No break yet
if ( pBreak != NULL ) *pBreak = 0;
DWORD s = 0, i = 0;
const char *strrep = pBegin;
if ( strrep == NULL ) strrep = "<!--$$$";
DWORD dwrep = strlen( strrep );
const char *endrep = pEnd;
if ( endrep == NULL ) endrep = "-->";
DWORD dwendrep = strlen( endrep );
// Pick up where we left off
if ( pdwBreak != NULL ) s = i = *pdwBreak;
// Lose old params
if ( params != NULL ) params->Destroy();
while ( i < dwin )
{
// Check for replace token
if ( ( dwin - i ) > dwrep &&
*(LPDWORD)&in[ i ] == *(LPDWORD)strrep &&
!strnicmp( &in[ i ], strrep, dwrep ) )
{
// Write out good data
if ( i != s )
{
out->Write( (LPVOID)&in[ s ], i - s );
} // end if
DWORD x = 0;
char token[ CWF_STRSIZE ];
// Skip replace flag
i += dwrep;
// Skip white space
while ( i < dwin && ( in[ i ] <= ' ' || in[ i ] > '~' ) ) i++;
if ( i >= dwin ) return TRUE;
// Copy token
while ( in[ i ] > ' ' && in[ i ] <= '~' &&
strnicmp( &in[ i ], endrep, dwendrep ) )
{ if ( x < sizeof( token ) - 1 ) token[ x++ ] = in[ i ]; i++; }
token[ x ] = 0;
DWORD p = i;
// Skip to end of replace tag
while ( i < dwin && strnicmp( &in[ i ], endrep, dwendrep ) &&
in[ i ] != 0 ) i++;
DWORD e = i;
while ( e > p && in[ e - 1 ] == ' ' ) e--;
// Read in vars if any
if ( e > p && params != NULL )
params->ReadInline( (LPBYTE)&in[ p ], e - p, sep );
// Skip end tag
if ( !strnicmp( &in[ i ], endrep, dwendrep ) ) i += dwendrep;
// Point to start of good data
s = i;
LPVAR pv = FindVar( token );
if ( pv != NULL )
{
// Check for break
if ( pBreak != NULL && pv->type == VAR_VOID )
{ if ( pdwBreak != NULL ) *pdwBreak = i;
strcpy( pBreak, token );
return TRUE;
} // end if
// Write out the replace value
else out->Write( pv->val, pv->size );
} // end if
else // Break
{
if ( pdwBreak != NULL ) *pdwBreak = i;
strcpy( pBreak, token );
return TRUE;
} // end else
} // end if
// Next
i++;
} // end while
// Write out what's left of the data
if ( i > s )
{
out->Write( (LPVOID)&in[ s ], i - s );
} // end if
return FALSE;
}
BOOL CVar::Replace(LPSTR out, LPDWORD op, DWORD dwout, LPCTSTR in, DWORD dwin, LPCTSTR pBegin, LPCTSTR pEnd, LPSTR pBreak, CVar *params, LPDWORD pdwBreak, char sep)
{_STTEX();
// Sanity check
if ( out == NULL || in == NULL ) return FALSE;
// No break yet
if ( pBreak != NULL ) *pBreak = 0;
DWORD s = 0, i = 0;
const char *strrep = pBegin;
if ( strrep == NULL ) strrep = "<!--$$$";
DWORD dwrep = strlen( strrep );
const char *endrep = pEnd;
if ( endrep == NULL ) endrep = "-->";
DWORD dwendrep = strlen( endrep );
// Pick up where we left off
if ( pdwBreak != NULL ) s = i = *pdwBreak;
// Lose old params
if ( params != NULL ) params->Destroy();
while ( i < dwin )
{
// Check for replace token
if ( ( dwin - i ) > dwrep &&
*(LPDWORD)&in[ i ] == *(LPDWORD)strrep &&
!strnicmp( &in[ i ], strrep, dwrep ) )
{
// Write out good data
if ( i != s )
{
*op += Write( out, *op, dwout, (LPVOID)&in[ s ], i - s );
} // end if
DWORD x = 0;
char token[ CWF_STRSIZE ];
// Skip replace flag
i += dwrep;
// Skip white space
while ( i < dwin && ( in[ i ] <= ' ' || in[ i ] > '~' ) ) i++;
if ( i >= dwin ) return TRUE;
// Copy token
while ( in[ i ] > ' ' && in[ i ] <= '~' &&
strnicmp( &in[ i ], endrep, dwendrep ) )
{ if ( x < sizeof( token ) - 1 ) token[ x++ ] = in[ i ]; i++; }
token[ x ] = 0;
DWORD p = i;
// Skip to end of replace tag
while ( i < dwin && strnicmp( &in[ i ], endrep, dwendrep ) &&
in[ i ] != 0 ) i++;
DWORD e = i;
while ( e > p && in[ e - 1 ] == ' ' ) e--;
// Read in vars if any
if ( e > p && params != NULL )
params->ReadInline( (LPBYTE)&in[ p ], e - p, sep );
// Skip end tag
if ( !strnicmp( &in[ i ], endrep, dwendrep ) ) i += dwendrep;
// Point to start of good data
s = i;
LPVAR pv = FindVar( token );
if ( pv != NULL )
{
// Check for break
if ( pBreak != NULL && pv->type == VAR_VOID )
{ if ( pdwBreak != NULL ) *pdwBreak = i;
strcpy( pBreak, token );
return TRUE;
} // end if
// Write out the replace value
else *op += Write( out, *op, dwout, pv->val, pv->size );
} // end if
else // Break
{
if ( pdwBreak != NULL ) *pdwBreak = i;
strcpy( pBreak, token );
return TRUE;
} // end else
} // end if
// Next
i++;
} // end while
// Write out what's left of the data
if ( i != s )
{
*op += Write( out, *op, dwout, (LPVOID)&in[ s ], i - s );
} // end if
// NULL terminate
out[ *op ] = 0;
return FALSE;
}
DWORD CVar::Write(LPVOID dst, DWORD ptr, DWORD max, LPVOID src, DWORD size)
{_STTEX();
// Ensure buffer space left
if ( ptr >= max ) return 0;
// How much can we copy?
if ( size > max - ptr ) size = max - ptr;
// Copy the data
memcpy( &( (LPBYTE)dst )[ ptr ], src, size );
return size;
}