fc8fdf810657f15decc9c302edbd97402148de3b
2 ** NetXMS - Network Management System
3 ** SNMP support library
4 ** Copyright (C) 2003, 2004, 2005, 2006 Victor Kirhenshtein
6 ** This program is free software; you can redistribute it and/or modify
7 ** it under the terms of the GNU General Public License as published by
8 ** the Free Software Foundation; either version 2 of the License, or
9 ** (at your option) any later version.
11 ** This program is distributed in the hope that it will be useful,
12 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
13 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 ** GNU General Public License for more details.
16 ** You should have received a copy of the GNU General Public License
17 ** along with this program; if not, write to the Free Software
18 ** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 #include "libnxsnmp.h"
31 #define DATA_BUFFER_SIZE 65536
35 // Constructor for ZFile
38 ZFile
::ZFile(FILE *pFile
, BOOL bCompress
, BOOL bWrite
)
40 m_bCompress
= bCompress
;
45 // Initialize compression stream
46 m_stream
.zalloc
= Z_NULL
;
47 m_stream
.zfree
= Z_NULL
;
48 m_stream
.opaque
= Z_NULL
;
49 m_stream
.avail_in
= 0;
50 m_stream
.next_in
= Z_NULL
;
52 m_nLastZLibError
= deflateInit(&m_stream
, 9);
54 m_nLastZLibError
= inflateInit(&m_stream
);
56 m_pDataBuffer
= (BYTE
*)malloc(DATA_BUFFER_SIZE
);
57 m_pCompBuffer
= (BYTE
*)malloc(DATA_BUFFER_SIZE
);
68 // Destructor for ZFile
73 safe_free(m_pDataBuffer
);
74 safe_free(m_pCompBuffer
);
79 // Write block to compressed file
82 int ZFile
::zwrite(void *pBuf
, int nLen
)
84 int nBytes
, nSrcPos
, nRet
;
86 for(nSrcPos
= 0, nRet
= 0; nSrcPos
< nLen
; nSrcPos
+= nBytes
)
88 nBytes
= min(nLen
- nSrcPos
, DATA_BUFFER_SIZE
- m_nBufferSize
);
89 memcpy(&m_pDataBuffer
[m_nBufferSize
], (BYTE
*)pBuf
+ nSrcPos
, nBytes
);
90 m_nBufferSize
+= nBytes
;
91 if (m_nBufferSize
== DATA_BUFFER_SIZE
)
93 // Buffer is full, compress and write it to file
94 m_stream
.next_in
= m_pDataBuffer
;
95 m_stream
.avail_in
= DATA_BUFFER_SIZE
;
98 m_stream
.next_out
= m_pCompBuffer
;
99 m_stream
.avail_out
= DATA_BUFFER_SIZE
;
100 deflate(&m_stream
, Z_NO_FLUSH
);
101 if (fwrite(m_pCompBuffer
, 1, DATA_BUFFER_SIZE
- m_stream
.avail_out
, m_pFile
) != DATA_BUFFER_SIZE
- m_stream
.avail_out
)
103 } while(m_stream
.avail_in
> 0);
114 // Write single character to compressed file
117 int ZFile
::zputc(int ch
)
122 return (zwrite(&bt
, 1) == 1) ? ch
: -1;
127 // Fill data buffer with new data from file if buffer is empty
130 BOOL ZFile
::FillDataBuffer(void)
134 if (m_nBufferSize
> 0)
137 if (m_stream
.avail_in
== 0)
139 // Read more data from disk
140 nBytes
= (int)fread(m_pCompBuffer
, 1, DATA_BUFFER_SIZE
, m_pFile
);
142 return FALSE
; // EOF or error
144 m_stream
.next_in
= m_pCompBuffer
;
145 m_stream
.avail_in
= nBytes
;
148 m_stream
.next_out
= m_pDataBuffer
;
149 m_stream
.avail_out
= DATA_BUFFER_SIZE
;
150 nRet
= inflate(&m_stream
, Z_NO_FLUSH
);
151 if ((nRet
== Z_OK
) || (nRet
== Z_STREAM_END
))
153 m_nBufferSize
= DATA_BUFFER_SIZE
- m_stream
.avail_out
;
154 m_pBufferPos
= m_pDataBuffer
;
163 // Read block from compressed file
166 int ZFile
::zread(void *pBuf
, int nLen
)
170 for(nDstPos
= 0; nDstPos
< nLen
; nDstPos
+= nBytes
)
172 if (!FillDataBuffer())
173 return 0; // EOF or error
174 nBytes
= min(nLen
- nDstPos
, m_nBufferSize
);
175 memcpy((BYTE
*)pBuf
+ nDstPos
, m_pBufferPos
, nBytes
);
176 m_pBufferPos
+= nBytes
;
177 m_nBufferSize
-= nBytes
;
184 // Read one character from compressed file
187 int ZFile
::zgetc(void)
191 return (zread(&ch
, 1) == 1) ? ch
: -1;
196 // Close compressed file
199 int ZFile
::zclose(void)
206 if (m_nBufferSize
> 0)
208 m_stream
.next_in
= m_pDataBuffer
;
209 m_stream
.avail_in
= m_nBufferSize
;
212 m_stream
.next_out
= m_pCompBuffer
;
213 m_stream
.avail_out
= DATA_BUFFER_SIZE
;
214 nRet
= deflate(&m_stream
, Z_FINISH
);
215 fwrite(m_pCompBuffer
, 1, DATA_BUFFER_SIZE
- m_stream
.avail_out
, m_pFile
);
216 } while(nRet
!= Z_STREAM_END
);
219 deflateEnd(&m_stream
);
223 inflateEnd(&m_stream
);
225 return fclose(m_pFile
);