cmdlib.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056
  1. /*
  2. ===========================================================================
  3. Copyright (C) 1997-2006 Id Software, Inc.
  4. This file is part of Quake 2 Tools source code.
  5. Quake 2 Tools source code is free software; you can redistribute it
  6. and/or modify it under the terms of the GNU General Public License as
  7. published by the Free Software Foundation; either version 2 of the License,
  8. or (at your option) any later version.
  9. Quake 2 Tools source code is distributed in the hope that it will be
  10. useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with Quake 2 Tools source code; if not, write to the Free Software
  15. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  16. ===========================================================================
  17. */
  18. // cmdlib.c
  19. #include "cmdlib.h"
  20. #include <sys/types.h>
  21. #include <sys/stat.h>
  22. #ifdef WIN32
  23. #include <direct.h>
  24. #endif
  25. #ifdef NeXT
  26. #include <libc.h>
  27. #endif
  28. #define BASEDIRNAME "quake2"
  29. #define PATHSEPERATOR '/'
  30. // set these before calling CheckParm
  31. int myargc;
  32. char **myargv;
  33. char com_token[1024];
  34. qboolean com_eof;
  35. qboolean archive;
  36. char archivedir[1024];
  37. /*
  38. ===================
  39. ExpandWildcards
  40. Mimic unix command line expansion
  41. ===================
  42. */
  43. #define MAX_EX_ARGC 1024
  44. int ex_argc;
  45. char *ex_argv[MAX_EX_ARGC];
  46. #ifdef _WIN32
  47. #include "io.h"
  48. void ExpandWildcards (int *argc, char ***argv)
  49. {
  50. struct _finddata_t fileinfo;
  51. int handle;
  52. int i;
  53. char filename[1024];
  54. char filebase[1024];
  55. char *path;
  56. ex_argc = 0;
  57. for (i=0 ; i<*argc ; i++)
  58. {
  59. path = (*argv)[i];
  60. if ( path[0] == '-'
  61. || ( !strstr(path, "*") && !strstr(path, "?") ) )
  62. {
  63. ex_argv[ex_argc++] = path;
  64. continue;
  65. }
  66. handle = _findfirst (path, &fileinfo);
  67. if (handle == -1)
  68. return;
  69. ExtractFilePath (path, filebase);
  70. do
  71. {
  72. sprintf (filename, "%s%s", filebase, fileinfo.name);
  73. ex_argv[ex_argc++] = copystring (filename);
  74. } while (_findnext( handle, &fileinfo ) != -1);
  75. _findclose (handle);
  76. }
  77. *argc = ex_argc;
  78. *argv = ex_argv;
  79. }
  80. #else
  81. void ExpandWildcards (int *argc, char ***argv)
  82. {
  83. }
  84. #endif
  85. #ifdef WIN_ERROR
  86. #include <windows.h>
  87. /*
  88. =================
  89. Error
  90. For abnormal program terminations in windowed apps
  91. =================
  92. */
  93. void Error (char *error, ...)
  94. {
  95. va_list argptr;
  96. char text[1024];
  97. char text2[1024];
  98. int err;
  99. err = GetLastError ();
  100. va_start (argptr,error);
  101. vsprintf (text, error,argptr);
  102. va_end (argptr);
  103. sprintf (text2, "%s\nGetLastError() = %i", text, err);
  104. MessageBox(NULL, text2, "Error", 0 /* MB_OK */ );
  105. exit (1);
  106. }
  107. #else
  108. /*
  109. =================
  110. Error
  111. For abnormal program terminations in console apps
  112. =================
  113. */
  114. void Error (char *error, ...)
  115. {
  116. va_list argptr;
  117. printf ("\n************ ERROR ************\n");
  118. va_start (argptr,error);
  119. vprintf (error,argptr);
  120. va_end (argptr);
  121. printf ("\n");
  122. exit (1);
  123. }
  124. #endif
  125. // only printf if in verbose mode
  126. qboolean verbose = false;
  127. void qprintf (char *format, ...)
  128. {
  129. va_list argptr;
  130. if (!verbose)
  131. return;
  132. va_start (argptr,format);
  133. vprintf (format,argptr);
  134. va_end (argptr);
  135. }
  136. /*
  137. qdir will hold the path up to the quake directory, including the slash
  138. f:\quake\
  139. /raid/quake/
  140. gamedir will hold qdir + the game directory (id1, id2, etc)
  141. */
  142. char qdir[1024];
  143. char gamedir[1024];
  144. void SetQdirFromPath (char *path)
  145. {
  146. char temp[1024];
  147. char *c;
  148. int len;
  149. if (!(path[0] == '/' || path[0] == '\\' || path[1] == ':'))
  150. { // path is partial
  151. Q_getwd (temp);
  152. strcat (temp, path);
  153. path = temp;
  154. }
  155. // search for "quake2" in path
  156. len = strlen(BASEDIRNAME);
  157. for (c=path+strlen(path)-1 ; c != path ; c--)
  158. if (!Q_strncasecmp (c, BASEDIRNAME, len))
  159. {
  160. strncpy (qdir, path, c+len+1-path);
  161. qprintf ("qdir: %s\n", qdir);
  162. c += len+1;
  163. while (*c)
  164. {
  165. if (*c == '/' || *c == '\\')
  166. {
  167. strncpy (gamedir, path, c+1-path);
  168. qprintf ("gamedir: %s\n", gamedir);
  169. return;
  170. }
  171. c++;
  172. }
  173. Error ("No gamedir in %s", path);
  174. return;
  175. }
  176. Error ("SetQdirFromPath: no '%s' in %s", BASEDIRNAME, path);
  177. }
  178. char *ExpandArg (char *path)
  179. {
  180. static char full[1024];
  181. if (path[0] != '/' && path[0] != '\\' && path[1] != ':')
  182. {
  183. Q_getwd (full);
  184. strcat (full, path);
  185. }
  186. else
  187. strcpy (full, path);
  188. return full;
  189. }
  190. char *ExpandPath (char *path)
  191. {
  192. static char full[1024];
  193. if (!qdir)
  194. Error ("ExpandPath called without qdir set");
  195. if (path[0] == '/' || path[0] == '\\' || path[1] == ':')
  196. return path;
  197. sprintf (full, "%s%s", qdir, path);
  198. return full;
  199. }
  200. char *ExpandPathAndArchive (char *path)
  201. {
  202. char *expanded;
  203. char archivename[1024];
  204. expanded = ExpandPath (path);
  205. if (archive)
  206. {
  207. sprintf (archivename, "%s/%s", archivedir, path);
  208. QCopyFile (expanded, archivename);
  209. }
  210. return expanded;
  211. }
  212. char *copystring(char *s)
  213. {
  214. char *b;
  215. b = malloc(strlen(s)+1);
  216. strcpy (b, s);
  217. return b;
  218. }
  219. /*
  220. ================
  221. I_FloatTime
  222. ================
  223. */
  224. double I_FloatTime (void)
  225. {
  226. time_t t;
  227. time (&t);
  228. return t;
  229. #if 0
  230. // more precise, less portable
  231. struct timeval tp;
  232. struct timezone tzp;
  233. static int secbase;
  234. gettimeofday(&tp, &tzp);
  235. if (!secbase)
  236. {
  237. secbase = tp.tv_sec;
  238. return tp.tv_usec/1000000.0;
  239. }
  240. return (tp.tv_sec - secbase) + tp.tv_usec/1000000.0;
  241. #endif
  242. }
  243. void Q_getwd (char *out)
  244. {
  245. #ifdef WIN32
  246. _getcwd (out, 256);
  247. strcat (out, "\\");
  248. #else
  249. getwd (out);
  250. strcat (out, "/");
  251. #endif
  252. }
  253. void Q_mkdir (char *path)
  254. {
  255. #ifdef WIN32
  256. if (_mkdir (path) != -1)
  257. return;
  258. #else
  259. if (mkdir (path, 0777) != -1)
  260. return;
  261. #endif
  262. if (errno != EEXIST)
  263. Error ("mkdir %s: %s",path, strerror(errno));
  264. }
  265. /*
  266. ============
  267. FileTime
  268. returns -1 if not present
  269. ============
  270. */
  271. int FileTime (char *path)
  272. {
  273. struct stat buf;
  274. if (stat (path,&buf) == -1)
  275. return -1;
  276. return buf.st_mtime;
  277. }
  278. /*
  279. ==============
  280. COM_Parse
  281. Parse a token out of a string
  282. ==============
  283. */
  284. char *COM_Parse (char *data)
  285. {
  286. int c;
  287. int len;
  288. len = 0;
  289. com_token[0] = 0;
  290. if (!data)
  291. return NULL;
  292. // skip whitespace
  293. skipwhite:
  294. while ( (c = *data) <= ' ')
  295. {
  296. if (c == 0)
  297. {
  298. com_eof = true;
  299. return NULL; // end of file;
  300. }
  301. data++;
  302. }
  303. // skip // comments
  304. if (c=='/' && data[1] == '/')
  305. {
  306. while (*data && *data != '\n')
  307. data++;
  308. goto skipwhite;
  309. }
  310. // handle quoted strings specially
  311. if (c == '\"')
  312. {
  313. data++;
  314. do
  315. {
  316. c = *data++;
  317. if (c=='\"')
  318. {
  319. com_token[len] = 0;
  320. return data;
  321. }
  322. com_token[len] = c;
  323. len++;
  324. } while (1);
  325. }
  326. // parse single characters
  327. if (c=='{' || c=='}'|| c==')'|| c=='(' || c=='\'' || c==':')
  328. {
  329. com_token[len] = c;
  330. len++;
  331. com_token[len] = 0;
  332. return data+1;
  333. }
  334. // parse a regular word
  335. do
  336. {
  337. com_token[len] = c;
  338. data++;
  339. len++;
  340. c = *data;
  341. if (c=='{' || c=='}'|| c==')'|| c=='(' || c=='\'' || c==':')
  342. break;
  343. } while (c>32);
  344. com_token[len] = 0;
  345. return data;
  346. }
  347. int Q_strncasecmp (char *s1, char *s2, int n)
  348. {
  349. int c1, c2;
  350. do
  351. {
  352. c1 = *s1++;
  353. c2 = *s2++;
  354. if (!n--)
  355. return 0; // strings are equal until end point
  356. if (c1 != c2)
  357. {
  358. if (c1 >= 'a' && c1 <= 'z')
  359. c1 -= ('a' - 'A');
  360. if (c2 >= 'a' && c2 <= 'z')
  361. c2 -= ('a' - 'A');
  362. if (c1 != c2)
  363. return -1; // strings not equal
  364. }
  365. } while (c1);
  366. return 0; // strings are equal
  367. }
  368. int Q_strcasecmp (char *s1, char *s2)
  369. {
  370. return Q_strncasecmp (s1, s2, 99999);
  371. }
  372. char *strupr (char *start)
  373. {
  374. char *in;
  375. in = start;
  376. while (*in)
  377. {
  378. *in = toupper(*in);
  379. in++;
  380. }
  381. return start;
  382. }
  383. char *strlower (char *start)
  384. {
  385. char *in;
  386. in = start;
  387. while (*in)
  388. {
  389. *in = tolower(*in);
  390. in++;
  391. }
  392. return start;
  393. }
  394. /*
  395. =============================================================================
  396. MISC FUNCTIONS
  397. =============================================================================
  398. */
  399. /*
  400. =================
  401. CheckParm
  402. Checks for the given parameter in the program's command line arguments
  403. Returns the argument number (1 to argc-1) or 0 if not present
  404. =================
  405. */
  406. int CheckParm (char *check)
  407. {
  408. int i;
  409. for (i = 1;i<myargc;i++)
  410. {
  411. if ( !Q_strcasecmp(check, myargv[i]) )
  412. return i;
  413. }
  414. return 0;
  415. }
  416. /*
  417. ================
  418. Q_filelength
  419. ================
  420. */
  421. int Q_filelength (FILE *f)
  422. {
  423. int pos;
  424. int end;
  425. pos = ftell (f);
  426. fseek (f, 0, SEEK_END);
  427. end = ftell (f);
  428. fseek (f, pos, SEEK_SET);
  429. return end;
  430. }
  431. FILE *SafeOpenWrite (char *filename)
  432. {
  433. FILE *f;
  434. f = fopen(filename, "wb");
  435. if (!f)
  436. Error ("Error opening %s: %s",filename,strerror(errno));
  437. return f;
  438. }
  439. FILE *SafeOpenRead (char *filename)
  440. {
  441. FILE *f;
  442. f = fopen(filename, "rb");
  443. if (!f)
  444. Error ("Error opening %s: %s",filename,strerror(errno));
  445. return f;
  446. }
  447. void SafeRead (FILE *f, void *buffer, int count)
  448. {
  449. if ( fread (buffer, 1, count, f) != (size_t)count)
  450. Error ("File read failure");
  451. }
  452. void SafeWrite (FILE *f, void *buffer, int count)
  453. {
  454. if (fwrite (buffer, 1, count, f) != (size_t)count)
  455. Error ("File write failure");
  456. }
  457. /*
  458. ==============
  459. FileExists
  460. ==============
  461. */
  462. qboolean FileExists (char *filename)
  463. {
  464. FILE *f;
  465. f = fopen (filename, "r");
  466. if (!f)
  467. return false;
  468. fclose (f);
  469. return true;
  470. }
  471. /*
  472. ==============
  473. LoadFile
  474. ==============
  475. */
  476. int LoadFile (char *filename, void **bufferptr)
  477. {
  478. FILE *f;
  479. int length;
  480. void *buffer;
  481. f = SafeOpenRead (filename);
  482. length = Q_filelength (f);
  483. buffer = malloc (length+1);
  484. ((char *)buffer)[length] = 0;
  485. SafeRead (f, buffer, length);
  486. fclose (f);
  487. *bufferptr = buffer;
  488. return length;
  489. }
  490. /*
  491. ==============
  492. TryLoadFile
  493. Allows failure
  494. ==============
  495. */
  496. int TryLoadFile (char *filename, void **bufferptr)
  497. {
  498. FILE *f;
  499. int length;
  500. void *buffer;
  501. *bufferptr = NULL;
  502. f = fopen (filename, "rb");
  503. if (!f)
  504. return -1;
  505. length = Q_filelength (f);
  506. buffer = malloc (length+1);
  507. ((char *)buffer)[length] = 0;
  508. SafeRead (f, buffer, length);
  509. fclose (f);
  510. *bufferptr = buffer;
  511. return length;
  512. }
  513. /*
  514. ==============
  515. SaveFile
  516. ==============
  517. */
  518. void SaveFile (char *filename, void *buffer, int count)
  519. {
  520. FILE *f;
  521. f = SafeOpenWrite (filename);
  522. SafeWrite (f, buffer, count);
  523. fclose (f);
  524. }
  525. void DefaultExtension (char *path, char *extension)
  526. {
  527. char *src;
  528. //
  529. // if path doesnt have a .EXT, append extension
  530. // (extension should include the .)
  531. //
  532. src = path + strlen(path) - 1;
  533. while (*src != PATHSEPERATOR && src != path)
  534. {
  535. if (*src == '.')
  536. return; // it has an extension
  537. src--;
  538. }
  539. strcat (path, extension);
  540. }
  541. void DefaultPath (char *path, char *basepath)
  542. {
  543. char temp[128];
  544. if (path[0] == PATHSEPERATOR)
  545. return; // absolute path location
  546. strcpy (temp,path);
  547. strcpy (path,basepath);
  548. strcat (path,temp);
  549. }
  550. void StripFilename (char *path)
  551. {
  552. int length;
  553. length = strlen(path)-1;
  554. while (length > 0 && path[length] != PATHSEPERATOR)
  555. length--;
  556. path[length] = 0;
  557. }
  558. void StripExtension (char *path)
  559. {
  560. int length;
  561. length = strlen(path)-1;
  562. while (length > 0 && path[length] != '.')
  563. {
  564. length--;
  565. if (path[length] == '/')
  566. return; // no extension
  567. }
  568. if (length)
  569. path[length] = 0;
  570. }
  571. /*
  572. ====================
  573. Extract file parts
  574. ====================
  575. */
  576. // FIXME: should include the slash, otherwise
  577. // backing to an empty path will be wrong when appending a slash
  578. void ExtractFilePath (char *path, char *dest)
  579. {
  580. char *src;
  581. src = path + strlen(path) - 1;
  582. //
  583. // back up until a \ or the start
  584. //
  585. while (src != path && *(src-1) != '\\' && *(src-1) != '/')
  586. src--;
  587. memcpy (dest, path, src-path);
  588. dest[src-path] = 0;
  589. }
  590. void ExtractFileBase (char *path, char *dest)
  591. {
  592. char *src;
  593. src = path + strlen(path) - 1;
  594. //
  595. // back up until a \ or the start
  596. //
  597. while (src != path && *(src-1) != PATHSEPERATOR)
  598. src--;
  599. while (*src && *src != '.')
  600. {
  601. *dest++ = *src++;
  602. }
  603. *dest = 0;
  604. }
  605. void ExtractFileExtension (char *path, char *dest)
  606. {
  607. char *src;
  608. src = path + strlen(path) - 1;
  609. //
  610. // back up until a . or the start
  611. //
  612. while (src != path && *(src-1) != '.')
  613. src--;
  614. if (src == path)
  615. {
  616. *dest = 0; // no extension
  617. return;
  618. }
  619. strcpy (dest,src);
  620. }
  621. /*
  622. ==============
  623. ParseNum / ParseHex
  624. ==============
  625. */
  626. int ParseHex (char *hex)
  627. {
  628. char *str;
  629. int num;
  630. num = 0;
  631. str = hex;
  632. while (*str)
  633. {
  634. num <<= 4;
  635. if (*str >= '0' && *str <= '9')
  636. num += *str-'0';
  637. else if (*str >= 'a' && *str <= 'f')
  638. num += 10 + *str-'a';
  639. else if (*str >= 'A' && *str <= 'F')
  640. num += 10 + *str-'A';
  641. else
  642. Error ("Bad hex number: %s",hex);
  643. str++;
  644. }
  645. return num;
  646. }
  647. int ParseNum (char *str)
  648. {
  649. if (str[0] == '$')
  650. return ParseHex (str+1);
  651. if (str[0] == '0' && str[1] == 'x')
  652. return ParseHex (str+2);
  653. return atol (str);
  654. }
  655. /*
  656. ============================================================================
  657. BYTE ORDER FUNCTIONS
  658. ============================================================================
  659. */
  660. #ifdef _SGI_SOURCE
  661. #define __BIG_ENDIAN__
  662. #endif
  663. #ifdef __BIG_ENDIAN__
  664. short LittleShort (short l)
  665. {
  666. byte b1,b2;
  667. b1 = l&255;
  668. b2 = (l>>8)&255;
  669. return (b1<<8) + b2;
  670. }
  671. short BigShort (short l)
  672. {
  673. return l;
  674. }
  675. int LittleLong (int l)
  676. {
  677. byte b1,b2,b3,b4;
  678. b1 = l&255;
  679. b2 = (l>>8)&255;
  680. b3 = (l>>16)&255;
  681. b4 = (l>>24)&255;
  682. return ((int)b1<<24) + ((int)b2<<16) + ((int)b3<<8) + b4;
  683. }
  684. int BigLong (int l)
  685. {
  686. return l;
  687. }
  688. float LittleFloat (float l)
  689. {
  690. union {byte b[4]; float f;} in, out;
  691. in.f = l;
  692. out.b[0] = in.b[3];
  693. out.b[1] = in.b[2];
  694. out.b[2] = in.b[1];
  695. out.b[3] = in.b[0];
  696. return out.f;
  697. }
  698. float BigFloat (float l)
  699. {
  700. return l;
  701. }
  702. #else
  703. short BigShort (short l)
  704. {
  705. byte b1,b2;
  706. b1 = l&255;
  707. b2 = (l>>8)&255;
  708. return (b1<<8) + b2;
  709. }
  710. short LittleShort (short l)
  711. {
  712. return l;
  713. }
  714. int BigLong (int l)
  715. {
  716. byte b1,b2,b3,b4;
  717. b1 = l&255;
  718. b2 = (l>>8)&255;
  719. b3 = (l>>16)&255;
  720. b4 = (l>>24)&255;
  721. return ((int)b1<<24) + ((int)b2<<16) + ((int)b3<<8) + b4;
  722. }
  723. int LittleLong (int l)
  724. {
  725. return l;
  726. }
  727. float BigFloat (float l)
  728. {
  729. union {byte b[4]; float f;} in, out;
  730. in.f = l;
  731. out.b[0] = in.b[3];
  732. out.b[1] = in.b[2];
  733. out.b[2] = in.b[1];
  734. out.b[3] = in.b[0];
  735. return out.f;
  736. }
  737. float LittleFloat (float l)
  738. {
  739. return l;
  740. }
  741. #endif
  742. //=======================================================
  743. // FIXME: byte swap?
  744. // this is a 16 bit, non-reflected CRC using the polynomial 0x1021
  745. // and the initial and final xor values shown below... in other words, the
  746. // CCITT standard CRC used by XMODEM
  747. #define CRC_INIT_VALUE 0xffff
  748. #define CRC_XOR_VALUE 0x0000
  749. static unsigned short crctable[256] =
  750. {
  751. 0x0000, 0x1021, 0x2042, 0x3063, 0x4084, 0x50a5, 0x60c6, 0x70e7,
  752. 0x8108, 0x9129, 0xa14a, 0xb16b, 0xc18c, 0xd1ad, 0xe1ce, 0xf1ef,
  753. 0x1231, 0x0210, 0x3273, 0x2252, 0x52b5, 0x4294, 0x72f7, 0x62d6,
  754. 0x9339, 0x8318, 0xb37b, 0xa35a, 0xd3bd, 0xc39c, 0xf3ff, 0xe3de,
  755. 0x2462, 0x3443, 0x0420, 0x1401, 0x64e6, 0x74c7, 0x44a4, 0x5485,
  756. 0xa56a, 0xb54b, 0x8528, 0x9509, 0xe5ee, 0xf5cf, 0xc5ac, 0xd58d,
  757. 0x3653, 0x2672, 0x1611, 0x0630, 0x76d7, 0x66f6, 0x5695, 0x46b4,
  758. 0xb75b, 0xa77a, 0x9719, 0x8738, 0xf7df, 0xe7fe, 0xd79d, 0xc7bc,
  759. 0x48c4, 0x58e5, 0x6886, 0x78a7, 0x0840, 0x1861, 0x2802, 0x3823,
  760. 0xc9cc, 0xd9ed, 0xe98e, 0xf9af, 0x8948, 0x9969, 0xa90a, 0xb92b,
  761. 0x5af5, 0x4ad4, 0x7ab7, 0x6a96, 0x1a71, 0x0a50, 0x3a33, 0x2a12,
  762. 0xdbfd, 0xcbdc, 0xfbbf, 0xeb9e, 0x9b79, 0x8b58, 0xbb3b, 0xab1a,
  763. 0x6ca6, 0x7c87, 0x4ce4, 0x5cc5, 0x2c22, 0x3c03, 0x0c60, 0x1c41,
  764. 0xedae, 0xfd8f, 0xcdec, 0xddcd, 0xad2a, 0xbd0b, 0x8d68, 0x9d49,
  765. 0x7e97, 0x6eb6, 0x5ed5, 0x4ef4, 0x3e13, 0x2e32, 0x1e51, 0x0e70,
  766. 0xff9f, 0xefbe, 0xdfdd, 0xcffc, 0xbf1b, 0xaf3a, 0x9f59, 0x8f78,
  767. 0x9188, 0x81a9, 0xb1ca, 0xa1eb, 0xd10c, 0xc12d, 0xf14e, 0xe16f,
  768. 0x1080, 0x00a1, 0x30c2, 0x20e3, 0x5004, 0x4025, 0x7046, 0x6067,
  769. 0x83b9, 0x9398, 0xa3fb, 0xb3da, 0xc33d, 0xd31c, 0xe37f, 0xf35e,
  770. 0x02b1, 0x1290, 0x22f3, 0x32d2, 0x4235, 0x5214, 0x6277, 0x7256,
  771. 0xb5ea, 0xa5cb, 0x95a8, 0x8589, 0xf56e, 0xe54f, 0xd52c, 0xc50d,
  772. 0x34e2, 0x24c3, 0x14a0, 0x0481, 0x7466, 0x6447, 0x5424, 0x4405,
  773. 0xa7db, 0xb7fa, 0x8799, 0x97b8, 0xe75f, 0xf77e, 0xc71d, 0xd73c,
  774. 0x26d3, 0x36f2, 0x0691, 0x16b0, 0x6657, 0x7676, 0x4615, 0x5634,
  775. 0xd94c, 0xc96d, 0xf90e, 0xe92f, 0x99c8, 0x89e9, 0xb98a, 0xa9ab,
  776. 0x5844, 0x4865, 0x7806, 0x6827, 0x18c0, 0x08e1, 0x3882, 0x28a3,
  777. 0xcb7d, 0xdb5c, 0xeb3f, 0xfb1e, 0x8bf9, 0x9bd8, 0xabbb, 0xbb9a,
  778. 0x4a75, 0x5a54, 0x6a37, 0x7a16, 0x0af1, 0x1ad0, 0x2ab3, 0x3a92,
  779. 0xfd2e, 0xed0f, 0xdd6c, 0xcd4d, 0xbdaa, 0xad8b, 0x9de8, 0x8dc9,
  780. 0x7c26, 0x6c07, 0x5c64, 0x4c45, 0x3ca2, 0x2c83, 0x1ce0, 0x0cc1,
  781. 0xef1f, 0xff3e, 0xcf5d, 0xdf7c, 0xaf9b, 0xbfba, 0x8fd9, 0x9ff8,
  782. 0x6e17, 0x7e36, 0x4e55, 0x5e74, 0x2e93, 0x3eb2, 0x0ed1, 0x1ef0
  783. };
  784. void CRC_Init(unsigned short *crcvalue)
  785. {
  786. *crcvalue = CRC_INIT_VALUE;
  787. }
  788. void CRC_ProcessByte(unsigned short *crcvalue, byte data)
  789. {
  790. *crcvalue = (*crcvalue << 8) ^ crctable[(*crcvalue >> 8) ^ data];
  791. }
  792. unsigned short CRC_Value(unsigned short crcvalue)
  793. {
  794. return crcvalue ^ CRC_XOR_VALUE;
  795. }
  796. //=============================================================================
  797. /*
  798. ============
  799. CreatePath
  800. ============
  801. */
  802. void CreatePath (char *path)
  803. {
  804. char *ofs, c;
  805. if (path[1] == ':')
  806. path += 2;
  807. for (ofs = path+1 ; *ofs ; ofs++)
  808. {
  809. c = *ofs;
  810. if (c == '/' || c == '\\')
  811. { // create the directory
  812. *ofs = 0;
  813. Q_mkdir (path);
  814. *ofs = c;
  815. }
  816. }
  817. }
  818. /*
  819. ============
  820. QCopyFile
  821. Used to archive source files
  822. ============
  823. */
  824. void QCopyFile (char *from, char *to)
  825. {
  826. void *buffer;
  827. int length;
  828. length = LoadFile (from, &buffer);
  829. CreatePath (to);
  830. SaveFile (to, buffer, length);
  831. free (buffer);
  832. }