00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079 #ifdef _WIN32_WCE
00080 #else
00081 #include <fcntl.h>
00082 #ifndef __MACOS__
00083 #include <sys/types.h>
00084 #include <sys/stat.h>
00085 #endif
00086 #include <errno.h>
00087 #endif
00088 #include <stdio.h>
00089 #include <string.h>
00090 #include <ctype.h>
00091
00092 #ifdef WIN32
00093 #ifndef _WIN32_WCE
00094 #include <winsock2.h>
00095 #include <aclapi.h>
00096 #endif
00097 #define SHUT_RDWR SD_BOTH
00098 #else
00099 #ifndef __MACOS__
00100 #include <sys/socket.h>
00101 #endif
00102 #include <utime.h>
00103 #include <signal.h>
00104 #ifndef SHUT_RDWR
00105 #define SHUT_RDWR 2
00106 #endif
00107 #ifndef SD_BOTH
00108 #define SD_BOTH 2
00109 #endif
00110 #endif
00111
00112 #ifdef vms
00113 #ifndef VMS
00114 #define VMS
00115 #endif
00116 #endif
00117
00118 #include "filesys.h"
00119 #include "errcodes.h"
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145 FILE *file_fopen(
00146 char *pszFileName,
00147 char *pszOpenMode
00148 ){
00149
00150
00151 #ifdef VMS
00152
00153 if( *pszOpenMode == "w" )
00154 return fopen(pszFileName,pszOpenMode,"rat=cr","rfm=var");
00155 else
00156 return fopen(pszFileName,pszOpenMode);
00157 #else
00158
00159 return fopen(pszFileName,pszOpenMode);
00160 #endif
00161 }
00162
00163
00164
00165
00166
00167
00168
00169
00170
00171 void file_fclose(FILE *fp
00172 ){
00173
00174
00175 fclose(fp);
00176 }
00177
00178
00179
00180
00181
00182
00183 long file_size(char *pszFileName
00184 ){
00185
00186
00187 struct stat buf;
00188 int LastChar,i,st;
00189
00190 i = strlen(pszFileName);
00191 if( i == 0 )return 0;
00192 #ifdef __MACOS__
00193 st = stat(pszFileName,&buf);
00194 #else
00195 i--;
00196 LastChar = pszFileName[i];
00197 if( LastChar == '/' )pszFileName[i] = (char)0;
00198 st = stat(pszFileName,&buf);
00199 if( LastChar == '/' )pszFileName[i] = LastChar;
00200 #endif
00201 if( st == -1 )return 0;
00202 return buf.st_size;
00203 }
00204
00205 #ifdef WIN32
00206
00207
00208
00209 #define MSEPOCH 116444736000000000L
00210 static void Utime2Filetime(long lTime,
00211 PFILETIME pFileTime){
00212 union myuft {
00213 LONGLONG llTime;
00214 FILETIME FT;
00215 } *p;
00216
00217 p = (union myuft *)pFileTime;
00218 p->llTime = lTime;
00219 p->llTime *= 10000000;
00220
00221 p->llTime += MSEPOCH;
00222 return;
00223 }
00224
00225 static void Filetime2Utime(long *plTime,
00226 PFILETIME pFileTime){
00227 LONGLONG llTime;
00228
00229 memcpy(&llTime,pFileTime,sizeof(llTime));
00230
00231
00232 llTime -= MSEPOCH;
00233
00234 llTime /= 10000000;
00235 *plTime = (long)llTime;
00236 return;
00237 }
00238 #endif
00239
00240
00241
00242
00243
00244
00245 long file_time_accessed(char *pszFileName
00246 ){
00247
00248
00249 #ifdef WIN32
00250 FILETIME FileTime;
00251 long lTime;
00252 HANDLE hFile;
00253 int LastChar,i;
00254
00255 i = strlen(pszFileName);
00256 if( i == 0 )return 0;
00257 i--;
00258 LastChar = pszFileName[i];
00259 if( LastChar == '/' || LastChar == '\\' )pszFileName[i] = (char)0;
00260 hFile = CreateFile(pszFileName,GENERIC_READ,0,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);
00261 if( LastChar == '/' || LastChar == '\\' )pszFileName[i] = LastChar;
00262
00263 if( hFile == INVALID_HANDLE_VALUE )return 0;
00264 if( !GetFileTime(hFile,NULL,&FileTime,NULL) ){
00265 CloseHandle(hFile);
00266 return 0;
00267 }
00268 CloseHandle(hFile);
00269 Filetime2Utime(&lTime,&FileTime);
00270 return lTime;
00271
00272 #else
00273 struct stat buf;
00274 int LastChar,i,st;
00275
00276 i = strlen(pszFileName);
00277 if( i == 0 )return 0;
00278 #ifdef __MACOS__
00279 st = stat(pszFileName,&buf);
00280 #else
00281 i--;
00282 LastChar = pszFileName[i];
00283 if( LastChar == '/' )pszFileName[i] = (char)0;
00284 st = stat(pszFileName,&buf);
00285 if( LastChar == '/' )pszFileName[i] = LastChar;
00286 #endif
00287 if( st == -1 )return 0;
00288 return buf.st_atime;
00289 #endif
00290 }
00291
00292
00293
00294
00295
00296
00297 long file_time_modified(char *pszFileName
00298 ){
00299
00300
00301 #ifdef WIN32
00302 FILETIME FileTime;
00303 unsigned long lTime;
00304 HANDLE hFile;
00305 SYSTEMTIME Syst;
00306 int LastChar,i;
00307
00308 i = strlen(pszFileName);
00309 if( i == 0 )return 0;
00310 i--;
00311 LastChar = pszFileName[i];
00312 if( LastChar == '/' || LastChar == '\\' )pszFileName[i] = (char)0;
00313 hFile = CreateFile(pszFileName,GENERIC_READ,0,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);
00314 if( LastChar == '/' || LastChar == '\\' )pszFileName[i] = LastChar;
00315
00316 if( hFile == INVALID_HANDLE_VALUE )return 0;
00317 if( !GetFileTime(hFile,NULL,NULL,&FileTime) ){
00318 CloseHandle(hFile);
00319 return 0;
00320 }
00321 CloseHandle(hFile);
00322 FileTimeToSystemTime(&FileTime,&Syst);
00323 Filetime2Utime(&lTime,&FileTime);
00324 return lTime;
00325
00326 #else
00327 struct stat buf;
00328 int LastChar,i,st;
00329
00330 i = strlen(pszFileName);
00331 if( i == 0 )return 0;
00332 #ifdef __MACOS__
00333 st = stat(pszFileName,&buf);
00334 #else
00335 i--;
00336 LastChar = pszFileName[i];
00337 if( LastChar == '/' )pszFileName[i] = (char)0;
00338 st = stat(pszFileName,&buf);
00339 if( LastChar == '/' )pszFileName[i] = LastChar;
00340 #endif
00341 if( st == -1 )return 0;
00342 return buf.st_mtime;
00343 #endif
00344 }
00345
00346
00347
00348
00349
00350
00351 long file_time_created(char *pszFileName
00352 ){
00353
00354
00355 #ifdef WIN32
00356 FILETIME FileTime;
00357 long lTime;
00358 HANDLE hFile;
00359 int LastChar,i;
00360
00361 i = strlen(pszFileName);
00362 if( i == 0 )return 0;
00363 i--;
00364 LastChar = pszFileName[i];
00365 if( LastChar == '/' || LastChar == '\\' )pszFileName[i] = (char)0;
00366 hFile = CreateFile(pszFileName,GENERIC_READ,0,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);
00367 if( LastChar == '/' || LastChar == '\\' )pszFileName[i] = LastChar;
00368
00369 if( hFile == INVALID_HANDLE_VALUE )return 0;
00370 if( !GetFileTime(hFile,&FileTime,NULL,NULL) ){
00371 CloseHandle(hFile);
00372 return 0;
00373 }
00374 CloseHandle(hFile);
00375 Filetime2Utime(&lTime,&FileTime);
00376 return lTime;
00377
00378 #else
00379 struct stat buf;
00380 int LastChar,i,st;
00381
00382 i = strlen(pszFileName);
00383 if( i == 0 )return 0;
00384 #ifdef __MACOS__
00385 st = stat(pszFileName,&buf);
00386 #else
00387 i--;
00388 LastChar = pszFileName[i];
00389 if( LastChar == '/' )pszFileName[i] = (char)0;
00390 st = stat(pszFileName,&buf);
00391 if( LastChar == '/' )pszFileName[i] = LastChar;
00392 #endif
00393 if( st == -1 )return 0;
00394 return buf.st_ctime;
00395 #endif
00396 }
00397
00398
00399
00400
00401
00402
00403 int file_isdir(char *pszFileName
00404 ){
00405
00406
00407 struct stat buf;
00408 int LastChar,i,st;
00409
00410 i = strlen(pszFileName);
00411 if( i == 0 )return 0;
00412 #ifdef __MACOS__
00413 st = stat(pszFileName,&buf);
00414 #else
00415 i--;
00416 LastChar = pszFileName[i];
00417 if( LastChar == '/' || LastChar == '\\' )pszFileName[i] = (char)0;
00418 st = stat(pszFileName,&buf);
00419 if( LastChar == '/' || LastChar == '\\' )pszFileName[i] = LastChar;
00420 #endif
00421 if( st == -1 )return 0;
00422 return (buf.st_mode&S_IFDIR) ? -1 : 0;
00423 }
00424
00425
00426
00427
00428
00429
00430 int file_isreg(char *pszFileName
00431 ){
00432
00433
00434 struct stat buf;
00435 int LastChar,i,st;
00436
00437 i = strlen(pszFileName);
00438 if( i == 0 )return 0;
00439 #ifdef __MACOS__
00440 st = stat(pszFileName,&buf);
00441 #else
00442 i--;
00443 LastChar = pszFileName[i];
00444 if( LastChar == '/' || LastChar == '\\' )pszFileName[i] = (char)0;
00445 st = stat(pszFileName,&buf);
00446 if( LastChar == '/' || LastChar == '\\' )pszFileName[i] = LastChar;
00447 #endif
00448 if( st == -1 )return 0;
00449 return (buf.st_mode&S_IFREG) ? -1 : 0;
00450 }
00451
00452
00453
00454
00455
00456
00457 int file_exists(char *pszFileName
00458 ){
00459
00460
00461 struct stat buf;
00462 int LastChar,i,st;
00463
00464 i = strlen(pszFileName);
00465 if( i == 0 )return 0;
00466 #ifdef __MACOS__
00467 st = stat(pszFileName,&buf);
00468 #else
00469 i--;
00470 LastChar = pszFileName[i];
00471 if( LastChar == '/' || LastChar == '\\' )pszFileName[i] = (char)0;
00472 st = stat(pszFileName,&buf);
00473 if( LastChar == '/' || LastChar == '\\' )pszFileName[i] = LastChar;
00474 #endif
00475 if( st == -1 )
00476 return 0;
00477 else
00478 return -1;
00479 }
00480
00481
00482
00483
00484
00485
00486
00487
00488 int file_truncate(FILE *fp,
00489 long lNewFileSize
00490 ){
00491
00492
00493 #if (defined(WIN32) || defined(__MACOS__))
00494 #if BCC32
00495 #define _chsize chsize
00496 #endif
00497 return _chsize(_fileno(fp),lNewFileSize);
00498 #else
00499 return ftruncate(fileno(fp),lNewFileSize);
00500 #endif
00501 }
00502
00503
00504
00505
00506
00507
00508
00509
00510 int file_fgetc(FILE *fp
00511 ){
00512
00513
00514 return fgetc(fp);
00515 }
00516
00517
00518
00519
00520
00521
00522
00523
00524 int file_ferror(FILE *fp
00525 ){
00526
00527
00528 return ferror(fp);
00529 }
00530
00531
00532
00533
00534
00535
00536
00537
00538 int file_fread(char *buf,
00539 int size,
00540 int count,
00541 FILE *fp
00542 ){
00543
00544
00545 return fread(buf,size,count,fp);
00546 }
00547
00548
00549
00550
00551
00552
00553
00554
00555 int file_fwrite(char *buf,
00556 int size,
00557 int count,
00558 FILE *fp
00559 ){
00560
00561
00562 return fwrite(buf,size,count,fp);
00563 }
00564
00565
00566
00567
00568
00569
00570
00571
00572 int file_fputc(int c, FILE *fp
00573 ){
00574
00575
00576 return fputc(c,fp);
00577 }
00578
00579
00580
00581
00582
00583
00584
00585
00586
00587 void file_setmode(FILE *fp,
00588 int mode
00589 ){
00590
00591
00592 #ifdef WIN32
00593 #if BCC32
00594 #define _setmode setmode
00595 #endif
00596 _setmode(_fileno(fp),mode);
00597 #endif
00598 return;
00599 }
00600
00601
00602
00603
00604
00605 void file_binmode(FILE *fp
00606 ){
00607
00608
00609 #ifdef WIN32
00610 file_setmode(fp,_O_BINARY);
00611 #endif
00612 return;
00613 }
00614
00615
00616
00617
00618
00619 void file_textmode(FILE *fp
00620 ){
00621
00622
00623 #ifdef WIN32
00624 file_setmode(fp,_O_TEXT);
00625 #endif
00626 return;
00627 }
00628
00629
00630
00631
00632
00633
00634 int file_flock(FILE *fp,
00635 int iLockType
00636 ){
00637
00638
00639 #define LK_LEN 0xffff0000
00640 #ifdef WIN32
00641 #define LK_ERR(f,i) ((f) ? (i = 0) : 0 )
00642
00643 OVERLAPPED o;
00644 int i = -1;
00645 HANDLE fh;
00646
00647 fh = (HANDLE)_get_osfhandle(_fileno(fp));
00648 memset(&o, 0, sizeof(o));
00649
00650 switch(iLockType) {
00651 case LOCK_SH:
00652 LK_ERR(LockFileEx(fh, 0, 0, LK_LEN, 0, &o),i);
00653 break;
00654 case LOCK_EX:
00655 LK_ERR(LockFileEx(fh, LOCKFILE_EXCLUSIVE_LOCK, 0, LK_LEN, 0, &o),i);
00656 break;
00657 case LOCK_SH|LOCK_NB:
00658 LK_ERR(LockFileEx(fh, LOCKFILE_FAIL_IMMEDIATELY, 0, LK_LEN, 0, &o),i);
00659 break;
00660 case LOCK_EX|LOCK_NB:
00661 LK_ERR(LockFileEx(fh,
00662 LOCKFILE_EXCLUSIVE_LOCK|LOCKFILE_FAIL_IMMEDIATELY,
00663 0, LK_LEN, 0, &o),i);
00664 break;
00665 case LOCK_UN:
00666 LK_ERR(UnlockFileEx(fh, 0, LK_LEN, 0, &o),i);
00667 break;
00668 default:
00669
00670 break;
00671 }
00672 return i;
00673 #elif defined(__MACOS__)
00674 return 0;
00675 #else
00676
00677 struct flock fl;
00678 int WaitOption;
00679
00680 switch(iLockType) {
00681 case LOCK_SH:
00682 iLockType = F_RDLCK;
00683 WaitOption = F_SETLKW;
00684 break;
00685 case LOCK_EX:
00686 iLockType = F_WRLCK;
00687 WaitOption = F_SETLKW;
00688 break;
00689 case LOCK_SH|LOCK_NB:
00690 iLockType = F_RDLCK;
00691 WaitOption = F_SETLK;
00692 break;
00693 case LOCK_EX|LOCK_NB:
00694 iLockType = F_WRLCK;
00695 WaitOption = F_SETLK;
00696 break;
00697 case LOCK_UN:
00698 iLockType = F_UNLCK;
00699 WaitOption = F_SETLKW;
00700 break;
00701 default:
00702 return -1;
00703 }
00704 fl.l_type = iLockType;
00705 fl.l_whence = SEEK_SET;
00706 fl.l_start = 0;
00707 fl.l_len = LK_LEN;
00708
00709 return fcntl( fileno( fp ), F_SETLKW , &fl );
00710
00711 #endif
00712 }
00713
00714
00715
00716
00717
00718
00719 int file_lock(FILE *fp,
00720 int iLockType,
00721 long lStart,
00722 long lLength
00723 ){
00724
00725
00726 #ifdef WIN32
00727 #undef LK_ERR
00728 #undef LK_LEN
00729 #define LK_ERR(f,i) return ((f) ? 0 : -1)
00730 #define LK_LEN 0xffff0000
00731 #define dwReserved 0L
00732 OVERLAPPED o;
00733 int i = -1;
00734 HANDLE fh;
00735
00736 fh = (HANDLE)_get_osfhandle(_fileno(fp));
00737 memset(&o, 0, sizeof(o));
00738 o.Offset = (DWORD)lStart;
00739
00740 switch(iLockType) {
00741 case LOCK_SH:
00742 LK_ERR(LockFileEx(fh, 0, dwReserved, lLength, 0, &o),i);
00743 case LOCK_EX:
00744 LK_ERR(LockFileEx(fh, LOCKFILE_EXCLUSIVE_LOCK, dwReserved, lLength, 0, &o),i);
00745 case LOCK_SH|LOCK_NB:
00746 LK_ERR(LockFileEx(fh, LOCKFILE_FAIL_IMMEDIATELY, dwReserved, lLength, 0, &o),i);
00747 case LOCK_EX|LOCK_NB:
00748 LK_ERR(LockFileEx(fh, LOCKFILE_EXCLUSIVE_LOCK|LOCKFILE_FAIL_IMMEDIATELY, dwReserved, lLength, 0, &o),i);
00749 case LOCK_UN:
00750 LK_ERR(UnlockFileEx(fh, 0, lLength, 0, &o),i);
00751 default:
00752 return -1;
00753 }
00754 #elif defined(__MACOS__)
00755 return 0;
00756 #else
00757 struct flock fl;
00758 int WaitOption;
00759
00760 switch(iLockType) {
00761 case LOCK_SH:
00762 iLockType = F_RDLCK;
00763 WaitOption = F_SETLKW;
00764 break;
00765 case LOCK_EX:
00766 iLockType = F_WRLCK;
00767 WaitOption = F_SETLKW;
00768 break;
00769 case LOCK_SH|LOCK_NB:
00770 iLockType = F_RDLCK;
00771 WaitOption = F_SETLK;
00772 break;
00773 case LOCK_EX|LOCK_NB:
00774 iLockType = F_WRLCK;
00775 WaitOption = F_SETLK;
00776 break;
00777 case LOCK_UN:
00778 iLockType = F_UNLCK;
00779 WaitOption = F_SETLKW;
00780 break;
00781 default:
00782 return -1;
00783 }
00784 fl.l_type = iLockType;
00785 fl.l_whence = SEEK_SET;
00786 fl.l_start = lStart;
00787 fl.l_len = lLength;
00788
00789 return fcntl( fileno( fp ), F_SETLKW , &fl );
00790 #endif
00791 }
00792
00793
00794
00795
00796
00797
00798
00799
00800 int file_feof(FILE *fp
00801 ){
00802
00803
00804 return feof(fp);
00805 }
00806
00807
00808
00809
00810
00811
00812
00813
00814
00815
00816
00817
00818 int file_mkdir(char *pszDirectoryName
00819 ){
00820
00821
00822 char *s;
00823
00824 #ifndef __MACOS__
00825 for( s = pszDirectoryName ; *s ; s++ )if( *s == '\\' )*s= '/';
00826 #endif
00827 #ifdef WIN32
00828 return _mkdir(pszDirectoryName);
00829 #else
00830 return mkdir(pszDirectoryName,0777);
00831 #endif
00832 }
00833
00834
00835
00836
00837
00838
00839
00840
00841
00842
00843 int file_rmdir(char *pszDirectoryName
00844 ){
00845
00846
00847 char *s;
00848
00849 #ifndef __MACOS__
00850 for( s = pszDirectoryName ; *s ; s++ )if( *s == '\\' )*s= '/';
00851 #endif
00852 #ifdef WIN32
00853
00854
00855 return _rmdir(pszDirectoryName);
00856 #else
00857 return rmdir(pszDirectoryName);
00858 #endif
00859 }
00860
00861
00862
00863
00864
00865
00866
00867
00868
00869 int file_remove(char *pszFileName
00870 ){
00871
00872
00873 char *s;
00874
00875 #ifndef __MACOS__
00876 for( s = pszFileName ; *s ; s++ )if( *s == '\\' )*s= '/';
00877 #endif
00878 return remove(pszFileName);
00879 }
00880
00881
00882
00883
00884
00885
00886 #define MAX_FNLEN 1024
00887 int file_deltree_r(char * buffer){
00888 tDIR DirList;
00889 DIR*pDirList;
00890
00891 struct dirent *pD;
00892 int dirlen;
00893
00894 dirlen=strlen(buffer);
00895 #ifdef __MACOS__
00896 if( buffer[dirlen-1] != ':' ){
00897 dirlen++;
00898 if( dirlen >= MAX_FNLEN )return -1;
00899 strcpy(buffer+dirlen-1,":");
00900 }
00901 #else
00902 if( buffer[dirlen-1] != '/' ){
00903 dirlen++;
00904 if( dirlen >= MAX_FNLEN )return -1;
00905 strcpy(buffer+dirlen-1,"/");
00906 }
00907 #endif
00908 pDirList = file_opendir(buffer,&DirList);
00909 if( pDirList == NULL )return -1;
00910 while( pD = file_readdir(pDirList) ){
00911
00912 if( pD->d_name[0] == '.' &&
00913 ( pD->d_name[1] == (char)0 ||
00914 ( pD->d_name[1] == '.' && pD->d_name[2] == (char)0 ) ) )continue;
00915 if( dirlen+strlen(pD->d_name) >= MAX_FNLEN )return -1;
00916 strcpy(buffer+dirlen,pD->d_name);
00917 if( file_isdir(buffer) )
00918 file_deltree_r(buffer);
00919 else
00920 file_remove(buffer);
00921 }
00922 file_closedir(pDirList);
00923 dirlen--;
00924 buffer[dirlen] = (char)0;
00925 return file_rmdir(buffer);
00926 }
00927
00928
00929
00930
00931
00932
00933 int file_deltree(char *pszDirectoryName
00934 ){
00935
00936
00937 char buffer[MAX_FNLEN];
00938 #ifndef __MACOS__
00939 char *s;
00940
00941 for( s = pszDirectoryName ; *s ; s++ )if( *s == '\\' )*s= '/';
00942 #endif
00943 if( ! file_exists(pszDirectoryName) )return -1;
00944 if( ! file_isdir(pszDirectoryName) )return -1;
00945
00946 if( strlen(pszDirectoryName) >= MAX_FNLEN )return -1;
00947
00948 strcpy(buffer,pszDirectoryName);
00949 return file_deltree_r(buffer);
00950 }
00951
00952
00953
00954
00955
00956
00957
00958
00959
00960
00961
00962
00963
00964
00965
00966
00967
00968
00969
00970
00971
00972
00973
00974
00975
00976
00977
00978
00979
00980 int file_MakeDirectory(char *pszDirectoryName
00981 ){
00982
00983
00984 char *s;
00985 int iDirNameLen,i,iResult;
00986
00987 #ifndef __MACOS__
00988 for( s=pszDirectoryName ; *s ; s++ )
00989 if( *s == '\\' )*s = '/';
00990 #endif
00991 iDirNameLen = strlen(pszDirectoryName);
00992
00993 i = 0;
00994 iResult = 0;
00995 while( i < iDirNameLen ){
00996 #ifdef __MACOS__
00997 while( pszDirectoryName[i] && pszDirectoryName[i] != ':' )i++;
00998 #else
00999 while( pszDirectoryName[i] && pszDirectoryName[i] != '/' )i++;
01000 #endif
01001 pszDirectoryName[i] = (char)0;
01002 if( file_exists(pszDirectoryName) ){
01003 if( ! file_isdir(pszDirectoryName) )
01004 return -1;
01005 else{
01006 iResult = 0;
01007 #ifdef __MACOS__
01008 if( i < iDirNameLen ) pszDirectoryName[i] = ':';
01009 #else
01010 if( i < iDirNameLen ) pszDirectoryName[i] = '/';
01011 #endif
01012 }
01013 }else{
01014 iResult = file_mkdir(pszDirectoryName);
01015 #ifdef __MACOS__
01016 if( i < iDirNameLen ) pszDirectoryName[i] = ':';
01017 #else
01018 if( i < iDirNameLen ) pszDirectoryName[i] = '/';
01019 #endif
01020 }
01021 i++;
01022 }
01023 return iResult;
01024 }
01025
01026
01027
01028
01029
01030
01031
01032
01033
01034
01035
01036
01037
01038
01039
01040
01041
01042
01043
01044
01045
01046
01047
01048
01049
01050
01051
01052
01053
01054
01055
01056
01057
01058
01059 DIR *file_opendir(char *pszDirectoryName,
01060 tDIR *pDirectory
01061 ){
01062
01063
01064 #ifdef WIN32
01065 if( strlen(pszDirectoryName) >= MAX_PATH - 4 )return NULL;
01066 strcpy(pDirectory->CurrentEntry.d_name,pszDirectoryName);
01067 strcat(pDirectory->CurrentEntry.d_name,"/*.*");
01068 pDirectory->hFindFile = FindFirstFile(pDirectory->CurrentEntry.d_name,&(pDirectory->FindFileData));
01069 if( pDirectory->hFindFile == INVALID_HANDLE_VALUE )return NULL;
01070 return pDirectory;
01071 #else
01072 return opendir(pszDirectoryName);
01073 #endif
01074 }
01075
01076
01077
01078
01079
01080
01081
01082
01083 struct dirent *file_readdir(DIR *pDirectory
01084 ){
01085
01086
01087 #ifdef WIN32
01088 char *s;
01089
01090 strcpy(pDirectory->CurrentEntry.d_name,pDirectory->FindFileData.cFileName);
01091 if( ! FindNextFile(pDirectory->hFindFile,&(pDirectory->FindFileData)) ){
01092 *(pDirectory->FindFileData.cFileName) = (char)0;
01093 }
01094 s = pDirectory->CurrentEntry.d_name;
01095 if( *s ){
01096 for( ; *s ; s++ )if( isupper(*s) )*s = tolower(*s);
01097 return &(pDirectory->CurrentEntry);
01098 }
01099 else
01100 return NULL;
01101 #else
01102 return readdir(pDirectory);
01103 #endif
01104 }
01105
01106
01107
01108
01109
01110
01111 void file_closedir(DIR *pDirectory
01112 ){
01113
01114
01115 #ifdef WIN32
01116 FindClose(pDirectory->hFindFile);
01117 #else
01118 closedir(pDirectory);
01119 #endif
01120 return;
01121 }
01122
01123
01124
01125
01126
01127
01128 void sys_sleep(long lSeconds
01129 ){
01130
01131
01132 #ifdef WIN32
01133 lSeconds *= 1000;
01134 Sleep((DWORD)lSeconds);
01135 #else
01136 sleep(lSeconds);
01137 #endif
01138 }
01139
01140
01141
01142
01143
01144
01145
01146
01147
01148
01149
01150
01151 int file_curdir(char *Buffer,
01152 unsigned long cbBuffer
01153 ){
01154
01155
01156 #ifdef WIN32
01157 int i;
01158
01159 i = GetCurrentDirectory(cbBuffer,Buffer);
01160 if( i == 0 )return -1;
01161 return 0;
01162 #else
01163 char *s;
01164
01165 s = getcwd(Buffer,cbBuffer);
01166 if( s == NULL )return -1;
01167 return 0;
01168 #endif
01169 }
01170
01171
01172
01173
01174
01175
01176 int file_chdir(char *Buffer
01177 ){
01178
01179
01180 #ifdef WIN32
01181 int i;
01182
01183 i = SetCurrentDirectory(Buffer);
01184 if( i == 0 )return -1;
01185 return 0;
01186 #else
01187 int i;
01188
01189 return chdir(Buffer);
01190 #endif
01191 }
01192
01193
01194
01195
01196
01197
01198
01199 #ifdef WIN32
01200 #define MAXNAMELEN 256
01201
01202 #define toUnicode(FROM,TO) \
01203 MultiByteToWideChar(CP_OEMCP, \
01204 0, \
01205 (char *)FROM, \
01206 strlen((char *)FROM)+1,\
01207 TO, \
01208 sizeof(TO))
01209
01210 #define fromUnicode(FROM,TO) \
01211 WideCharToMultiByte(CP_OEMCP, \
01212 0, \
01213 FROM, \
01214 -1, \
01215 TO, \
01216 sizeof(TO), \
01217 NULL, \
01218 NULL)
01219
01220 #define SZ_SD_BUF 100
01221
01222
01223 BOOL
01224 GetAccountSid(
01225 LPTSTR SystemName,
01226 LPTSTR AccountName,
01227 PSID *Sid
01228 )
01229 {
01230 LPTSTR ReferencedDomain=NULL;
01231 DWORD cbSid=128;
01232 DWORD cchReferencedDomain=16;
01233 SID_NAME_USE peUse;
01234 BOOL bSuccess=FALSE;
01235 #if BCC32
01236 BOOL bLeave=FALSE;
01237 #define __finally if( bLeave )
01238 #define __leave {bLeave=TRUE; break;}
01239 #define __try do
01240 #define __end_try while(0);
01241 #else
01242 #define __end_try
01243 #endif
01244 __try {
01245
01246
01247
01248
01249 *Sid = (PSID)HeapAlloc(GetProcessHeap(), 0, cbSid);
01250
01251 if(*Sid == NULL) __leave;
01252
01253 ReferencedDomain = (LPTSTR)HeapAlloc(
01254 GetProcessHeap(),
01255 0,
01256 cchReferencedDomain * sizeof(TCHAR)
01257 );
01258
01259 if(ReferencedDomain == NULL) __leave;
01260
01261
01262
01263
01264 while(!LookupAccountName(
01265 SystemName,
01266 AccountName,
01267 *Sid,
01268 &cbSid,
01269 ReferencedDomain,
01270 &cchReferencedDomain,
01271 &peUse
01272 )) {
01273 if (GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
01274
01275
01276
01277 *Sid = (PSID)HeapReAlloc(
01278 GetProcessHeap(),
01279 0,
01280 *Sid,
01281 cbSid
01282 );
01283 if(*Sid == NULL) __leave;
01284
01285 ReferencedDomain = (LPTSTR)HeapReAlloc(
01286 GetProcessHeap(),
01287 0,
01288 ReferencedDomain,
01289 cchReferencedDomain * sizeof(TCHAR)
01290 );
01291 if(ReferencedDomain == NULL) __leave;
01292 }
01293 else __leave;
01294 }
01295
01296
01297
01298
01299 bSuccess = TRUE;
01300
01301 } __end_try
01302 __finally {
01303
01304
01305
01306
01307
01308 HeapFree(GetProcessHeap(), 0, ReferencedDomain);
01309
01310 if(!bSuccess) {
01311 if(*Sid != NULL) {
01312 HeapFree(GetProcessHeap(), 0, *Sid);
01313 *Sid = NULL;
01314 }
01315 }
01316
01317 }
01318
01319 return bSuccess;
01320 }
01321
01322 BOOL
01323 GetSid(
01324 LPTSTR AccountName,
01325 PSID *Sid
01326 )
01327 {
01328
01329
01330
01331
01332
01333
01334
01335
01336
01337
01338
01339
01340
01341
01342
01343 typedef NET_API_STATUS (*tNetGetDCName)(LPWSTR,LPWSTR,LPBYTE *);
01344 static tNetGetDCName pNetGetDCName = NULL;
01345 #define NetGetDCName pNetGetDCName
01346
01347 HINSTANCE Netapi32DllInstance;
01348 PBYTE wszDomainController;
01349 UCHAR szDomainController[MAXNAMELEN];
01350 UCHAR szDomain[MAXNAMELEN];
01351 WCHAR wszDomain[MAXNAMELEN];
01352 char *s,*p;
01353
01354
01355 s = AccountName;
01356 p = szDomain;
01357 while( *s && *s != '\\' ){
01358 *p++ = *s++;
01359 }
01360 if( *s ){
01361 *p = (char)0;
01362 AccountName = s+1;
01363 }
01364 else
01365 *szDomain = (char)0;
01366 if( *szDomain ) {
01367 toUnicode(szDomain,wszDomain);
01368 if( NetGetDCName == NULL ){
01369 if( (Netapi32DllInstance = LoadLibrary("netapi32")) != NULL ){
01370 NetGetDCName = (tNetGetDCName)GetProcAddress(Netapi32DllInstance,"NetGetDCName");
01371 }
01372 }
01373 if( NetGetDCName && NetGetDCName(NULL,wszDomain,&wszDomainController) != NERR_Success )return FALSE;
01374 fromUnicode((LPWSTR)wszDomainController,szDomainController);
01375 return GetAccountSid(szDomainController,
01376 AccountName,
01377 Sid);
01378 } else {
01379 return GetAccountSid(NULL,
01380 AccountName,
01381 Sid);
01382 }
01383 }
01384
01385 BOOL SetPrivilege(
01386 HANDLE hToken,
01387 LPCTSTR Privilege,
01388 BOOL bEnablePrivilege
01389 )
01390 {
01391 TOKEN_PRIVILEGES tp;
01392 LUID luid;
01393 TOKEN_PRIVILEGES tpPrevious;
01394 DWORD cbPrevious=sizeof(TOKEN_PRIVILEGES);
01395 BOOL CloseAtEnd=FALSE;
01396
01397
01398
01399
01400 if (hToken==NULL) {
01401 if (!OpenProcessToken(GetCurrentProcess(),TOKEN_ADJUST_PRIVILEGES|TOKEN_QUERY,&hToken))return FALSE;
01402 CloseAtEnd=TRUE;
01403 }
01404
01405 if (!LookupPrivilegeValue( NULL, Privilege, &luid )) {
01406 return FALSE;
01407 }
01408
01409
01410
01411
01412 tp.PrivilegeCount = 1;
01413 tp.Privileges[0].Luid = luid;
01414 tp.Privileges[0].Attributes = 0;
01415
01416 AdjustTokenPrivileges(
01417 hToken,
01418 FALSE,
01419 &tp,
01420 sizeof(TOKEN_PRIVILEGES),
01421 &tpPrevious,
01422 &cbPrevious
01423 );
01424
01425 if (GetLastError() != ERROR_SUCCESS) {
01426 return FALSE;
01427 }
01428
01429
01430
01431
01432 tpPrevious.PrivilegeCount = 1;
01433 tpPrevious.Privileges[0].Luid = luid;
01434
01435 if(bEnablePrivilege) {
01436 tpPrevious.Privileges[0].Attributes |= (SE_PRIVILEGE_ENABLED);
01437 }
01438 else {
01439 tpPrevious.Privileges[0].Attributes ^= (SE_PRIVILEGE_ENABLED &
01440 tpPrevious.Privileges[0].Attributes);
01441 }
01442
01443 AdjustTokenPrivileges(
01444 hToken,
01445 FALSE,
01446 &tpPrevious,
01447 cbPrevious,
01448 NULL,
01449 NULL
01450 );
01451
01452 if (GetLastError() != ERROR_SUCCESS) {
01453 return FALSE;
01454 }
01455
01456
01457 if (CloseAtEnd) {
01458 CloseHandle(hToken);
01459 }
01460
01461 return TRUE;
01462 }
01463 #endif //WIN32
01464
01465
01466
01467
01468
01469
01470
01471
01472
01473
01474
01475
01476
01477
01478
01479
01480
01481
01482
01483 int file_chown(char *pszFile,
01484 char *pszOwner
01485 ){
01486
01487
01488 #ifdef WIN32
01489 UCHAR ucFileSDBuf[SZ_SD_BUF] = "";
01490 PSECURITY_DESCRIPTOR psdFileSDrel = (PSECURITY_DESCRIPTOR)&ucFileSDBuf;
01491 PSECURITY_DESCRIPTOR psdFileSDabs = NULL;
01492 PSID psidOwner;
01493
01494 if (GetVersion() & 0x80000000)return COMMAND_ERROR_CHOWN_NOT_SUPPORTED;
01495
01496 SetPrivilege(NULL,SE_TAKE_OWNERSHIP_NAME,TRUE);
01497 SetPrivilege(NULL,SE_RESTORE_NAME,TRUE);
01498 SetPrivilege(NULL,SE_BACKUP_NAME,TRUE);
01499 SetPrivilege(NULL,SE_CHANGE_NOTIFY_NAME,TRUE);
01500
01501 if (!GetSid(pszOwner,&psidOwner))return COMMAND_ERROR_CHOWN_INVALID_USER;
01502
01503 if( !InitializeSecurityDescriptor(psdFileSDrel, SECURITY_DESCRIPTOR_REVISION))
01504 return COMMAND_ERROR_CHOWN_SET_OWNER;
01505 if (!SetSecurityDescriptorOwner(psdFileSDrel,psidOwner, FALSE))
01506 return COMMAND_ERROR_CHOWN_SET_OWNER;
01507 if (!IsValidSecurityDescriptor(psdFileSDrel))
01508 return COMMAND_ERROR_CHOWN_SET_OWNER;
01509 if (!SetFileSecurity(pszFile,(SECURITY_INFORMATION)(OWNER_SECURITY_INFORMATION),psdFileSDrel))
01510 return COMMAND_ERROR_CHOWN_SET_OWNER;
01511
01512 return 0;
01513 #elif defined(__MACOS__)
01514
01515 return 0;
01516 #else
01517 struct passwd *pasw;
01518
01519 pasw = getpwnam(pszOwner);
01520 if( pasw == NULL )return COMMAND_ERROR_CHOWN_INVALID_USER;
01521 if( chown(pszFile,pasw->pw_uid,-1) )return COMMAND_ERROR_CHOWN_SET_OWNER;
01522 #endif
01523 }
01524
01525
01526
01527
01528
01529
01530 int file_getowner(char *pszFileName,
01531 char *pszOwnerBuffer,
01532 long cbOwnerBuffer
01533 ){
01534
01535
01536 #ifdef WIN32
01537 #define UNAMEMAXSIZE 256
01538
01539 PSID psid;
01540 PSECURITY_DESCRIPTOR ppSecurityDescriptor;
01541 DWORD cbName=UNAMEMAXSIZE;
01542 char Name[UNAMEMAXSIZE];
01543 char ReferencedDomainName[256];
01544 DWORD cbReferencedDomainName = 256;
01545 SID_NAME_USE peUse;
01546 int Result;
01547
01548 Result =
01549 GetNamedSecurityInfo(pszFileName,
01550 SE_FILE_OBJECT,
01551 OWNER_SECURITY_INFORMATION,
01552 &psid,
01553 NULL,
01554 NULL,
01555 NULL,
01556 &ppSecurityDescriptor);
01557
01558 if( Result )return Result;
01559
01560 Result =
01561 LookupAccountSid(NULL,
01562 psid,
01563 Name,
01564 &cbName ,
01565 ReferencedDomainName,
01566 &cbReferencedDomainName,
01567 &peUse);
01568
01569 LocalFree( ppSecurityDescriptor );
01570 if( Result ){
01571 if( (cbName=strlen(Name)) +
01572 (cbReferencedDomainName=strlen(ReferencedDomainName)) +
01573 2 <= (unsigned)cbOwnerBuffer ){
01574 if( cbReferencedDomainName ){
01575 strcpy(pszOwnerBuffer,ReferencedDomainName);
01576 strcat(pszOwnerBuffer,"\\");
01577 }else
01578 *pszOwnerBuffer = (char)0;
01579 strcat(pszOwnerBuffer,Name);
01580 }
01581 return 0;
01582 }
01583 return 1;
01584 #elif defined(__MACOS__)
01585
01586 char *owner = "Apple";
01587
01588 if( strlen(owner) < cbOwnerBuffer ){
01589 strcpy(pszOwnerBuffer,owner);
01590 return 0;
01591 }
01592 return 1;
01593 #else
01594
01595 struct stat FileState;
01596 struct passwd *pasw;
01597
01598 if( stat(pszFileName,&FileState) )return 1;
01599
01600 pasw = getpwuid(FileState.st_uid);
01601 if( strlen(pasw->pw_name) < cbOwnerBuffer ){
01602 strcpy(pszOwnerBuffer,pasw->pw_name);
01603 return 0;
01604 }
01605 return 1;
01606
01607 #endif
01608 }
01609
01610
01611 #ifdef WIN32
01612
01613
01614
01615
01616
01617 static HANDLE my_CreateFile(char *pszFile, int *fFlag){
01618 HANDLE hFile;
01619 DWORD attrib;
01620
01621 *fFlag = 0;
01622 hFile = CreateFile(pszFile,
01623 GENERIC_WRITE,
01624 FILE_SHARE_READ | FILE_SHARE_WRITE,
01625 NULL,
01626 OPEN_EXISTING,
01627 FILE_ATTRIBUTE_NORMAL | FILE_FLAG_BACKUP_SEMANTICS,
01628 0);
01629 if( hFile != INVALID_HANDLE_VALUE )return hFile;
01630
01631 attrib = GetFileAttributes(pszFile);
01632 if( attrib & FILE_ATTRIBUTE_READONLY ){
01633 *fFlag = 1;
01634
01635 SetFileAttributes(pszFile,attrib & ~FILE_ATTRIBUTE_READONLY);
01636
01637 hFile = CreateFile(pszFile,
01638 GENERIC_WRITE,
01639 FILE_SHARE_READ | FILE_SHARE_WRITE,
01640 NULL,
01641 OPEN_EXISTING,
01642 FILE_ATTRIBUTE_NORMAL | FILE_FLAG_BACKUP_SEMANTICS,
01643 0);
01644
01645 if( hFile != INVALID_HANDLE_VALUE )return hFile;
01646
01647 SetFileAttributes(pszFile,attrib);
01648 *fFlag = 0;
01649 }
01650 return hFile;
01651 }
01652
01653
01654
01655
01656
01657 static void my_CloseHandle(HANDLE hFile, char *pszFile, int *fFlag){
01658 DWORD attrib;
01659
01660 if( *fFlag ){
01661
01662
01663 attrib = GetFileAttributes(pszFile);
01664 attrib |= FILE_ATTRIBUTE_READONLY;
01665 SetFileAttributes(pszFile,attrib);
01666 }
01667 CloseHandle(hFile);
01668 }
01669 #endif
01670
01671
01672
01673
01674
01675
01676
01677
01678
01679
01680
01681
01682
01683 int file_SetCreateTime(char *pszFile,
01684 long lTime
01685 ){
01686
01687
01688 #ifdef WIN32
01689 FILETIME FileTime;
01690 HANDLE hFile;
01691 int fFlag;
01692
01693 Utime2Filetime(lTime,&FileTime);
01694 hFile = my_CreateFile(pszFile,&fFlag);
01695 if( hFile == INVALID_HANDLE_VALUE )
01696 return COMMAND_ERROR_CREATIME_FAIL;
01697 if( !SetFileTime(hFile,&FileTime,NULL,NULL) ){
01698 my_CloseHandle(hFile,pszFile,&fFlag);
01699 return COMMAND_ERROR_CREATIME_FAIL;
01700 }
01701 my_CloseHandle(hFile,pszFile,&fFlag);
01702 return 0;
01703 #else
01704 return 0;
01705 #endif
01706 }
01707
01708
01709
01710
01711
01712
01713
01714
01715
01716
01717
01718 int file_SetModifyTime(char *pszFile,
01719 long lTime
01720 ){
01721
01722
01723 #ifdef WIN32
01724 FILETIME FileTime;
01725 HANDLE hFile;
01726 int fFlag;
01727
01728 Utime2Filetime(lTime,&FileTime);
01729 hFile = my_CreateFile(pszFile,&fFlag);
01730 if( hFile == INVALID_HANDLE_VALUE )
01731 return COMMAND_ERROR_MODTIME_FAIL;
01732 if( !SetFileTime(hFile,NULL,NULL,&FileTime) ){
01733 my_CloseHandle(hFile,pszFile,&fFlag);
01734 return COMMAND_ERROR_MODTIME_FAIL;
01735 }
01736 my_CloseHandle(hFile,pszFile,&fFlag);
01737 return 0;
01738 #else
01739 struct utimbuf uTime;
01740
01741 uTime.modtime = lTime;
01742 uTime.actime = file_time_accessed(pszFile);
01743 if( utime(pszFile,&uTime) == -1 )return COMMAND_ERROR_MODTIME_FAIL;
01744 return 0;
01745 #endif
01746 }
01747
01748
01749
01750
01751
01752
01753
01754
01755
01756
01757
01758 int file_SetAccessTime(char *pszFile,
01759 long lTime
01760 ){
01761
01762
01763 #ifdef WIN32
01764 FILETIME FileTime;
01765 HANDLE hFile;
01766 int fFlag;
01767
01768 Utime2Filetime(lTime,&FileTime);
01769 hFile = my_CreateFile(pszFile,&fFlag);
01770 if( hFile == INVALID_HANDLE_VALUE )
01771 return COMMAND_ERROR_ACCTIM_FAIL;
01772 if( !SetFileTime(hFile,NULL,&FileTime,NULL) ){
01773 my_CloseHandle(hFile,pszFile,&fFlag);
01774 return COMMAND_ERROR_ACCTIM_FAIL;
01775 }
01776 my_CloseHandle(hFile,pszFile,&fFlag);
01777 return 0;
01778 #else
01779 struct utimbuf uTime;
01780
01781 uTime.actime = lTime;
01782 uTime.modtime = file_time_modified(pszFile);
01783 if( utime(pszFile,&uTime) == -1 )return COMMAND_ERROR_ACCTIM_FAIL;
01784 return 0;
01785 #endif
01786 }
01787
01788
01789 #ifdef WIN32
01790 #define WIN32SOCKETINIT WORD wVersionRequested; \
01791 WSADATA wsaData; int err; \
01792 wVersionRequested = MAKEWORD( 2, 2 ); \
01793 err = WSAStartup( wVersionRequested, &wsaData );\
01794 if ( err != 0 )return err;
01795 #else
01796 #define WIN32SOCKETINIT int err;
01797 #endif
01798
01799
01800
01801
01802
01803
01804
01805
01806
01807
01808
01809
01810 int file_gethostname(char *pszBuffer,
01811 long cbBuffer
01812 ){
01813
01814
01815
01816
01817 #ifndef __NO_SOCKETS__
01818 WIN32SOCKETINIT
01819
01820 return gethostname(pszBuffer,(int)cbBuffer);
01821 #else
01822 return 1;
01823 #endif
01824 }
01825
01826
01827
01828
01829
01830
01831
01832
01833
01834
01835
01836
01837
01838 int file_gethost(char *pszBuffer,
01839 struct hostent *pHost
01840 ){
01841
01842
01843
01844
01845
01846
01847
01848
01849
01850
01851
01852
01853
01854 #ifndef __NO_SOCKETS__
01855 int i,OctetCounter,IPnumber;
01856 unsigned char addr[4],*s;
01857 struct hostent *q;
01858 WIN32SOCKETINIT
01859
01860
01861 IPnumber = 1;
01862 addr[0] = 0;
01863 OctetCounter = 0;
01864 for( i = 0 ; pszBuffer[i] && pszBuffer[i] != ':' ; i++ ){
01865 if( pszBuffer[i] == '.' ){
01866 OctetCounter ++;
01867 if( OctetCounter > 3 ){
01868 IPnumber = 0;
01869 break;
01870 }
01871 addr[OctetCounter] = 0;
01872 continue;
01873 }
01874 if( ! isdigit(pszBuffer[i]) ){
01875 IPnumber = 0;
01876 while( pszBuffer[i] && pszBuffer[i] != ':' )i++;
01877 break;
01878 }
01879 addr[OctetCounter] = 10*addr[OctetCounter] + pszBuffer[i] - '0';
01880 }
01881
01882
01883
01884 if( IPnumber && OctetCounter == 3 ){
01885 q = gethostbyaddr(addr,4,AF_INET);
01886 if( q == NULL )return 1;
01887 memcpy(pHost,q,sizeof(struct hostent));
01888 return 0;
01889 }else{
01890
01891
01892 if( pszBuffer[i] ){
01893 s = (unsigned char *)malloc(i+2);
01894 if( s == NULL )return 1;
01895 memcpy(s,pszBuffer,i);
01896 s[i] = (char)0;
01897 q = gethostbyname(s);
01898 free(s);
01899 }else q = gethostbyname(pszBuffer);
01900 if( q == NULL )return 1;
01901 memcpy(pHost,q,sizeof(struct hostent));
01902 return 0;
01903 }
01904 #else
01905 return 1;
01906 #endif
01907 }
01908
01909
01910
01911
01912
01913
01914
01915
01916
01917
01918
01919
01920
01921
01922
01923
01924
01925
01926
01927
01928 int file_tcpconnect(SOCKET *sClient,
01929 char *pszRemoteSocket
01930 ){
01931
01932
01933 #ifndef __NO_SOCKETS__
01934 unsigned long iaddr,octet;
01935 unsigned int iPort,IPnumber,OctetCounter,i;
01936 struct sockaddr_in RemoteAddress;
01937 struct hostent RemoteMachine;
01938 static unsigned long pow[4] = { 1, 256 , 256*256, 256*256*256 };
01939 WIN32SOCKETINIT
01940
01941 IPnumber = 1;
01942 iaddr = 0;
01943 octet = 0;
01944 OctetCounter = 0;
01945 for( i = 0 ; pszRemoteSocket[i] ; i++ ){
01946 if( pszRemoteSocket[i] == '.' || pszRemoteSocket[i] == ':'){
01947 iaddr = iaddr + octet * pow[OctetCounter];
01948 octet = 0;
01949 OctetCounter ++;
01950 if( OctetCounter > 4 ){
01951 IPnumber = 0;
01952 break;
01953 }
01954 if( pszRemoteSocket[i] == ':')break;
01955 continue;
01956 }
01957 if( ! isdigit(pszRemoteSocket[i]) ){
01958 IPnumber = 0;
01959 while( pszRemoteSocket[i] && pszRemoteSocket[i] != ':' )i++;
01960 break;
01961 }
01962 octet = 10*octet + pszRemoteSocket[i] - '0';
01963 }
01964
01965 while( pszRemoteSocket[i] && pszRemoteSocket[i] != ':' )i++;
01966 if( pszRemoteSocket[i] != ':' )return 1;
01967 i++;
01968 for( iPort = 0 ; pszRemoteSocket[i] ; i++ ){
01969 if( ! isdigit(pszRemoteSocket[i]) )return 1;
01970 iPort = 10*iPort + pszRemoteSocket[i] - '0';
01971 }
01972
01973
01974
01975 if( !IPnumber || OctetCounter != 4 ){
01976 if( err = file_gethost(pszRemoteSocket,&RemoteMachine) )return err;
01977 memcpy(&iaddr,RemoteMachine.h_addr,4);
01978 }
01979 #ifdef WIN32
01980 RemoteAddress.sin_addr.S_un.S_addr = iaddr;
01981 #else
01982 RemoteAddress.sin_addr.s_addr = iaddr;
01983 #endif
01984 RemoteAddress.sin_port = htons((unsigned short)iPort);
01985 RemoteAddress.sin_family = AF_INET;
01986 *sClient = socket(AF_INET,SOCK_STREAM,0);
01987 return connect((*sClient),(struct sockaddr *)&RemoteAddress,sizeof(RemoteAddress));
01988 #else
01989 return 1;
01990 #endif
01991 }
01992
01993
01994
01995
01996
01997
01998 int file_tcpsend(SOCKET sClient,
01999 char *pszBuffer,
02000 long cbBuffer,
02001 int iFlags
02002 ){
02003
02004
02005 #ifndef __NO_SOCKETS__
02006 WIN32SOCKETINIT
02007
02008 return send(sClient,pszBuffer,(int)cbBuffer,iFlags);
02009 #else
02010 return 1;
02011 #endif
02012 }
02013
02014
02015
02016
02017
02018
02019 int file_tcprecv(SOCKET sClient,
02020 char *pszBuffer,
02021 long cbBuffer,
02022 int iFlags
02023 ){
02024
02025
02026 #ifndef __NO_SOCKETS__
02027 WIN32SOCKETINIT
02028
02029 return recv(sClient,pszBuffer,(int)cbBuffer,iFlags);
02030 #else
02031 return 1;
02032 #endif
02033 }
02034
02035
02036
02037
02038
02039
02040 int file_tcpclose(SOCKET sClient
02041 ){
02042
02043
02044 #ifndef __NO_SOCKETS__
02045 WIN32SOCKETINIT
02046
02047 if( shutdown(sClient,SHUT_RDWR) )return 1;
02048 #ifdef WIN32
02049 closesocket(sClient);
02050 #else
02051 close(sClient);
02052 #endif
02053 return 0;
02054 #else
02055 return 1;
02056 #endif
02057 }
02058
02059
02060
02061
02062
02063
02064
02065
02066
02067
02068
02069 int file_killproc(long pid
02070 ){
02071
02072
02073 #ifdef WIN32
02074 HANDLE hProcess;
02075
02076 hProcess = OpenProcess(PROCESS_TERMINATE,0,(DWORD)pid);
02077 if( hProcess == NULL )return 1;
02078 if( TerminateProcess(hProcess,1) )return 0;
02079 return 1;
02080 #elif defined(__MACOS__)
02081 return 1;
02082 #else
02083 return kill( (pid_t)pid,9);
02084 #endif
02085 }
02086
02087
02088
02089
02090
02091
02092
02093
02094 typedef unsigned char des_cblock[8];
02095
02096 typedef struct des_ks_struct
02097 {
02098 union {
02099 des_cblock _;
02100
02101
02102 unsigned long pad[2];
02103 } ks;
02104 #define _ ks._
02105 } des_key_schedule[16];
02106
02107 #define DES_KEY_SZ (sizeof(des_cblock))
02108 #define DES_ENCRYPT 1
02109 #define DES_DECRYPT 0
02110
02111 #define ITERATIONS 16
02112 #define HALF_ITERATIONS 8
02113
02114 #define c2l(c,l) (l =((unsigned long)(*((c)++))) , \
02115 l|=((unsigned long)(*((c)++)))<< 8, \
02116 l|=((unsigned long)(*((c)++)))<<16, \
02117 l|=((unsigned long)(*((c)++)))<<24)
02118
02119 #define l2c(l,c) (*((c)++)=(unsigned char)(((l) )&0xff), \
02120 *((c)++)=(unsigned char)(((l)>> 8)&0xff), \
02121 *((c)++)=(unsigned char)(((l)>>16)&0xff), \
02122 *((c)++)=(unsigned char)(((l)>>24)&0xff))
02123
02124 static unsigned long SPtrans[8][64]={
02125
02126 0x00820200, 0x00020000, 0x80800000, 0x80820200,
02127 0x00800000, 0x80020200, 0x80020000, 0x80800000,
02128 0x80020200, 0x00820200, 0x00820000, 0x80000200,
02129 0x80800200, 0x00800000, 0x00000000, 0x80020000,
02130 0x00020000, 0x80000000, 0x00800200, 0x00020200,
02131 0x80820200, 0x00820000, 0x80000200, 0x00800200,
02132 0x80000000, 0x00000200, 0x00020200, 0x80820000,
02133 0x00000200, 0x80800200, 0x80820000, 0x00000000,
02134 0x00000000, 0x80820200, 0x00800200, 0x80020000,
02135 0x00820200, 0x00020000, 0x80000200, 0x00800200,
02136 0x80820000, 0x00000200, 0x00020200, 0x80800000,
02137 0x80020200, 0x80000000, 0x80800000, 0x00820000,
02138 0x80820200, 0x00020200, 0x00820000, 0x80800200,
02139 0x00800000, 0x80000200, 0x80020000, 0x00000000,
02140 0x00020000, 0x00800000, 0x80800200, 0x00820200,
02141 0x80000000, 0x80820000, 0x00000200, 0x80020200,
02142
02143 0x10042004, 0x00000000, 0x00042000, 0x10040000,
02144 0x10000004, 0x00002004, 0x10002000, 0x00042000,
02145 0x00002000, 0x10040004, 0x00000004, 0x10002000,
02146 0x00040004, 0x10042000, 0x10040000, 0x00000004,
02147 0x00040000, 0x10002004, 0x10040004, 0x00002000,
02148 0x00042004, 0x10000000, 0x00000000, 0x00040004,
02149 0x10002004, 0x00042004, 0x10042000, 0x10000004,
02150 0x10000000, 0x00040000, 0x00002004, 0x10042004,
02151 0x00040004, 0x10042000, 0x10002000, 0x00042004,
02152 0x10042004, 0x00040004, 0x10000004, 0x00000000,
02153 0x10000000, 0x00002004, 0x00040000, 0x10040004,
02154 0x00002000, 0x10000000, 0x00042004, 0x10002004,
02155 0x10042000, 0x00002000, 0x00000000, 0x10000004,
02156 0x00000004, 0x10042004, 0x00042000, 0x10040000,
02157 0x10040004, 0x00040000, 0x00002004, 0x10002000,
02158 0x10002004, 0x00000004, 0x10040000, 0x00042000,
02159
02160 0x41000000, 0x01010040, 0x00000040, 0x41000040,
02161 0x40010000, 0x01000000, 0x41000040, 0x00010040,
02162 0x01000040, 0x00010000, 0x01010000, 0x40000000,
02163 0x41010040, 0x40000040, 0x40000000, 0x41010000,
02164 0x00000000, 0x40010000, 0x01010040, 0x00000040,
02165 0x40000040, 0x41010040, 0x00010000, 0x41000000,
02166 0x41010000, 0x01000040, 0x40010040, 0x01010000,
02167 0x00010040, 0x00000000, 0x01000000, 0x40010040,
02168 0x01010040, 0x00000040, 0x40000000, 0x00010000,
02169 0x40000040, 0x40010000, 0x01010000, 0x41000040,
02170 0x00000000, 0x01010040, 0x00010040, 0x41010000,
02171 0x40010000, 0x01000000, 0x41010040, 0x40000000,
02172 0x40010040, 0x41000000, 0x01000000, 0x41010040,
02173 0x00010000, 0x01000040, 0x41000040, 0x00010040,
02174 0x01000040, 0x00000000, 0x41010000, 0x40000040,
02175 0x41000000, 0x40010040, 0x00000040, 0x01010000,
02176
02177 0x00100402, 0x04000400, 0x00000002, 0x04100402,
02178 0x00000000, 0x04100000, 0x04000402, 0x00100002,
02179 0x04100400, 0x04000002, 0x04000000, 0x00000402,
02180 0x04000002, 0x00100402, 0x00100000, 0x04000000,
02181 0x04100002, 0x00100400, 0x00000400, 0x00000002,
02182 0x00100400, 0x04000402, 0x04100000, 0x00000400,
02183 0x00000402, 0x00000000, 0x00100002, 0x04100400,
02184 0x04000400, 0x04100002, 0x04100402, 0x00100000,
02185 0x04100002, 0x00000402, 0x00100000, 0x04000002,
02186 0x00100400, 0x04000400, 0x00000002, 0x04100000,
02187 0x04000402, 0x00000000, 0x00000400, 0x00100002,
02188 0x00000000, 0x04100002, 0x04100400, 0x00000400,
02189 0x04000000, 0x04100402, 0x00100402, 0x00100000,
02190 0x04100402, 0x00000002, 0x04000400, 0x00100402,
02191 0x00100002, 0x00100400, 0x04100000, 0x04000402,
02192 0x00000402, 0x04000000, 0x04000002, 0x04100400,
02193
02194 0x02000000, 0x00004000, 0x00000100, 0x02004108,
02195 0x02004008, 0x02000100, 0x00004108, 0x02004000,
02196 0x00004000, 0x00000008, 0x02000008, 0x00004100,
02197 0x02000108, 0x02004008, 0x02004100, 0x00000000,
02198 0x00004100, 0x02000000, 0x00004008, 0x00000108,
02199 0x02000100, 0x00004108, 0x00000000, 0x02000008,
02200 0x00000008, 0x02000108, 0x02004108, 0x00004008,
02201 0x02004000, 0x00000100, 0x00000108, 0x02004100,
02202 0x02004100, 0x02000108, 0x00004008, 0x02004000,
02203 0x00004000, 0x00000008, 0x02000008, 0x02000100,
02204 0x02000000, 0x00004100, 0x02004108, 0x00000000,
02205 0x00004108, 0x02000000, 0x00000100, 0x00004008,
02206 0x02000108, 0x00000100, 0x00000000, 0x02004108,
02207 0x02004008, 0x02004100, 0x00000108, 0x00004000,
02208 0x00004100, 0x02004008, 0x02000100, 0x00000108,
02209 0x00000008, 0x00004108, 0x02004000, 0x02000008,
02210
02211 0x20000010, 0x00080010, 0x00000000, 0x20080800,
02212 0x00080010, 0x00000800, 0x20000810, 0x00080000,
02213 0x00000810, 0x20080810, 0x00080800, 0x20000000,
02214 0x20000800, 0x20000010, 0x20080000, 0x00080810,
02215 0x00080000, 0x20000810, 0x20080010, 0x00000000,
02216 0x00000800, 0x00000010, 0x20080800, 0x20080010,
02217 0x20080810, 0x20080000, 0x20000000, 0x00000810,
02218 0x00000010, 0x00080800, 0x00080810, 0x20000800,
02219 0x00000810, 0x20000000, 0x20000800, 0x00080810,
02220 0x20080800, 0x00080010, 0x00000000, 0x20000800,
02221 0x20000000, 0x00000800, 0x20080010, 0x00080000,
02222 0x00080010, 0x20080810, 0x00080800, 0x00000010,
02223 0x20080810, 0x00080800, 0x00080000, 0x20000810,
02224 0x20000010, 0x20080000, 0x00080810, 0x00000000,
02225 0x00000800, 0x20000010, 0x20000810, 0x20080800,
02226 0x20080000, 0x00000810, 0x00000010, 0x20080010,
02227
02228 0x00001000, 0x00000080, 0x00400080, 0x00400001,
02229 0x00401081, 0x00001001, 0x00001080, 0x00000000,
02230 0x00400000, 0x00400081, 0x00000081, 0x00401000,
02231 0x00000001, 0x00401080, 0x00401000, 0x00000081,
02232 0x00400081, 0x00001000, 0x00001001, 0x00401081,
02233 0x00000000, 0x00400080, 0x00400001, 0x00001080,
02234 0x00401001, 0x00001081, 0x00401080, 0x00000001,
02235 0x00001081, 0x00401001, 0x00000080, 0x00400000,
02236 0x00001081, 0x00401000, 0x00401001, 0x00000081,
02237 0x00001000, 0x00000080, 0x00400000, 0x00401001,
02238 0x00400081, 0x00001081, 0x00001080, 0x00000000,
02239 0x00000080, 0x00400001, 0x00000001, 0x00400080,
02240 0x00000000, 0x00400081, 0x00400080, 0x00001080,
02241 0x00000081, 0x00001000, 0x00401081, 0x00400000,
02242 0x00401080, 0x00000001, 0x00001001, 0x00401081,
02243 0x00400001, 0x00401080, 0x00401000, 0x00001001,
02244
02245 0x08200020, 0x08208000, 0x00008020, 0x00000000,
02246 0x08008000, 0x00200020, 0x08200000, 0x08208020,
02247 0x00000020, 0x08000000, 0x00208000, 0x00008020,
02248 0x00208020, 0x08008020, 0x08000020, 0x08200000,
02249 0x00008000, 0x00208020, 0x00200020, 0x08008000,
02250 0x08208020, 0x08000020, 0x00000000, 0x00208000,
02251 0x08000000, 0x00200000, 0x08008020, 0x08200020,
02252 0x00200000, 0x00008000, 0x08208000, 0x00000020,
02253 0x00200000, 0x00008000, 0x08000020, 0x08208020,
02254 0x00008020, 0x08000000, 0x00000000, 0x00208000,
02255 0x08200020, 0x08008020, 0x08008000, 0x00200020,
02256 0x08208000, 0x00000020, 0x00200020, 0x08008000,
02257 0x08208020, 0x00200000, 0x08200000, 0x08000020,
02258 0x00208000, 0x00008020, 0x08008020, 0x08200000,
02259 0x00000020, 0x08208000, 0x00208020, 0x00000000,
02260 0x08000000, 0x08200020, 0x00008000, 0x00208020};
02261 static unsigned long skb[8][64]={
02262
02263 0x00000000,0x00000010,0x20000000,0x20000010,
02264 0x00010000,0x00010010,0x20010000,0x20010010,
02265 0x00000800,0x00000810,0x20000800,0x20000810,
02266 0x00010800,0x00010810,0x20010800,0x20010810,
02267 0x00000020,0x00000030,0x20000020,0x20000030,
02268 0x00010020,0x00010030,0x20010020,0x20010030,
02269 0x00000820,0x00000830,0x20000820,0x20000830,
02270 0x00010820,0x00010830,0x20010820,0x20010830,
02271 0x00080000,0x00080010,0x20080000,0x20080010,
02272 0x00090000,0x00090010,0x20090000,0x20090010,
02273 0x00080800,0x00080810,0x20080800,0x20080810,
02274 0x00090800,0x00090810,0x20090800,0x20090810,
02275 0x00080020,0x00080030,0x20080020,0x20080030,
02276 0x00090020,0x00090030,0x20090020,0x20090030,
02277 0x00080820,0x00080830,0x20080820,0x20080830,
02278 0x00090820,0x00090830,0x20090820,0x20090830,
02279
02280 0x00000000,0x02000000,0x00002000,0x02002000,
02281 0x00200000,0x02200000,0x00202000,0x02202000,
02282 0x00000004,0x02000004,0x00002004,0x02002004,
02283 0x00200004,0x02200004,0x00202004,0x02202004,
02284 0x00000400,0x02000400,0x00002400,0x02002400,
02285 0x00200400,0x02200400,0x00202400,0x02202400,
02286 0x00000404,0x02000404,0x00002404,0x02002404,
02287 0x00200404,0x02200404,0x00202404,0x02202404,
02288 0x10000000,0x12000000,0x10002000,0x12002000,
02289 0x10200000,0x12200000,0x10202000,0x12202000,
02290 0x10000004,0x12000004,0x10002004,0x12002004,
02291 0x10200004,0x12200004,0x10202004,0x12202004,
02292 0x10000400,0x12000400,0x10002400,0x12002400,
02293 0x10200400,0x12200400,0x10202400,0x12202400,
02294 0x10000404,0x12000404,0x10002404,0x12002404,
02295 0x10200404,0x12200404,0x10202404,0x12202404,
02296
02297 0x00000000,0x00000001,0x00040000,0x00040001,
02298 0x01000000,0x01000001,0x01040000,0x01040001,
02299 0x00000002,0x00000003,0x00040002,0x00040003,
02300 0x01000002,0x01000003,0x01040002,0x01040003,
02301 0x00000200,0x00000201,0x00040200,0x00040201,
02302 0x01000200,0x01000201,0x01040200,0x01040201,
02303 0x00000202,0x00000203,0x00040202,0x00040203,
02304 0x01000202,0x01000203,0x01040202,0x01040203,
02305 0x08000000,0x08000001,0x08040000,0x08040001,
02306 0x09000000,0x09000001,0x09040000,0x09040001,
02307 0x08000002,0x08000003,0x08040002,0x08040003,
02308 0x09000002,0x09000003,0x09040002,0x09040003,
02309 0x08000200,0x08000201,0x08040200,0x08040201,
02310 0x09000200,0x09000201,0x09040200,0x09040201,
02311 0x08000202,0x08000203,0x08040202,0x08040203,
02312 0x09000202,0x09000203,0x09040202,0x09040203,
02313
02314 0x00000000,0x00100000,0x00000100,0x00100100,
02315 0x00000008,0x00100008,0x00000108,0x00100108,
02316 0x00001000,0x00101000,0x00001100,0x00101100,
02317 0x00001008,0x00101008,0x00001108,0x00101108,
02318 0x04000000,0x04100000,0x04000100,0x04100100,
02319 0x04000008,0x04100008,0x04000108,0x04100108,
02320 0x04001000,0x04101000,0x04001100,0x04101100,
02321 0x04001008,0x04101008,0x04001108,0x04101108,
02322 0x00020000,0x00120000,0x00020100,0x00120100,
02323 0x00020008,0x00120008,0x00020108,0x00120108,
02324 0x00021000,0x00121000,0x00021100,0x00121100,
02325 0x00021008,0x00121008,0x00021108,0x00121108,
02326 0x04020000,0x04120000,0x04020100,0x04120100,
02327 0x04020008,0x04120008,0x04020108,0x04120108,
02328 0x04021000,0x04121000,0x04021100,0x04121100,
02329 0x04021008,0x04121008,0x04021108,0x04121108,
02330
02331 0x00000000,0x10000000,0x00010000,0x10010000,
02332 0x00000004,0x10000004,0x00010004,0x10010004,
02333 0x20000000,0x30000000,0x20010000,0x30010000,
02334 0x20000004,0x30000004,0x20010004,0x30010004,
02335 0x00100000,0x10100000,0x00110000,0x10110000,
02336 0x00100004,0x10100004,0x00110004,0x10110004,
02337 0x20100000,0x30100000,0x20110000,0x30110000,
02338 0x20100004,0x30100004,0x20110004,0x30110004,
02339 0x00001000,0x10001000,0x00011000,0x10011000,
02340 0x00001004,0x10001004,0x00011004,0x10011004,
02341 0x20001000,0x30001000,0x20011000,0x30011000,
02342 0x20001004,0x30001004,0x20011004,0x30011004,
02343 0x00101000,0x10101000,0x00111000,0x10111000,
02344 0x00101004,0x10101004,0x00111004,0x10111004,
02345 0x20101000,0x30101000,0x20111000,0x30111000,
02346 0x20101004,0x30101004,0x20111004,0x30111004,
02347
02348 0x00000000,0x08000000,0x00000008,0x08000008,
02349 0x00000400,0x08000400,0x00000408,0x08000408,
02350 0x00020000,0x08020000,0x00020008,0x08020008,
02351 0x00020400,0x08020400,0x00020408,0x08020408,
02352 0x00000001,0x08000001,0x00000009,0x08000009,
02353 0x00000401,0x08000401,0x00000409,0x08000409,
02354 0x00020001,0x08020001,0x00020009,0x08020009,
02355 0x00020401,0x08020401,0x00020409,0x08020409,
02356 0x02000000,0x0A000000,0x02000008,0x0A000008,
02357 0x02000400,0x0A000400,0x02000408,0x0A000408,
02358 0x02020000,0x0A020000,0x02020008,0x0A020008,
02359 0x02020400,0x0A020400,0x02020408,0x0A020408,
02360 0x02000001,0x0A000001,0x02000009,0x0A000009,
02361 0x02000401,0x0A000401,0x02000409,0x0A000409,
02362 0x02020001,0x0A020001,0x02020009,0x0A020009,
02363 0x02020401,0x0A020401,0x02020409,0x0A020409,
02364
02365 0x00000000,0x00000100,0x00080000,0x00080100,
02366 0x01000000,0x01000100,0x01080000,0x01080100,
02367 0x00000010,0x00000110,0x00080010,0x00080110,
02368 0x01000010,0x01000110,0x01080010,0x01080110,
02369 0x00200000,0x00200100,0x00280000,0x00280100,
02370 0x01200000,0x01200100,0x01280000,0x01280100,
02371 0x00200010,0x00200110,0x00280010,0x00280110,
02372 0x01200010,0x01200110,0x01280010,0x01280110,
02373 0x00000200,0x00000300,0x00080200,0x00080300,
02374 0x01000200,0x01000300,0x01080200,0x01080300,
02375 0x00000210,0x00000310,0x00080210,0x00080310,
02376 0x01000210,0x01000310,0x01080210,0x01080310,
02377 0x00200200,0x00200300,0x00280200,0x00280300,
02378 0x01200200,0x01200300,0x01280200,0x01280300,
02379 0x00200210,0x00200310,0x00280210,0x00280310,
02380 0x01200210,0x01200310,0x01280210,0x01280310,
02381
02382 0x00000000,0x04000000,0x00040000,0x04040000,
02383 0x00000002,0x04000002,0x00040002,0x04040002,
02384 0x00002000,0x04002000,0x00042000,0x04042000,
02385 0x00002002,0x04002002,0x00042002,0x04042002,
02386 0x00000020,0x04000020,0x00040020,0x04040020,
02387 0x00000022,0x04000022,0x00040022,0x04040022,
02388 0x00002020,0x04002020,0x00042020,0x04042020,
02389 0x00002022,0x04002022,0x00042022,0x04042022,
02390 0x00000800,0x04000800,0x00040800,0x04040800,
02391 0x00000802,0x04000802,0x00040802,0x04040802,
02392 0x00002800,0x04002800,0x00042800,0x04042800,
02393 0x00002802,0x04002802,0x00042802,0x04042802,
02394 0x00000820,0x04000820,0x00040820,0x04040820,
02395 0x00000822,0x04000822,0x00040822,0x04040822,
02396 0x00002820,0x04002820,0x00042820,0x04042820,
02397 0x00002822,0x04002822,0x00042822,0x04042822,
02398 };
02399
02400
02401 #define PERM_OP(a,b,t,n,m) ((t)=((((a)>>(n))^(b))&(m)),\
02402 (b)^=(t),\
02403 (a)^=((t)<<(n)))
02404
02405 #define HPERM_OP(a,t,n,m) ((t)=((((a)<<(16-(n)))^(a))&(m)),\
02406 (a)=(a)^(t)^(t>>(16-(n))))\
02407
02408 static char shifts2[16]={0,0,1,1,1,1,1,1,0,1,1,1,1,1,1,0};
02409
02410 static int body(
02411 unsigned long *out0,
02412 unsigned long *out1,
02413 des_key_schedule ks,
02414 unsigned long Eswap0,
02415 unsigned long Eswap1);
02416
02417 static int
02418 des_set_key(des_cblock *key, des_key_schedule schedule)
02419 {
02420 register unsigned long c,d,t,s;
02421 register unsigned char *in;
02422 register unsigned long *k;
02423 register int i;
02424
02425 k=(unsigned long *)schedule;
02426 in=(unsigned char *)key;
02427
02428 c2l(in,c);
02429 c2l(in,d);
02430
02431
02432
02433
02434 PERM_OP (d,c,t,4,0x0f0f0f0f);
02435 HPERM_OP(c,t,-2,0xcccc0000);
02436 HPERM_OP(d,t,-2,0xcccc0000);
02437 PERM_OP (d,c,t,1,0x55555555);
02438 PERM_OP (c,d,t,8,0x00ff00ff);
02439 PERM_OP (d,c,t,1,0x55555555);
02440 d= (((d&0x000000ff)<<16)| (d&0x0000ff00) |
02441 ((d&0x00ff0000)>>16)|((c&0xf0000000)>>4));
02442 c&=0x0fffffff;
02443
02444 for (i=0; i<ITERATIONS; i++)
02445 {
02446 if (shifts2[i])
02447 { c=((c>>2)|(c<<26)); d=((d>>2)|(d<<26)); }
02448 else
02449 { c=((c>>1)|(c<<27)); d=((d>>1)|(d<<27)); }
02450 c&=0x0fffffff;
02451 d&=0x0fffffff;
02452
02453
02454 s= skb[0][ (c )&0x3f ]|
02455 skb[1][((c>> 6)&0x03)|((c>> 7)&0x3c)]|
02456 skb[2][((c>>13)&0x0f)|((c>>14)&0x30)]|
02457 skb[3][((c>>20)&0x01)|((c>>21)&0x06) |
02458 ((c>>22)&0x38)];
02459 t= skb[4][ (d )&0x3f ]|
02460 skb[5][((d>> 7)&0x03)|((d>> 8)&0x3c)]|
02461 skb[6][ (d>>15)&0x3f ]|
02462 skb[7][((d>>21)&0x0f)|((d>>22)&0x30)];
02463
02464
02465 *(k++)=((t<<16)|(s&0x0000ffff))&0xffffffff;
02466 s= ((s>>16)|(t&0xffff0000));
02467
02468 s=(s<<4)|(s>>28);
02469 *(k++)=s&0xffffffff;
02470 }
02471 return(0);
02472 }
02473
02474
02475
02476
02477
02478
02479
02480
02481
02482
02483 #ifdef ALT_ECB
02484 #define D_ENCRYPT(L,R,S) \
02485 v=(R^(R>>16)); \
02486 u=(v&E0); \
02487 v=(v&E1); \
02488 u=((u^(u<<16))^R^s[S ])<<2; \
02489 t=(v^(v<<16))^R^s[S+1]; \
02490 t=(t>>2)|(t<<30); \
02491 L^= \
02492 *(unsigned long *)(des_SP+0x0100+((t )&0xfc))+ \
02493 *(unsigned long *)(des_SP+0x0300+((t>> 8)&0xfc))+ \
02494 *(unsigned long *)(des_SP+0x0500+((t>>16)&0xfc))+ \
02495 *(unsigned long *)(des_SP+0x0700+((t>>24)&0xfc))+ \
02496 *(unsigned long *)(des_SP+ ((u )&0xfc))+ \
02497 *(unsigned long *)(des_SP+0x0200+((u>> 8)&0xfc))+ \
02498 *(unsigned long *)(des_SP+0x0400+((u>>16)&0xfc))+ \
02499 *(unsigned long *)(des_SP+0x0600+((u>>24)&0xfc));
02500 #else
02501 #define D_ENCRYPT(L,R,S) \
02502 v=(R^(R>>16)); \
02503 u=(v&E0); \
02504 v=(v&E1); \
02505 u=(u^(u<<16))^R^s[S ]; \
02506 t=(v^(v<<16))^R^s[S+1]; \
02507 t=(t>>4)|(t<<28); \
02508 L^= SPtrans[1][(t )&0x3f]| \
02509 SPtrans[3][(t>> 8)&0x3f]| \
02510 SPtrans[5][(t>>16)&0x3f]| \
02511 SPtrans[7][(t>>24)&0x3f]| \
02512 SPtrans[0][(u )&0x3f]| \
02513 SPtrans[2][(u>> 8)&0x3f]| \
02514 SPtrans[4][(u>>16)&0x3f]| \
02515 SPtrans[6][(u>>24)&0x3f];
02516 #endif
02517
02518 unsigned char con_salt[128]={
02519 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
02520 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
02521 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
02522 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
02523 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
02524 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,
02525 0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,
02526 0x0A,0x0B,0x05,0x06,0x07,0x08,0x09,0x0A,
02527 0x0B,0x0C,0x0D,0x0E,0x0F,0x10,0x11,0x12,
02528 0x13,0x14,0x15,0x16,0x17,0x18,0x19,0x1A,
02529 0x1B,0x1C,0x1D,0x1E,0x1F,0x20,0x21,0x22,
02530 0x23,0x24,0x25,0x20,0x21,0x22,0x23,0x24,
02531 0x25,0x26,0x27,0x28,0x29,0x2A,0x2B,0x2C,
02532 0x2D,0x2E,0x2F,0x30,0x31,0x32,0x33,0x34,
02533 0x35,0x36,0x37,0x38,0x39,0x3A,0x3B,0x3C,
02534 0x3D,0x3E,0x3F,0x00,0x00,0x00,0x00,0x00,
02535 };
02536
02537 unsigned char cov_2char[64]={
02538 0x2E,0x2F,0x30,0x31,0x32,0x33,0x34,0x35,
02539 0x36,0x37,0x38,0x39,0x41,0x42,0x43,0x44,
02540 0x45,0x46,0x47,0x48,0x49,0x4A,0x4B,0x4C,
02541 0x4D,0x4E,0x4F,0x50,0x51,0x52,0x53,0x54,
02542 0x55,0x56,0x57,0x58,0x59,0x5A,0x61,0x62,
02543 0x63,0x64,0x65,0x66,0x67,0x68,0x69,0x6A,
02544 0x6B,0x6C,0x6D,0x6E,0x6F,0x70,0x71,0x72,
02545 0x73,0x74,0x75,0x76,0x77,0x78,0x79,0x7A
02546 };
02547
02548
02549
02550
02551
02552
02553
02554
02555
02556
02557
02558
02559
02560 char *file_fcrypt(char *buf, char *salt, char *buff
02561 ){
02562
02563
02564 unsigned int i,j,x,y;
02565 unsigned long Eswap0=0,Eswap1=0;
02566 unsigned long out[2],ll;
02567 des_cblock key;
02568 des_key_schedule ks;
02569 unsigned char bb[9];
02570 unsigned char *b=bb;
02571 unsigned char c,u;
02572
02573
02574
02575
02576
02577
02578
02579
02580
02581 x=buff[0]=((salt[0] == '\0')?'A':salt[0]);
02582 Eswap0=con_salt[x];
02583 x=buff[1]=((salt[1] == '\0')?'A':salt[1]);
02584 Eswap1=con_salt[x]<<4;
02585
02586 for (i=0; i<8; i++)
02587 {
02588 c= *(buf++);
02589 if (!c) break;
02590 key[i]=(c<<1);
02591 }
02592 for (; i<8; i++)
02593 key[i]=0;
02594
02595 des_set_key((des_cblock *)(key),ks);
02596 body(&out[0],&out[1],ks,Eswap0,Eswap1);
02597
02598 ll=out[0]; l2c(ll,b);
02599 ll=out[1]; l2c(ll,b);
02600 y=0;
02601 u=0x80;
02602 bb[8]=0;
02603 for (i=2; i<13; i++)
02604 {
02605 c=0;
02606 for (j=0; j<6; j++)
02607 {
02608 c<<=1;
02609 if (bb[y] & u) c|=1;
02610 u>>=1;
02611 if (!u)
02612 {
02613 y++;
02614 u=0x80;
02615 }
02616 }
02617 buff[i]=cov_2char[c];
02618 }
02619 buff[13]='\0';
02620 return buff;
02621 }
02622
02623 static int
02624 body( unsigned long *out0,
02625 unsigned long *out1,
02626 des_key_schedule ks,
02627 unsigned long Eswap0,
02628 unsigned long Eswap1)
02629 {
02630 register unsigned long l,r,t,u,v;
02631 #ifdef ALT_ECB
02632 register unsigned char *des_SP=(unsigned char *)SPtrans;
02633 #endif
02634 register unsigned long *s;
02635 register int i,j;
02636 register unsigned long E0,E1;
02637
02638 l=0;
02639 r=0;
02640
02641 s=(unsigned long *)ks;
02642 E0=Eswap0;
02643 E1=Eswap1;
02644
02645 for (j=0; j<25; j++)
02646 {
02647 for (i=0; i<(ITERATIONS*2); i+=4)
02648 {
02649 D_ENCRYPT(l,r, i);
02650 D_ENCRYPT(r,l, i+2);
02651 }
02652 t=l;
02653 l=r;
02654 r=t;
02655 }
02656 t=r;
02657 r=(l>>1)|(l<<31);
02658 l=(t>>1)|(t<<31);
02659
02660 l&=0xffffffff;
02661 r&=0xffffffff;
02662
02663 PERM_OP(r,l,t, 1,0x55555555);
02664 PERM_OP(l,r,t, 8,0x00ff00ff);
02665 PERM_OP(r,l,t, 2,0x33333333);
02666 PERM_OP(l,r,t,16,0x0000ffff);
02667 PERM_OP(r,l,t, 4,0x0f0f0f0f);
02668
02669 *out0=l;
02670 *out1=r;
02671 return(0);
02672 }
02673
02674
02675
02676
02677
02678
02679
02680
02681
02682
02683
02684
02685
02686
02687
02688
02689
02690
02691
02692
02693
02694
02695
02696 long file_CreateProcess(char *pszCommandLine
02697 ){
02698
02699
02700 #ifdef WIN32
02701 STARTUPINFO SupInfo;
02702 PROCESS_INFORMATION ProcInfo;
02703
02704 SupInfo.cb = sizeof(SupInfo);
02705 SupInfo.lpReserved = NULL;
02706 SupInfo.lpDesktop = NULL;
02707 SupInfo.lpTitle = NULL;
02708 SupInfo.dwFlags = 0;
02709 SupInfo.cbReserved2 = 0;
02710 SupInfo.lpReserved2 = NULL;
02711
02712 if( ! CreateProcess(NULL,
02713 pszCommandLine,
02714 NULL,
02715 NULL,
02716 0,
02717 NORMAL_PRIORITY_CLASS,
02718 NULL,
02719 NULL,
02720 &SupInfo,
02721 &ProcInfo ) )return 0;
02722 CloseHandle(ProcInfo.hProcess);
02723 return ProcInfo.dwProcessId;
02724 #elif defined(__MACOS__)
02725
02726 return 0;
02727 #else
02728 char *pszMyCommandLine;
02729 char **argv;
02730 long i,argc;
02731 int ThePreviousCharacterWasSpace;
02732
02733 if( i=fork() )return i;
02734
02735
02736 argc = 1;
02737 for( i = 0 ; pszCommandLine[i] ; i++ ){
02738 if( pszCommandLine[i] == '"' ){
02739 i ++;
02740 while( ( pszCommandLine[i] ) && ( pszCommandLine[i] != '"' ) ) i++;
02741 if( pszCommandLine[i] == '"' ) i++;
02742 }
02743 if( pszCommandLine[i] == ' ' ) {
02744 argc++;
02745 i++;
02746 while( ( pszCommandLine[i] ) && ( pszCommandLine[i] == ' ' ) ) i++;
02747 }
02748 }
02749
02750 pszMyCommandLine = (char *)malloc(i+1);
02751 if( pszMyCommandLine == NULL )return 0;
02752 memcpy(pszMyCommandLine,pszCommandLine,i+1);
02753 argv = (char **)malloc((argc+1)*sizeof(char *));
02754 if( argv == NULL ){
02755 free(pszMyCommandLine);
02756 return 0;
02757 }
02758
02759
02760 ThePreviousCharacterWasSpace = 1;
02761 argc = 0;
02762 for( i = 0 ; pszMyCommandLine[i] ; i++ ){
02763 if( pszMyCommandLine[i] == '"' ) {
02764 i++;
02765 if( ThePreviousCharacterWasSpace ){
02766 ThePreviousCharacterWasSpace = 0;
02767 argv[argc++] = pszMyCommandLine+i;
02768 }
02769 while( ( pszMyCommandLine[i] ) && ( pszMyCommandLine[i] != '"' ) ) i++;
02770 if( pszMyCommandLine[i] == '"' ) pszMyCommandLine[i] = (char)0;
02771 }
02772 if( ThePreviousCharacterWasSpace ){
02773 ThePreviousCharacterWasSpace = 0;
02774 argv[argc++] = pszMyCommandLine+i;
02775 }
02776 if( pszMyCommandLine[i] == ' ' ){
02777 ThePreviousCharacterWasSpace = 1;
02778 pszMyCommandLine[i] = (char)0;
02779 }
02780 }
02781 argv[argc] = NULL;
02782
02783 execvp(argv[0],argv);
02784 exit(1);
02785 #endif
02786 }
02787
02788
02789
02790
02791
02792
02793
02794
02795
02796
02797
02798
02799
02800
02801
02802
02803
02804
02805
02806 int file_CreateProcessEx(char *pszCommandLine,
02807 long lTimeOut,
02808 unsigned long *plPid,
02809 unsigned long *plExitCode
02810 ){
02811
02812
02813
02814
02815
02816
02817
02818
02819
02820
02821
02822
02823
02824
02825
02826
02827
02828
02829
02830
02831
02832
02833
02834
02835
02836
02837
02838
02839
02840
02841
02842
02843 #ifdef WIN32
02844 STARTUPINFO SupInfo;
02845 PROCESS_INFORMATION ProcInfo;
02846 int iError;
02847 unsigned long ulExitCode;
02848
02849 SupInfo.cb = sizeof(SupInfo);
02850 SupInfo.lpReserved = NULL;
02851 SupInfo.lpDesktop = NULL;
02852 SupInfo.lpTitle = NULL;
02853 SupInfo.dwFlags = 0;
02854 SupInfo.cbReserved2 = 0;
02855 SupInfo.lpReserved2 = NULL;
02856
02857 if( ! CreateProcess(NULL,
02858 pszCommandLine,
02859 NULL,
02860 NULL,
02861 0,
02862 NORMAL_PRIORITY_CLASS,
02863 NULL,
02864 NULL,
02865 &SupInfo,
02866 &ProcInfo ) )return FILESYSE_NOTSTARTED;
02867
02868 if( plPid )*plPid = ProcInfo.dwProcessId;
02869 if( WaitForSingleObject(ProcInfo.hProcess, lTimeOut == -1 ? INFINITE : lTimeOut*1000) == WAIT_TIMEOUT ){
02870 CloseHandle(ProcInfo.hProcess);
02871 return FILESYSE_TIMEOUT;
02872 }
02873 iError = ! GetExitCodeProcess(ProcInfo.hProcess,&ulExitCode);
02874 if( plExitCode )*plExitCode = ulExitCode;
02875 CloseHandle(ProcInfo.hProcess);
02876 return iError ? FILESYSE_NOCODE : FILESYSE_SUCCESS;
02877 #elif defined(__MACOS__)
02878
02879 return 0;
02880 #else
02881 char *pszMyCommandLine;
02882 char **argv;
02883 long i,argc;
02884 int ThePreviousCharacterWasSpace;
02885 int status;
02886
02887 if( i=fork() ){
02888 if( plPid )*plPid = i;
02889 while( lTimeOut-- ){
02890 sleep(1);
02891 waitpid(i,&status,WNOHANG);
02892 if( WIFEXITED(status) ){
02893 *plExitCode = WEXITSTATUS(status);
02894 return FILESYSE_SUCCESS;
02895 }
02896 }
02897 return FILESYSE_TIMEOUT;
02898 }
02899
02900
02901 argc = 1;
02902 for( i = 0 ; pszCommandLine[i] ; i++ ){
02903 if( pszCommandLine[i] == '"' ){
02904 i ++;
02905 while( ( pszCommandLine[i] ) && ( pszCommandLine[i] != '"' ) ) i++;
02906 if( pszCommandLine[i] == '"' ) i++;
02907 }
02908 if( pszCommandLine[i] == ' ' ) {
02909 argc++;
02910 i++;
02911 while( ( pszCommandLine[i] ) && ( pszCommandLine[i] == ' ' ) ) i++;
02912 }
02913 }
02914
02915 pszMyCommandLine = (char *)malloc(i+1);
02916 if( pszMyCommandLine == NULL )return 0;
02917 memcpy(pszMyCommandLine,pszCommandLine,i+1);
02918 argv = (char **)malloc((argc+1)*sizeof(char *));
02919 if( argv == NULL ){
02920 free(pszMyCommandLine);
02921 return 0;
02922 }
02923
02924
02925 ThePreviousCharacterWasSpace = 1;
02926 argc = 0;
02927 for( i = 0 ; pszMyCommandLine[i] ; i++ ){
02928 if( pszMyCommandLine[i] == '"' ) {
02929 i++;
02930 if( ThePreviousCharacterWasSpace ){
02931 ThePreviousCharacterWasSpace = 0;
02932 argv[argc++] = pszMyCommandLine+i;
02933 }
02934 while( ( pszMyCommandLine[i] ) && ( pszMyCommandLine[i] != '"' ) ) i++;
02935 if( pszMyCommandLine[i] == '"' ) pszMyCommandLine[i] = (char)0;
02936 }
02937 if( ThePreviousCharacterWasSpace ){
02938 ThePreviousCharacterWasSpace = 0;
02939 argv[argc++] = pszMyCommandLine+i;
02940 }
02941 if( pszMyCommandLine[i] == ' ' ){
02942 ThePreviousCharacterWasSpace = 1;
02943 pszMyCommandLine[i] = (char)0;
02944 }
02945 }
02946 argv[argc] = NULL;
02947
02948 execvp(argv[0],argv);
02949 exit(1);
02950 #endif
02951 }
02952
02953
02954
02955
02956
02957
02958
02959
02960
02961
02962
02963
02964 int file_waitpid(long pid,
02965 unsigned long *plExitCode
02966 ){
02967
02968
02969 #ifdef WIN32
02970 HANDLE hProcess;
02971 unsigned long ulExitCode;
02972
02973 hProcess = OpenProcess(PROCESS_QUERY_INFORMATION,0,(DWORD)pid);
02974 if( hProcess == NULL ){
02975 *plExitCode = 0;
02976 return 1;
02977 }
02978
02979 if( ! GetExitCodeProcess(hProcess,&ulExitCode) ){
02980
02981 CloseHandle(hProcess);
02982 return FILESYSE_NOCODE;
02983 }
02984
02985 if( plExitCode )*plExitCode = ulExitCode;
02986 CloseHandle(hProcess);
02987
02988 return (ulExitCode == STILL_ACTIVE) ? 0 : 1;
02989
02990 #else
02991 int status;
02992
02993 waitpid(pid,&status,WNOHANG);
02994 if( WIFEXITED(status) ){
02995 *plExitCode = WEXITSTATUS(status);
02996 return 1;
02997 }
02998 return 0;
02999 #endif
03000 }
03001