home *** CD-ROM | disk | FTP | other *** search
/ Garbo / Garbo.cdr / mac / unix / hqxtobin.shr / mactypes.h < prev    next >
C/C++ Source or Header  |  1989-04-10  |  7KB  |  152 lines

  1. #include <stdio.h>
  2. #include <sys/types.h>
  3. #include <sys/dir.h>
  4. #include <sys/stat.h>
  5. #include <sys/timeb.h>
  6.  
  7. /* Useful, though not particularly Mac related, values */
  8. typedef unsigned char byte;     /* one byte, obviously */
  9. typedef unsigned short word;    /* must be 2 bytes */
  10. typedef unsigned long ulong;    /* 4 bytes */
  11. #define TRUE  1
  12. #define FALSE 0
  13. #define CR 0x0d
  14. #define LF 0x0a
  15.  
  16. /* Compatibility issues */
  17. #ifdef BSD
  18. #define mac2word (word) ntohs
  19. #define mac2long (ulong) ntohl
  20. #define word2mac (word) htons
  21. #define long2mac (ulong) htonl
  22. #else
  23. #define mac2word
  24. #define mac2long
  25. #define word2mac
  26. #define long2mac
  27. #endif
  28.  
  29. #ifdef MAXNAMLEN/* 4.2 BSD, stdio.h */
  30. #define SYSNAMELEN MAXNAMLEN
  31. #else
  32. #define SYSNAMELEN DIRSIZ
  33. #endif
  34.  
  35. #define NAMELEN 63              /* maximum legal Mac file name length */
  36. #define BINNAMELEN 68           /* NAMELEN + len(".bin\0") */
  37.  
  38. /* Format of a bin file:
  39. A bin file is composed of 128 byte blocks.  The first block is the
  40. info_header (see below).  Then comes the data fork, null padded to fill the
  41. last block.  Then comes the resource fork, padded to fill the last block.  A
  42. proposal to follow with the text of the Get Info box has not been implemented,
  43. to the best of my knowledge.  Version, zero1 and zero2 are what the receiving
  44. program looks at to determine if a MacBinary transfer is being initiated.
  45. */ 
  46. typedef struct {     /* info file header (128 bytes). Unfortunately, these
  47.                         longs don't align to word boundaries */
  48.             byte version;           /* there is only a version 0 at this time */
  49.             byte nlen;              /* Length of filename. */
  50.             byte name[NAMELEN];     /* Filename (only 1st nlen are significant)*/
  51.             byte type[4];           /* File type. */
  52.             byte auth[4];           /* File creator. */
  53.             byte flags;             /* file flags: LkIvBnSyBzByChIt */
  54.             byte zero1;             /* Locked, Invisible,Bundle, System */
  55.                                     /* Bozo, Busy, Changed, Init */
  56.             byte icon_vert[2];      /* Vertical icon position within window */
  57.             byte icon_horiz[2];     /* Horizontal icon postion in window */
  58.             byte window_id[2];      /* Window or folder ID. */
  59.             byte protect;           /* = 1 for protected file, 0 otherwise */
  60.             byte zero2;
  61.             byte dlen[4];           /* Data Fork length (bytes) -   most sig.  */
  62.             byte rlen[4];           /* Resource Fork length         byte first */
  63.             byte ctim[4];           /* File's creation date. */
  64.             byte mtim[4];           /* File's "last modified" date. */
  65.             byte ilen[2];           /* (Proposed) GetInfo message length */
  66.             byte unused[27];
  67.             } info_header;
  68.  
  69. /* The *.info file of a MacTerminal file transfer either has exactly this
  70. structure or has the protect bit in bit 6 (near the sign bit) of byte zero1.
  71. The code I have for macbin suggests the difference, but I'm not so sure */
  72.  
  73. /* Format of a hqx file:
  74. It begins with a line that begins "(This file
  75. and the rest is 64 character lines (except possibly the last, and not
  76. including newlines) where the first begins and the last ends with a colon.
  77. The characters between colons should be only from the set in tr86, below,
  78. each of which corresponds to 6 bits of data.  Once that is translated to
  79. 8 bit bytes, you have the real data, except that the byte 0x90 may 
  80. indicate, if the following character is nonzero, that the previous
  81. byte is to be repeated 1 to 255 times.  The byte 0x90 is represented by
  82. 0x9000.  The information in the file is the hqx_buf (see below),
  83. a CRC word, the data fork, a CRC word, the resource fork, and a CRC word.
  84. There is considerable confusion about the flags.  An official looking document
  85. unclearly states that the init bit is always clear, as is the following byte.
  86. The experience of others suggests, however, that this is not the case.
  87. */
  88.  
  89. #define HQXLINELEN 64
  90. typedef struct {
  91.             byte version;           /* there is only a version 0 at this time */
  92.             byte type[4];           /* File type. */
  93.             byte auth[4];           /* File creator. */
  94.             byte flags;             /* file flags: LkIvBnSyBzByChIt */
  95.             byte protect;           /* ?Pr??????, don't know what ? bits mean */
  96.             byte dlen[4];           /* Data Fork length (bytes) -   most sig.  */
  97.             byte rlen[4];           /* Resource Fork length         byte first */
  98.             } hqx_header;
  99. typedef struct {     /* hqx file header buffer (includes file name) */
  100.             byte nlen;              /* Length of filename. */
  101.             byte name[NAMELEN];     /* Filename: only nlen actually appear */
  102.             hqx_header all_the_rest;/* and all the rest follows immediately */
  103.             } hqx_buf;
  104.  
  105. /* Format of a Packit file:
  106. Repeat the following sequence for each file in the Packit file:
  107.     4 byte identifier ("PMag" = not compressed, "Pma4" = compressed)
  108.     320 byte compression data (if compressed file)
  109.         = preorder transversal of Huffman tree
  110.         255 0 bits corresponding to nonleaf nodes
  111.         256 1 bits corresponding to leaf nodes
  112.         256 bytes associating leaf nodes with bytes
  113.         1   completely wasted bit
  114.     92 byte header (see pit_header below) *
  115.     2 bytes CRC word for header *
  116.     data fork (length from header) *
  117.     resource fork (length from header) *
  118.     2 bytes CRC word for forks *
  119.  
  120. Last file is followed by the 4 byte Ascii string, "Pend", and then the EOF.
  121. The CRC calculations differ from those in the binhex format.
  122.  
  123. * these are in compressed form if compression is on for the file
  124.  
  125. */
  126.  
  127. typedef struct {     /* Packit file header (92 bytes) */
  128.             byte nlen;              /* Length of filename. */
  129.             byte name[NAMELEN];     /* Filename (only 1st nlen are significant)*/
  130.             byte type[4];           /* File type. */
  131.             byte auth[4];           /* File creator. */
  132.             byte flags;             /* file flags: LkIvBnSyBzByChIt */
  133.             byte zero1;
  134.             byte protect;           /* = 1 for protected file, 0 otherwise */
  135.             byte zero2;
  136.             byte dlen[4];           /* Data Fork length (bytes) -   most sig.  */
  137.             byte rlen[4];           /* Resource Fork length         byte first */
  138.             byte ctim[4];           /* File's creation date. */
  139.             byte mtim[4];           /* File's "last modified" date. */
  140.             } pit_header;
  141.  
  142. /* types for constructing the Huffman tree */
  143. typedef struct branch_st {
  144.             byte flag;
  145.             struct branch_st *one, *zero;
  146.             } branch;
  147.  
  148. typedef struct leaf_st {
  149.             byte flag;
  150.             byte data;
  151.             } leaf;
  152.