📄 zutil.c
字号:
/****************************************************************************** Copyright (c) 2004, UTStarcom, Inc.** All Rights Reserved.**** Subsystem : all modules** File : zutil.c** Created By : Pengliang** Created On : 2/2/ 2005**** Purpose:** ****************************************************************************//* zutil.c -- target dependent utility functions for the compression library NOTE:the fiel realize that the dynamic malloc,calloc,free function in the array[n]."n"can be set according to your necessary. the file will be usefull in embeded system which does provide heap memory and dynamic malloc ,calloc,free function because of memory restriction. */#include "stdio.h"#include "string.h"#include "zutil.h"#include "infblock.h"#define UNZIP_NULL (void *)0typedef unsigned int unzip_size_t; #define UNZIP_HEADER struct unzip_mem UNZIP_HEADER { UNZIP_HEADER *next; unsigned size; }; UNZIP_HEADER unzip_membase; UNZIP_HEADER * unzip_avail ; char unzip_badlist;static char temp_time =0;#define UNZIP_MAXSIZE ((unsigned)(-sizeof(UNZIP_HEADER) - 1))#define UNZIP_NORM(x) (x)#define unzip_addr(x) (x)#define unzip_norm(x) (x)#define UNZIP_XEQ(a,b) ((a) == (b))#define UNZIP_XNE(a,b) ((a) != (b))#define UNZIP_XLT(a,b) ((a) < (b))#define UNZIP_XLE(a,b) ((a) <= (b))#define UNZIP_XGT(a,b) ((a) > (b))#define UNZIP_XGE(a,b) ((a) >= (b))#define UNZIP_ALIGN_MASK 3 /* Word alignment for all 68k processors */#if 1#define unzip_zalloc_mem (char*)0x00340000 char *UNZIP_HEAP =(char*)unzip_zalloc_mem ; char * UNZIP_HEAP_START = (char*) unzip_zalloc_mem;#define UNZIP_HEAP_SIZE 128*1024 #else char unzip_zalloc_mem[64*1024+3];/*use to heap memory.*/ char *UNZIP_HEAP =unzip_zalloc_mem ; char * UNZIP_HEAP_START = unzip_zalloc_mem;#define UNZIP_HEAP_SIZE 64*1024#endifvoid unzip_free (void *ptr){ register UNZIP_HEADER *p, *q; register UNZIP_HEADER *endp, *endq; if (ptr == UNZIP_NULL) return; p = (UNZIP_HEADER *)(unzip_norm(unzip_addr((char*)ptr) - sizeof(UNZIP_HEADER))); for (q=unzip_avail; UNZIP_XLT(p,q) || UNZIP_XGT(p,q->next); q=q->next) /* find the right */ if (UNZIP_XGE(q,q->next) && (UNZIP_XGE(p,q)||UNZIP_XLE(p,q->next))) /* place to insert */ break; endp = UNZIP_NORM(p) + p->size; endq = UNZIP_NORM(q) + q->size; if (UNZIP_XLT(p,endq) &&UNZIP_XGT(endp,endq) || UNZIP_XLT(p,q->next) && UNZIP_XGT(endp,q->next)) { unzip_badlist = 1; /* free list corrupted */ return; } if (UNZIP_XEQ(p,q->next) || UNZIP_XGE(p,q) && UNZIP_XLE(endp,endq)) /* block already freed ? */ { unzip_avail = q; return; } if (UNZIP_XEQ(endp,q->next)) { /* free block on the right ? */ p->size += q->next->size; p->next = q->next->next; } else p->next = q->next; if (UNZIP_XEQ(endq,p)) { /* free block on the left ? */ q->size += p->size; q->next = p->next; } else q->next = p; unzip_avail = q;}char *unzip_sbrk(unsigned incr){ char *oldBreak, *newBreak; oldBreak = UNZIP_HEAP = /* Do alignment fixup if necessary */ (char *) (((unsigned long) UNZIP_HEAP + UNZIP_ALIGN_MASK) & (~UNZIP_ALIGN_MASK)); if (incr) { newBreak = oldBreak + incr; if ((newBreak > (UNZIP_HEAP_START +UNZIP_HEAP_SIZE)) /* out of space ? */ || (newBreak <=UNZIP_HEAP_START)) /* memory wrap ? */ return (char *) (-1); UNZIP_HEAP = newBreak; } return oldBreak;}void *unzip_malloc (unzip_size_t size){ register UNZIP_HEADER *p, *q; /* ptrs to current & last HEADER */ register UNZIP_HEADER *highest; /* highest free block in memory */ unsigned units, nblock;if(temp_time ==0) { unzip_avail=unzip_membase.next = &unzip_membase;/* initialize for malloc */ temp_time++; UNZIP_HEAP_START = UNZIP_HEAP; } if (size == 0) /* get malloc fix to match previous */ size = 1; /* behavior for spr 40719 */ if (size > UNZIP_MAXSIZE) { return UNZIP_NULL; } units = (size+sizeof(UNZIP_HEADER)-1)/sizeof(UNZIP_HEADER)+1; q = unzip_avail; /* start search from here */ if (!unzip_badlist) /* quit if free list is bad */ for (p=q->next; ; q=p, p=p->next) { /* is this free block big enough ? */ if (p->size >= units) { if (p->size <= units+2) q->next = p->next; /* < 2 units left, use whole block */ else { /* cut up block */ q->next = UNZIP_NORM(p) + units; /* start of new block */ q->next->size = p->size - units;/* size of new block */ q->next->next = p->next; /* next link of new block */ p->size = units; /* size of allocated block */ } unzip_avail = q; /* next time, start from here */ return ((char *)(UNZIP_NORM(p) + 1));/* user memory starts after header */ } if (UNZIP_XEQ(p,unzip_avail)) { /* end of list, get more memory */ nblock = (units<128) ? 128:units; /* get at least 128 units */ if ((p=(UNZIP_HEADER *)unzip_sbrk(nblock*sizeof(UNZIP_HEADER))) == (UNZIP_HEADER *)-1) { /* Re-use variables !! */ highest = unzip_avail; /* Failed allocating big block*/ while (UNZIP_XGT (highest->next, highest)) /* find highest free */ highest = highest->next; nblock = units; /* Check for free block at end of list */ if (UNZIP_NORM ((char *)&highest[highest->size]) == UNZIP_NORM (unzip_sbrk(0))) nblock -= highest->size; /* Try to allocate smallest possible block */ if ((p=(UNZIP_HEADER *)unzip_sbrk(nblock*sizeof(UNZIP_HEADER))) == (UNZIP_HEADER *)-1) { return UNZIP_NULL; } } p->size = nblock; unzip_free((char *)(UNZIP_NORM(p) + 1));/* add to free list */ p = unzip_avail; } } return UNZIP_NULL; /* out of memory or bad free list */} void *unzip_calloc (unzip_size_t nelem, unzip_size_t elsize){ register char *ptr; register unsigned nbytes; register long size; { nbytes = size = (long)nelem * elsize; if (nbytes != size) /* size > largest unsigned int */ { return UNZIP_NULL; } } { if ((ptr = unzip_malloc(nbytes)) != UNZIP_NULL) memset (ptr, (int) 0, nbytes); /* fill with zero's */ return ptr; }}/* inflate private state */struct internal_state_debug { /* mode */ enum { METHOD, /* waiting for method byte */ FLAG, /* waiting for flag byte */ DICT4, /* four dictionary check bytes to go */ DICT3, /* three dictionary check bytes to go */ DICT2, /* two dictionary check bytes to go */ DICT1, /* one dictionary check byte to go */ DICT0, /* waiting for inflateSetDictionary */ BLOCKS, /* decompressing blocks */ CHECK4, /* four check bytes to go */ CHECK3, /* three check bytes to go */ CHECK2, /* two check bytes to go */ CHECK1, /* one check byte to go */ DONE, /* finished check, done */ BAD} /* got an error--stay here */ mode; /* current inflate mode */ /* mode dependent information */ union { uInt method; /* if FLAGS, method byte */ struct { uLong was; /* computed check value */ uLong need; /* stream check value */ } check; /* if CHECK, check values to compare */ uInt marker; /* if BAD, inflateSync's marker bytes count */ } sub; /* submode */ /* mode independent information */ int nowrap; /* flag for no wrapper */ uInt wbits; /* log2(window size) (8..15, defaults to 15) */ inflate_blocks_statef *blocks; /* current inflate_blocks state */};struct internal_state {int dummy;}; /* for buggy compilers */unzip_const char *z_errmsg[10] = {"need dictionary", /* Z_NEED_DICT 2 */"stream end", /* Z_STREAM_END 1 */"", /* Z_OK 0 */"file error", /* Z_ERRNO (-1) */"stream error", /* Z_STREAM_ERROR (-2) */"data error", /* Z_DATA_ERROR (-3) */"insufficient memory", /* Z_MEM_ERROR (-4) */"buffer error", /* Z_BUF_ERROR (-5) */"incompatible version",/* Z_VERSION_ERROR (-6) */""};unzip_const char *zlibVersion(){ return ZLIB_VERSION;}#ifdef DEBUGvoid z_error (m) char *m;{ fprintf(stderr, "%s\n", m); exit(1);}#endif#ifndef HAVE_MEMCPYvoid zmemcpy(dest, source, len) Bytef* dest; Bytef* source; uInt len;{ if (len == 0) return; do { *dest++ = *source++; /* ??? to be unrolled */ } while (--len != 0);}int zmemcmp(s1, s2, len) Bytef* s1; Bytef* s2; uInt len;{ uInt j; for (j = 0; j < len; j++) { if (s1[j] != s2[j]) return 2*(s1[j] > s2[j])-1; } return 0;}void zmemzero(dest, len) Bytef* dest; uInt len;{ if (len == 0) return; do { *dest++ = 0; /* ??? to be unrolled */ } while (--len != 0);}#endif#ifndef MY_ZCALLOC /* Any system without a special alloc function */voidpf zcalloc (voidpf opaque, unsigned items, unsigned size){ if (opaque) items += size - size; /* make compiler happy */ return (voidpf)unzip_calloc(items, size); }void zcfree (voidpf opaque, voidpf ptr){ unzip_free(ptr); if (opaque) return; /* make compiler happy */}#endif /* MY_ZCALLOC */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -