home *** CD-ROM | disk | FTP | other *** search
/ DOS/V Power Report 1999 October / VPR9910A.BIN / OLS / unrar32005 / unrar32005.lzh / src / unrarapi.cxx < prev    next >
C/C++ Source or Header  |  1998-04-03  |  4KB  |  147 lines

  1. /*
  2.  *   Copyright (c) 1998 T. Kamei (kamei@jsdlab.co.jp)
  3.  *
  4.  *   Permission to use, copy, modify, and distribute this software
  5.  * and its documentation for any purpose is hereby granted provided
  6.  * that the above copyright notice and this permission notice appear
  7.  * in all copies of the software and related documentation.
  8.  *
  9.  *                          NO WARRANTY
  10.  *
  11.  *   THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY WARRANTIES;
  12.  * WITHOUT EVEN THE IMPLIED WARRANTIES OF MERCHANTABILITY OR FITNESS
  13.  * FOR A PARTICULAR PURPOSE.
  14.  */
  15.  
  16. #include <windows.h>
  17. #include <stdio.h>
  18. #include "comm-arc.h"
  19. #include "unrarapi.h"
  20.  
  21. HINSTANCE
  22. load_rarapi ()
  23. {
  24.   HINSTANCE h = LoadLibrary ("unrar.dll");
  25.   if (!h)
  26.     return 0;
  27.   if ((rarOpenArchive = (HANDLE (__stdcall *)(RAROpenArchiveData *))
  28.        GetProcAddress (h, "RAROpenArchive"))
  29.       && (rarCloseArchive = (int (__stdcall *)(HANDLE))
  30.           GetProcAddress (h, "RARCloseArchive"))
  31.       && (rarReadHeader = (int (__stdcall *)(HANDLE, RARHeaderData *))
  32.           GetProcAddress (h, "RARReadHeader"))
  33.       && (rarProcessFile = (int (__stdcall *)(HANDLE, int, char *, char *))
  34.           GetProcAddress (h, "RARProcessFile"))
  35.       && (rarSetChangeVolProc = (void (__stdcall *)(HANDLE, int (__cdecl *)(char *, int)))
  36.           GetProcAddress (h, "RARSetChangeVolProc"))
  37.       && (rarSetProcessDataProc = (void (__stdcall *)(HANDLE, int (__cdecl *)(unsigned char *, int)))
  38.           GetProcAddress (h, "RARSetProcessDataProc"))
  39.       && (rarSetPassword = (void (__stdcall *)(HANDLE, const char *))
  40.           GetProcAddress (h, "RARSetPassword")))
  41.     return h;
  42.   FreeLibrary (h);
  43.   return 0;
  44. }
  45.  
  46. int
  47. rarData::open (const char *path, int mode, char *buf, int size)
  48. {
  49.   oad.ArcName = (char *)path;
  50.   oad.CmtBuf = buf;
  51.   oad.CmtBufSize = size;
  52.   oad.OpenMode = mode;
  53.   hd.CmtBuf = 0;
  54.   hd.CmtBufSize = 0;
  55.   h = rarOpenArchive (&oad);
  56.   return int (h);
  57. }
  58.  
  59. int
  60. rarData::close ()
  61. {
  62.   if (!h)
  63.     return 0;
  64.   int x = rarCloseArchive (h);
  65.   h = 0;
  66.   return x;
  67. }
  68.  
  69. int
  70. file_executable_p (const char *path)
  71. {
  72.   int f = 0;
  73.   FILE *fp = fopen (path, "rb");
  74.   if (fp)
  75.     {
  76.       IMAGE_DOS_HEADER dos;
  77.       IMAGE_NT_HEADERS nt;
  78.       f = (fread (&dos, sizeof dos, 1, fp) == 1
  79.            && dos.e_magic == IMAGE_DOS_SIGNATURE
  80.            && !fseek (fp, dos.e_lfanew, SEEK_SET)
  81.            && fread (&nt, sizeof nt, 1, fp) == 1
  82.            && nt.Signature == IMAGE_NT_SIGNATURE);
  83.       fclose (fp);
  84.     }
  85.   return f;
  86. }
  87.  
  88. int
  89. calc_ratio (u_long comp_sz, u_long orig_sz)
  90. {
  91.   return orig_sz ? int (double (comp_sz) / orig_sz * 1000) : 0;
  92. }
  93.  
  94. const char *
  95. method_string (int method)
  96. {
  97.   switch (method)
  98.     {
  99.     case 0x30:
  100.       return "storing";
  101.     case 0x31:
  102.       return "fastest";
  103.     case 0x32:
  104.       return "fast";
  105.     case 0x33:
  106.       return "normal";
  107.     case 0x34:
  108.       return "good";
  109.     case 0x35:
  110.       return "best";
  111.     default:
  112.       return "unknown";
  113.     }
  114. }
  115.  
  116. int
  117. os_type (int os)
  118. {
  119.   switch (os)
  120.     {
  121.     case 0:
  122.       return OSTYPE_MSDOS;
  123.     case 1:
  124.       return OSTYPE_OS2;
  125.     case 2:
  126.       return OSTYPE_WINDOWS95; // Win32
  127.     case 3:
  128.       return OSTYPE_UNIX;
  129.     default:
  130.       return OSTYPE_UNKNOWN;
  131.     }
  132. }
  133.  
  134. const char *
  135. attr_string (int attr)
  136. {
  137.   static char b[5];
  138.   b[0] = (attr & FILE_ATTRIBUTE_DIRECTORY
  139.           ? 'd'
  140.           : (attr & FILE_ATTRIBUTE_ARCHIVE
  141.              ? 'a' : '-'));
  142.   b[1] = attr & FILE_ATTRIBUTE_SYSTEM ? 's' : '-';
  143.   b[2] = attr & FILE_ATTRIBUTE_HIDDEN ? 'h' : '-';
  144.   b[3] = attr & FILE_ATTRIBUTE_READONLY ? '-' : 'w';
  145.   return b;
  146. }
  147.