Subversion Repositories autosfx

Rev

Blame | Last modification | View Log | RSS feed

  1. #include "stdafx.h"
  2. #pragma hdrstop
  3.  
  4. /*
  5.   Common.cpp - common definitions and functions
  6. ************************************************************************
  7.  Copyright (C) 2009, 2010  by Russell J. Peters, Roger Aelbrecht
  8.  
  9.    This file is part of TZipMaster Version 1.9.
  10.  
  11.     TZipMaster is free software: you can redistribute it and/or modify
  12.     it under the terms of the GNU Lesser General Public License as published by
  13.     the Free Software Foundation, either version 3 of the License, or
  14.     (at your option) any later version.
  15.  
  16.     TZipMaster is distributed in the hope that it will be useful,
  17.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  18.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  19.     GNU Lesser General Public License for more details.
  20.  
  21.     You should have received a copy of the GNU Lesser General Public License
  22.     along with TZipMaster.  If not, see <http://www.gnu.org/licenses/>.
  23.  
  24.     contact: problems@delphizip.org (include ZipMaster in the subject).
  25.     updates: http://www.delphizip.org
  26.     DelphiZip maillist subscribe at http://www.freelists.org/list/delphizip
  27. ************************************************************************/
  28.  
  29. #include "common.h"
  30. #include "cpyrght.h"
  31. #include "dz_errs.h"
  32.  
  33. #undef _DZ_FILE_
  34. #define _DZ_FILE_ DZ_COMMON_CPP
  35.                                      
  36.  
  37. int __fastcall Close_Handle(HANDLE *h)
  38. {
  39.     HANDLE ht = *h;
  40.  
  41.     if (ht != INVALID_HANDLE_VALUE)
  42.     {
  43.         *h = INVALID_HANDLE_VALUE;
  44.         return CloseHandle(ht);
  45.         }
  46.     return 0;
  47. }
  48.  
  49. __int64 SetFilePointer64(HANDLE hf, __int64 ofs, int from)
  50. {
  51.     typedef union
  52.     {
  53.         __int64 i64;
  54.  
  55.         struct
  56.         {
  57.             unsigned lo;
  58.             int hi;
  59.         };
  60.     }_I64;
  61.  
  62.     _I64 o;
  63.     o.i64 = ofs;
  64.     o.lo = SetFilePointer(hf, o.lo, (LONG*) & o.hi, from);
  65.  
  66.     if (o.lo == INVALID_SET_FILE_POINTER  && GetLastError())
  67.         return -1;
  68.  
  69.     return o.i64;
  70. }
  71.  
  72. void Cleanup_Process(void)
  73. {
  74. // nothing to do
  75. }
  76.  
  77. DWORD __fastcall GetFileAttrs(const TCHAR *p)
  78. {
  79.     WIN32_FIND_DATA fdata;
  80.     HANDLE          fh;
  81.     DWORD           ret = -1; // no_file indicator
  82.  
  83.     fh = FindFirstFile(p, &fdata);
  84.  
  85.     if (fh != INVALID_HANDLE_VALUE)
  86.     {
  87.         ret = fdata.dwFileAttributes;
  88.         FindClose(fh);
  89.     }
  90.  
  91.     return ret;
  92. }
  93.  
  94. // Return the Unix time_t value (GMT/UTC time) for the DOS format (local)
  95. //   time dostime, where dostime is a four byte value (date in most significant
  96. //   word, time in least significant word), see dostime() function. dostime ::
  97. //   DOS time to convert.
  98. time_t dos2unixtime(ulg dostime)
  99. {
  100.  
  101.     struct tm     *t;             // argument for mktime()
  102.     const time_t  clock = time(NULL);
  103.  
  104.     t = localtime(&clock);
  105.  
  106.     // Convert DOS time to UNIX time_t format
  107.     t->tm_sec = (((int)dostime) << 1) & 0x3E;
  108.     t->tm_min = (((int)dostime) >> 5) & 0x3F;
  109.     t->tm_hour = (((int)dostime) >> 11) & 0x1F;
  110.     t->tm_mday = (int)(dostime >> 16) & 0x1F;
  111.     t->tm_mon = ((int)(dostime >> 21) & 0x0F) - 1;
  112.     t->tm_year = ((int)(dostime >> 25) & 0x7F) + 80;
  113.  
  114.     return mktime(t);
  115. }
  116.  
  117. // Convert the date y/n/d and time h:m:s to a four byte DOS date and time
  118. //   (date in high two bytes, time in low two bytes allowing magnitude
  119. //   comparison). y :: Year. n :: Month. d :: Day. h :: Hour. m :: Minute. s ::
  120. //   Second.
  121. ulg dostime(int y, int n, int d, int h, int m, int s)
  122. {
  123.     return y < 1980 ? dostime(1980, 1, 1, 0, 0, 0) : (((ulg) y - 1980) << 25) |
  124.            ((ulg) n << 21) | ((ulg) d << 16) | ((ulg) h << 11) | ((ulg) m << 5) |
  125.            ((ulg) s >> 1);
  126. }
  127.  
  128. // Return the Unix time t in DOS format, rounded up to the next two second
  129. //   boundary. t :: Unix time to convert.
  130. ulg unix2dostime(time_t *t)
  131. {
  132.     time_t    t_even;
  133.  
  134.     struct tm *s;                 // result of localtime()
  135.     t_even = (*t + 1) & (~1); // Round up to even seconds.
  136.     s = localtime(&t_even);     // Use local time since MSDOS does.
  137.  
  138.     if (s)
  139.     {
  140.         // Russell Peters s can be null
  141.         return dostime(s->tm_year + 1900, s->tm_mon + 1, s->tm_mday, s->tm_hour, s->tm_min,
  142.                        s->tm_sec);
  143.     }
  144.  
  145.     return dostime(1980, 1, 1, 0, 0, 1);
  146. }
  147.  
  148. char * __fastcall zstrdupB(const char* from)
  149. {
  150.     int len = strlen(from);
  151.     if (!len)
  152.         return NULL;
  153.     char *tmp = new char[len+1];
  154.     strcpy(tmp, from);
  155.     tmp[len] = 0;
  156.     return tmp;
  157. }                                      
  158.  
  159. TCHAR * __fastcall zstrdup(const TCHAR* from)
  160. {
  161.     int len = _tcslen(from);
  162.     if (!len)
  163.         return NULL;
  164.     TCHAR *tmp = new TCHAR[len+1];
  165.     _tcscpy(tmp, from);
  166.     tmp[len] = 0;
  167.     return tmp;
  168. }
  169.  
  170.  
  171. const unsigned char* __fastcall FindTag(WORD tag, const unsigned char *p, unsigned &siz)
  172. {
  173. #pragma pack(push, 1)
  174. union XTAG
  175. {
  176.         struct
  177.         {
  178.                 unsigned short tg;
  179.                 unsigned short sz;
  180.         };
  181.         unsigned xtag;
  182. }xtg;
  183. #pragma pack(pop)
  184.  
  185.         if (!p || siz < sizeof(XTAG))
  186.                 return NULL;
  187.         const unsigned char *e = p + siz - 1;
  188.     while (p + 3 < e)
  189.         {
  190.                 xtg = *((const XTAG*)p)++;
  191.                 if (xtg.tg == tag)
  192.                 {
  193.                         siz = xtg.sz;
  194.                         return p;
  195.                 }
  196.                 p += xtg.sz;
  197.     }
  198.     return NULL;
  199. }
  200.  
  201.