简体   繁体   中英

What is the purpose of the various types added by zlib and how can I use them?

I am currently trying, with the goal of learning the basic usage of zlib, to create a small utility to compress and decompress files in C++. I am using the compress2 and uncompress functions provided by zlib to facilitate this. Both of these functions, however, take various types that seem specific to zlib ( Bytef , uLongf , etc.) without any automatic conversions between them and C++ types (or, rather, the pointers to each of these types). This makes simple code to interface with zlib more complex, unless I write my entire application based around zlib's types.

My question has 3 parts:

  • What is the purpose of these types as opposed to built-in types such as unsigned long , which I am using in my own file I/O code to represent file lengths?
  • What is the proper way to use these types? Can I reinterpret cast my char pointers to data to (de)compress to Bytef pointers without changing the data length from the length of my char array? Because char is one byte and Bytef 's name suggests that it is the same length, I imagine that I can, but I want to make sure. Can I simply assign an unsigned long (or other non-zlib integral type) to uLongf and other zlib seemingly-integral types?
  • Where is zlib's official documentation on these types?

I skimmed the zlib manual and fully read the sections that could possibly seem relevant, along with a ctrl+f aided search, to no avail. My search engine also does not know the answer.

  • For portability, your file lengths should use off_t , not unsigned long . On some systems those are different sizes, with off_t being longer.
  • Yes, you can just cast between Bytef and char . ( Bytef is actually unsigned char , but there is no conversion required.) uLong is simply unsigned long . (See zconf.h.)
  • zlib's documentation is zlib.h, where those types are called out as parameters of the functions. You can use zlibCompileFlags() to determine the number of bits in each type. (See zlib.h.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM