diff --git a/build-mac/autogen-result.tar.gz b/build-mac/autogen-result.tar.gz index db52782..2482e8d 100644 Binary files a/build-mac/autogen-result.tar.gz and b/build-mac/autogen-result.tar.gz differ diff --git a/build-mac/libetpan.xcodeproj/project.pbxproj b/build-mac/libetpan.xcodeproj/project.pbxproj index cf17c41..bec14bb 100644 --- a/build-mac/libetpan.xcodeproj/project.pbxproj +++ b/build-mac/libetpan.xcodeproj/project.pbxproj @@ -7,6 +7,7 @@ objects = { /* Begin PBXBuildFile section */ + 2307A00C170AAA5500C43C59 /* mailstream_compress.c in Sources */ = {isa = PBXBuildFile; fileRef = 2307A00A170AAA5500C43C59 /* mailstream_compress.c */; }; 365DFFD215D1C93100F2DD85 /* xgmmsgid.c in Sources */ = {isa = PBXBuildFile; fileRef = 365DFFD115D1C93100F2DD85 /* xgmmsgid.c */; }; 8A75ECDA17040F92007F9972 /* mailimap_sort.c in Sources */ = {isa = PBXBuildFile; fileRef = 8A75ECD917040F91007F9972 /* mailimap_sort.c */; }; 8A75ECDB17040F92007F9972 /* mailimap_sort.c in Sources */ = {isa = PBXBuildFile; fileRef = 8A75ECD917040F91007F9972 /* mailimap_sort.c */; }; @@ -49,6 +50,9 @@ C6635C3B16DFF10E0066276E /* condstore.c in Sources */ = {isa = PBXBuildFile; fileRef = C6635C3816DFF10E0066276E /* condstore.c */; }; C6667DEF1342ACCD00969A8E /* xlist.c in Sources */ = {isa = PBXBuildFile; fileRef = C6667DED1342ACCD00969A8E /* xlist.c */; }; C6667DF11342ACCD00969A8E /* xlist.c in Sources */ = {isa = PBXBuildFile; fileRef = C6667DED1342ACCD00969A8E /* xlist.c */; }; + C668E2DA1736004400A2BB47 /* mailimap_compress.c in Sources */ = {isa = PBXBuildFile; fileRef = C668E2D81736004400A2BB47 /* mailimap_compress.c */; }; + C668E2DB1736004400A2BB47 /* mailimap_compress.c in Sources */ = {isa = PBXBuildFile; fileRef = C668E2D81736004400A2BB47 /* mailimap_compress.c */; }; + C668E2DC1736004400A2BB47 /* mailimap_compress.c in Sources */ = {isa = PBXBuildFile; fileRef = C668E2D81736004400A2BB47 /* mailimap_compress.c */; }; C682E21C15B315EF00BE9DA7 /* acl.c in Sources */ = {isa = PBXBuildFile; fileRef = C6F9E9EE105335BC0059C3BA /* acl.c */; }; C682E21D15B315EF00BE9DA7 /* acl_parser.c in Sources */ = {isa = PBXBuildFile; fileRef = C6F9E9F0105335BC0059C3BA /* acl_parser.c */; }; C682E21E15B315EF00BE9DA7 /* acl_sender.c in Sources */ = {isa = PBXBuildFile; fileRef = C6F9E9F2105335BC0059C3BA /* acl_sender.c */; }; @@ -561,6 +565,8 @@ /* End PBXContainerItemProxy section */ /* Begin PBXFileReference section */ + 2307A00A170AAA5500C43C59 /* mailstream_compress.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = mailstream_compress.c; sourceTree = ""; }; + 2307A00B170AAA5500C43C59 /* mailstream_compress.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = mailstream_compress.h; sourceTree = ""; }; 365DFFD115D1C93100F2DD85 /* xgmmsgid.c */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.c; path = xgmmsgid.c; sourceTree = ""; }; 365DFFD815D1CF1800F2DD85 /* xgmmsgid.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = xgmmsgid.h; sourceTree = ""; }; 8A75ECD917040F91007F9972 /* mailimap_sort.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = mailimap_sort.c; sourceTree = ""; }; @@ -599,6 +605,8 @@ C6635C3916DFF10E0066276E /* condstore.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = condstore.h; sourceTree = ""; }; C6667DED1342ACCD00969A8E /* xlist.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = xlist.c; sourceTree = ""; }; C6667DEE1342ACCD00969A8E /* xlist.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = xlist.h; sourceTree = ""; }; + C668E2D81736004400A2BB47 /* mailimap_compress.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = mailimap_compress.c; sourceTree = ""; }; + C668E2D91736004400A2BB47 /* mailimap_compress.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = mailimap_compress.h; sourceTree = ""; }; C682E2C015B315EF00BE9DA7 /* libetpan-ios.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = "libetpan-ios.a"; sourceTree = BUILT_PRODUCTS_DIR; }; C69AB10A10546FE500F32FBD /* libetpan.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = libetpan.a; sourceTree = BUILT_PRODUCTS_DIR; }; C6CE9B1514AA9C8900D20BA6 /* xgmlabels.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = xgmlabels.c; sourceTree = ""; }; @@ -1082,6 +1090,8 @@ C6F9E86A105335BC0059C3BA /* mailstream_cancel_types.h */, C6EFB8761433F1F300F805C0 /* mailstream_cfstream.c */, C6EFB8771433F1F300F805C0 /* mailstream_cfstream.h */, + 2307A00A170AAA5500C43C59 /* mailstream_compress.c */, + 2307A00B170AAA5500C43C59 /* mailstream_compress.h */, C6F9E86B105335BC0059C3BA /* mailstream_helper.c */, C6F9E86C105335BC0059C3BA /* mailstream_helper.h */, C6F9E86D105335BC0059C3BA /* mailstream_low.c */, @@ -1447,6 +1457,8 @@ C6F9E9FF105335BC0059C3BA /* idle.h */, C6F9EA00105335BC0059C3BA /* mailimap.c */, C6F9EA01105335BC0059C3BA /* mailimap.h */, + C668E2D81736004400A2BB47 /* mailimap_compress.c */, + C668E2D91736004400A2BB47 /* mailimap_compress.h */, C6F9EA02105335BC0059C3BA /* mailimap_extension.c */, C6F9EA03105335BC0059C3BA /* mailimap_extension.h */, C6F9EA04105335BC0059C3BA /* mailimap_extension_types.h */, @@ -2033,6 +2045,8 @@ C6F61F741701409B0073032E /* xgmthrid.c in Sources */, 8A75ECDA17040F92007F9972 /* mailimap_sort.c in Sources */, 8A75ECE6170414BA007F9972 /* mailimap_sort_types.c in Sources */, + 2307A00C170AAA5500C43C59 /* mailstream_compress.c in Sources */, + C668E2DA1736004400A2BB47 /* mailimap_compress.c in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; @@ -2213,6 +2227,7 @@ C6F61F761701409B0073032E /* xgmthrid.c in Sources */, 8A75ECDC17040F92007F9972 /* mailimap_sort.c in Sources */, 8A75ECE8170414BA007F9972 /* mailimap_sort_types.c in Sources */, + C668E2DC1736004400A2BB47 /* mailimap_compress.c in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; @@ -2393,6 +2408,7 @@ C6F61F751701409B0073032E /* xgmthrid.c in Sources */, 8A75ECDB17040F92007F9972 /* mailimap_sort.c in Sources */, 8A75ECE7170414BA007F9972 /* mailimap_sort_types.c in Sources */, + C668E2DB1736004400A2BB47 /* mailimap_compress.c in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; diff --git a/configure.ac b/configure.ac index e104933..f993138 100644 --- a/configure.ac +++ b/configure.ac @@ -64,7 +64,7 @@ API_AGE=`expr $API_CURRENT - $API_COMPATIBILITY` API_VERSION="$API_CURRENT:$API_REVISION:$API_AGE" AC_SUBST(API_VERSION) -dnl FIXME: This should be the CVS revision. +dnl FIXME: This should be the git revision. BUILD_REVISION=0 AC_SUBST(BUILD_REVISION) BUILD_TIMESTAMP=`date +'%Y-%m-%dT%H:%M%z'` @@ -670,6 +670,46 @@ if test "x$libexpat" = "xyes" ; then AC_DEFINE([HAVE_EXPAT], 1, [Define to use expat]) fi +AC_ARG_WITH([zlib], + [AS_HELP_STRING([--with-zlib], + [include classes for streaming compressed data in and out @<:@default=check@:>@])], + [],[with_zlib=check]) + +# Check for zlib. +HAVE_ZLIB=0 +AS_IF([test "$with_zlib" != no], [ + AC_MSG_CHECKING([zlib version]) + + # First check the zlib header version. + AC_COMPILE_IFELSE( + [AC_LANG_PROGRAM([[ + #include + #if !defined(ZLIB_VERNUM) || (ZLIB_VERNUM < 0x1204) + # error zlib version too old + #endif + ]], [])], [ + AC_MSG_RESULT([ok (1.2.0.4 or later)]) + + # Also need to add -lz to the linker flags and make sure this succeeds. + AC_SEARCH_LIBS([zlibVersion], [z], [ + AC_DEFINE([HAVE_ZLIB], [1], [Enable classes using zlib compression.]) + HAVE_ZLIB=1 + LDFLAGS="$LDFLAGS -lz" + ], [ + AS_IF([test "$with_zlib" != check], [ + AC_MSG_FAILURE([--with-zlib was given, but no working zlib library was found]) + ]) + ]) + ], [ + AS_IF([test "$with_zlib" = check], [ + AC_MSG_RESULT([headers missing or too old (requires 1.2.0.4)]) + ], [ + AC_MSG_FAILURE([--with-zlib was given, but zlib headers were not present or were too old (requires 1.2.0.4)]) + ]) + ]) +]) +AM_CONDITIONAL([HAVE_ZLIB], [test $HAVE_ZLIB = 1]) + dnl Version VERSION="$VERSION_MAJOR.$VERSION_MINOR" test -d .git && VERSION="$VERSION-dev-"`date +'%Y%m%d'` diff --git a/src/data-types/Makefile.am b/src/data-types/Makefile.am index 8cae729..ed412a4 100644 --- a/src/data-types/Makefile.am +++ b/src/data-types/Makefile.am @@ -33,6 +33,7 @@ etpaninclude_HEADERS = \ mmapstring.h mailstream.h mailstream_helper.h mail.h \ mailstream_low.h \ mailstream_socket.h mailstream_ssl.h mailstream_cfstream.h \ + mailstream_compress.h \ mailstream_types.h \ carray.h clist.h chash.h \ charconv.h mailsem.h maillock.h @@ -50,4 +51,5 @@ libdata_types_la_SOURCES = connect.h connect.c base64.h hmac-md5.h \ mailsasl.c mailstream_cancel_types.h mailstream_cancel.h \ mailstream_cancel.c timeutils.h timeutils.c \ mmapstring_private.h mailstream_ssl_private.h \ - mailstream_cfstream.c mailstream_cfstream.h + mailstream_cfstream.c mailstream_cfstream.h \ + mailstream_compress.c mailstream_compress.h diff --git a/src/data-types/mailstream_compress.c b/src/data-types/mailstream_compress.c new file mode 100644 index 0000000..b45f6c5 --- /dev/null +++ b/src/data-types/mailstream_compress.c @@ -0,0 +1,316 @@ +/* + * libEtPan! -- a mail stuff library + * + * Copyright (C) 2001, 2013 - DINH Viet Hoa + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the libEtPan! project nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +/* Created by Ian Ragsdale on 3/8/13. */ + +#include "mailstream_compress.h" + +#if !HAVE_ZLIB + +mailstream_low * mailstream_low_compress_open(mailstream_low * ms) +{ + return NULL; +} + +#else + +#include +#include +#include +#include + +#include "mailstream_low.h" +#include "mailstream_low.h" +#include "mailstream_cancel.h" + +#define CHUNK_SIZE 1024 + +static ssize_t mailstream_low_compress_read(mailstream_low * s, void * buf, size_t count); +static ssize_t mailstream_low_compress_write(mailstream_low * s, const void * buf, size_t count); +static int mailstream_low_compress_close(mailstream_low * s); +static int mailstream_low_compress_get_fd(mailstream_low * s); +static struct mailstream_cancel * mailstream_low_compress_get_cancel(mailstream_low * s); +static void mailstream_low_compress_free(mailstream_low * s); +static void mailstream_low_compress_cancel(mailstream_low * s); + +typedef struct mailstream_compress_data +{ + mailstream_low * ms; + z_stream *compress_stream; + z_stream *decompress_stream; + unsigned char input_buf[CHUNK_SIZE]; + unsigned char output_buf[CHUNK_SIZE]; +} compress_data; + +static mailstream_low_driver local_mailstream_compress_driver = { + /* mailstream_read */ mailstream_low_compress_read, + /* mailstream_write */ mailstream_low_compress_write, + /* mailstream_close */ mailstream_low_compress_close, + /* mailstream_get_fd */ mailstream_low_compress_get_fd, + /* mailstream_free */ mailstream_low_compress_free, + /* mailstream_cancel */ mailstream_low_compress_cancel, + /* mailstream_get_cancel */ mailstream_low_compress_get_cancel, +}; + +mailstream_low_driver * mailstream_compress_driver = &local_mailstream_compress_driver; + +mailstream_low * mailstream_low_compress_open(mailstream_low * ms) +{ + mailstream_low * s; + + /* stores the original mailstream */ + struct mailstream_compress_data * compress_data = malloc(sizeof(* compress_data)); + if (compress_data == NULL) + goto err; + + /* allocate deflate state */ + compress_data->compress_stream = malloc(sizeof(z_stream)); + compress_data->compress_stream->zalloc = Z_NULL; + compress_data->compress_stream->zfree = Z_NULL; + compress_data->compress_stream->opaque = Z_NULL; + /* these specific settings are very important - don't change without looking at the COMPRESS RFC */ + int ret = deflateInit2(compress_data->compress_stream, Z_BEST_SPEED, Z_DEFLATED, -15, 8, Z_DEFAULT_STRATEGY); + if (ret != Z_OK) { + goto free_compress_data; + } + compress_data->compress_stream->avail_in = 0; + compress_data->compress_stream->avail_out = 0; + + /* allocate inflate state */ + compress_data->decompress_stream = malloc(sizeof(z_stream)); + compress_data->decompress_stream->zalloc = Z_NULL; + compress_data->decompress_stream->zfree = Z_NULL; + compress_data->decompress_stream->opaque = Z_NULL; + /* these specific settings are very important - don't change without looking at the COMPRESS RFC */ + ret = inflateInit2(compress_data->decompress_stream, -15); + if (ret != Z_OK) { + goto free_compress_data; + } + compress_data->decompress_stream->avail_in = 0; + compress_data->decompress_stream->avail_out = 0; + + compress_data->ms = ms; + + s = mailstream_low_new(compress_data, mailstream_compress_driver); + if (s == NULL) + goto free_compress_data; + + return s; + + free_compress_data: + if (compress_data->compress_stream) { + deflateEnd(compress_data->compress_stream); + free(compress_data->compress_stream); + } + if (compress_data->decompress_stream) { + inflateEnd(compress_data->decompress_stream); + free(compress_data->decompress_stream); + } + free(compress_data); + err: + return NULL; +} + +ssize_t mailstream_low_compress_read(mailstream_low * s, void * buf, size_t count) { + compress_data *data = s->data; + data->ms->timeout = s->timeout; + z_stream *strm = data->decompress_stream; + + int zr; + + do { + /* if there is no compressed data, read more */ + if (strm->avail_in == 0) { + int read = data->ms->driver->mailstream_read(data->ms, data->input_buf, CHUNK_SIZE); + if (read <= 0) { + return read; + } + strm->avail_in = read; + strm->next_in = data->input_buf; + } + + /* set the output buffer */ + strm->next_out = buf; + strm->avail_out = count; + + /* uncompress any waiting data */ + zr = inflate(strm, Z_NO_FLUSH); + } + /* + it's possible that there was data in the stream, but not enough that zlib could figure + out what to do with it. in this case, read some more and try again. + */ + while (zr == Z_OK && strm->avail_in == 0 && strm->avail_out == count); + + /* if we got an error, return -1 to close the connection */ + if (zr < 0) { + return -1; + } + + /* let the client know how much data was read */ + return count - strm->avail_out; +} + +/* + mostly copied from mailstream_ssl.c + removed their windows support - we only need iOS +*/ +static int wait_write_compress(mailstream_low * s) +{ + fd_set fds_read; + fd_set fds_write; + struct timeval timeout; + int r; + int fd; + int max_fd; + int cancelled; + int write_enabled; + + // use the session timeout if set + if (s->timeout) { + timeout.tv_sec = s->timeout; + timeout.tv_usec = 0; + } else { + timeout = mailstream_network_delay; + } + + FD_ZERO(&fds_read); + struct mailstream_cancel * cancel = mailstream_low_compress_get_cancel(s); + fd = mailstream_cancel_get_fd(cancel); + FD_SET(fd, &fds_read); + FD_ZERO(&fds_write); + FD_SET(mailstream_low_compress_get_fd(s), &fds_write); + + max_fd = mailstream_low_compress_get_fd(s); + if (fd > max_fd) + max_fd = fd; + + r = select(max_fd + 1, &fds_read, &fds_write, NULL, &timeout); + if (r <= 0) + return -1; + + cancelled = FD_ISSET(fd, &fds_read); + write_enabled = FD_ISSET(mailstream_low_compress_get_fd(s), &fds_write); + + if (cancelled) { + /* cancelled */ + mailstream_cancel_ack(cancel); + return -1; + } + + if (!write_enabled) + return 0; + + return 1; +} + +ssize_t mailstream_low_compress_write(mailstream_low * s, +const void * buf, size_t count) { + + int zr, wr; + compress_data *data = s->data; + data->ms->timeout = s->timeout; + z_stream *strm = data->compress_stream; + + strm->next_in = (Bytef *)buf; + /* we won't try to compress more than CHUNK_SIZE at a time so we always have enough buffer space */ + int compress_len = MIN(count, CHUNK_SIZE); + strm->avail_in = compress_len; + strm->avail_out = CHUNK_SIZE; + strm->next_out = data->output_buf; + + zr = deflate(strm, Z_PARTIAL_FLUSH); + assert(zr == Z_OK); + if (zr < 0) { + printf("Error deflating: %d %s", zr, strm->msg); + return -1; + } + + /* + wait until we can write to the buffer - we want to avoid any situation where we can have a partial write, + because with a partial write we can't tell the caller how much uncompressed data was written + */ + wait_write_compress(s); + + /* write the data to the underlying mailstream */ + wr = data->ms->driver->mailstream_write(data->ms, data->output_buf, CHUNK_SIZE - strm->avail_out); + + /* + if we were unable to write all the compressed data to the underlying stream, we're in a bit of trouble + we don't know how much UNcompressed data was written, so we can't let the client know how much to retry + so, we return -1 and hope that the wait_write call ensures this never happens + */ + int len = CHUNK_SIZE-strm->avail_out; + if (wr < len) { + return -1; + assert(0); + } + + /* let the caller know how much data we wrote */ + return compress_len - strm->avail_in; +} + +int mailstream_low_compress_close(mailstream_low * s) { + compress_data *data = s->data; + return data->ms->driver->mailstream_close(data->ms); +} + +int mailstream_low_compress_get_fd(mailstream_low * s) { + compress_data *data = s->data; + return data->ms->driver->mailstream_get_fd(data->ms); +} + +struct mailstream_cancel * mailstream_low_compress_get_cancel(mailstream_low * s) { + compress_data *data = s->data; + return data->ms->driver->mailstream_get_cancel(data->ms); +} + +void mailstream_low_compress_free(mailstream_low * s) { + compress_data *data = s->data; + data->ms->driver->mailstream_free(data->ms); + if (data->compress_stream) { + deflateEnd(data->compress_stream); + free(data->compress_stream); + } + if (data->decompress_stream) { + inflateEnd(data->decompress_stream); + free(data->decompress_stream); + } + free(data); + free(s); +} + +void mailstream_low_compress_cancel(mailstream_low * s) { + compress_data *data = s->data; + data->ms->driver->mailstream_cancel(data->ms); +} + +#endif diff --git a/src/data-types/mailstream_compress.h b/src/data-types/mailstream_compress.h new file mode 100644 index 0000000..66a593c --- /dev/null +++ b/src/data-types/mailstream_compress.h @@ -0,0 +1,53 @@ +/* + * libEtPan! -- a mail stuff library + * + * Copyright (C) 2001, 2013 - DINH Viet Hoa + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the libEtPan! project nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +/* Created by Ian Ragsdale on 3/8/13. */ + +#ifndef MAILSTREAM_COMPRESS_H +#define MAILSTREAM_COMPRESS_H + +#define USE_DEFLATE 1 + +#include + +#ifdef __cplusplus +extern "C" { +#endif + +extern mailstream_low_driver * mailstream_compress_driver; + +struct mailstream_compress_context; + +/* exported methods */ +LIBETPAN_EXPORT +mailstream_low * mailstream_low_compress_open(mailstream_low * ms); + +#endif \ No newline at end of file diff --git a/src/low-level/imap/Makefile.am b/src/low-level/imap/Makefile.am index e20a805..7ce948e 100644 --- a/src/low-level/imap/Makefile.am +++ b/src/low-level/imap/Makefile.am @@ -46,7 +46,8 @@ etpaninclude_HEADERS = \ mailimap_id.h mailimap_id_types.h \ enable.h condstore.h condstore_types.h \ qresync.h qresync_types.h \ - mailimap_sort.h mailimap_sort_types.h + mailimap_sort.h mailimap_sort_types.h \ + mailimap_compress.h AM_CPPFLAGS = -I$(top_builddir)/include \ -I$(top_srcdir)/src/data-types @@ -96,5 +97,6 @@ libimap_la_SOURCES = \ enable.h enable.c \ condstore.h condstore.c condstore_types.h condstore_types.c condstore_private.h \ qresync.h qresync.c qresync_types.h qresync_types.c qresync_private.h \ - mailimap_sort.c mailimap_sort_types.c - + mailimap_sort.c mailimap_sort.h \ + mailimap_sort_types.c mailimap_sort_types.h \ + mailimap_compress.c mailimap_compress.h diff --git a/src/low-level/imap/mailimap.h b/src/low-level/imap/mailimap.h index 5249ed8..f27a809 100644 --- a/src/low-level/imap/mailimap.h +++ b/src/low-level/imap/mailimap.h @@ -62,6 +62,8 @@ extern "C" { #include #include #include +#include +#include /* mailimap_connect() @@ -615,6 +617,8 @@ int mailimap_unsubscribe(mailimap * session, const char * mb); This function starts change the mode of the connection to switch to SSL connection. + It won't change the stream connection to SSL rightway. + See mailimap_socket_starttls() will switch the mailstream too. @param session IMAP session diff --git a/src/low-level/imap/mailimap_compress.c b/src/low-level/imap/mailimap_compress.c new file mode 100644 index 0000000..2ede7a8 --- /dev/null +++ b/src/low-level/imap/mailimap_compress.c @@ -0,0 +1,102 @@ +/* + * libEtPan! -- a mail stuff library + * + * Copyright (C) 2001, 2005 - DINH Viet Hoa + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the libEtPan! project nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include "mailimap_compress.h" + +#include "mailimap.h" +#include "mailimap_sender.h" +#include "mailstream_compress.h" + +int mailimap_compress(mailimap * session) +{ + struct mailimap_response * response; + int r; + int res; + int error_code; + mailstream_low * compressed_stream; + mailstream_low * low; + + r = mailimap_send_current_tag(session); + if (r != MAILIMAP_NO_ERROR) { + res = r; + goto err; + } + + r = mailimap_token_send(session->imap_stream, "COMPRESS DEFLATE"); + if (r != MAILIMAP_NO_ERROR) { + res = r; + goto err; + } + + r = mailimap_crlf_send(session->imap_stream); + if (r != MAILIMAP_NO_ERROR) { + res = r; + goto err; + } + + if (mailstream_flush(session->imap_stream) == -1) { + res = MAILIMAP_ERROR_STREAM; + goto err; + } + + if (mailimap_read_line(session) == NULL) { + res = MAILIMAP_ERROR_STREAM; + goto err; + } + + r = mailimap_parse_response(session, &response); + if (r != MAILIMAP_NO_ERROR) { + res = r; + goto err; + } + + error_code = response->rsp_resp_done->rsp_data.rsp_tagged->rsp_cond_state->rsp_type; + + mailimap_response_free(response); + + if (error_code != MAILIMAP_RESP_COND_STATE_OK) { + res = MAILIMAP_ERROR_EXTENSION; + goto err; + } + + low = mailstream_get_low(session->imap_stream); + compressed_stream = mailstream_low_compress_open(low); + if (compressed_stream == NULL) { + res = MAILIMAP_ERROR_STREAM; + goto err; + } + mailstream_set_low(session->imap_stream, compressed_stream); + + return MAILIMAP_NO_ERROR; + +err: + return res; +} diff --git a/src/low-level/imap/mailimap_compress.h b/src/low-level/imap/mailimap_compress.h new file mode 100644 index 0000000..d1a1e5e --- /dev/null +++ b/src/low-level/imap/mailimap_compress.h @@ -0,0 +1,53 @@ +/* + * libEtPan! -- a mail stuff library + * + * Copyright (C) 2001, 2005 - DINH Viet Hoa + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the libEtPan! project nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#ifndef MAILIMAP_COMPRESS_H + +#define MAILIMAP_COMPRESS_H + +#include + +/* + mailimap_compress() + + This function will request IMAP compression by sending + a COMPRESS command. It will also change the stream connection to + a compressed stream (mailstream_compress). + + @param session IMAP session + + @return the return code is one of MAILIMAP_ERROR_XXX or + MAILIMAP_NO_ERROR codes + */ + +int mailimap_compress(mailimap * session); + +#endif