ut_compress_cryptopp_gzip.cpp

Go to the documentation of this file.
00001 /*
00002  !concept {:name => "Gzipping files with Crypto++",
00003    :desc => "Straightforward gzip file compression with Crypto++."}
00004 */
00005 
00006 /*
00007   To decompress, use
00008     gunzip compressed-file
00009 
00010   See these documents file format details.
00011     http://en.wikipedia.org/wiki/Gzip#File_format
00012     http://tools.ietf.org/html/rfc1952
00013 
00014   If we do not end up requiring the other features of Crypto++
00015   we might consider using just zlib, see for example
00016   http://stackoverflow.com/questions/4988236/how-to-i-compress-a-tarball-with-gzip
00017  */
00018 
00019 #include "ut_compress.h"
00020 
00021 #include "ac_app_context.h"
00022 #include "application_config.h"
00023 #include "er_errors.h"
00024 
00025 #include "cryptopp561/files.h"
00026 #include "cryptopp561/gzip.h"
00027 
00028 #include <glib/gstdio.h>
00029 
00030 #include <stdio.h>
00031 #include <string.h>
00032 #include <assert.h>
00033 
00034 // Throws a CryptoPP::Exception on error.
00035 static void gzip_file(const char *in, const char *out)
00036 {
00037   CryptoPP::FileSource(in, true, 
00038            new CryptoPP::Gzip(new CryptoPP::FileSink(out)));
00039 }
00040 
00041 static gboolean compress_ft(const char* fs, const char* ft, GError** error)
00042 {
00043   try {
00044     gzip_file(fs, ft);
00045   } catch (const CryptoPP::Exception& ex) {
00046     if (error)
00047       *error = gx_error_new(domain_cryptopp, 0, "gzip compression of '%s' failed: %s", fs, ex.what());
00048     return FALSE;
00049   }
00050   return TRUE;
00051 }
00052 
00053 extern "C" gboolean compress_file(const char* fn, GError** error)
00054 {
00055   char* ft = tempnam(LOG_UPLOADS_DIR, "zlog_"); // caller must free result
00056   if (!ft) {
00057     if (error)
00058       *error = gx_error_new(domain_posix, errno,
00059           "tempnam failed: %s (%d)",
00060           strerror(errno), errno);
00061     return FALSE;
00062   }
00063 
00064   if (!compress_ft(fn, ft, error)) {
00065     g_free(ft);
00066     return FALSE;
00067   }
00068 
00069   if (g_unlink(fn)) {
00070     if (error)
00071       *error = gx_error_new(G_FILE_ERROR, g_file_error_from_errno(errno), 
00072           "error deleting file '%s': %s (%d)", fn, 
00073           strerror(errno), errno);
00074     g_free(ft);
00075     return FALSE;
00076   }
00077 
00078   if (rename(ft, fn)) {
00079     if (error)
00080       *error = gx_error_new(domain_posix, errno, 
00081                             "failed to rename '%s' as '%s': %s (%d)", 
00082                             ft, fn, strerror(errno), errno);
00083     g_free(ft);
00084     return FALSE;
00085   }
00086 
00087   g_free(ft);
00088   
00089   return TRUE;
00090 }
00091 
00092 /**
00093 
00094 Copyright 2010-2011 Helsinki Institute for Information Technology
00095 (HIIT) and the authors. All rights reserved.
00096 
00097 Authors: Tero Hasu <tero.hasu@hut.fi>
00098 
00099 Permission is hereby granted, free of charge, to any person
00100 obtaining a copy of this software and associated documentation files
00101 (the "Software"), to deal in the Software without restriction,
00102 including without limitation the rights to use, copy, modify, merge,
00103 publish, distribute, sublicense, and/or sell copies of the Software,
00104 and to permit persons to whom the Software is furnished to do so,
00105 subject to the following conditions:
00106 
00107 The above copyright notice and this permission notice shall be
00108 included in all copies or substantial portions of the Software.
00109 
00110 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
00111 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00112 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
00113 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
00114 BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
00115 ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
00116 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
00117 SOFTWARE.
00118 
00119  **/

ContextLogger2—ContextLogger2 Logger Daemon Internals—Generated on Mon May 2 13:49:57 2011 by Doxygen 1.6.1