473,378 Members | 1,346 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Problems using zlib - buffer problem or something else?

Hi,

I use zlib to write data structures to a compressed file, using the
gzwrite function. Afterwards I read the data back with gzread. I notice
that this works well when the data written is not that much, but when
there is more data to write, after a while I get data errors when
reading back the data.
Error in main: couldn't read stat
zlib error -3: test512-20070531-18h10m02.stat.gz: data error


I wonder whether I am doing something wrong. The data I write are simply
C data structures, struct acmp_stat, which have a fixed size.

I am using my internal buffer, after noticing that it helped the
problem. However, now, even though I buffer I get problems. Writing is
done with:

Expand|Select|Wrap|Line Numbers
  1. if (data->buflen + sizeof(*s) > data->bufmaxlen)
  2.         if (0 != acmp_flush_buffer(data))
  3.             printf("Error: could not flush buffer\n");
  4.  
  5.     acmp_buffer_stat(data);
Data being:
Expand|Select|Wrap|Line Numbers
  1. struct acmp_data {
  2.     void *fd;
  3.     void *buf;
  4.     size_t buflen;
  5.     size_t bufmaxlen;
  6.  
  7.     char *statpath;
  8.  
  9.     struct acmp_stat **stat;
  10. };
The flush buffer function is:

Expand|Select|Wrap|Line Numbers
  1. static int
  2. acmp_flush_buffer(struct acmp_data *data)
  3. {
  4.     size_t len;
  5.  
  6.     assert(NULL != data);
  7.     assert(NULL != data->fd);
  8.  
  9.     len = (size_t)gzwrite((gzFile)data->fd, data->buf, data->buflen);
  10.  
  11.     if (len != data->buflen)
  12.         return 1;
  13.  
  14.     data->buflen = 0;
  15.     return 0;
  16. }

and acmp_buffer_stat simply:

Expand|Select|Wrap|Line Numbers
  1. static void
  2. acmp_buffer_stat(struct acmp_data *data)
  3. {
  4.     assert(NULL != data);
  5.  
  6.     memcpy(data->buf + data->buflen, data->stat[data->curcpu], sizeof(**data->stat));
  7.     data->buflen += sizeof(**data->stat);
  8. }

Of course I have opened and closed the file before and after using this
code. The buffer is currently assigned a size of 1 MiB.

Reading back the data is done with:

Expand|Select|Wrap|Line Numbers
  1. static int
  2. acmp_read_stat(/*@out@*/ struct acmp_stat *stat, void *fd)
  3. {
  4.     int err;
  5.     const char *errmsg;
  6.     size_t len;
  7.  
  8.     assert(NULL != fd);
  9.     assert(stat != NULL);
  10.  
  11.     len = (size_t)gzread((gzFile)fd, (void *)stat, sizeof(*stat));
  12.     errmsg = gzerror((gzFile)fd, &err);
  13.     if (0 > err)
  14.         printf("zlib error %d: %s \n", err, errmsg);
  15.  
  16.     if (sizeof(*stat) != len)
  17.         return 1;
  18.  
  19.     return 0;
  20. }


The code looks ok to me, and the weird thing is that I only get errors
when the amount of data written is large. I didn't find a clear point
yet, from where it seems to fail.
Also notice that there are no errors when writing, but only when reading
back. The files are not touched between reading and writing, and are
cleanly closed.

Am I doing something wrong here?

Thanks a lot, Thomas
Jun 3 '07 #1
2 2922
Not sure if this is your problem but zlib does state that your source buffer must be at least 0.1 times larger than the source buffer plus 12 bytes to cope with the overhead of zlib data streams.
Nov 14 '07 #2
Check out this example:
http://www.gamedev.net/reference/articles/article2279.asp
Nov 14 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Andres Rosado-Sepulveda | last post by:
Hello, I'm having trouble compiling PHP 4.3.4 on Solaris 8. This is the error message it is showing: -- start -- Undefined first referenced symbol ...
0
by: fishboy | last post by:
Howdy, Sorry if this is a double post. First try seemed to go into hyperspace. I'm working on a personal project. It's going to be a multipart binary attachment downloader that will search...
0
by: Robert Brewer | last post by:
Hello, I realize I'm a bit over my head, here, but I'm trying to pull waypoint data from a Garmin eTrex Legend, and having mixed success. On a Win98 box last night, I was able to connect and...
0
by: Tony | last post by:
Hello, Any assistance would be appreciated: As the subject suggests I am trying to gunzip files using the Compress::Zlib module. The following is basically the code that I am working with: ...
11
by: Arturo DiDonna | last post by:
Hello everyone. I am trying to compile someone else code and I am stuck with compilation problems using the g++ 3.3 compiler. Basically, when compiling the following code, I get this error...
10
by: Xah Lee | last post by:
today i need to use Python to decompress gzip files. since i'm familiar with Python doc and have 10 years of computing experience with 4 years in unix admin and perl, i have quickly located the...
6
by: Dawn Minnis | last post by:
Hi (running Win xp and developing using Miracle C. Running applications in windows command prompt) I'm new to the group so be gentle with me. I am currently writing a C program to perform...
47
by: Bonj | last post by:
I downloaded the gzlib library from zlib in order to do compression. (http://www.gzip.org/zlib) The prototype of the compression function seems to be int compress (Bytef *dest, uLongf *destLen,...
4
by: Anonymous | last post by:
Slightly OT, but can't find a zlib specific ng - so hopefuly, someone can point out why uncompressed strings are not matching the original strings (lots of strange characters at end of string). ...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.