easyzlib.h
1 /* easyzlib.h header
2 
3  easyzlib release 1.0
4  Copyright (C) 2008 First Objective Software, Inc. All rights reserved
5  This entire notice must be retained in this source code
6  Redistributing this source code requires written permission
7  This software is provided "as is", with no warranty.
8  Latest fixes enhancements and documentation at www.firstobject.com
9 */
10 
11 #ifndef _EASYZLIB_H
12 #define _EASYZLIB_H
13 
14 /* Return codes */
15 #define EZ_STREAM_ERROR (-2)
16 #define EZ_DATA_ERROR (-3)
17 #define EZ_MEM_ERROR (-4)
18 #define EZ_BUF_ERROR (-5)
19 
20 /* Calculate maximum compressed length from uncompressed length */
21 #define EZ_COMPRESSMAXDESTLENGTH(n) (n+(((n)/1000)+1)+12)
22 
23 #ifdef __cplusplus
24 extern "C"
25 {
26 #endif /* __cplusplus */
27 
28 int ezcompress(unsigned char* pDest, long* pnDestLen, const unsigned char* pSrc, long nSrcLen);
29 int ezuncompress(unsigned char* pDest, long* pnDestLen, const unsigned char* pSrc, long nSrcLen);
30 
31 #ifdef __cplusplus
32 }
33 
34 struct ezbuffer
35 {
36  ezbuffer()
37  {
38  Init();
39  };
40  ezbuffer(int n)
41  {
42  Init();
43  Alloc(n);
44  };
45  ~ezbuffer()
46  {
47  Release();
48  };
49  unsigned char* Alloc(int n)
50  {
51  if (nSize<n)
52  {
53  Release();
54  pBuf=new unsigned char[n];
55  nSize = n;
56  }
57  nLen=n;
58  return pBuf;
59  };
60  void Release()
61  {
62  if (pBuf)
63  {
64  delete[] pBuf;
65  Init();
66  }
67  };
68  unsigned char* pBuf;
69  long nLen; /* data length */
70 private:
71  void Init()
72  {
73  pBuf = NULL;
74  nSize=0;
75  nLen=0;
76  };
77  long nSize; /* bytes allocated */
78 };
79 
80 #define EZ_CHECKLENGTH 8192
81 
82 int ezcompress(ezbuffer& bufDest, const ezbuffer& bufSrc)
83 {
84  if (bufDest.nLen == 0)
85  {
86  bufDest.Alloc(EZ_CHECKLENGTH);
87  }
88  int nErr = ezcompress(bufDest.pBuf, &bufDest.nLen, bufSrc.pBuf, bufSrc.nLen);
89  if (nErr == EZ_BUF_ERROR)
90  {
91  bufDest.Alloc(bufDest.nLen);
92  nErr = ezcompress(bufDest.pBuf, &bufDest.nLen, bufSrc.pBuf, bufSrc.nLen);
93  }
94  return nErr;
95 };
96 
97 int ezuncompress(ezbuffer& bufDest, const ezbuffer& bufSrc)
98 {
99  if (bufDest.nLen == 0)
100  {
101  bufDest.Alloc(EZ_CHECKLENGTH);
102  }
103  int nErr = ezuncompress(bufDest.pBuf, &bufDest.nLen, bufSrc.pBuf, bufSrc.nLen);
104  if (nErr == EZ_BUF_ERROR)
105  {
106  bufDest.Alloc(bufDest.nLen);
107  nErr = ezuncompress(bufDest.pBuf, &bufDest.nLen, bufSrc.pBuf, bufSrc.nLen);
108  }
109  return nErr;
110 };
111 
112 #ifdef MCD_STR
113 /* CMarkup designated string class and macros */
114 
115 int ezcompress(ezbuffer& bufDest, const MCD_STR& strSrc)
116 {
117  int nSrcLen = MCD_STRLENGTH(strSrc) * sizeof(MCD_CHAR);
118  /* alternatively: bufDest.Alloc( EZ_COMPRESSMAXDESTLENGTH(nSrcLen) ); // >.1% + 12 */
119  unsigned char pTempDest[EZ_CHECKLENGTH];
120  long nTempLen = EZ_CHECKLENGTH;
121  int nErr = ezcompress(pTempDest, &nTempLen, (const unsigned char*)MCD_2PCSZ(strSrc), nSrcLen);
122  bufDest.Alloc(nTempLen);
123  nErr = ezcompress(bufDest.pBuf, &bufDest.nLen, (const unsigned char*)MCD_2PCSZ(strSrc), nSrcLen);
124  return nErr;
125 }
126 
127 int ezuncompress(MCD_STR& strDest, const ezbuffer& bufSrc)
128 {
129  unsigned char pTempDest[EZ_CHECKLENGTH];
130  long nTempLen = EZ_CHECKLENGTH;
131  int nErr = ezuncompress(pTempDest, &nTempLen, bufSrc.pBuf, bufSrc.nLen);
132  int nDestStrLen = nTempLen / sizeof(MCD_CHAR);
133  MCD_CHAR* p = MCD_GETBUFFER(strDest,nDestStrLen);
134  nErr = ezuncompress((unsigned char*)p, &nTempLen, bufSrc.pBuf, bufSrc.nLen);
135  MCD_RELEASEBUFFER(strDest,p,nDestStrLen);
136  return nErr;
137 }
138 
139 #endif /* MCD_STR */
140 
141 #endif /* __cplusplus */
142 
143 #endif /* _EASYZLIB_H */