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

📄 beos.c

📁 给出了 zip 压缩算法的完整实现过程。
💻 C
📖 第 1 页 / 共 2 页
字号:
                zipwarn( "attribute size mismatch: ", buff );            }            /* Wave my magic wand... this swaps all the Be types to big- */            /* endian automagically.                                     */            (void)swap_data( fa_info.type, ptr, fa_info.size,                             B_SWAP_HOST_TO_BENDIAN );            ptr += fa_info.size;        }        fa_ent = fs_read_attr_dir( fa_dir );    }    /* ----------------------------------------------------------------- */    /* Close the attribute directory.                                    */    retval = fs_close_attr_dir( fa_dir );    /* TODO: check retval != EOK */    /* ----------------------------------------------------------------- */    /* If the buffer is too big, shrink it.                              */    if( attrs_size < INITIAL_BUFF_SIZE ) {        *attr_buff = (char *)realloc( *attr_buff, attrs_size );        if( *attr_buff == NULL ) {            /* This really shouldn't happen... */            close( fd );            return ENOMEM;        }    }    *total_size = attrs_size;    close( fd );    return EOK;}/* ---------------------------------------------------------------------- *//* Add a 'UT' extra field to the zlist data pointed to by z.              */#define EB_L_UT_SIZE    (EB_HEADSIZE + EB_UT_LEN(2))#define EB_C_UT_SIZE    (EB_HEADSIZE + EB_UT_LEN(1))local int add_UT_ef( struct zlist far *z ){    char        *l_ef = NULL;    char        *c_ef = NULL;    struct stat  s;#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 > USHRT_MAX ||        z->cext + EB_C_UT_SIZE > USHRT_MAX ) {        return ZE_MEM;    }    /* stat() the file (or the symlink) to get the data; if we can't get */    /* the data, there's no point in trying to fill out the fields.      */    if(LSSTAT( z->name, &s ) ) {        return ZE_OPEN;    }    /* 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_ATIME);    *l_ef++ = (char)(s.st_mtime);    *l_ef++ = (char)(s.st_mtime >> 8);    *l_ef++ = (char)(s.st_mtime >> 16);    *l_ef++ = (char)(s.st_mtime >> 24);    *l_ef++ = (char)(s.st_atime);    *l_ef++ = (char)(s.st_atime >> 8);    *l_ef++ = (char)(s.st_atime >> 16);    *l_ef++ = (char)(s.st_atime >> 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;}/* ---------------------------------------------------------------------- *//* Add a 'Ux' extra field to the zlist data pointed to by z.              */#define EB_L_UX2_SIZE   (EB_HEADSIZE + EB_UX2_MINLEN)#define EB_C_UX2_SIZE   (EB_HEADSIZE)local int add_Ux_ef( struct zlist far *z ){    char        *l_ef = NULL;    char        *c_ef = NULL;    struct stat  s;    /* Check to make sure we've got enough room in the extra fields. */    if( z->ext + EB_L_UX2_SIZE > USHRT_MAX ||        z->cext + EB_C_UX2_SIZE > USHRT_MAX ) {        return ZE_MEM;    }    /* stat() the file (or the symlink) to get the data; if we can't get */    /* the data, there's no point in trying to fill out the fields.      */    if(LSSTAT( z->name, &s ) ) {        return ZE_OPEN;    }    /* 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_UX2_SIZE );    } else {        l_ef = (char *)malloc( EB_L_UX2_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_UX2_SIZE );    } else {        c_ef = (char *)malloc( EB_C_UX2_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++ = 'x';    *l_ef++ = (char)(EB_UX2_MINLEN);    *l_ef++ = (char)(EB_UX2_MINLEN >> 8);    *l_ef++ = (char)(s.st_uid);    *l_ef++ = (char)(s.st_uid >> 8);    *l_ef++ = (char)(s.st_gid);    *l_ef++ = (char)(s.st_gid >> 8);    z->ext += EB_L_UX2_SIZE;    /* Now add the central version of the field. */    *c_ef++ = 'U';    *c_ef++ = 'x';    *c_ef++ = 0;    *c_ef++ = 0;    z->cext += EB_C_UX2_SIZE;    return ZE_OK;}/* ---------------------------------------------------------------------- *//* Add a 'Be' extra field to the zlist data pointed to by z.              */#define EB_L_BE_SIZE    (EB_HEADSIZE + EB_L_BE_LEN) /* + attr size */#define EB_C_BE_SIZE    (EB_HEADSIZE + EB_C_BE_LEN)/* maximum memcompress overhead is the sum of the compression header length *//* (6 = ush compression type, ulg CRC) and the worstcase deflate overhead   *//* when uncompressible data are kept in 2 "stored" blocks (5 per block =    *//* byte blocktype + 2 * ush blocklength) */#define MEMCOMPRESS_OVERHEAD    (EB_MEMCMPR_HSIZ + EB_DEFLAT_EXTRA)local int add_Be_ef( struct zlist far *z ){    char *l_ef       = NULL;    char *c_ef       = NULL;    char *attrbuff   = NULL;    off_t attrsize   = 0;    char *compbuff   = NULL;    ush   compsize   = 0;    uch   flags      = 0;    /* Check to make sure we've got enough room in the extra fields. */    if( z->ext + EB_L_BE_SIZE > USHRT_MAX ||        z->cext + EB_C_BE_SIZE > USHRT_MAX ) {        return ZE_MEM;    }    /* Attempt to load up a buffer full of the file's attributes. */    {        int retval;        retval = get_attr_dir( z->name, &attrbuff, &attrsize );        if( retval != EOK ) {            return ZE_OPEN;        }        if( attrsize == 0 ) {            return ZE_OK;        }        if( attrbuff == NULL ) {            return ZE_LOGIC;        }        /* Check for way too much data. */        if( attrsize > (off_t)ULONG_MAX ) {            zipwarn( "uncompressed attributes truncated", "" );            attrsize = (off_t)(ULONG_MAX - MEMCOMPRESS_OVERHEAD);        }    }    if( verbose ) {        printf( "\t[in=%lu]", (unsigned long)attrsize );    }    /* Try compressing the data */    compbuff = (char *)malloc( (size_t)attrsize + MEMCOMPRESS_OVERHEAD );    if( compbuff == NULL ) {        return ZE_MEM;    }    compsize = memcompress( compbuff,                            (size_t)attrsize + MEMCOMPRESS_OVERHEAD,                            attrbuff,                            (size_t)attrsize );    if( verbose ) {        printf( " [out=%u]", compsize );    }    /* Attempt to optimise very small attributes. */    if( compsize > attrsize ) {        free( compbuff );        compsize = (ush)attrsize;        compbuff = attrbuff;        flags = EB_BE_FL_NATURAL;    }    /* Check to see if we really have enough room in the EF for the data. */    if( ( z->ext + compsize + EB_L_BE_LEN ) > USHRT_MAX ) {        compsize = USHRT_MAX - EB_L_BE_LEN - z->ext;    }    /* 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_BE_SIZE + compsize );    } else {        l_ef = (char *)malloc( EB_L_BE_SIZE + compsize );        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_BE_SIZE );    } else {        c_ef = (char *)malloc( EB_C_BE_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++ = 'B';    *l_ef++ = 'e';    *l_ef++ = (char)(compsize + EB_L_BE_LEN);    *l_ef++ = (char)((compsize + EB_L_BE_LEN) >> 8);    *l_ef++ = (char)((unsigned long)attrsize);    *l_ef++ = (char)((unsigned long)attrsize >> 8);    *l_ef++ = (char)((unsigned long)attrsize >> 16);    *l_ef++ = (char)((unsigned long)attrsize >> 24);    *l_ef++ = flags;    memcpy( l_ef, compbuff, (size_t)compsize );    z->ext += EB_L_BE_SIZE + compsize;    /* And the central version. */    *c_ef++ = 'B';    *c_ef++ = 'e';    *c_ef++ = (char)(EB_C_BE_LEN);    *c_ef++ = (char)(EB_C_BE_LEN >> 8);    *c_ef++ = (char)compsize;    *c_ef++ = (char)(compsize >> 8);    *c_ef++ = (char)(compsize >> 16);    *c_ef++ = (char)(compsize >> 24);    *c_ef++ = flags;    z->cext += EB_C_BE_SIZE;    return ZE_OK;}/* Extra field info:   - 'UT' - UNIX time extra field   - 'Ux' - UNIX uid/gid extra field   - 'Be' - BeOS file attributes 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 'Be' field added to the end and the size of the 'Be' field   in the central header.   See the end of beos/osdep.h for a simple explanation of the 'Be' EF   layout. */int set_extra_field(z, z_utim)  struct zlist far *z;  iztimes *z_utim;  /* store full data in local header but just modification time stamp info     in central header */{    int retval;    /* Tell picky compilers to shut up about unused variables. */    z_utim = z_utim;    /* Check to make sure z is valid. */    if( z == NULL ) {        return ZE_LOGIC;    }    /* This function is much simpler now that I've moved the extra fields */    /* out... it simplified the 'Be' code, too.                           */    retval = add_UT_ef( z );    if( retval != ZE_OK ) {        return retval;    }    retval = add_Ux_ef( z );    if( retval != ZE_OK ) {        return retval;    }    retval = add_Be_ef( z );    if( retval != ZE_OK ) {        return retval;    }    return ZE_OK;}/* ---------------------------------------------------------------------- *//* Set a file's MIME type.                                                */void setfiletype( const char *file, const char *type ){    int fd;    attr_info fa;    ssize_t wrote_bytes;    fd = open( file, O_RDWR );    if( fd < 0 ) {        zipwarn( "can't open zipfile to write file type", "" );        return;    }    fa.type = B_MIME_STRING_TYPE;    fa.size = (off_t)(strlen( type ) + 1);    wrote_bytes = fs_write_attr( fd, BE_FILE_TYPE_NAME, fa.type, 0,                                 type, fa.size );    if( wrote_bytes != (ssize_t)fa.size ) {        zipwarn( "couldn't write complete file type", "" );    }    close( fd );}int deletedir(d)char *d;                /* directory to delete *//* Delete the directory *d if it is empty, do nothing otherwise.   Return the result of rmdir(), delete(), or system().   For VMS, d must be in format [x.y]z.dir;1  (not [x.y.z]). */{# ifdef NO_RMDIR    /* code from Greg Roelofs, who horked it from Mark Edwards (unzip) */    int r, len;    char *s;              /* malloc'd string for system command */    len = strlen(d);    if ((s = malloc(len + 34)) == NULL)      return 127;    sprintf(s, "IFS=\" \t\n\" /bin/rmdir %s 2>/dev/null", d);    r = system(s);    free(s);    return r;# else /* !NO_RMDIR */    return rmdir(d);# endif /* ?NO_RMDIR */}#endif /* !UTIL *//******************************//*  Function version_local()  *//******************************/void version_local(){    static ZCONST char CompiledWith[] = "Compiled with %s%s for %s%s%s%s.\n\n";    printf(CompiledWith,#ifdef __MWERKS__      "Metrowerks CodeWarrior", "",#else#  ifdef __GNUC__      "gcc ", __VERSION__,#  endif#endif      "BeOS",#ifdef __POWERPC__      " (PowerPC)",#else#  ifdef __INTEL__      " (x86)",#  else      " (UNKNOWN!)",#  endif#endif#ifdef __DATE__      " on ", __DATE__#else      "", ""#endif    );} /* end function version_local() */

⌨️ 快捷键说明

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