⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 extrafld.c

📁 给出了 zip 压缩算法的完整实现过程。
💻 C
📖 第 1 页 / 共 3 页
字号:
/*  Copyright (c) 1990-2005 Info-ZIP.  All rights reserved.  See the accompanying file LICENSE, version 2004-May-22 or later  (the contents of which are also included in zip.h) for terms of use.  If, for some reason, both of these files are missing, the Info-ZIP license  also may be found at:  ftp://ftp.info-zip.org/pub/infozip/license.html*//*---------------------------------------------------------------------------  extrafld.c  contains functions to build extra-fields.  ---------------------------------------------------------------------------*//*****************************************************************************//*  Includes                                                                 *//*****************************************************************************/#include <sound.h>#include "zip.h"#include "unixlike.h"#include "helpers.h"#include "pathname.h"/*****************************************************************************//*  Macros, typedefs                                                         *//*****************************************************************************//* ---------------------------------------------------------------------- *//* Add a 'MAC3' extra field to the zlist data pointed to by z.            *//* This is the (new) Info-zip extra block for Macintosh */#define EB_MAC3_HLEN          14    /* fixed length part of MAC3's header */#define EB_L_MAC3_FINFO_LEN   52    /* fixed part of MAC3 compressible data */#define EB_MAX_OF_VARDATA   1300    /* max possible datasize */#define EB_L_MAC3_SIZE    (EB_HEADSIZE + EB_MAC3_HLEN)#define EB_C_MAC3_SIZE    (EB_HEADSIZE + EB_MAC3_HLEN)#define MEMCOMPRESS_HEADER      6   /* ush compression type, ulg CRC */#define DEFLAT_WORSTCASE_ADD    5   /* byte blocktype, 2 * ush blocklength */#define MEMCOMPRESS_OVERHEAD    (MEMCOMPRESS_HEADER + DEFLAT_WORSTCASE_ADD)#define EXTRAFLD_MAX    (unsigned)0xFFFF#define EB_M3_FL_COMPRESS   0x00#define EB_M3_FL_DATFRK     0x01    /* data is data-fork */#define EB_M3_FL_NOCHANGE   0x02    /* filename will be not changed */#define EB_M3_FL_UNCMPR     0x04    /* data is 'natural' (not compressed) */#define EB_M3_FL_TIME64     0x08    /* time is coded in 64 bit */#define EB_M3_FL_NOUTC      0x10    /* only 'local' time-stamps are stored */#define EB_L_UT_SIZE    (EB_HEADSIZE + EB_UT_LEN(2))#define EB_C_UT_SIZE    (EB_HEADSIZE + EB_UT_LEN(1))/* disable compressing of extra field#define MAC_EXTRAFLD_UNCMPR       *//* ---------------------------------------------------------------------- *//* Add a 'JLEE' extra field to the zlist data pointed to by z.            *//* This is the (old) Info-zip resource-fork extra block for Macintosh(last Revision 1996-09-22) Layout made by Johnny Lee, Code made by me :-)  */#define EB_L_JLEE_LEN  40       /* fixed length of JLEE's header */#define EB_C_JLEE_LEN  40       /* fixed length of JLEE's header */#define EB_L_JLEE_SIZE    (EB_HEADSIZE + EB_L_JLEE_LEN)#define EB_C_JLEE_SIZE    (EB_HEADSIZE + EB_C_JLEE_LEN)/*****************************************************************************//*  Global Vars                                                              *//*****************************************************************************/extern MacZipGlobals    MacZip;extern unsigned long count_of_Zippedfiles;/*****************************************************************************//*  Prototypes                                                               *//*****************************************************************************/static int add_UT_ef(struct zlist far *z, iztimes *z_utim);static int add_JLEE_ef(struct zlist far *z);  /* old mac extra field */static int add_MAC3_ef(struct zlist far *z);  /* new mac extra field */static void make_extrafield_JLEE(char *l_ef);static unsigned make_extrafield_MAC3(char *ef);static char *make_EF_Head_MAC3(char *ef, unsigned compsize, ulg attrsize,                               unsigned flag);static void print_extra_info(void);void UserStop(void);/*****************************************************************************//*  Functions                                                                *//*****************************************************************************//** Set the extra-field's for each compressed file*/int set_extra_field(struct zlist far *z, iztimes *z_utim)  /* store full data in local header but just modification time stamp info     in central header */{    int retval;    Assert_it(z, "set_extra_field","")    Assert_it(z_utim, "set_extra_field","")    z_utim = z_utim;    /* Check to make sure z is valid. */    if( z == NULL ) {        return ZE_LOGIC;    }    /* Resource forks are always binary */    if (MacZip.CurrentFork == ResourceFork) z->att = BINARY;    if (noisy)        {        count_of_Zippedfiles++;        InformProgress(MacZip.RawCountOfItems, count_of_Zippedfiles );        }    /*    PrintFileInfo();    */    switch (MacZip.MacZipMode)        {        case JohnnyLee_EF:            {            retval = add_JLEE_ef( z );            if (retval != ZE_OK) return retval;            break;            }        case NewZipMode_EF:            {     /*  */#ifdef USE_EF_UT_TIME            retval = add_UT_ef(z, z_utim);            if (retval != ZE_OK) return retval;#endif            retval = add_MAC3_ef( z );            if (retval != ZE_OK) return retval;            break;            }        default:            {            printerr("Unknown Extrafieldmode", -1, -1, __LINE__, __FILE__, "");            return ZE_LOGIC;  /* function should never reach this point */            }    }    /* MacStat information is now outdated and       must be refreshed for the next file */    MacZip.isMacStatValid = false;    return ZE_OK;}#ifdef USE_EF_UT_TIME/** Build and add the Unix time extra-field. This extra field* will be included be default. Johnny Lee's implementation does* not use this kind of extra-field.* All datas are in Intel (=little-endian) format Extra field info:   - 'UT' - UNIX time extra field   This is done the same way ../unix/unix.c stores the 'UT'/'Ux' fields   (full data in local header, only modification time in central header),   with the 'M3' field added to the end and the size of the 'M3' field   in the central header. */static int add_UT_ef(struct zlist far *z, iztimes *z_utim){    char        *l_ef = NULL;    char        *c_ef = NULL;    Assert_it(z, "add_UT_ef","")#ifdef IZ_CHECK_TZ    if (!zp_tz_is_valid)        return ZE_OK;           /* skip silently if no valid TZ info */#endif    /* We can't work if there's no entry to work on. */    if( z == NULL ) {        return ZE_LOGIC;    }    /* Check to make sure we've got enough room in the extra fields. */    if( z->ext + EB_L_UT_SIZE > EXTRAFLD_MAX ||        z->cext + EB_C_UT_SIZE > EXTRAFLD_MAX ) {        return ZE_MEM;    }    /* Allocate memory for the local and central extra fields. */    if( z->extra && z->ext != 0 ) {        l_ef = (char *)realloc( z->extra, z->ext + EB_L_UT_SIZE );    } else {        l_ef = (char *)malloc( EB_L_UT_SIZE );        z->ext = 0;    }    if( l_ef == NULL ) {        return ZE_MEM;    }    z->extra = l_ef;    l_ef += z->ext;    if( z->cextra && z->cext != 0 ) {        c_ef = (char *)realloc( z->cextra, z->cext + EB_C_UT_SIZE );    } else {        c_ef = (char *)malloc( EB_C_UT_SIZE );        z->cext = 0;    }    if( c_ef == NULL ) {        return ZE_MEM;    }    z->cextra = c_ef;    c_ef += z->cext;    /* Now add the local version of the field. */    *l_ef++ = 'U';    *l_ef++ = 'T';    *l_ef++ = (char)(EB_UT_LEN(2)); /* length of data in local EF */    *l_ef++ = (char)0;    *l_ef++ = (char)(EB_UT_FL_MTIME | EB_UT_FL_CTIME);    *l_ef++ = (char)(z_utim->mtime);    *l_ef++ = (char)(z_utim->mtime >> 8);    *l_ef++ = (char)(z_utim->mtime >> 16);    *l_ef++ = (char)(z_utim->mtime >> 24);    *l_ef++ = (char)(z_utim->ctime);    *l_ef++ = (char)(z_utim->ctime >> 8);    *l_ef++ = (char)(z_utim->ctime >> 16);    *l_ef++ = (char)(z_utim->ctime >> 24);    z->ext += EB_L_UT_SIZE;    /* Now add the central version. */    memcpy(c_ef, l_ef-EB_L_UT_SIZE, EB_C_UT_SIZE);    c_ef[EB_LEN] = (char)(EB_UT_LEN(1)); /* length of data in central EF */    z->cext += EB_C_UT_SIZE;    return ZE_OK;}#endif /* USE_EF_UT_TIME *//** Build and add the old 'Johnny Lee' Mac extra field* All native datas are in Motorola (=big-endian) format*/static int add_JLEE_ef( struct zlist far *z ){    char *l_ef       = NULL;    char *c_ef       = NULL;    Assert_it(z, "add_JLEE_ef","")    /* Check to make sure we've got enough room in the extra fields. */    if ( z->ext + EB_L_JLEE_SIZE > EXTRAFLD_MAX ||        z->cext + EB_C_JLEE_SIZE > EXTRAFLD_MAX ) {        return ZE_MEM;    }    /* Allocate memory for the local extra fields. */    if ( z->extra && z->ext != 0 ) {        l_ef = (char *)realloc( z->extra, z->ext + EB_L_JLEE_SIZE );    } else {        l_ef = (char *)malloc( EB_L_JLEE_SIZE );        z->ext = 0;    }    if ( l_ef == NULL ) {        return ZE_MEM;    }    z->extra = l_ef;    l_ef += z->ext;    /* Allocate memory for the  central extra fields. */    if ( z->cextra && z->cext != 0 ) {        c_ef = (char *)realloc( z->cextra, z->cext + EB_C_JLEE_SIZE );    } else {        c_ef = (char *)malloc( EB_C_JLEE_SIZE );        z->cext = 0;    }    if ( c_ef == NULL ) {        return ZE_MEM;    }    z->cextra = c_ef;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -