-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdate_main.c
1535 lines (1201 loc) · 28.2 KB
/
update_main.c
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
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#define WIN32_LEAN_AND_MEAN
#include "q_shared.h"
#include "qcommon.h"
#include "sec_common.h"
#include <windows.h>
#include <Psapi.h>
#include <stdio.h>
#include <direct.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <mmsystem.h>
#include <Shellapi.h>
#include "blake\blake2.h"
#include "windows_inc.h"
typedef struct{
HINSTANCE reflib_library; // Handle to refresh DLL
qboolean reflib_active;
HWND hWnd; //0xcc1b6fc
HINSTANCE hInstance; //0xcc1b700
qboolean activeApp;
qboolean isMinimized; //0xcc1b708
OSVERSIONINFO osversion;
// when we get a windows message, we store the time off so keyboard processing
// can know the exact time of an event
unsigned sysMsgTime; //0xcc1b710
} WinVars_t;
#define PATH_SEP '\\'
wchar_t* FS_GetSavePath();
/*
================
Sys_Milliseconds
================
*/
int Sys_Milliseconds( void ) {
int sys_curtime;
static int sys_timeBase;
static qboolean initialized = qfalse;
if ( !initialized ) {
sys_timeBase = timeGetTime();
initialized = qtrue;
}
sys_curtime = timeGetTime() - sys_timeBase;
return sys_curtime;
}
/*
=============
Com_Error
Both client and server can use this, and it will
do the appropriate thing.
=============
*/
void Com_Error( int null, const char *fmt, ... ) {
va_list argptr;
char com_errorMessage[2048];
va_start (argptr,fmt);
Q_vsnprintf (com_errorMessage, sizeof(com_errorMessage),fmt,argptr);
va_end (argptr);
MessageBoxA(NULL, com_errorMessage, "Call of Duty 4 - Update has Failed", MB_OK | MB_ICONERROR | MB_SETFOREGROUND);
CoD4UpdateShutdown(1);
}
/*
=============
Com_ErrorUni
Both client and server can use this, and it will
do the appropriate thing.
=============
*/
void Com_ErrorUni( const wchar_t *fmt, ... ) {
va_list argptr;
wchar_t com_errorMessage[2048];
int numchar = sizeof(com_errorMessage) / sizeof(wchar_t);
va_start (argptr,fmt);
vsnwprintf (com_errorMessage, numchar, fmt, argptr );
va_end (argptr);
MessageBoxW(NULL, com_errorMessage, L"Call of Duty 4 - Update has Failed", MB_OK | MB_ICONERROR | MB_APPLMODAL);
CoD4UpdateShutdown(1);;
}
/*
=============
Com_Printf
Both client and server can use this, and it will output
to the apropriate place.
A raw string should NEVER be passed as fmt, because of "%f" type crashers.
=============
*/
void QDECL Com_Printf( const char *fmt, ... ) {
va_list argptr;
char msg[MAXPRINTMSG];
va_start (argptr,fmt);
Q_vsnprintf (msg, sizeof(msg), fmt, argptr);
va_end (argptr);
Conbuf_AppendText( msg );
}
/*
=============
Com_PrintStatus
Both client and server can use this, and it will output
to the apropriate place.
A raw string should NEVER be passed as fmt, because of "%f" type crashers.
=============
*/
void QDECL Com_PrintStatus( const char *fmt, ... ) {
va_list argptr;
char msg[MAXPRINTMSG];
va_start (argptr,fmt);
Q_vsnprintf (msg, sizeof(msg), fmt, argptr);
va_end (argptr);
Sys_SetStatusText( msg, 0 );
Conbuf_AppendText( msg );
}
/*
====================
FS_StripTrailingSeperator
Fix things up differently for win/unix/mac
====================
*/
static void FS_StripTrailingSeperator( char *path ) {
int len = strlen(path);
if(path[len -1] == PATH_SEP)
{
path[len -1] = '\0';
}
}
/*
====================
FS_StripTrailingSeperatorUni
Fix things up differently for win/unix/mac
====================
*/
static void FS_StripTrailingSeperatorUni( wchar_t *path ) {
int len = wcslen(path);
if(path[len -1] == PATH_SEPUNI)
{
path[len -1] = L'\0';
}
}
/*
====================
FS_ReplaceSeparators
Fix things up differently for win/unix/mac
====================
*/
void FS_ReplaceSeparators( char *path ) {
char *s;
for ( s = path ; *s ; s++ ) {
if ( *s == '/' || *s == '\\' ) {
*s = PATH_SEP;
}
}
}
/*
====================
FS_ReplaceSeparatorsUni
Fix things up differently for win/unix/mac
====================
*/
void FS_ReplaceSeparatorsUni( wchar_t *path ) {
wchar_t *s;
for ( s = path ; *s ; s++ ) {
if ( *s == L'/' || *s == L'\\' ) {
*s = PATH_SEPUNI;
}
}
}
void Sys_Mkdir(const char* dir)
{
_mkdir(dir);
}
void Sys_MkdirUni(const wchar_t* dir)
{
_wmkdir(dir);
}
/*
===========
FS_RemoveOSPath
===========
*/
void FS_RemoveOSPath( const char *osPath ) {
remove( osPath );
}
/*
============
FS_CreatePath
Creates any directories needed to store the given filename
============
*/
qboolean FS_CreatePath (char *OSPath) {
char *ofs;
// make absolutely sure that it can't back up the path
// FIXME: is c: allowed???
if ( strstr( OSPath, ".." ) || strstr( OSPath, "::" ) ) {
Com_Printf( "WARNING: refusing to create relative path \"%s\"\n", OSPath );
return qtrue;
}
for (ofs = OSPath+1 ; *ofs ; ofs++) {
if (*ofs == PATH_SEP) {
// create the directory
*ofs = 0;
Sys_Mkdir (OSPath);
*ofs = PATH_SEP;
}
}
return qfalse;
}
/*
=================
FS_CopyFile
Copy a fully specified file from one place to another
=================
*/
void FS_CopyFile( char *fromOSPath, char *toOSPath ) {
FILE *f;
int len;
byte *buf;
f = fopen( fromOSPath, "rb" );
if ( !f ) {
return;
}
fseek( f, 0, SEEK_END );
len = ftell( f );
fseek( f, 0, SEEK_SET );
// we are using direct malloc instead of Z_Malloc here, so it
// probably won't work on a mac... Its only for developers anyway...
buf = malloc( len );
if ( fread( buf, 1, len, f ) != len ) {
Com_Error(0, "Short read in FS_Copyfiles()\n" );
}
fclose( f );
if ( FS_CreatePath( toOSPath ) ) {
return;
}
f = fopen( toOSPath, "wb" );
if ( !f ) {
free( buf ); //DAJ free as well
return;
}
if ( fwrite( buf, 1, len, f ) != len ) {
Com_Error(0, "Short write in FS_Copyfiles()\n" );
}
fclose( f );
free( buf );
}
/*
============
FS_CreatePathUni
Creates any directories needed to store the given filename
============
*/
qboolean FS_CreatePathUni (wchar_t *OSPath) {
wchar_t *ofs;
// make absolutely sure that it can't back up the path
// FIXME: is c: allowed???
if ( wcsstr( OSPath, L".." ) || wcsstr( OSPath, L"::" ) ) {
Com_Printf( "WARNING: refusing to create relative unicode path\n" );
return qtrue;
}
if(OSPath[0] == L'\0')
{
return qtrue;
}
for (ofs = OSPath+1 ; *ofs ; ofs++) {
if (*ofs == PATH_SEPUNI) {
// create the directory
*ofs = 0;
Sys_MkdirUni(OSPath);
*ofs = PATH_SEPUNI;
}
}
return qfalse;
}
qboolean Sys_CopyFileUni(wchar_t* fromOSPath, wchar_t* toOSPath)
{
if(CopyFileW( fromOSPath, toOSPath, FALSE) == 0)
{
return qfalse;
}
return qtrue;
}
/*
=================
FS_CopyFileUni
Copy a fully specified file from one place to another
=================
*/
qboolean FS_CopyFileUni( wchar_t *fromOSPath, wchar_t *toOSPath )
{
qboolean suc;
if ( FS_CreatePathUni( toOSPath ) ) {
return qfalse;
}
suc = Sys_CopyFileUni(fromOSPath, toOSPath);
return suc;
}
/*
===========
FS_RemoveOSPathUni
===========
*/
void FS_RemoveOSPathUni( const wchar_t *osPath ) {
_wremove( osPath );
}
/*
===========
FS_RenameOSPath
===========
*/
void FS_RenameOSPath( const char *from_ospath, const char *to_ospath ) {
if (rename( from_ospath, to_ospath )) {
// Failed, try copying it and deleting the original
FS_CopyFile ( (char*)from_ospath, (char*)to_ospath );
FS_RemoveOSPath ( from_ospath );
}
}
/*
===========
FS_RenameOSPathUni
===========
*/
void FS_RenameOSPathUni( const wchar_t *from_ospath, const wchar_t *to_ospath ) {
if (_wrename( from_ospath, to_ospath )) {
// Failed, try copying it and deleting the original
FS_CopyFileUni ( (wchar_t*)from_ospath, (wchar_t*)to_ospath );
FS_RemoveOSPathUni ( from_ospath );
}
}
/*
===========
FS_FileExistsOSPath
===========
*/
qboolean FS_FileExistsOSPath( const char *osPath ) {
FILE* fh = fopen( osPath, "rb" );
if(fh == NULL)
return qfalse;
fclose( fh );
return qtrue;
}
/*
===========
FS_FileExistsOSPathUni
===========
*/
qboolean FS_FileExistsOSPathUni( const wchar_t *osPath ) {
FILE* fh = _wfopen( osPath, L"rb" );
if(fh == NULL)
return qfalse;
fclose( fh );
return qtrue;
}
/*
================
FS_filelengthOSPath
If this is called on a non-unique FILE (from a pak file),
it will return the size of the pak file, not the expected
size of the file.
================
*/
int FS_filelengthOSPath( FILE* h ) {
int pos;
int end;
pos = ftell (h);
fseek (h, 0, SEEK_END);
end = ftell (h);
fseek (h, pos, SEEK_SET);
return end;
}
/*
===========
FS_FOpenFileReadOSPathUni
search for a file somewhere below the home path, base path or cd path
we search in that order, matching FS_SV_FOpenFileRead order
===========
*/
int FS_FOpenFileReadOSPathUni( const wchar_t *filename, FILE **fp ) {
wchar_t ospath[MAX_OSPATH];
FILE* fh;
Q_strncpyzUni( ospath, filename, sizeof( ospath ) );
fh = _wfopen( ospath, L"rb" );
if ( !fh ){
*fp = NULL;
return -1;
}
*fp = fh;
return FS_filelengthOSPath(fh);
}
/*
===========
FS_FOpenFileReadOSPath
search for a file somewhere below the home path, base path or cd path
we search in that order, matching FS_SV_FOpenFileRead order
===========
*/
int FS_FOpenFileReadOSPath( const char *filename, FILE **fp ) {
char ospath[MAX_OSPATH];
FILE* fh;
Q_strncpyz( ospath, filename, sizeof( ospath ) );
fh = fopen( ospath, "rb" );
if ( !fh ){
*fp = NULL;
return -1;
}
*fp = fh;
return FS_filelengthOSPath(fh);
}
/*
==============
FS_FCloseFileOSPath
==============
*/
qboolean FS_FCloseFileOSPath( FILE* f ) {
if (f) {
fclose (f);
return qtrue;
}
return qfalse;
}
/*
=================
FS_ReadOSPath
Properly handles partial reads
=================
*/
int FS_ReadOSPath( void *buffer, int len, FILE* f ) {
int block, remaining;
int read;
byte *buf;
if ( !f ) {
return 0;
}
buf = (byte *)buffer;
remaining = len;
while (remaining) {
block = remaining;
read = fread (buf, 1, block, f);
if (read == 0)
{
return len-remaining; //Com_Error (ERR_FATAL, "FS_Read: 0 bytes read");
}
if (read == -1) {
Com_Error (0,"FS_ReadOSPath: -1 bytes read");
}
remaining -= read;
buf += read;
}
return len;
}
/*
============
FS_ReadFileOSPath
Filename are relative to the quake search path
a null buffer will just return the file length without loading
============
*/
int FS_ReadFileOSPath( const char *ospath, void **buffer ) {
byte* buf;
int len;
FILE* h;
if ( !ospath || !ospath[0] ) {
Com_Error(0, "FS_ReadFile with empty name\n" );
}
buf = NULL; // quiet compiler warning
// look for it in the filesystem or pack files
len = FS_FOpenFileReadOSPath( ospath, &h );
if ( len == -1 ) {
if ( buffer ) {
*buffer = NULL;
}
return -1;
}
if ( !buffer ) {
FS_FCloseFileOSPath( h );
return len;
}
buf = malloc(len+1);
*buffer = buf;
FS_ReadOSPath (buf, len, h);
// guarantee that it will have a trailing 0 for string operations
buf[len] = 0;
FS_FCloseFileOSPath( h );
return len;
}
void FS_BuildOSPathForThreadUni(const wchar_t *base, const char *game, const char *qpath, wchar_t *fs_path, int fs_thread)
{
wchar_t basename[MAX_OSPATH];
wchar_t gamename[MAX_OSPATH];
wchar_t qpathname[MAX_QPATH];
int len;
if ( !game || !*game )
game = "";
Q_strncpyzUni(basename, base, sizeof(basename));
Q_StrToWStr(gamename, game, sizeof(gamename));
Q_StrToWStr(qpathname, qpath, sizeof(qpathname));
len = wcslen(basename);
if(len > 0 && (basename[len -1] == L'/' || basename[len -1] == L'\\'))
{
basename[len -1] = L'\0';
}
len = wcslen(gamename);
if(len > 0 && (gamename[len -1] == L'/' || gamename[len -1] == L'\\'))
{
gamename[len -1] = L'\0';
}
if( Com_sprintfUni(fs_path, sizeof(wchar_t) * MAX_OSPATH, L"%s/%s/%s", basename, gamename, qpathname) >= MAX_OSPATH )
{
if ( fs_thread )
{
fs_path[0] = 0;
return;
}
Com_Error(ERR_FATAL, "FS_BuildOSPath: os path length exceeded");
}
FS_ReplaceSeparatorsUni(fs_path);
FS_StripTrailingSeperatorUni(fs_path);
}
void FS_BuildOSPathForThread(const char *base, const char *game, const char *qpath, char *fs_path, int fs_thread)
{
char basename[MAX_OSPATH];
char gamename[MAX_OSPATH];
int len;
if ( !game || !*game )
game = "";
Q_strncpyz(basename, base, sizeof(basename));
Q_strncpyz(gamename, game, sizeof(gamename));
len = strlen(basename);
if(len > 0 && (basename[len -1] == '/' || basename[len -1] == '\\'))
{
basename[len -1] = '\0';
}
len = strlen(gamename);
if(len > 0 && (gamename[len -1] == '/' || gamename[len -1] == '\\'))
{
gamename[len -1] = '\0';
}
if ( Com_sprintf(fs_path, MAX_OSPATH, "%s/%s/%s", basename, gamename, qpath) >= MAX_OSPATH )
{
if ( fs_thread )
{
fs_path[0] = 0;
return;
}
Com_Error(ERR_FATAL, "FS_BuildOSPath: os path length exceeded");
}
FS_ReplaceSeparators(fs_path);
FS_StripTrailingSeperator(fs_path);
}
/*
===========
FS_SV_FOpenFileWriteSavePath
===========
*/
FILE * FS_SV_FOpenFileWriteSavePath( const char *filename ) {
wchar_t ospath[MAX_OSPATH];
FILE* fh;
FS_BuildOSPathForThreadUni( FS_GetSavePath(), filename, "", ospath, 0 );
if ( FS_CreatePathUni( ospath ) ) {
return NULL;
}
// enabling the following line causes a recursive function call loop
// when running with +set logfile 1 +set developer 1
//Com_DPrintf( "writing to: %s\n", ospath );
fh = _wfopen( ospath, L"wb" );
if ( !fh ){
return NULL;
}
return fh;
}
/*
============
FS_SV_WriteFileToSavePath
Filename are reletive to the quake search path
============
*/
int FS_SV_WriteFileToSavePath( const char *qpath, const void *buffer, int size ) {
FILE* f;
if ( !qpath || !buffer ) {
Com_Error( ERR_FATAL, "FS_WriteFile: NULL parameter" );
}
f = FS_SV_FOpenFileWriteSavePath( qpath );
if ( !f ) {
Com_Printf( "Failed to open %s\n", qpath );
return 0;
}
if ( fwrite( buffer, 1, size, f ) != size ) {
Com_PrintError("Short write in FS_SV_WriteFileToSavePath()\n" );
size = 0;
}
fclose( f );
return size;
}
qboolean FS_SV_FileExists(const char *filename)
{
char ospath[1024];
FS_BuildOSPathForThread(FS_GetInstallPath(), filename, "", ospath, 0);
return FS_FileExistsOSPath( ospath );
}
void FS_SV_Rename(const char* from, const char* to)
{
char from_ospath[1024];
char to_ospath[1024];
FS_BuildOSPathForThread(FS_GetInstallPath(), from, "", from_ospath, 0);
FS_BuildOSPathForThread(FS_GetInstallPath(), to, "", to_ospath, 0);
FS_RenameOSPath( from_ospath, to_ospath );
}
void FS_SV_Remove(const char* filename)
{
char ospath[1024];
FS_BuildOSPathForThread(FS_GetInstallPath(), filename, "", ospath, 0);
FS_RemoveOSPath(ospath);
}
static int cmd_argc;
static char *cmd_argv[MAX_STRING_TOKENS]; // points into cmd_tokenized
static char cmd_tokenized[BIG_INFO_STRING + MAX_STRING_TOKENS]; // will have 0 bytes inserted
static int sv_cmd_argc;
static char *sv_cmd_argv[MAX_STRING_TOKENS]; // points into cmd_tokenized
static char sv_cmd_tokenized[BIG_INFO_STRING + MAX_STRING_TOKENS]; // will have 0 bytes inserted
/*
============
Cmd_TokenizeString
Parses the given string into command line tokens.
The text is copied to a seperate buffer and 0 characters
are inserted in the apropriate place, The argv array
will point into this temporary buffer.
============
*/
void Cmd_TokenizeString( const char *text_in ) {
const char *text;
char *textOut;
// clear previous args
cmd_argc = 0;
if ( !text_in ) {
return;
}
text = text_in;
textOut = cmd_tokenized;
while ( 1 ) {
if ( cmd_argc == MAX_STRING_TOKENS ) {
return; // this is usually something malicious
}
while ( 1 ) {
// skip whitespace
while ( *text && *text <= ' ' ) {
text++;
}
if ( !*text ) {
return; // all tokens parsed
}
// skip // comments
if ( text[0] == '/' && text[1] == '/' ) {
return; // all tokens parsed
}
// skip /* */ comments
if ( text[0] == '/' && text[1] == '*' ) {
while ( *text && ( text[0] != '*' || text[1] != '/' ) ) {
text++;
}
if ( !*text ) {
return; // all tokens parsed
}
text += 2;
} else {
break; // we are ready to parse a token
}
}
// handle quoted strings
if ( *text == '"' ) {
cmd_argv[cmd_argc] = textOut;
cmd_argc++;
text++;
while ( *text && *text != '"' ) {
*textOut++ = *text++;
}
*textOut++ = 0;
if ( !*text ) {
return; // all tokens parsed
}
text++;
continue;
}
// regular token
cmd_argv[cmd_argc] = textOut;
cmd_argc++;
// skip until whitespace, quote, or command
while ( *text > ' ' ) {
if ( text[0] == '"' ) {
break;
}
if ( text[0] == '/' && text[1] == '/' ) {
break;
}
// skip /* */ comments
if ( text[0] == '/' && text[1] == '*' ) {
break;
}
*textOut++ = *text++;
}
*textOut++ = 0;
if ( !*text ) {
return; // all tokens parsed
}
}
}
/*
============
Cmd_TokenizeString
Parses the given string into command line tokens.
The text is copied to a seperate buffer and 0 characters
are inserted in the apropriate place, The argv array
will point into this temporary buffer.
============
*/
void SV_Cmd_TokenizeString( const char *text_in ) {
const char *text;
char *textOut;
// clear previous args
sv_cmd_argc = 0;
if ( !text_in ) {
return;
}
text = text_in;
textOut = sv_cmd_tokenized;
while ( 1 ) {
if ( sv_cmd_argc == MAX_STRING_TOKENS ) {
return; // this is usually something malicious
}
while ( 1 ) {
// skip whitespace
while ( *text && *text <= ' ' ) {
text++;
}
if ( !*text ) {
return; // all tokens parsed
}
// skip // comments
if ( text[0] == '/' && text[1] == '/' ) {
return; // all tokens parsed
}
// skip /* */ comments
if ( text[0] == '/' && text[1] == '*' ) {
while ( *text && ( text[0] != '*' || text[1] != '/' ) ) {
text++;
}
if ( !*text ) {
return; // all tokens parsed
}
text += 2;
} else {
break; // we are ready to parse a token
}
}
// handle quoted strings
if ( *text == '"' ) {
sv_cmd_argv[sv_cmd_argc] = textOut;
sv_cmd_argc++;
text++;
while ( *text && *text != '"' ) {
*textOut++ = *text++;
}
*textOut++ = 0;
if ( !*text ) {
return; // all tokens parsed
}
text++;
continue;
}
// regular token
sv_cmd_argv[sv_cmd_argc] = textOut;
sv_cmd_argc++;
// skip until whitespace, quote, or command
while ( *text > ' ' ) {
if ( text[0] == '"' ) {
break;
}
if ( text[0] == '/' && text[1] == '/' ) {
break;
}
// skip /* */ comments
if ( text[0] == '/' && text[1] == '*' ) {
break;
}
*textOut++ = *text++;
}
*textOut++ = 0;
if ( !*text ) {
return; // all tokens parsed
}
}
}