libmspack
|
00001 /* libmspack -- a library for working with Microsoft compression formats. 00002 * (C) 2003-2013 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 00158 #ifndef LIB_MSPACK_H 00159 #define LIB_MSPACK_H 1 00160 00161 #ifdef __cplusplus 00162 extern "C" { 00163 #endif 00164 00165 #include <sys/types.h> 00166 #include <stdlib.h> 00167 00191 #define MSPACK_SYS_SELFTEST(result) do { \ 00192 (result) = mspack_sys_selftest_internal(sizeof(off_t)); \ 00193 } while (0) 00194 00196 extern int mspack_sys_selftest_internal(int); 00197 00229 extern int mspack_version(int entity); 00230 00232 #define MSPACK_VER_LIBRARY (0) 00233 00234 #define MSPACK_VER_SYSTEM (1) 00235 00236 #define MSPACK_VER_MSCABD (2) 00237 00238 #define MSPACK_VER_MSCABC (3) 00239 00240 #define MSPACK_VER_MSCHMD (4) 00241 00242 #define MSPACK_VER_MSCHMC (5) 00243 00244 #define MSPACK_VER_MSLITD (6) 00245 00246 #define MSPACK_VER_MSLITC (7) 00247 00248 #define MSPACK_VER_MSHLPD (8) 00249 00250 #define MSPACK_VER_MSHLPC (9) 00251 00252 #define MSPACK_VER_MSSZDDD (10) 00253 00254 #define MSPACK_VER_MSSZDDC (11) 00255 00256 #define MSPACK_VER_MSKWAJD (12) 00257 00258 #define MSPACK_VER_MSKWAJC (13) 00259 00260 #define MSPACK_VER_MSOABD (14) 00261 00262 #define MSPACK_VER_MSOABC (15) 00263 00264 /* --- file I/O abstraction ------------------------------------------------ */ 00265 00285 struct mspack_system { 00310 struct mspack_file * (*open)(struct mspack_system *self, 00311 const char *filename, 00312 int mode); 00313 00321 void (*close)(struct mspack_file *file); 00322 00334 int (*read)(struct mspack_file *file, 00335 void *buffer, 00336 int bytes); 00337 00351 int (*write)(struct mspack_file *file, 00352 void *buffer, 00353 int bytes); 00354 00378 int (*seek)(struct mspack_file *file, 00379 off_t offset, 00380 int mode); 00381 00389 off_t (*tell)(struct mspack_file *file); 00390 00405 void (*message)(struct mspack_file *file, 00406 const char *format, 00407 ...); 00408 00419 void * (*alloc)(struct mspack_system *self, 00420 size_t bytes); 00421 00428 void (*free)(void *ptr); 00429 00442 void (*copy)(void *src, 00443 void *dest, 00444 size_t bytes); 00445 00452 void *null_ptr; 00453 }; 00454 00456 #define MSPACK_SYS_OPEN_READ (0) 00457 00458 #define MSPACK_SYS_OPEN_WRITE (1) 00459 00460 #define MSPACK_SYS_OPEN_UPDATE (2) 00461 00462 #define MSPACK_SYS_OPEN_APPEND (3) 00463 00465 #define MSPACK_SYS_SEEK_START (0) 00466 00467 #define MSPACK_SYS_SEEK_CUR (1) 00468 00469 #define MSPACK_SYS_SEEK_END (2) 00470 00476 struct mspack_file { 00477 int dummy; 00478 }; 00479 00480 /* --- error codes --------------------------------------------------------- */ 00481 00483 #define MSPACK_ERR_OK (0) 00484 00485 #define MSPACK_ERR_ARGS (1) 00486 00487 #define MSPACK_ERR_OPEN (2) 00488 00489 #define MSPACK_ERR_READ (3) 00490 00491 #define MSPACK_ERR_WRITE (4) 00492 00493 #define MSPACK_ERR_SEEK (5) 00494 00495 #define MSPACK_ERR_NOMEMORY (6) 00496 00497 #define MSPACK_ERR_SIGNATURE (7) 00498 00499 #define MSPACK_ERR_DATAFORMAT (8) 00500 00501 #define MSPACK_ERR_CHECKSUM (9) 00502 00503 #define MSPACK_ERR_CRUNCH (10) 00504 00505 #define MSPACK_ERR_DECRUNCH (11) 00506 00507 /* --- functions available in library -------------------------------------- */ 00508 00513 extern struct mscab_compressor * 00514 mspack_create_cab_compressor(struct mspack_system *sys); 00515 00520 extern struct mscab_decompressor * 00521 mspack_create_cab_decompressor(struct mspack_system *sys); 00522 00526 extern void mspack_destroy_cab_compressor(struct mscab_compressor *self); 00527 00531 extern void mspack_destroy_cab_decompressor(struct mscab_decompressor *self); 00532 00533 00538 extern struct mschm_compressor * 00539 mspack_create_chm_compressor(struct mspack_system *sys); 00540 00545 extern struct mschm_decompressor * 00546 mspack_create_chm_decompressor(struct mspack_system *sys); 00547 00551 extern void mspack_destroy_chm_compressor(struct mschm_compressor *self); 00552 00556 extern void mspack_destroy_chm_decompressor(struct mschm_decompressor *self); 00557 00558 00563 extern struct mslit_compressor * 00564 mspack_create_lit_compressor(struct mspack_system *sys); 00565 00570 extern struct mslit_decompressor * 00571 mspack_create_lit_decompressor(struct mspack_system *sys); 00572 00576 extern void mspack_destroy_lit_compressor(struct mslit_compressor *self); 00577 00581 extern void mspack_destroy_lit_decompressor(struct mslit_decompressor *self); 00582 00583 00588 extern struct mshlp_compressor * 00589 mspack_create_hlp_compressor(struct mspack_system *sys); 00590 00595 extern struct mshlp_decompressor * 00596 mspack_create_hlp_decompressor(struct mspack_system *sys); 00597 00601 extern void mspack_destroy_hlp_compressor(struct mshlp_compressor *self); 00602 00606 extern void mspack_destroy_hlp_decompressor(struct mshlp_decompressor *self); 00607 00608 00613 extern struct msszdd_compressor * 00614 mspack_create_szdd_compressor(struct mspack_system *sys); 00615 00620 extern struct msszdd_decompressor * 00621 mspack_create_szdd_decompressor(struct mspack_system *sys); 00622 00626 extern void mspack_destroy_szdd_compressor(struct msszdd_compressor *self); 00627 00631 extern void mspack_destroy_szdd_decompressor(struct msszdd_decompressor *self); 00632 00633 00638 extern struct mskwaj_compressor * 00639 mspack_create_kwaj_compressor(struct mspack_system *sys); 00640 00645 extern struct mskwaj_decompressor * 00646 mspack_create_kwaj_decompressor(struct mspack_system *sys); 00647 00651 extern void mspack_destroy_kwaj_compressor(struct mskwaj_compressor *self); 00652 00656 extern void mspack_destroy_kwaj_decompressor(struct mskwaj_decompressor *self); 00657 00658 00663 extern struct msoab_compressor * 00664 mspack_create_oab_compressor(struct mspack_system *sys); 00665 00670 extern struct msoab_decompressor * 00671 mspack_create_oab_decompressor(struct mspack_system *sys); 00672 00676 extern void mspack_destroy_oab_compressor(struct msoab_compressor *self); 00677 00681 extern void mspack_destroy_oab_decompressor(struct msoab_decompressor *self); 00682 00683 00684 /* --- support for .CAB (MS Cabinet) file format --------------------------- */ 00685 00697 struct mscabd_cabinet { 00703 struct mscabd_cabinet *next; 00704 00710 const char *filename; 00711 00713 off_t base_offset; 00714 00716 unsigned int length; 00717 00719 struct mscabd_cabinet *prevcab; 00720 00722 struct mscabd_cabinet *nextcab; 00723 00725 char *prevname; 00726 00728 char *nextname; 00729 00733 char *previnfo; 00734 00738 char *nextinfo; 00739 00741 struct mscabd_file *files; 00742 00744 struct mscabd_folder *folders; 00745 00750 unsigned short set_id; 00751 00757 unsigned short set_index; 00758 00769 unsigned short header_resv; 00770 00782 int flags; 00783 }; 00784 00786 #define MSCAB_HDR_RESV_OFFSET (0x28) 00787 00789 #define MSCAB_HDR_PREVCAB (0x01) 00790 00791 #define MSCAB_HDR_NEXTCAB (0x02) 00792 00793 #define MSCAB_HDR_RESV (0x04) 00794 00804 struct mscabd_folder { 00809 struct mscabd_folder *next; 00810 00820 int comp_type; 00821 00827 unsigned int num_blocks; 00828 }; 00829 00837 #define MSCABD_COMP_METHOD(comp_type) ((comp_type) & 0x0F) 00838 00845 #define MSCABD_COMP_LEVEL(comp_type) (((comp_type) >> 8) & 0x1F) 00846 00848 #define MSCAB_COMP_NONE (0) 00849 00850 #define MSCAB_COMP_MSZIP (1) 00851 00852 #define MSCAB_COMP_QUANTUM (2) 00853 00854 #define MSCAB_COMP_LZX (3) 00855 00861 struct mscabd_file { 00866 struct mscabd_file *next; 00867 00876 char *filename; 00877 00879 unsigned int length; 00880 00893 int attribs; 00894 00896 char time_h; 00898 char time_m; 00900 char time_s; 00901 00903 char date_d; 00905 char date_m; 00907 int date_y; 00908 00910 struct mscabd_folder *folder; 00911 00913 unsigned int offset; 00914 }; 00915 00917 #define MSCAB_ATTRIB_RDONLY (0x01) 00918 00919 #define MSCAB_ATTRIB_HIDDEN (0x02) 00920 00921 #define MSCAB_ATTRIB_SYSTEM (0x04) 00922 00923 #define MSCAB_ATTRIB_ARCH (0x20) 00924 00925 #define MSCAB_ATTRIB_EXEC (0x40) 00926 00927 #define MSCAB_ATTRIB_UTF_NAME (0x80) 00928 00930 #define MSCABD_PARAM_SEARCHBUF (0) 00931 00932 #define MSCABD_PARAM_FIXMSZIP (1) 00933 00934 #define MSCABD_PARAM_DECOMPBUF (2) 00935 00937 struct mscab_compressor { 00938 int dummy; 00939 }; 00940 00948 struct mscab_decompressor { 00969 struct mscabd_cabinet * (*open) (struct mscab_decompressor *self, 00970 const char *filename); 00971 01001 void (*close)(struct mscab_decompressor *self, 01002 struct mscabd_cabinet *cab); 01003 01038 struct mscabd_cabinet * (*search) (struct mscab_decompressor *self, 01039 const char *filename); 01040 01081 int (*append) (struct mscab_decompressor *self, 01082 struct mscabd_cabinet *cab, 01083 struct mscabd_cabinet *nextcab); 01084 01103 int (*prepend) (struct mscab_decompressor *self, 01104 struct mscabd_cabinet *cab, 01105 struct mscabd_cabinet *prevcab); 01106 01129 int (*extract)(struct mscab_decompressor *self, 01130 struct mscabd_file *file, 01131 const char *filename); 01132 01155 int (*set_param)(struct mscab_decompressor *self, 01156 int param, 01157 int value); 01158 01170 int (*last_error)(struct mscab_decompressor *self); 01171 }; 01172 01173 /* --- support for .CHM (HTMLHelp) file format ----------------------------- */ 01174 01183 struct mschmc_file { 01185 int section; 01186 01189 const char *filename; 01190 01193 char *chm_filename; 01194 01198 off_t length; 01199 }; 01200 01209 struct mschmd_section { 01211 struct mschmd_header *chm; 01212 01218 unsigned int id; 01219 }; 01220 01226 struct mschmd_sec_uncompressed { 01228 struct mschmd_section base; 01229 01231 off_t offset; 01232 }; 01233 01239 struct mschmd_sec_mscompressed { 01241 struct mschmd_section base; 01242 01244 struct mschmd_file *content; 01245 01247 struct mschmd_file *control; 01248 01250 struct mschmd_file *rtable; 01251 01255 struct mschmd_file *spaninfo; 01256 }; 01257 01263 struct mschmd_header { 01265 unsigned int version; 01266 01274 unsigned int timestamp; 01275 01280 unsigned int language; 01281 01286 const char *filename; 01287 01289 off_t length; 01290 01292 struct mschmd_file *files; 01293 01300 struct mschmd_file *sysfiles; 01301 01303 struct mschmd_sec_uncompressed sec0; 01304 01306 struct mschmd_sec_mscompressed sec1; 01307 01309 off_t dir_offset; 01310 01312 unsigned int num_chunks; 01313 01315 unsigned int chunk_size; 01316 01318 unsigned int density; 01319 01328 unsigned int depth; 01329 01335 unsigned int index_root; 01336 01341 unsigned int first_pmgl; 01342 01347 unsigned int last_pmgl; 01348 01353 unsigned char **chunk_cache; 01354 }; 01355 01361 struct mschmd_file { 01366 struct mschmd_file *next; 01367 01372 struct mschmd_section *section; 01373 01375 off_t offset; 01376 01378 off_t length; 01379 01381 char *filename; 01382 }; 01383 01385 #define MSCHMC_ENDLIST (0) 01386 01387 #define MSCHMC_UNCOMP (1) 01388 01389 #define MSCHMC_MSCOMP (2) 01390 01392 #define MSCHMC_PARAM_TIMESTAMP (0) 01393 01394 #define MSCHMC_PARAM_LANGUAGE (1) 01395 01396 #define MSCHMC_PARAM_LZXWINDOW (2) 01397 01398 #define MSCHMC_PARAM_DENSITY (3) 01399 01400 #define MSCHMC_PARAM_INDEX (4) 01401 01409 struct mschm_compressor { 01441 int (*generate)(struct mschm_compressor *self, 01442 struct mschmc_file file_list[], 01443 const char *output_file); 01444 01498 int (*use_temporary_file)(struct mschm_compressor *self, 01499 int use_temp_file, 01500 const char *temp_file); 01546 int (*set_param)(struct mschm_compressor *self, 01547 int param, 01548 unsigned int value); 01549 01558 int (*last_error)(struct mschm_compressor *self); 01559 }; 01560 01568 struct mschm_decompressor { 01589 struct mschmd_header *(*open)(struct mschm_decompressor *self, 01590 const char *filename); 01591 01609 void (*close)(struct mschm_decompressor *self, 01610 struct mschmd_header *chm); 01611 01630 int (*extract)(struct mschm_decompressor *self, 01631 struct mschmd_file *file, 01632 const char *filename); 01633 01645 int (*last_error)(struct mschm_decompressor *self); 01646 01669 struct mschmd_header *(*fast_open)(struct mschm_decompressor *self, 01670 const char *filename); 01671 01710 int (*fast_find)(struct mschm_decompressor *self, 01711 struct mschmd_header *chm, 01712 const char *filename, 01713 struct mschmd_file *f_ptr, 01714 int f_size); 01715 }; 01716 01717 /* --- support for .LIT (EBook) file format -------------------------------- */ 01718 01720 struct mslit_compressor { 01721 int dummy; 01722 }; 01723 01725 struct mslit_decompressor { 01726 int dummy; 01727 }; 01728 01729 01730 /* --- support for .HLP (MS Help) file format ------------------------------ */ 01731 01733 struct mshlp_compressor { 01734 int dummy; 01735 }; 01736 01738 struct mshlp_decompressor { 01739 int dummy; 01740 }; 01741 01742 01743 /* --- support for SZDD file format ---------------------------------------- */ 01744 01746 #define MSSZDDC_PARAM_MISSINGCHAR (0) 01747 01749 #define MSSZDD_FMT_NORMAL (0) 01750 01752 #define MSSZDD_FMT_QBASIC (1) 01753 01759 struct msszddd_header { 01761 int format; 01762 01764 off_t length; 01765 01773 char missing_char; 01774 }; 01775 01783 struct msszdd_compressor { 01819 int (*compress)(struct msszdd_compressor *self, 01820 const char *input, 01821 const char *output, 01822 off_t length); 01823 01845 int (*set_param)(struct msszdd_compressor *self, 01846 int param, 01847 unsigned int value); 01848 01857 int (*last_error)(struct mschm_decompressor *self); 01858 }; 01859 01867 struct msszdd_decompressor { 01887 struct msszddd_header *(*open)(struct msszdd_decompressor *self, 01888 const char *filename); 01889 01903 void (*close)(struct msszdd_decompressor *self, 01904 struct msszddd_header *szdd); 01905 01919 int (*extract)(struct msszdd_decompressor *self, 01920 struct msszddd_header *szdd, 01921 const char *filename); 01922 01940 int (*decompress)(struct msszdd_decompressor *self, 01941 const char *input, 01942 const char *output); 01943 01955 int (*last_error)(struct msszdd_decompressor *self); 01956 }; 01957 01958 /* --- support for KWAJ file format ---------------------------------------- */ 01959 01961 #define MSKWAJC_PARAM_COMP_TYPE (0) 01962 01966 #define MSKWAJC_PARAM_INCLUDE_LENGTH (1) 01967 01969 #define MSKWAJ_COMP_NONE (0) 01970 01971 #define MSKWAJ_COMP_XOR (1) 01972 01973 #define MSKWAJ_COMP_SZDD (2) 01974 01975 #define MSKWAJ_COMP_LZH (3) 01976 01977 #define MSKWAJ_COMP_MSZIP (4) 01978 01980 #define MSKWAJ_HDR_HASLENGTH (0x01) 01981 01983 #define MSKWAJ_HDR_HASUNKNOWN1 (0x02) 01984 01986 #define MSKWAJ_HDR_HASUNKNOWN2 (0x04) 01987 01989 #define MSKWAJ_HDR_HASFILENAME (0x08) 01990 01992 #define MSKWAJ_HDR_HASFILEEXT (0x10) 01993 01995 #define MSKWAJ_HDR_HASEXTRATEXT (0x20) 01996 02002 struct mskwajd_header { 02006 unsigned short comp_type; 02007 02009 off_t data_offset; 02010 02012 int headers; 02013 02015 off_t length; 02016 02018 char *filename; 02019 02023 char *extra; 02024 02026 unsigned short extra_length; 02027 }; 02028 02036 struct mskwaj_compressor { 02055 int (*compress)(struct mskwaj_compressor *self, 02056 const char *input, 02057 const char *output, 02058 off_t length); 02059 02083 int (*set_param)(struct mskwaj_compressor *self, 02084 int param, 02085 unsigned int value); 02086 02087 02105 int (*set_filename)(struct mskwaj_compressor *self, 02106 const char *filename); 02107 02125 int (*set_extra_data)(struct mskwaj_compressor *self, 02126 void *data, 02127 size_t bytes); 02128 02137 int (*last_error)(struct mschm_decompressor *self); 02138 }; 02139 02147 struct mskwaj_decompressor { 02167 struct mskwajd_header *(*open)(struct mskwaj_decompressor *self, 02168 const char *filename); 02169 02182 void (*close)(struct mskwaj_decompressor *self, 02183 struct mskwajd_header *kwaj); 02184 02198 int (*extract)(struct mskwaj_decompressor *self, 02199 struct mskwajd_header *kwaj, 02200 const char *filename); 02201 02219 int (*decompress)(struct mskwaj_decompressor *self, 02220 const char *input, 02221 const char *output); 02222 02234 int (*last_error)(struct mskwaj_decompressor *self); 02235 }; 02236 02237 /* --- support for .LZX (Offline Address Book) file format ----------------- */ 02238 02246 struct msoab_compressor { 02261 int (*compress) (struct msoab_compressor *self, 02262 const char *input, 02263 const char *output); 02264 02285 int (*compress_incremental) (struct msoab_compressor *self, 02286 const char *input, 02287 const char *base, 02288 const char *output); 02289 }; 02290 02298 struct msoab_decompressor { 02314 int (*decompress) (struct msoab_decompressor *self, 02315 const char *input, 02316 const char *output); 02317 02343 int (*decompress_incremental) (struct msoab_decompressor *self, 02344 const char *input, 02345 const char *base, 02346 const char *output); 02347 }; 02348 02349 #ifdef __cplusplus 02350 } 02351 #endif 02352 02353 #endif