examples/PIPS/openclibz/src/compress.c

Go to the documentation of this file.
00001 /*
00002 Copyright (c) 2006-2010 Nokia Corporation and/or its subsidiary(-ies). All rights reserved.
00003 
00004 Redistribution and use in source and binary forms, with or without
00005 modification, are permitted provided that the following conditions are met:
00006 
00007 * Redistributions of source code must retain the above copyright notice, this
00008   list of conditions and the following disclaimer.
00009 * Redistributions in binary form must reproduce the above copyright notice,
00010   this list of conditions and the following disclaimer in the documentation
00011   and/or other materials provided with the distribution.
00012 * Neither the name of Nokia Corporation nor the names of its contributors
00013   may be used to endorse or promote products derived from this software
00014   without specific prior written permission.
00015 
00016 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00017 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00018 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00019 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
00020 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00021 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
00022 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00023 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00024 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00025 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00026 
00027 Description:  
00028 */
00029 
00030 
00031 
00032 
00041 #include "OpenCLibzheader.h"
00042 #include <limits.h>
00043 
00044 //
00045 // GzCompress
00046 // Compress the input file "in" and generates "out" file
00047 //
00048 void GzCompress(FILE   *in,gzFile out)
00049 {
00050     static char buf[BUFLEN];
00051     int len;
00052     
00053 
00054 #ifdef USE_MMAP
00055     /* Try first compressing with mmap. If mmap fails 
00056      use the normal fread loop.
00057      */
00058     if (GzCompressMmap(in, out) == Z_OK) return;
00059 #endif
00060     for (;;) 
00061     {
00062         len = (int)fread(buf, 1, sizeof(buf), in);
00063         if (len < 0) 
00064         Error("error reading the file \n");
00065     
00066         if (len == 0) break;
00067 
00068         if (gzwrite(out, buf, (unsigned)len) != len) 
00069         Error("error writing to the file \n");;
00070         
00071     }
00072     fclose(in);
00073     
00074     if (gzclose(out) != Z_OK) // libz api to close a compressed file 
00075     Error("failed gzclose");
00076 }
00077 
00078 
00079 
00080 
00081 
00082 
00083 #ifdef USE_MMAP 
00084 
00085 //
00086 // GzCompressMmap
00087 // Try compressing the input file at once using mmap. Return Z_OK 
00088 // If success, otherwise it returns Z_ERRNO .
00089 // 
00090 int GzCompressMmap(FILE   * in,gzFile out)
00091 {
00092     int len;
00093     int err;
00094     int ifd = fileno(in);
00095     caddr_t buf;    /* mmap'ed buffer for the entire input file */
00096     off_t buf_len;  /* length of the input file */
00097     struct stat sb;
00098 
00099     /* Determine the size of the file, needed for mmap: */
00100     
00101     if (fstat(ifd, &sb) < 0) 
00102     return Z_ERRNO;
00103     
00104     buf_len = sb.st_size;
00105     
00106     if (buf_len <= 0) 
00107     return Z_ERRNO;
00108 
00109     /* Now do the actual mmap: */
00110     
00111     buf =(char*) mmap((caddr_t) 0, buf_len, PROT_READ, MAP_SHARED, ifd, (off_t)0);
00112     
00113     //mmap is an openc api 
00114     
00115     if (buf == (caddr_t)(-1)) 
00116     return Z_ERRNO;
00117 
00118     /* Compress the whole file at once: */
00119     len = gzwrite(out, (char *)buf, (unsigned)buf_len);
00120 
00121     if (len != (int)buf_len) 
00122     Error(gzerror(out, &err));
00123 
00124     munmap(buf, buf_len);
00125     fclose(in);
00126     if (gzclose(out) != Z_OK) // libz api to close a compressed file 
00127     Error("failed gzclose");
00128     
00129     return Z_OK;
00130 }
00131 #endif /* USE_MMAP */
00132 
00133 
00134 
00135 
00136 
00137 //
00138 // -----------------------------------------------------------------------------
00139 // FileCompress
00140 // Compresses the file using mode value
00141 // -----------------------------------------------------------------------------
00142 //
00143 void FileCompress(char  * file,char  * mode)
00144 {
00145     static char outfile[MAX_NAME_LEN];
00146     FILE  *in;
00147     gzFile out;
00148         char choice[3];
00149 
00150     strcpy(outfile, file);
00151     strcat(outfile, GZ_SUFFIX);
00152 
00153     in = fopen(file, "rb");
00154     if (in == NULL) 
00155     {    
00156     Error("file not found ...try again \n");
00157     }
00158     out = gzopen(outfile, mode);
00159     if (out == NULL) 
00160     {       
00161     Error("error opening outfile\n ");        
00162     }
00163     GzCompress(in, out);
00164    
00165     
00166     printf("do you want to delete the original file (y /n) \n");
00167     
00168     scanf("%s",choice);
00169     /*I am using scanf("%s",...) because of some problem in getchar() as 
00170     well as scanf("%c"...) .   I have reported the bug */
00171     
00172     if(choice[0] == 'y')
00173     unlink(file);
00174     
00175     printf(" Congrats .... compression done ...u can find .gz file in the same directory as of the source file\n");
00176 }
00177 
00178 //
00179 // -----------------------------------------------------------------------------
00180 // StringCompress
00181 // Compresses the string
00182 // This function compresses the string entered by the user and shows the
00183 // compressed string and then decompressess it 
00184 // Actually This function will show the content on the screen and demonstarte 
00185 // how strings can be compressed
00186 // -----------------------------------------------------------------------------
00187 //
00188 
00189 void StringCompress()
00190 {
00191     Byte *compr, *uncompr;
00192     uLong len,comprLen,uncomprLen;
00193     char *hello;
00194      
00195     hello = (char *)malloc(100*sizeof(char));
00196   
00197     printf("enter the string(maximum 100 characters) to compress....\n");
00198 
00199 
00200 
00201     scanf("%s",hello); //accepts the string from the user
00202     len = (uLong)strlen(hello)+1;
00203     comprLen = 10 * len; 
00204     uncomprLen = comprLen;
00205     
00206     //memory allocation
00207     compr    = (Byte*)calloc((uInt)comprLen, 1);  
00208     uncompr  = (Byte*)calloc((uInt)uncomprLen, 1);
00209                 
00210                 
00211                 
00212     //compress is one of the api of libz library used to compress data
00213     
00214     compress(compr, &comprLen, (const Bytef*)hello, len);
00215 
00216     strcpy((char*)uncompr, "garbage"); //initialization
00217     
00218     printf("compressed string is : %s\n\n", (char *)compr);
00219 
00220     //uncompress is one of the api of libz library used to uncompress data
00221     
00222     uncompress(uncompr, &uncomprLen, compr, comprLen);
00223 
00224    
00225     printf("press enter to uncompress it\n");
00226     
00227     getchar();
00228     getchar();
00229     
00230     if (strcmp((char*)uncompr, hello)) 
00231     {
00232         Error("bad uncompress\n");  
00233         free(compr);
00234         free(uncompr);        
00235     }
00236     else 
00237     {
00238         printf("uncompressed string is : %s\n", (char *)uncompr);
00239         free(compr);
00240         free(uncompr);
00241     }
00242     
00243 }
00244 
00245 /*  End of File */

Generated by  doxygen 1.6.2