libz.so Library Analysis

Source: Internet
Author: User

From:http://blog.chinaunix.net/uid-12773189-id-84605.html

1. See which package the library file is provided by
Open the/usr/lib directory when you are free (because I know this directory has some library files that we developers can use) to see which libraries are in my system.
See a libz.so, and then use the command to view: Dpkg-s/usr/lib/libz.so

This command outputs the following information: Zlib1g-dev:/usr/lib/libz.so

That is, the package Zlib1g-dev in my system provides libz.so this library file.
2. View package Details
You can see what this package does with the following commands: Apt-cache show Zlib1g-dev

Part of the output is: Description:compression library-development
Zlib is a library implementing the Deflate compression method found
In Gzip and PKZIP. The includes the development support
Files.
Bugs:mailto:[email protected]
Origin:ubuntu

Obviously this package provides something related to the development of gzip and PKZIP compression algorithms.

Continue to view the details of the Zlib1g-dev package with the following command: Dpkg-l Zlib1g-dev

This command outputs the following information:/.
/usr
/usr/lib
/usr/lib/libz.a
/usr/include
/usr/include/zlib.h
/usr/include/zconf.h
/usr/share
/usr/share/doc
/usr/share/doc/zlib1g-dev
/usr/share/doc/zlib1g-dev/examples
/usr/share/doc/zlib1g-dev/examples/example.c.gz
/usr/share/doc/zlib1g-dev/examples/contrib.tar.gz
/usr/share/doc/zlib1g-dev/examples/minigzip.c.gz
/usr/share/doc/zlib1g-dev/changelog.gz
/usr/share/doc/zlib1g-dev/faq.gz
/usr/share/doc/zlib1g-dev/readme.gz
/usr/share/doc/zlib1g-dev/copyright
/usr/share/doc/zlib1g-dev/changelog. Debian.gz
/usr/share/doc/zlib1g-dev/algorithm.txt.gz
/usr/share/man
/usr/share/man/man3
/usr/share/man/man3/zlib.3.gz
/usr/lib/libz.so

As can be seen from here, the Zlib1g-dev package provides library files Libz.a and libz.so, as well as header files Zlib.h and zconf.h, plus/usr/share/doc/ The Zlib1g-dev directory provides documentation on how this library file is used, even in the/usr/share/doc/zlib1g-dev/examples directory, such as example.c.gz, which is a complete gzip compression/decompression example.
If you fully understand the example.c.gz and minigzip.c.gz Two example programs, you will definitely write your own program to compress and decompress the work.
3. See what calls the library file provides
If we do not look at the source code examples provided in the package information above, you can also use the NM command to see which calls are provided by the library file.
For example, we use the following NM command to see what calls the Libz.so library file provides: nm-d/usr/lib/libz.so

The output of this command is as follows: 00001810 T Adler32
00001ad0 T Adler32_combine
00013b30 A __bss_start
U Clearerr
00001c70 T Compress
00001BA0 T Compress2
00001b80 T Compressbound
00001cd0 T CRC32
00002320 T Crc32_combine
W __cxa_finalize
00005da0 T Deflate
00005150 T Deflatebound
00004bb0 T deflatecopy
000107e0 R Deflate_copyright
00004890 T Deflateend
00005640 T Deflateinit_
00005360 T deflateinit2_
00006b80 T Deflateparams
00004810 T Deflateprime
000051C0 T Deflatereset
00004fe0 T Deflatesetdictionary
000047e0 T Deflatesetheader
00004850 T Deflatetune
00010820 R _dist_code
00013b30 A _edata
00013b34 A _end
U __errno_location
U fclose
U Fdopen
U feof
U ferror
U Fflush
0000e784 T _fini
U fopen
U fprintf
U FPUTC
U fread
U Free
U fseek
U Ftell
U fwrite
00001cb0 T get_crc_table
W __gmon_start__
00002570 T Gzclearerr
00002B50 T Gzclose
00002550 T Gzdirect
00003BA0 T Gzdopen
00002520 T gzeof
000025b0 T Gzerror
00002D40 T Gzflush
000046C0 T gzgetc
00004640 T Gzgets
00003C20 T Gzopen
00002A10 T gzprintf
00002900 T GZPUTC
000028C0 T gzputs
00003C40 T Gzread
00002950 T Gzrewind
00004410 T Gzseek
00002ab0 T Gzsetparams
00004600 T Gztell
000024d0 T gzungetc
000027d0 T Gzwrite
0000a490 T Inflate
0000C4E0 T Inflateback
0000c490 T Inflatebackend
0000c390 T Inflatebackinit_
0000a000 T inflatecopy
00012720 R Inflate_copyright
00009f40 T Inflateend
0000E1C0 T Inflate_fast
00009fa0 T Inflategetheader
00009F00 T Inflateinit_
00009df0 T inflateinit2_
00009da0 T Inflateprime
00009d00 T Inflatereset
0000a380 T Inflatesetdictionary
0000c1a0 T Inflatesync
00009fd0 T Inflatesyncpoint
0000d770 T inflate_table
000013d0 T _init
W _jv_registerclasses
00010A20 R _length_code
U malloc
U memcpy
U memset
U sprintf
U __stack_chk_fail
U strcat
U strcpy
U strerror
U strlen
00008390 T _tr_align
00009240 T _tr_flush_block
000074E0 T _tr_init
00008210 T _tr_stored_block
00007C10 T _tr_tally
00004710 T uncompress
U vsnprintf
00009cd0 T Zcalloc
00009CA0 T Zcfree
00013900 D z_errmsg
00009c70 T Zerror
00009C60 T Zlibcompileflags
00009C40 T zlibversion

The first column in these outputs is the value of the call provided by the library libz.so, the second column is the type of the call, and the third column is the name of the call (which can be understood as a function name that we often say).
The detailed usage of NM can be referred to in the man nm manual, where we will first care about the second column, the type T, which our application can invoke.
/************ about this document ********************************************
*filename: This is how I learned C programming in Linux-I learned to take advantage of the existing development libraries on Linux systems.
*purpose: Explains how to use the existing library files in Linux for development
*wrote By:zhoulifa ([email protected]) Zhou Lifa (http://zhoulifa.bokee.com)
Linux Enthusiasts Linux knowledge propagator Soho developers are best at C language
*date time:2007-02-06 20:00
*note: Anyone can copy code and use these documents, of course, including your commercial use
* But please follow the GPL
*thanks To:google
*hope: I hope more and more people contribute their strength for the development of science and technology.
* Science and technology stand on the shoulders of giants progress faster! Thanks to the contributions of the source of the predecessors!
*********************************************************************/
And since we only care about the use of gzip compression and decompression methods, we can filter by command: nm-d/usr/lib/libz.so | grep GZ

The output of this command is as follows: 00002570 T Gzclearerr
00002B50 T Gzclose
00002550 T Gzdirect
00003BA0 T Gzdopen
00002520 T gzeof
000025b0 T Gzerror
00002D40 T Gzflush
000046C0 T gzgetc
00004640 T Gzgets
00003C20 T Gzopen
00002A10 T gzprintf
00002900 T GZPUTC
000028C0 T gzputs
00003C40 T Gzread
00002950 T Gzrewind
00004410 T Gzseek
00002ab0 T Gzsetparams
00004600 T Gztell
000024d0 T gzungetc
000027d0 T Gzwrite

Obviously, if you have in Linux under the C language programming operation of the file, the above function name, should be familiar with it? Would you change the previous GZ to F, most of which are the file manipulation functions that you are familiar with stdio.h? If you remove GZ directly, is that all the file manipulation functions provided by fcntl.h?
4. View the definition of a function
Open/usr/include/zlib.h, search for these functions that appear above, and you will soon find Gzopen's description: Zextern gzfile zexport Gzopen of (const char *path, const char *mode) );
/*
Opens a gzip (. gz) file for reading or writing. The mode parameter
is as in fopen ("RB" or "WB") but can also include a compression level
("WB9") or a strategy: ' F ' for filtered data as in "wb6f", ' H ' for
Huffman only compression as in "wb1h", or ' R ' for run-length encoding
As in "wb1r". (See the description of DeflateInit2 for more information
About the Strategy parameter.)

Gzopen can used to read a file which are not in gzip format; In this
Case Gzread would directly read from the file without decompression.

Gzopen returns NULL if the file could not is opened or if there was
Insufficient memory to allocate the (DE) compression state; Errno
can be checked to distinguish the cases (if errno is zero, the
Zlib error is z_mem_error). */

Did you see that? With the Gzopen function you can open or create a compressed file *.gz, the use of the method is more clear here.
Again, we will see a description of all the functions above: Zextern int zexport gzread of (gzfile file, VOIDP buf, unsigned len);
Zextern int Zexport Gzwrite of (gzfile file, voidpc buf, unsigned len);
Zextern int Zexport Gzflush of ((gzfile file, int flush));
Zextern z_off_t Zexport Gzseek of ((gzfile file, z_off_t offset, int whence));
Zextern int Zexport Gzclose of ((gzfile file));
......

5. Use Library
With this information, we can write our own program to use everything that the library file provides. For example, the following code: #include <zlib.h>/* Because the GZ-related function is defined in this header file */
#include <zconf.h>
#include <stdio.h>/* because printf-related functions are defined in this header file */
#include <errno.h>/* because errno-related functions are defined in this header file */
#include <stdlib.h>/* Because the Exit function is defined in this header file */
#include <string.h>/* Because the strlen function is defined in this header file */

int main (int argc, char **argv)
{
Gzfile MYGZFP;

MYGZFP = Gzopen (Argv[1], "w");
if (MYGZFP = = NULL) {
printf ("Open file '%s ' in writing" error message:%s ", Argv[1], strerror (errno));
Exit (0);
}
Gzwrite (MYGZFP, argv[2], strlen (argv[2]));
Gzclose (MYGZFP);
return 0;
}


Compile this program with this command:
GCC Gzfile.c-lz
Note: In order to reference libz.so this library
File, we must use-LZ.
Run this command:
./a.out mygzfile.gz "Hello, this is a test program that generates a compressed file of its own. "
A compressed file named Mygzfile.gz will be generated under the current directory.
You can open this compressed file to see if it is in your own writing information it?

libz.so Library Analysis

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.