libmspack
|
00001 /* libmspack -- a library for working with Microsoft compression formats. 00002 * (C) 2003-2011 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 00153 #ifndef LIB_MSPACK_H 00154 #define LIB_MSPACK_H 1 00155 00156 #ifdef __cplusplus 00157 extern "C" { 00158 #endif 00159 00160 #include <sys/types.h> 00161 #include <stdlib.h> 00162 00186 #define MSPACK_SYS_SELFTEST(result) do { \ 00187 (result) = mspack_sys_selftest_internal(sizeof(off_t)); \ 00188 } while (0) 00189 00191 extern int mspack_sys_selftest_internal(int); 00192 00222 extern int mspack_version(int entity); 00223 00225 #define MSPACK_VER_LIBRARY (0) 00226 00227 #define MSPACK_VER_SYSTEM (1) 00228 00229 #define MSPACK_VER_MSCABD (2) 00230 00231 #define MSPACK_VER_MSCABC (3) 00232 00233 #define MSPACK_VER_MSCHMD (4) 00234 00235 #define MSPACK_VER_MSCHMC (5) 00236 00237 #define MSPACK_VER_MSLITD (6) 00238 00239 #define MSPACK_VER_MSLITC (7) 00240 00241 #define MSPACK_VER_MSHLPD (8) 00242 00243 #define MSPACK_VER_MSHLPC (9) 00244 00245 #define MSPACK_VER_MSSZDDD (10) 00246 00247 #define MSPACK_VER_MSSZDDC (11) 00248 00249 #define MSPACK_VER_MSKWAJD (12) 00250 00251 #define MSPACK_VER_MSKWAJC (13) 00252 00253 /* --- file I/O abstraction ------------------------------------------------ */ 00254 00274 struct mspack_system { 00299 struct mspack_file * (*open)(struct mspack_system *self, 00300 const char *filename, 00301 int mode); 00302 00310 void (*close)(struct mspack_file *file); 00311 00323 int (*read)(struct mspack_file *file, 00324 void *buffer, 00325 int bytes); 00326 00340 int (*write)(struct mspack_file *file, 00341 void *buffer, 00342 int bytes); 00343 00367 int (*seek)(struct mspack_file *file, 00368 off_t offset, 00369 int mode); 00370 00378 off_t (*tell)(struct mspack_file *file); 00379 00394 void (*message)(struct mspack_file *file, 00395 const char *format, 00396 ...); 00397 00408 void * (*alloc)(struct mspack_system *self, 00409 size_t bytes); 00410 00417 void (*free)(void *ptr); 00418 00431 void (*copy)(void *src, 00432 void *dest, 00433 size_t bytes); 00434 00441 void *null_ptr; 00442 }; 00443 00445 #define MSPACK_SYS_OPEN_READ (0) 00446 00447 #define MSPACK_SYS_OPEN_WRITE (1) 00448 00449 #define MSPACK_SYS_OPEN_UPDATE (2) 00450 00451 #define MSPACK_SYS_OPEN_APPEND (3) 00452 00454 #define MSPACK_SYS_SEEK_START (0) 00455 00456 #define MSPACK_SYS_SEEK_CUR (1) 00457 00458 #define MSPACK_SYS_SEEK_END (2) 00459 00465 struct mspack_file { 00466 int dummy; 00467 }; 00468 00469 /* --- error codes --------------------------------------------------------- */ 00470 00472 #define MSPACK_ERR_OK (0) 00473 00474 #define MSPACK_ERR_ARGS (1) 00475 00476 #define MSPACK_ERR_OPEN (2) 00477 00478 #define MSPACK_ERR_READ (3) 00479 00480 #define MSPACK_ERR_WRITE (4) 00481 00482 #define MSPACK_ERR_SEEK (5) 00483 00484 #define MSPACK_ERR_NOMEMORY (6) 00485 00486 #define MSPACK_ERR_SIGNATURE (7) 00487 00488 #define MSPACK_ERR_DATAFORMAT (8) 00489 00490 #define MSPACK_ERR_CHECKSUM (9) 00491 00492 #define MSPACK_ERR_CRUNCH (10) 00493 00494 #define MSPACK_ERR_DECRUNCH (11) 00495 00496 /* --- functions available in library -------------------------------------- */ 00497 00502 extern struct mscab_compressor * 00503 mspack_create_cab_compressor(struct mspack_system *sys); 00504 00509 extern struct mscab_decompressor * 00510 mspack_create_cab_decompressor(struct mspack_system *sys); 00511 00515 extern void mspack_destroy_cab_compressor(struct mscab_compressor *self); 00516 00520 extern void mspack_destroy_cab_decompressor(struct mscab_decompressor *self); 00521 00522 00527 extern struct mschm_compressor * 00528 mspack_create_chm_compressor(struct mspack_system *sys); 00529 00534 extern struct mschm_decompressor * 00535 mspack_create_chm_decompressor(struct mspack_system *sys); 00536 00540 extern void mspack_destroy_chm_compressor(struct mschm_compressor *self); 00541 00545 extern void mspack_destroy_chm_decompressor(struct mschm_decompressor *self); 00546 00547 00552 extern struct mslit_compressor * 00553 mspack_create_lit_compressor(struct mspack_system *sys); 00554 00559 extern struct mslit_decompressor * 00560 mspack_create_lit_decompressor(struct mspack_system *sys); 00561 00565 extern void mspack_destroy_lit_compressor(struct mslit_compressor *self); 00566 00570 extern void mspack_destroy_lit_decompressor(struct mslit_decompressor *self); 00571 00572 00577 extern struct mshlp_compressor * 00578 mspack_create_hlp_compressor(struct mspack_system *sys); 00579 00584 extern struct mshlp_decompressor * 00585 mspack_create_hlp_decompressor(struct mspack_system *sys); 00586 00590 extern void mspack_destroy_hlp_compressor(struct mshlp_compressor *self); 00591 00595 extern void mspack_destroy_hlp_decompressor(struct mshlp_decompressor *self); 00596 00597 00602 extern struct msszdd_compressor * 00603 mspack_create_szdd_compressor(struct mspack_system *sys); 00604 00609 extern struct msszdd_decompressor * 00610 mspack_create_szdd_decompressor(struct mspack_system *sys); 00611 00615 extern void mspack_destroy_szdd_compressor(struct msszdd_compressor *self); 00616 00620 extern void mspack_destroy_szdd_decompressor(struct msszdd_decompressor *self); 00621 00622 00627 extern struct mskwaj_compressor * 00628 mspack_create_kwaj_compressor(struct mspack_system *sys); 00629 00634 extern struct mskwaj_decompressor * 00635 mspack_create_kwaj_decompressor(struct mspack_system *sys); 00636 00640 extern void mspack_destroy_kwaj_compressor(struct mskwaj_compressor *self); 00641 00645 extern void mspack_destroy_kwaj_decompressor(struct mskwaj_decompressor *self); 00646 00647 00648 /* --- support for .CAB (MS Cabinet) file format --------------------------- */ 00649 00661 struct mscabd_cabinet { 00667 struct mscabd_cabinet *next; 00668 00674 const char *filename; 00675 00677 off_t base_offset; 00678 00680 unsigned int length; 00681 00683 struct mscabd_cabinet *prevcab; 00684 00686 struct mscabd_cabinet *nextcab; 00687 00689 char *prevname; 00690 00692 char *nextname; 00693 00697 char *previnfo; 00698 00702 char *nextinfo; 00703 00705 struct mscabd_file *files; 00706 00708 struct mscabd_folder *folders; 00709 00714 unsigned short set_id; 00715 00721 unsigned short set_index; 00722 00733 unsigned short header_resv; 00734 00746 int flags; 00747 }; 00748 00750 #define MSCAB_HDR_RESV_OFFSET (0x28) 00751 00753 #define MSCAB_HDR_PREVCAB (0x01) 00754 00755 #define MSCAB_HDR_NEXTCAB (0x02) 00756 00757 #define MSCAB_HDR_RESV (0x04) 00758 00768 struct mscabd_folder { 00773 struct mscabd_folder *next; 00774 00784 int comp_type; 00785 00791 unsigned int num_blocks; 00792 }; 00793 00801 #define MSCABD_COMP_METHOD(comp_type) ((comp_type) & 0x0F) 00802 00809 #define MSCABD_COMP_LEVEL(comp_type) (((comp_type) >> 8) & 0x1F) 00810 00812 #define MSCAB_COMP_NONE (0) 00813 00814 #define MSCAB_COMP_MSZIP (1) 00815 00816 #define MSCAB_COMP_QUANTUM (2) 00817 00818 #define MSCAB_COMP_LZX (3) 00819 00825 struct mscabd_file { 00830 struct mscabd_file *next; 00831 00840 char *filename; 00841 00843 unsigned int length; 00844 00857 int attribs; 00858 00860 char time_h; 00862 char time_m; 00864 char time_s; 00865 00867 char date_d; 00869 char date_m; 00871 int date_y; 00872 00874 struct mscabd_folder *folder; 00875 00877 unsigned int offset; 00878 }; 00879 00881 #define MSCAB_ATTRIB_RDONLY (0x01) 00882 00883 #define MSCAB_ATTRIB_HIDDEN (0x02) 00884 00885 #define MSCAB_ATTRIB_SYSTEM (0x04) 00886 00887 #define MSCAB_ATTRIB_ARCH (0x20) 00888 00889 #define MSCAB_ATTRIB_EXEC (0x40) 00890 00891 #define MSCAB_ATTRIB_UTF_NAME (0x80) 00892 00894 #define MSCABD_PARAM_SEARCHBUF (0) 00895 00896 #define MSCABD_PARAM_FIXMSZIP (1) 00897 00898 #define MSCABD_PARAM_DECOMPBUF (2) 00899 00901 struct mscab_compressor { 00902 int dummy; 00903 }; 00904 00912 struct mscab_decompressor { 00933 struct mscabd_cabinet * (*open) (struct mscab_decompressor *self, 00934 const char *filename); 00935 00965 void (*close)(struct mscab_decompressor *self, 00966 struct mscabd_cabinet *cab); 00967 01002 struct mscabd_cabinet * (*search) (struct mscab_decompressor *self, 01003 const char *filename); 01004 01045 int (*append) (struct mscab_decompressor *self, 01046 struct mscabd_cabinet *cab, 01047 struct mscabd_cabinet *nextcab); 01048 01067 int (*prepend) (struct mscab_decompressor *self, 01068 struct mscabd_cabinet *cab, 01069 struct mscabd_cabinet *prevcab); 01070 01093 int (*extract)(struct mscab_decompressor *self, 01094 struct mscabd_file *file, 01095 const char *filename); 01096 01119 int (*set_param)(struct mscab_decompressor *self, 01120 int param, 01121 int value); 01122 01134 int (*last_error)(struct mscab_decompressor *self); 01135 }; 01136 01137 /* --- support for .CHM (HTMLHelp) file format ----------------------------- */ 01138 01147 struct mschmc_file { 01149 int section; 01150 01153 const char *filename; 01154 01157 char *chm_filename; 01158 01162 off_t length; 01163 }; 01164 01173 struct mschmd_section { 01175 struct mschmd_header *chm; 01176 01182 unsigned int id; 01183 }; 01184 01190 struct mschmd_sec_uncompressed { 01192 struct mschmd_section base; 01193 01195 off_t offset; 01196 }; 01197 01203 struct mschmd_sec_mscompressed { 01205 struct mschmd_section base; 01206 01208 struct mschmd_file *content; 01209 01211 struct mschmd_file *control; 01212 01214 struct mschmd_file *rtable; 01215 01219 struct mschmd_file *spaninfo; 01220 }; 01221 01227 struct mschmd_header { 01229 unsigned int version; 01230 01238 unsigned int timestamp; 01239 01244 unsigned int language; 01245 01250 const char *filename; 01251 01253 off_t length; 01254 01256 struct mschmd_file *files; 01257 01264 struct mschmd_file *sysfiles; 01265 01267 struct mschmd_sec_uncompressed sec0; 01268 01270 struct mschmd_sec_mscompressed sec1; 01271 01273 off_t dir_offset; 01274 01276 unsigned int num_chunks; 01277 01279 unsigned int chunk_size; 01280 01282 unsigned int density; 01283 01292 unsigned int depth; 01293 01299 unsigned int index_root; 01300 01305 unsigned int first_pmgl; 01306 01311 unsigned int last_pmgl; 01312 01317 unsigned char **chunk_cache; 01318 }; 01319 01325 struct mschmd_file { 01330 struct mschmd_file *next; 01331 01336 struct mschmd_section *section; 01337 01339 off_t offset; 01340 01342 off_t length; 01343 01345 char *filename; 01346 }; 01347 01349 #define MSCHMC_ENDLIST (0) 01350 01351 #define MSCHMC_UNCOMP (1) 01352 01353 #define MSCHMC_MSCOMP (2) 01354 01356 #define MSCHMC_PARAM_TIMESTAMP (0) 01357 01358 #define MSCHMC_PARAM_LANGUAGE (1) 01359 01360 #define MSCHMC_PARAM_LZXWINDOW (2) 01361 01362 #define MSCHMC_PARAM_DENSITY (3) 01363 01364 #define MSCHMC_PARAM_INDEX (4) 01365 01373 struct mschm_compressor { 01405 int (*generate)(struct mschm_compressor *self, 01406 struct mschmc_file file_list[], 01407 const char *output_file); 01408 01462 int (*use_temporary_file)(struct mschm_compressor *self, 01463 int use_temp_file, 01464 const char *temp_file); 01510 int (*set_param)(struct mschm_compressor *self, 01511 int param, 01512 unsigned int value); 01513 01522 int (*last_error)(struct mschm_compressor *self); 01523 }; 01524 01532 struct mschm_decompressor { 01553 struct mschmd_header *(*open)(struct mschm_decompressor *self, 01554 const char *filename); 01555 01573 void (*close)(struct mschm_decompressor *self, 01574 struct mschmd_header *chm); 01575 01594 int (*extract)(struct mschm_decompressor *self, 01595 struct mschmd_file *file, 01596 const char *filename); 01597 01609 int (*last_error)(struct mschm_decompressor *self); 01610 01633 struct mschmd_header *(*fast_open)(struct mschm_decompressor *self, 01634 const char *filename); 01635 01674 int (*fast_find)(struct mschm_decompressor *self, 01675 struct mschmd_header *chm, 01676 const char *filename, 01677 struct mschmd_file *f_ptr, 01678 int f_size); 01679 }; 01680 01681 /* --- support for .LIT (EBook) file format -------------------------------- */ 01682 01684 struct mslit_compressor { 01685 int dummy; 01686 }; 01687 01689 struct mslit_decompressor { 01690 int dummy; 01691 }; 01692 01693 01694 /* --- support for .HLP (MS Help) file format ------------------------------ */ 01695 01697 struct mshlp_compressor { 01698 int dummy; 01699 }; 01700 01702 struct mshlp_decompressor { 01703 int dummy; 01704 }; 01705 01706 01707 /* --- support for SZDD file format ---------------------------------------- */ 01708 01710 #define MSSZDDC_PARAM_MISSINGCHAR (0) 01711 01713 #define MSSZDD_FMT_NORMAL (0) 01714 01716 #define MSSZDD_FMT_QBASIC (1) 01717 01723 struct msszddd_header { 01725 int format; 01726 01728 off_t length; 01729 01737 char missing_char; 01738 }; 01739 01747 struct msszdd_compressor { 01783 int (*compress)(struct msszdd_compressor *self, 01784 const char *input, 01785 const char *output, 01786 off_t length); 01787 01809 int (*set_param)(struct msszdd_compressor *self, 01810 int param, 01811 unsigned int value); 01812 01821 int (*last_error)(struct mschm_decompressor *self); 01822 }; 01823 01831 struct msszdd_decompressor { 01851 struct msszddd_header *(*open)(struct msszdd_decompressor *self, 01852 const char *filename); 01853 01867 void (*close)(struct msszdd_decompressor *self, 01868 struct msszddd_header *szdd); 01869 01883 int (*extract)(struct msszdd_decompressor *self, 01884 struct msszddd_header *szdd, 01885 const char *filename); 01886 01904 int (*decompress)(struct msszdd_decompressor *self, 01905 const char *input, 01906 const char *output); 01907 01919 int (*last_error)(struct msszdd_decompressor *self); 01920 }; 01921 01922 /* --- support for KWAJ file format ---------------------------------------- */ 01923 01925 #define MSKWAJC_PARAM_COMP_TYPE (0) 01926 01930 #define MSKWAJC_PARAM_INCLUDE_LENGTH (1) 01931 01933 #define MSKWAJ_COMP_NONE (0) 01934 01935 #define MSKWAJ_COMP_XOR (1) 01936 01937 #define MSKWAJ_COMP_SZDD (2) 01938 01939 #define MSKWAJ_COMP_LZH (3) 01940 01942 #define MSKWAJ_HDR_HASLENGTH (0x01) 01943 01945 #define MSKWAJ_HDR_HASUNKNOWN1 (0x02) 01946 01948 #define MSKWAJ_HDR_HASUNKNOWN2 (0x04) 01949 01951 #define MSKWAJ_HDR_HASFILENAME (0x08) 01952 01954 #define MSKWAJ_HDR_HASFILEEXT (0x10) 01955 01957 #define MSKWAJ_HDR_HASEXTRATEXT (0x20) 01958 01964 struct mskwajd_header { 01968 unsigned short comp_type; 01969 01971 off_t data_offset; 01972 01974 int headers; 01975 01977 off_t length; 01978 01980 char *filename; 01981 01985 char *extra; 01986 01988 unsigned short extra_length; 01989 }; 01990 01998 struct mskwaj_compressor { 02017 int (*compress)(struct mskwaj_compressor *self, 02018 const char *input, 02019 const char *output, 02020 off_t length); 02021 02045 int (*set_param)(struct mskwaj_compressor *self, 02046 int param, 02047 unsigned int value); 02048 02049 02067 int (*set_filename)(struct mskwaj_compressor *self, 02068 const char *filename); 02069 02087 int (*set_extra_data)(struct mskwaj_compressor *self, 02088 void *data, 02089 size_t bytes); 02090 02099 int (*last_error)(struct mschm_decompressor *self); 02100 }; 02101 02109 struct mskwaj_decompressor { 02129 struct mskwajd_header *(*open)(struct mskwaj_decompressor *self, 02130 const char *filename); 02131 02144 void (*close)(struct mskwaj_decompressor *self, 02145 struct mskwajd_header *kwaj); 02146 02160 int (*extract)(struct mskwaj_decompressor *self, 02161 struct mskwajd_header *kwaj, 02162 const char *filename); 02163 02181 int (*decompress)(struct mskwaj_decompressor *self, 02182 const char *input, 02183 const char *output); 02184 02196 int (*last_error)(struct mskwaj_decompressor *self); 02197 }; 02198 02199 #ifdef __cplusplus 02200 } 02201 #endif 02202 02203 #endif