libmspack

mspack.h

Go to the documentation of this file.
00001 /* libmspack -- a library for working with Microsoft compression formats.
00002  * (C) 2003-2010 Stuart Caie <kyzer@4u.net>
00003  *
00004  * libmspack is free software; you can redistribute it and/or modify it under
00005  * the terms of the GNU Lesser General Public License (LGPL) version 2.1
00006  *
00007  * This program is distributed in the hope that it will be useful,
00008  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00009  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00010  * GNU Lesser General Public License for more details.
00011  *
00012  * You should have received a copy of the GNU Lesser General Public License
00013  * along with this program; if not, write to the Free Software
00014  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00015  */
00016 
00118 #ifndef LIB_MSPACK_H
00119 #define LIB_MSPACK_H 1
00120 
00121 #ifdef __cplusplus
00122 extern "C" {
00123 #endif
00124 
00125 #include <sys/types.h>
00126 #include <stdlib.h>
00127 #ifndef _MSC_VER
00128 # include <unistd.h>
00129 #endif /* _MSC_VER */
00130 
00154 #define MSPACK_SYS_SELFTEST(result)  do { \
00155   (result) = mspack_sys_selftest_internal(sizeof(off_t)); \
00156 } while (0)
00157 
00159 extern int mspack_sys_selftest_internal(int);
00160 
00190 extern int mspack_version(int entity);
00191 
00193 #define MSPACK_VER_LIBRARY   (0)
00194 
00195 #define MSPACK_VER_SYSTEM    (1)
00196 
00197 #define MSPACK_VER_MSCABD    (2)
00198 
00199 #define MSPACK_VER_MSCABC    (3)
00200 
00201 #define MSPACK_VER_MSCHMD    (4)
00202 
00203 #define MSPACK_VER_MSCHMC    (5)
00204 
00205 #define MSPACK_VER_MSLITD    (6)
00206 
00207 #define MSPACK_VER_MSLITC    (7)
00208 
00209 #define MSPACK_VER_MSHLPD    (8)
00210 
00211 #define MSPACK_VER_MSHLPC    (9)
00212 
00213 #define MSPACK_VER_MSSZDDD   (10)
00214 
00215 #define MSPACK_VER_MSSZDDC   (11)
00216 
00217 #define MSPACK_VER_MSKWAJD   (12)
00218 
00219 #define MSPACK_VER_MSKWAJC   (13)
00220 
00221 /* --- file I/O abstraction ------------------------------------------------ */
00222 
00242 struct mspack_system {
00267   struct mspack_file * (*open)(struct mspack_system *self,
00268                                char *filename,
00269                                int mode);
00270 
00278   void (*close)(struct mspack_file *file);
00279 
00291   int (*read)(struct mspack_file *file,
00292               void *buffer,
00293               int bytes);
00294 
00308   int (*write)(struct mspack_file *file,
00309                void *buffer,
00310                int bytes);
00311 
00335   int (*seek)(struct mspack_file *file,
00336               off_t offset,
00337               int mode);
00338 
00346   off_t (*tell)(struct mspack_file *file);
00347   
00362   void (*message)(struct mspack_file *file,
00363                   char *format,
00364                   ...);
00365 
00376   void * (*alloc)(struct mspack_system *self,
00377                   size_t bytes);
00378   
00385   void (*free)(void *ptr);
00386 
00399   void (*copy)(void *src,
00400                void *dest,
00401                size_t bytes);
00402 
00409   void *null_ptr;
00410 };
00411 
00413 #define MSPACK_SYS_OPEN_READ   (0)
00414 
00415 #define MSPACK_SYS_OPEN_WRITE  (1)
00416 
00417 #define MSPACK_SYS_OPEN_UPDATE (2)
00418 
00419 #define MSPACK_SYS_OPEN_APPEND (3)
00420 
00422 #define MSPACK_SYS_SEEK_START  (0)
00423 
00424 #define MSPACK_SYS_SEEK_CUR    (1)
00425 
00426 #define MSPACK_SYS_SEEK_END    (2)
00427 
00433 struct mspack_file {
00434   int dummy;
00435 };
00436 
00437 /* --- error codes --------------------------------------------------------- */
00438 
00440 #define MSPACK_ERR_OK          (0)
00441 
00442 #define MSPACK_ERR_ARGS        (1)
00443 
00444 #define MSPACK_ERR_OPEN        (2)
00445 
00446 #define MSPACK_ERR_READ        (3)
00447 
00448 #define MSPACK_ERR_WRITE       (4)
00449 
00450 #define MSPACK_ERR_SEEK        (5)
00451 
00452 #define MSPACK_ERR_NOMEMORY    (6)
00453 
00454 #define MSPACK_ERR_SIGNATURE   (7)
00455 
00456 #define MSPACK_ERR_DATAFORMAT  (8)
00457 
00458 #define MSPACK_ERR_CHECKSUM    (9)
00459 
00460 #define MSPACK_ERR_CRUNCH      (10)
00461 
00462 #define MSPACK_ERR_DECRUNCH    (11)
00463 
00464 /* --- functions available in library -------------------------------------- */
00465 
00470 extern struct mscab_compressor *
00471   mspack_create_cab_compressor(struct mspack_system *sys);
00472 
00477 extern struct mscab_decompressor *
00478   mspack_create_cab_decompressor(struct mspack_system *sys);
00479 
00483 extern void mspack_destroy_cab_compressor(struct mscab_compressor *self);
00484 
00488 extern void mspack_destroy_cab_decompressor(struct mscab_decompressor *self);
00489 
00490 
00495 extern struct mschm_compressor *
00496   mspack_create_chm_compressor(struct mspack_system *sys);
00497 
00502 extern struct mschm_decompressor *
00503   mspack_create_chm_decompressor(struct mspack_system *sys);
00504 
00508 extern void mspack_destroy_chm_compressor(struct mschm_compressor *self);
00509 
00513 extern void mspack_destroy_chm_decompressor(struct mschm_decompressor *self);
00514 
00515 
00520 extern struct mslit_compressor *
00521   mspack_create_lit_compressor(struct mspack_system *sys);
00522 
00527 extern struct mslit_decompressor *
00528   mspack_create_lit_decompressor(struct mspack_system *sys);
00529 
00533 extern void mspack_destroy_lit_compressor(struct mslit_compressor *self);
00534 
00538 extern void mspack_destroy_lit_decompressor(struct mslit_decompressor *self);
00539 
00540 
00545 extern struct mshlp_compressor *
00546   mspack_create_hlp_compressor(struct mspack_system *sys);
00547 
00552 extern struct mshlp_decompressor *
00553   mspack_create_hlp_decompressor(struct mspack_system *sys);
00554 
00558 extern void mspack_destroy_hlp_compressor(struct mshlp_compressor *self);
00559 
00563 extern void mspack_destroy_hlp_decompressor(struct mshlp_decompressor *self);
00564 
00565 
00570 extern struct msszdd_compressor *
00571   mspack_create_szdd_compressor(struct mspack_system *sys);
00572 
00577 extern struct msszdd_decompressor *
00578   mspack_create_szdd_decompressor(struct mspack_system *sys);
00579 
00583 extern void mspack_destroy_szdd_compressor(struct msszdd_compressor *self);
00584 
00588 extern void mspack_destroy_szdd_decompressor(struct msszdd_decompressor *self);
00589 
00590 
00595 extern struct mskwaj_compressor *
00596   mspack_create_kwaj_compressor(struct mspack_system *sys);
00597 
00602 extern struct mskwaj_decompressor *
00603   mspack_create_kwaj_decompressor(struct mspack_system *sys);
00604 
00608 extern void mspack_destroy_kwaj_compressor(struct mskwaj_compressor *self);
00609 
00613 extern void mspack_destroy_kwaj_decompressor(struct mskwaj_decompressor *self);
00614 
00615 
00616 /* --- support for .CAB (MS Cabinet) file format --------------------------- */
00617 
00629 struct mscabd_cabinet {
00635   struct mscabd_cabinet *next;
00636 
00642   char *filename;
00643   
00645   off_t base_offset;
00646 
00648   unsigned int length;
00649 
00651   struct mscabd_cabinet *prevcab;
00652 
00654   struct mscabd_cabinet *nextcab;
00655 
00657   char *prevname;
00658 
00660   char *nextname;
00661 
00665   char *previnfo;
00666 
00670   char *nextinfo;
00671 
00673   struct mscabd_file *files;
00674 
00676   struct mscabd_folder *folders;
00677 
00682   unsigned short set_id;
00683 
00689   unsigned short set_index;
00690 
00701   unsigned short header_resv;
00702 
00714   int flags;
00715 };
00716 
00718 #define MSCAB_HDR_RESV_OFFSET (0x28)
00719 
00721 #define MSCAB_HDR_PREVCAB (0x01)
00722 
00723 #define MSCAB_HDR_NEXTCAB (0x02)
00724 
00725 #define MSCAB_HDR_RESV    (0x04)
00726 
00736 struct mscabd_folder {
00741   struct mscabd_folder *next;
00742 
00752   int comp_type;
00753 
00759   unsigned int num_blocks;
00760 };
00761 
00769 #define MSCABD_COMP_METHOD(comp_type) ((comp_type) & 0x0F)
00770 
00777 #define MSCABD_COMP_LEVEL(comp_type) (((comp_type) >> 8) & 0x1F)
00778 
00780 #define MSCAB_COMP_NONE       (0)
00781 
00782 #define MSCAB_COMP_MSZIP      (1)
00783 
00784 #define MSCAB_COMP_QUANTUM    (2)
00785 
00786 #define MSCAB_COMP_LZX        (3)
00787 
00793 struct mscabd_file {
00798   struct mscabd_file *next;
00799 
00808   char *filename;
00809 
00811   unsigned int length;
00812 
00825   int attribs;
00826 
00828   char time_h;
00830   char time_m;
00832   char time_s;
00833 
00835   char date_d;
00837   char date_m;
00839   int date_y;
00840 
00842   struct mscabd_folder *folder;
00843 
00845   unsigned int offset;
00846 };
00847 
00849 #define MSCAB_ATTRIB_RDONLY   (0x01)
00850 
00851 #define MSCAB_ATTRIB_HIDDEN   (0x02)
00852 
00853 #define MSCAB_ATTRIB_SYSTEM   (0x04)
00854 
00855 #define MSCAB_ATTRIB_ARCH     (0x20)
00856 
00857 #define MSCAB_ATTRIB_EXEC     (0x40)
00858 
00859 #define MSCAB_ATTRIB_UTF_NAME (0x80)
00860 
00862 #define MSCABD_PARAM_SEARCHBUF (0)
00863 
00864 #define MSCABD_PARAM_FIXMSZIP  (1)
00865 
00866 #define MSCABD_PARAM_DECOMPBUF (2)
00867 
00869 struct mscab_compressor {
00870   int dummy; 
00871 };
00872 
00880 struct mscab_decompressor {
00901   struct mscabd_cabinet * (*open) (struct mscab_decompressor *self,
00902                                    char *filename);
00903 
00933   void (*close)(struct mscab_decompressor *self,
00934                 struct mscabd_cabinet *cab);
00935 
00970   struct mscabd_cabinet * (*search) (struct mscab_decompressor *self,
00971                                      char *filename);
00972 
01013   int (*append) (struct mscab_decompressor *self,
01014                  struct mscabd_cabinet *cab,
01015                  struct mscabd_cabinet *nextcab);
01016 
01035   int (*prepend) (struct mscab_decompressor *self,
01036                   struct mscabd_cabinet *cab,
01037                   struct mscabd_cabinet *prevcab);
01038 
01061   int (*extract)(struct mscab_decompressor *self,
01062                  struct mscabd_file *file,
01063                  char *filename);
01064 
01087   int (*set_param)(struct mscab_decompressor *self,
01088                    int param,
01089                    int value);
01090 
01102   int (*last_error)(struct mscab_decompressor *self);
01103 };
01104 
01105 /* --- support for .CHM (HTMLHelp) file format ----------------------------- */
01106 
01115 struct mschmc_file {
01117   int section;
01118 
01121   char *filename;
01122 
01125   char *chm_filename;
01126 
01130   off_t length;
01131 };
01132 
01141 struct mschmd_section {
01143   struct mschmd_header *chm;
01144 
01150   unsigned int id;
01151 };
01152 
01158 struct mschmd_sec_uncompressed {
01160   struct mschmd_section base;
01161 
01163   off_t offset;
01164 };
01165 
01171 struct mschmd_sec_mscompressed {
01173   struct mschmd_section base;
01174 
01176   struct mschmd_file *content;
01177 
01179   struct mschmd_file *control;
01180 
01182   struct mschmd_file *rtable;
01183 };
01184 
01190 struct mschmd_header {
01192   unsigned int version;
01193 
01201   unsigned int timestamp;
01202       
01207   unsigned int language;
01208 
01213   char *filename;
01214 
01216   off_t length;
01217 
01219   struct mschmd_file *files;
01220 
01227   struct mschmd_file *sysfiles;
01228 
01230   struct mschmd_sec_uncompressed sec0;
01231 
01233   struct mschmd_sec_mscompressed sec1;
01234 
01236   off_t dir_offset;
01237 
01239   unsigned int num_chunks;
01240 
01242   unsigned int chunk_size;
01243 
01245   unsigned int density;
01246 
01255   unsigned int depth;
01256 
01262   unsigned int index_root;
01263 };
01264 
01270 struct mschmd_file {
01275   struct mschmd_file *next;
01276 
01281   struct mschmd_section *section;
01282 
01284   off_t offset;
01285 
01287   off_t length;
01288 
01290   char *filename;
01291 };
01292 
01294 #define MSCHMC_ENDLIST   (0)
01295 
01296 #define MSCHMC_UNCOMP    (1)
01297 
01298 #define MSCHMC_MSCOMP    (2)
01299  
01301 #define MSCHMC_PARAM_TIMESTAMP  (0)
01302 
01303 #define MSCHMC_PARAM_LANGUAGE   (1)
01304 
01305 #define MSCHMC_PARAM_LZXWINDOW  (2)
01306 
01307 #define MSCHMC_PARAM_DENSITY    (3)
01308 
01309 #define MSCHMC_PARAM_INDEX      (4)
01310 
01318 struct mschm_compressor {
01350   int (*generate)(struct mschm_compressor *self,
01351                   struct mschmc_file file_list[],
01352                   char *output_file);
01353 
01407   int (*use_temporary_file)(struct mschm_compressor *self,
01408                             int use_temp_file,
01409                             char *temp_file);
01455   int (*set_param)(struct mschm_compressor *self,
01456                    int param,
01457                    unsigned int value);
01458 
01467   int (*last_error)(struct mschm_compressor *self);
01468 };
01469 
01477 struct mschm_decompressor {
01498   struct mschmd_header *(*open)(struct mschm_decompressor *self,
01499                                 char *filename);
01500 
01518   void (*close)(struct mschm_decompressor *self,
01519                 struct mschmd_header *chm);
01520 
01539   int (*extract)(struct mschm_decompressor *self,
01540                  struct mschmd_file *file,
01541                  char *filename);
01542 
01554   int (*last_error)(struct mschm_decompressor *self);
01555 
01578   struct mschmd_header *(*fast_open)(struct mschm_decompressor *self,
01579                                      char *filename);
01580 
01619   int (*fast_find)(struct mschm_decompressor *self,
01620                    struct mschmd_header *chm,
01621                    char *filename,
01622                    struct mschmd_file *f_ptr,
01623                    int f_size);
01624 };
01625 
01626 /* --- support for .LIT (EBook) file format -------------------------------- */
01627 
01629 struct mslit_compressor {
01630   int dummy; 
01631 };
01632 
01634 struct mslit_decompressor {
01635   int dummy; 
01636 };
01637 
01638 
01639 /* --- support for .HLP (MS Help) file format ------------------------------ */
01640 
01642 struct mshlp_compressor {
01643   int dummy; 
01644 };
01645 
01647 struct mshlp_decompressor {
01648   int dummy; 
01649 };
01650 
01651 
01652 /* --- support for SZDD file format ---------------------------------------- */
01653 
01655 #define MSSZDDC_PARAM_MISSINGCHAR (0)
01656 
01658 #define MSSZDD_FMT_NORMAL (0)
01659 
01661 #define MSSZDD_FMT_QBASIC (1)
01662 
01668 struct msszddd_header {
01670   int format;
01671 
01673   off_t length;
01674 
01682   char missing_char;
01683 };
01684 
01692 struct msszdd_compressor {
01728   int (*compress)(struct msszdd_compressor *self,
01729                   char *input,
01730                   char *output,
01731                   off_t length);
01732 
01754   int (*set_param)(struct msszdd_compressor *self,
01755                    int param,
01756                    unsigned int value);
01757 
01766   int (*last_error)(struct mschm_decompressor *self);
01767 };
01768 
01776 struct msszdd_decompressor {
01796   struct msszddd_header *(*open)(struct msszdd_decompressor *self,
01797                                  char *filename);
01798 
01812   void (*close)(struct msszdd_decompressor *self,
01813                 struct msszddd_header *szdd);
01814 
01828   int (*extract)(struct msszdd_decompressor *self,
01829                  struct msszddd_header *szdd,
01830                  char *filename);
01831 
01849   int (*decompress)(struct msszdd_decompressor *self,
01850                     char *input,
01851                     char *output);
01852 
01864   int (*last_error)(struct msszdd_decompressor *self);
01865 };
01866 
01867 /* --- support for KWAJ file format ---------------------------------------- */
01868 
01870 #define MSKWAJC_PARAM_COMP_TYPE  (0)
01871 
01875 #define MSKWAJC_PARAM_INCLUDE_LENGTH (1)
01876 
01878 #define MSKWAJ_COMP_NONE (0)
01879 
01880 #define MSKWAJ_COMP_XOR (1)
01881 
01882 #define MSKWAJ_COMP_SZDD (2)
01883 
01884 #define MSKWAJ_COMP_LZH (3)
01885 
01887 #define MSKWAJ_HDR_HASLENGTH (0x01)
01888 
01890 #define MSKWAJ_HDR_HASUNKNOWN1 (0x02)
01891 
01893 #define MSKWAJ_HDR_HASUNKNOWN2 (0x04)
01894 
01896 #define MSKWAJ_HDR_HASFILENAME (0x08)
01897 
01899 #define MSKWAJ_HDR_HASFILEEXT (0x10)
01900 
01902 #define MSKWAJ_HDR_HASEXTRATEXT (0x20)
01903 
01909 struct mskwajd_header {
01913   unsigned short comp_type;
01914 
01916   off_t data_offset;
01917 
01919   int headers;
01920 
01922   off_t length;
01923 
01925   char *filename;
01926 
01930   char *extra;
01931 
01933   unsigned short extra_length;
01934 };
01935 
01943 struct mskwaj_compressor {
01962   int (*compress)(struct mskwaj_compressor *self,
01963                   char *input,
01964                   char *output,
01965                   off_t length);
01966 
01990   int (*set_param)(struct mskwaj_compressor *self,
01991                    int param,
01992                    unsigned int value);
01993 
01994 
02012   int (*set_filename)(struct mskwaj_compressor *self,
02013                       char *filename);
02014 
02032   int (*set_extra_data)(struct mskwaj_compressor *self,
02033                         void *data,
02034                         size_t bytes);
02035 
02044   int (*last_error)(struct mschm_decompressor *self);
02045 };
02046 
02054 struct mskwaj_decompressor {
02074   struct mskwajd_header *(*open)(struct mskwaj_decompressor *self,
02075                                  char *filename);
02076 
02089   void (*close)(struct mskwaj_decompressor *self,
02090                 struct mskwajd_header *kwaj);
02091 
02105   int (*extract)(struct mskwaj_decompressor *self,
02106                  struct mskwajd_header *kwaj,
02107                  char *filename);
02108 
02126   int (*decompress)(struct mskwaj_decompressor *self,
02127                     char *input,
02128                     char *output);
02129 
02141   int (*last_error)(struct mskwaj_decompressor *self);
02142 };
02143 
02144 #ifdef __cplusplus
02145 };
02146 #endif
02147 
02148 #endif
 All Data Structures Files Functions Variables Defines