home *** CD-ROM | disk | FTP | other *** search
/ Aminet 33 / Aminet 33 - October 1999.iso / Aminet / disk / misc / TransADF.lha / TransADF / Source / util.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-10-05  |  5.9 KB  |  263 lines

  1. /* util.c - Miscellaneous functions and macros
  2. ** Copyright (C) 1997,1998 Karl J. Ots
  3. ** 
  4. ** This program is free software; you can redistribute it and/or modify
  5. ** it under the terms of the GNU General Public License as published by
  6. ** the Free Software Foundation; either version 2 of the License, or
  7. ** (at your option) any later version.
  8. ** 
  9. ** This program is distributed in the hope that it will be useful,
  10. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12. ** GNU General Public License for more details.
  13. ** 
  14. ** You should have received a copy of the GNU General Public License
  15. ** along with this program; if not, write to the Free Software
  16. ** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  17. */
  18.  
  19. /*------------------------------------*/
  20. /* Miscellaneous functions and macros */
  21. /*------------------------------------*/
  22.  
  23. #include <exec/types.h>
  24. #include <dos/dos.h>
  25. #ifndef COMPILE_LITE
  26. #  include <clib/dos_protos.h>
  27.  
  28. #  include <time.h>
  29. #endif /* COMPILE_LITE */
  30.  
  31. #include <string.h>
  32.  
  33. #include "util.h"
  34. #ifndef COMPILE_LITE
  35. #  include "gzip.h"
  36. #  include "pkzip.h"
  37. #endif /* COMPILE_LITE */
  38.  
  39.  
  40. /*
  41. ** Convert a BString to a CString.
  42. ** String must be duplicated if you wish to keep it.
  43. */
  44. STRPTR b2cstr (BSTR bstring)
  45. {
  46.   static char cstring[255];
  47.   STRPTR realbstr;
  48.   UBYTE len;
  49.   int i;
  50.   
  51.   realbstr = (STRPTR) BADDR (bstring);
  52.   len = realbstr[0];
  53.   
  54.   for (i=0; i<len; i++)
  55.     cstring[i] = realbstr[i+1];
  56.   cstring[i] = '\0';
  57.   
  58.   return cstring;
  59. }
  60.  
  61.  
  62. /* The following routines are only used in conjunction with de/compression */
  63. #ifndef COMPILE_LITE
  64.  
  65. /* 
  66. ** Return the file type, based on the 'magic number'
  67. ** ie the first few charcters.
  68. ** This function preserves current file position.
  69. */
  70. ULONG getFileType (BPTR file)
  71. {
  72.   UBYTE mag_num[4];
  73.   static UBYTE pkzip_mag[4] = {'P', 'K', 0x03, 0x04};
  74.   static UBYTE gzip_mag[2]  = {0x1F, 0x8B};
  75.   static UBYTE dos_mag[3]   = {'D',  'O',  'S'};
  76.   LONG oldp;
  77.   
  78.   oldp = Seek (file, 0, OFFSET_BEGINNING);
  79.   if ( Read (file, mag_num, 4) != 4) return FT_UNKNOWN;
  80.   Seek (file, oldp, OFFSET_BEGINNING);
  81.   
  82.   /* These are straight-forward comparisons */
  83.   if (!memcmp (mag_num, pkzip_mag, 4))
  84.     return FT_PKZIP;
  85.   if (!memcmp (mag_num, gzip_mag, 2))
  86.     return FT_GZIP;
  87.   if (!memcmp (mag_num, dos_mag, 3))
  88.     return FT_DOS;
  89.   
  90.   /* Zlib is a little tricky - The first char is usually 0x78, and the */
  91.   /* first two chars, when viewed together as an unsigned 16-bit int,  */
  92.   /* is a multiple of 31.                                              */
  93.   /* This probably needs a litte more work :)                          */
  94.   if ((mag_num[0] == 0x78) && !(((UWORD *)mag_num)[0] % 31))
  95.     return FT_ZLIB;
  96.   
  97.   /* If we get here, the type is unknown */
  98.   return FT_UNKNOWN;
  99. }
  100.  
  101.  
  102. /*
  103. ** Output a Header to the specified file depending on fileType.
  104. ** Return TRUE if no errors. else FALSE.
  105. */
  106. BOOL writeHead (BPTR outFile, STRPTR origName, ULONG fileType)
  107. {
  108.   /* Assume that the file is at the start */
  109.  
  110.   switch (fileType)
  111.   {
  112.   case FT_ZLIB:
  113.     /* No action necessary */
  114.     return TRUE;
  115.   
  116.   case FT_GZIP:
  117.     return writeGZHead (outFile, origName);
  118.   
  119.   case FT_PKZIP:
  120.     return writePKZHead (outFile, origName);
  121.   
  122.   case FT_PKZIP_ADD:
  123.     return writePKZHeadAdd (outFile, origName);
  124.   }
  125.   
  126.   /* Unknown type, error! */
  127.   return FALSE;
  128. }
  129.  
  130.  
  131. /*
  132. ** Finish writing a file depending on fileType.
  133. ** Return TRUE if no errors. else FALSE.
  134. */
  135. BOOL finishFile (BPTR outFile, ULONG CRC, ULONG CSize, ULONG USize, 
  136.                 ULONG fileType)
  137. {
  138.   switch (fileType)
  139.   {
  140.   case FT_ZLIB:
  141.     /* No action necessary */
  142.     return TRUE;
  143.   
  144.   case FT_GZIP:
  145.     return finishGZFile (outFile, CRC, USize);
  146.   
  147.   case FT_PKZIP:
  148.     return finishPKZFile (outFile, CRC, CSize, USize);
  149.     
  150.   case FT_PKZIP_ADD:
  151.     return finishPKZFileAdd (outFile, CRC, CSize, USize);
  152.   }
  153.   
  154.   /* Unknown type, error! */
  155.   return FALSE;
  156. }
  157.  
  158.  
  159. /*
  160. ** Skip the header of a specified file depending on file type.
  161. ** Return TRUE if no errors. else FALSE.
  162. */
  163. BOOL skipHead (BPTR inFile, STRPTR origName, ULONG fileType)
  164. {
  165.   /* Assume that the file is at the start */
  166.  
  167.   switch (fileType)
  168.   {
  169.   case FT_ZLIB:
  170.     /* No action necessary */
  171.     return TRUE;
  172.   
  173.   case FT_GZIP:
  174.     return skipGZHead (inFile);
  175.   
  176.   case FT_PKZIP:
  177.     return skipPKZHead (inFile, origName);
  178.   }
  179.   
  180.   /* Unknown type, error! */
  181.   return FALSE;
  182. }
  183.  
  184.  
  185. /*
  186. ** Read the a file depending on fileType and return the CRC
  187. ** and USize in supplied arrays.
  188. ** Return TRUE if no errors, else FALSE.
  189. */
  190. BOOL readTail (BPTR inFile, ULONG *CRC, ULONG *USize, ULONG fileType)
  191. {
  192.   switch (fileType)
  193.   {
  194.   case FT_ZLIB:
  195.     /* No action necessary */
  196.     return TRUE;
  197.   
  198.   case FT_GZIP:
  199.     return readGZTail (inFile, CRC, USize);
  200.   
  201.   case FT_PKZIP:
  202.     return readPKZTail (inFile, CRC, USize);
  203.   }
  204.   
  205.   /* Unknown type, error! */
  206.   return FALSE;
  207. }
  208.  
  209.  
  210. /*
  211. ** Return the current date and time in UNIX format.
  212. ** (ie No. seconds after 1-Jan-1970).
  213. */
  214. ULONG unixDate (void)
  215. {
  216.   struct DateStamp ds;
  217.   
  218.   DateStamp (&ds);
  219.   
  220.   /* Need to add 2922 Days (1970->1978) */
  221.   
  222.   return ((ds.ds_Tick / TICKS_PER_SECOND) + 
  223.           (ds.ds_Minute * 60) +
  224.           ((ds.ds_Days + 2922) * 86400));
  225. }
  226.  
  227.  
  228. /*
  229. ** Return the current date and time in DOS format.
  230. */
  231. ULONG  dosDate (void)
  232. {
  233.   /* The DOS date format is as follows:
  234.    * 7 bits: Year after 1980 (0 - 127).
  235.    * 4 bits: Month (1 - 12).
  236.    * 5 bits: Day (1 - 31).
  237.    * (16 bits)
  238.    * 
  239.    * 5 bits: Hour (0 - 23).
  240.    * 6 bits: Minute (0 - 59).
  241.    * 5 bits: Seconds / 2 (0 - 29).
  242.    * (16 bits)
  243.    * 
  244.    * Total = 32 bits = 1 long word.
  245.    */
  246.   
  247.   time_t t;
  248.   struct tm *tp;
  249.   
  250.   /* Get the time */
  251.   t = time (NULL); tp = localtime (&t);
  252.   
  253.   /* Adjust year to within DOS range */
  254.   if (tp->tm_year < 80) return 0x00210000;   /* 1-1-1980, 00:00:00 */
  255.   
  256.   return (((tp->tm_year - 80) << 25) | ((tp->tm_mon+1) << 21) | 
  257.           (tp->tm_mday << 16) | (tp->tm_hour << 11) | (tp->tm_min << 5) |
  258.           (tp->tm_sec >> 1));
  259. }
  260.  
  261.  
  262. #endif /* COMPILE_LITE */
  263.