home *** CD-ROM | disk | FTP | other *** search
/ napalm.napnet.hu / 2015-02-12.napalm.napnet.hu.tar / napalm.napnet.hu / programok / kommunikacio / mpp700.exe / mUnzip-dll.c < prev    next >
C/C++ Source or Header  |  2002-03-24  |  11KB  |  436 lines

  1. // mUnzip DLL
  2.  
  3. // inclusions
  4. #include <windows.h>
  5.  
  6. #include "..\..\structs.h"
  7. #include "..\..\decs.h"
  8.  
  9.  
  10. // macros
  11. #define VERSION            "1.0"
  12.  
  13. #define MIRCDECL        int __stdcall
  14. #define MIRCPARAMS    HWND mWnd, HWND aWnd, char *data, char *parms, BOOL show, BOOL nopause
  15.  
  16. #define WM_MCOMMAND    WM_USER + 200
  17.  
  18. #define RetErr(Msg, Desc)        { lstrcpy(data, "E_" Msg " " Desc); return 3; }
  19. #define RetOK                { lstrcpy(data, "S_OK"); return 3; }
  20.  
  21. #define case_flag(fcexp, fchar)    \
  22.     case fcexp: \
  23.         mDCL.n ## fchar ## flag = TRUE; \
  24.         break;
  25.  
  26.  
  27. // forward references
  28. BOOL mIRCExec(char *Command);
  29. BOOL mIRCSign(char *Message);
  30.  
  31. int WINAPI fPrint(LPSTR buf, unsigned long size);
  32. int WINAPI fReplace(char *filename);
  33. int WINAPI fPassword(char *p, int n, const char *m, const char *name);
  34. void WINAPI fAppMsg(unsigned long ucsize, unsigned long csiz, unsigned cfactor,
  35.     unsigned mo, unsigned dy, unsigned yr, unsigned hh, unsigned mm, char c, LPSTR filename,
  36.         LPSTR methbuf, unsigned long crc, char fCrypt);
  37. int WINAPI fCallBk(LPCSTR buf, unsigned long size);
  38.  
  39.  
  40. // global variables and structures
  41. typedef struct {
  42.     DWORD mVersion;
  43.     HWND mHwnd;
  44.     BOOL mKeep;
  45. } LOADINFO;
  46.  
  47. char *MapView, Signal[51], Rpl[10];
  48. HWND hWnd;
  49. UINT Commenting;
  50.  
  51.  
  52. // standard function replacements
  53. char *Kstrchr(char *string, char chr) {
  54.     while (*string) {
  55.         if (*string == chr) return string;
  56.         string++;
  57.     }
  58.     return NULL;
  59. }
  60.  
  61.  
  62. // now, the code
  63.  
  64. void __stdcall LoadDll(LOADINFO *li) {
  65.     li->mKeep = TRUE;
  66. }
  67.  
  68. /*
  69.     Use: Unzip -[flags] [signal] [wildmask] <zip filename> <output dir>
  70.     Specify -S to define your own signal suffix, -M to define wildmask
  71. */
  72. MIRCDECL Unzip(MIRCPARAMS) {
  73.   DCL mDCL;
  74.     USERFUNCTIONS UserFuncs;
  75.     char *p = data, *p2, *infile, *outdir, Flags[30], *Mask;
  76.     HANDLE hFile;
  77.     BOOL HasSignal = FALSE, HasMask = FALSE;
  78.     int RVal;
  79.  
  80.     hFile = CreateFileMapping(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0, 1024, "mIRC");
  81.     if (!hFile) RetErr("NOCOMM", "Can't communicate with mIRC");
  82.     MapView = (char*)MapViewOfFile(hFile, FILE_MAP_ALL_ACCESS, 0, 0, 0);
  83.     if (!MapView) RetErr("NOCOMM", "Can't communicate with mIRC");
  84.     hWnd = mWnd;
  85.  
  86.     mDCL.ExtractOnlyNewer = FALSE;    // -E
  87.     mDCL.SpaceToUnderscore = FALSE;
  88.     mDCL.PromptToOverwrite = TRUE;
  89.     mDCL.fQuiet = 0;            // -Q<n>
  90.     mDCL.ncflag = FALSE;
  91.     mDCL.ntflag = FALSE;    // -t
  92.     mDCL.nvflag = FALSE;    // -v
  93.     mDCL.nfflag = FALSE;    // -f
  94.     mDCL.nzflag = FALSE;    // -z
  95.     mDCL.ndflag = 0;            // -d = 1
  96.     mDCL.noflag = FALSE;    // -o
  97.     mDCL.naflag = FALSE;    // -a
  98.     mDCL.nZIflag = FALSE; // -Z
  99.     mDCL.C_flag = TRUE;
  100.     mDCL.fPrivilege = 1;    // ???
  101.  
  102.     ZeroMemory(&UserFuncs, sizeof(UserFuncs));
  103.     UserFuncs.print = fPrint;
  104.     UserFuncs.sound = NULL;
  105.     UserFuncs.replace = fReplace;
  106.     UserFuncs.password = fPassword;
  107.     UserFuncs.SendApplicationMessage = fAppMsg;
  108.     UserFuncs.ServCallBk = fCallBk;
  109.  
  110.     // read flags...
  111.     if (*p == '-')
  112.         for (p++; *p && *p != ' '; p++)
  113.             switch (*p) {
  114.                 case 'E':
  115.                     mDCL.ExtractOnlyNewer = TRUE;
  116.                     mDCL.PromptToOverwrite = FALSE;
  117.                     mDCL.noflag = FALSE;
  118.                     break;
  119.                 case 'o':
  120.                     mDCL.noflag = TRUE;
  121.                     mDCL.PromptToOverwrite = FALSE;
  122.                     mDCL.ExtractOnlyNewer = FALSE;
  123.                     break;
  124.                 case 'Q':
  125.                     p++;
  126.                     if ((*p == '1') || (*p == '2'))
  127.                         mDCL.fQuiet = *p - '0';
  128.                     else if ((*p == ' ') || !*p)
  129.                         RetErr("INVPARM", "Invalid parameters");
  130.                     break;
  131.                 case 'Z':
  132.                     mDCL.nZIflag = TRUE;
  133.                     break;
  134.                 case 'd':
  135.                     mDCL.ndflag = 1;
  136.                     break;
  137.                 case 'S':
  138.                     HasSignal = TRUE;
  139.                     break;
  140.                 case 'M':
  141.                     HasMask = TRUE;
  142.                     break;
  143.                 // these are macro shortcuts to simpler flags
  144.                 case_flag('t', t)
  145.                 case_flag('v', v)
  146.                 case_flag('f', f)
  147.                 case_flag('z', z)
  148.                 case_flag('a', a)
  149.             }
  150.     Commenting = mDCL.nzflag? 1 : 0;
  151.     // skip spaces
  152.     while (*p == ' ') p++;
  153.     // no more parameters? bad.
  154.     if (!*p) RetErr("MISSPARM", "Missing parameters");
  155.  
  156.     // include signal, if specified
  157.     if (HasSignal) {
  158.         p2 = strchr(p, ' ');
  159.         if (!p2) RetErr("INVPARM", "Invalid parameters");
  160.         // let's see if we won't get a buffer overflow...
  161.         if (p2 - p >= sizeof(Signal)) RetErr("INVSIGN", "Signal too long");
  162.         p2 = Signal;
  163.         while (*p != ' ') {
  164.             if ((*p == '*') || (*p == '?')) RetErr("INVSIGN", "Invalid character in signal");
  165.             *p2 = *p;
  166.             p++;
  167.             p2++;
  168.         }
  169.         // skip spaces
  170.         while (*p == ' ') p++;
  171.         // no more parameters? bad.
  172.         if (!*p) RetErr("MISSPARM", "Missing parameters");
  173.     }
  174.     else Signal[0] = '\0';
  175.  
  176.     // include mask, if specified
  177.     if (HasMask) {
  178.         p2 = strchr(p, ' ');
  179.         if (!p2) RetErr("INVPARM", "Invalid parameters");
  180.         Mask = GlobalAlloc(0, p2 - p + 1);
  181.         if (Mask) {
  182.             *p2 = Mask[0] = '\0';
  183.             lstrcpy(Mask, p);
  184.         }
  185.         p = ++p2;
  186.         // skip spaces
  187.         while (*p == ' ') p++;
  188.         // no more parameters? bad.
  189.         if (!*p) RetErr("MISSPARM", "Missing parameters");
  190.     }
  191.     else
  192.         Mask = NULL;
  193.  
  194.     // read infile now
  195.     // if the filename is surrounded by quotes...
  196.     if (*p == '"') {
  197.         // ...read until the next quote
  198.         infile = ++p;
  199.         p = Kstrchr(p, '"');
  200.         // the quote wasn't closed? bad.
  201.         if (!p) RetErr("INVPARM", "Invalid parameters");
  202.         *p = '\0';
  203.     }
  204.     else {
  205.         // ...read until next space
  206.         infile = p;
  207.         p = Kstrchr(p, ' ');
  208.         // no space? that means no third parameter, which is bad.
  209.         if (!p) RetErr("INVPARM", "Invalid parameters");
  210.         *p = '\0';
  211.     }
  212.     // go to next character...
  213.     p++;
  214.     // skip spaces
  215.     while (*p == ' ') p++;
  216.     // no more parameters? bad.
  217.     if (!*p) RetErr("MISSPARM", "Missing parameters");
  218.  
  219.     // finally, read the output directory
  220.     // if the directory is surrounded by quotes...
  221.     if (*p == '"') {
  222.         // ...read until the next quote
  223.         outdir = ++p;
  224.         p = Kstrchr(p, '"');
  225.         // the quote wasn't closed? bad.
  226.         if (!p) RetErr("INVPARM", "Invalid parameters");
  227.         *p = '\0';
  228.     }
  229.     else {
  230.         // ...read until next space
  231.         outdir = p;
  232.         p = Kstrchr(p, ' ');
  233.         // no space? that means the parameter ends here.
  234.         // if it has a space, though, stop it.
  235.         if (p) *p = '\0';
  236.     }
  237.  
  238.     //mDCL.lpszZipFN = "D:\\Temp\\K2 Collection.zip";
  239.     //mDCL.lpszExtractDir = "D:\\Temp\\K2\\";
  240.     mDCL.lpszZipFN = infile;
  241.     mDCL.lpszExtractDir = outdir;
  242.  
  243.     RVal = Wiz_SingleEntryUnzip(Mask? 1 : 0, Mask? &Mask : NULL, 0, NULL, &mDCL, &UserFuncs);
  244.     if (Mask) GlobalFree(Mask);
  245.  
  246.     UnmapViewOfFile(MapView);
  247.     CloseHandle(hFile);
  248.  
  249.     if (RVal == 80) RVal = 0; // don't consider it an error
  250.     switch (RVal) {
  251.         case 0:
  252.         case 1:
  253.         case 2:
  254.             Flags[0] = '\0';
  255.             if (UserFuncs.cchComment) lstrcat(Flags, "c");
  256.             if (RVal) lstrcat(Flags, "w"); // warnings or errors happened
  257.             if (mDCL.nvflag)
  258.                 wsprintf(data, "S_OK |+%s|%u %u %u%%|%lu", Flags,
  259.                     UserFuncs.TotalSizeComp, UserFuncs.TotalSize, UserFuncs.CompFactor,
  260.                     UserFuncs.NumMembers);
  261.             else
  262.                 wsprintf(data, "S_OK |+%s", Flags);
  263.             return 3;
  264.  
  265.         case 3:
  266.             RetErr("INVFILE", "Invalid file format");
  267.  
  268.         case 4:
  269.         case 5:
  270.         case 6:
  271.         case 7:
  272.             RetErr("NOMEM", "Not enough memory");
  273.  
  274.         case 9:
  275.             RetErr("FNF", "File not found");
  276.  
  277.         case 11:
  278.             RetErr("NOMATCH", "No matches for the specified pattern");
  279.  
  280.         case 50:
  281.             RetErr("DISKFULL", "Disk is full");
  282.  
  283.         case 51:
  284.             RetErr("SUDDENEOF", "End of file encountered prematurely");
  285.  
  286.         case 81:
  287.             RetErr("UNSUPRT", "Unsupported decompression methods or decryption found");
  288.  
  289.         default:
  290.             wsprintf(data, "E_%i Unknown error %i", RVal, RVal);
  291.             return 3;
  292.     }
  293. }
  294.  
  295. /*
  296.     DLLInfo
  297.     Returns some information about the DLL
  298. */
  299. MIRCDECL DLLInfo(MIRCPARAMS) {
  300.     lstrcpy(data, "S_OK mUnzip DLL v" VERSION " ⌐ Kamek 2002 -- based on Info-ZIP's source");
  301.     return 3;
  302. }
  303.  
  304. /*
  305.     Reply <reply>
  306.     Tells the dll to perform some reply
  307. */
  308. MIRCDECL Reply(MIRCPARAMS) {
  309.     if (lstrlen(data) >= sizeof(Rpl)) RetErr("INVRPL", "Reply is too long");
  310.     lstrcpy(Rpl, data);
  311.     CharLower(Rpl);
  312.     RetOK;
  313. }
  314.  
  315.  
  316.  
  317. void FixPaths(char *Path) {
  318.     char *p = Path;
  319.  
  320.     while (p = Kstrchr(p, '/')) *p = '\\';
  321. }
  322.  
  323. BOOL MatchStart(char *Text, char *Start) {
  324.     UINT i;
  325.  
  326.     for (i = 0; Start[i]; i++)
  327.         if (Text[i] != Start[i]) return FALSE;
  328.     return TRUE;
  329. }
  330.  
  331.  
  332.  
  333. BOOL mIRCExec(char *Command) {
  334.     lstrcpy(MapView, Command);
  335.     return SendMessage(hWnd, WM_MCOMMAND, 1, 0);
  336. }
  337.  
  338. BOOL mIRCSign(char *Message) {
  339.     char Buffer[900], *p = Buffer;
  340.  
  341.     if (Signal[0]) 
  342.         wsprintf(Buffer, "/.signal -n mUnzip_%s %s", Signal, Message);
  343.     else
  344.         wsprintf(Buffer, "/.signal -n mUnzip %s", Message);
  345.  
  346.     while (p = Kstrchr(p, 10)) *p = ' ';
  347.     return mIRCExec(Buffer);
  348. }
  349.  
  350.  
  351.  
  352.  
  353. int WINAPI fPrint(LPSTR buf, unsigned long size) {
  354.     if (size > 1) {
  355.         char Buffer[900];
  356.  
  357.         //MessageBox(NULL, Buffer, "Debug", MB_OK);
  358.         if (Commenting == 1) {
  359.             char Buf2[900], *p = Buffer, *lp = Buffer;
  360.  
  361.             lstrcpy(Buffer, buf);
  362.             while (p = Kstrchr(p, 10)) {
  363.                 *p = '\0';
  364.                 if (p - lp < sizeof(Buf2)) {
  365.                     wsprintf(Buf2, "comment %s", lp);
  366.                     mIRCSign(Buf2);
  367.                 }
  368.                 p++;
  369.                 lp = p;
  370.             }
  371.             if (lstrlen(lp)) {
  372.                 wsprintf(Buf2, "comment %s", lp);
  373.                 mIRCSign(Buf2);
  374.             }
  375.             Commenting = 2;
  376.         }
  377.         else if (Commenting == 2);
  378.         else {
  379.             wsprintf(Buffer, "echo %s", buf);
  380.             if (MatchStart(buf, "Target file exists.") || MatchStart(buf, "  inflating:") ||
  381.                     MatchStart(buf, "  testing:") || MatchStart(buf, "   skipping:"))
  382.                 FixPaths(Buffer);
  383.             mIRCSign(Buffer);
  384.         }
  385.     }
  386.     return size;
  387. }
  388.  
  389.  
  390. int WINAPI fReplace(char *filename) {
  391.     char Buffer[900];
  392.  
  393.     Rpl[0] = '\0';
  394.     wsprintf(Buffer, "replace %s", filename);
  395.     FixPaths(Buffer);
  396.     mIRCSign(Buffer);
  397.     if (!lstrcmp(Rpl, "yes")) return 102;
  398.     if (!lstrcmp(Rpl, "yes all")) return 103;
  399.     if (!lstrcmp(Rpl, "no all")) return 104;
  400.     return 100; // "no", default
  401. }
  402.  
  403.  
  404. int WINAPI fPassword(char *p, int n, const char *m, const char *name) {
  405.     // no password handling for now
  406.     return 1;
  407. }
  408.  
  409.  
  410. void WINAPI fAppMsg(unsigned long ucsize, unsigned long csiz, unsigned cfactor,
  411.         unsigned mo, unsigned dy, unsigned yr, unsigned hh, unsigned mm, char c, LPSTR filename,
  412.         LPSTR methbuf, unsigned long crc, char fCrypt) {
  413.     char Buffer[900], FBuf[MAX_PATH], Flags[10];
  414.     UINT Year = yr;
  415.  
  416.     if (Year < 70) Year += 2000;
  417.     else if (Year < 1000) Year += 1900;
  418.     lstrcpy(FBuf, filename);
  419.     FixPaths(FBuf);
  420.     Flags[0] = '\0';
  421.     if (fCrypt == 'E') lstrcat(Flags, "e");
  422.     wsprintf(Buffer, "list %s|%lu %lu %u%%|%02u/%02u/%u %02u:%02u|%lx %s +%s", FBuf,
  423.         ucsize, csiz, cfactor,  mo, dy, Year, hh, mm, crc, methbuf, Flags);
  424.     mIRCSign(Buffer);
  425. }
  426.  
  427. int WINAPI fCallBk(LPCSTR filename, unsigned long fsize) {
  428.     char Buffer[900];
  429.  
  430.     Rpl[0] = '\0';
  431.     wsprintf(Buffer, "extracted %s|%lu", filename, fsize);
  432.     FixPaths(Buffer);
  433.     mIRCSign(Buffer);
  434.     if (!lstrcmp(Rpl, "stop")) return 1;
  435.     return 0;
  436. }