//
// $Id: ZlibOutStream.m,v 1.6 2005/05/18 06:46:54 will_mason Exp $
//
// vi: set ft=objc:

/*
 * ObjectiveLib - a library of containers and algorithms for Objective-C
 *
 * Copyright (c) 2004
 * Will Mason
 *
 * Portions:
 *
 * Copyright (c) 1994
 * Hewlett-Packard Company
 *
 * Copyright (c) 1996,1997
 * Silicon Graphics Computer Systems, Inc.
 *
 * Copyright (c) 1997
 * Moscow Center for SPARC Technology
 *
 * Copyright (c) 1999 
 * Boris Fomitchev
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * You may contact the author at will_mason@users.sourceforge.net.
 */

#import "ZlibOutStreamPackage.h"
#import "OutStreamPackage.h"
#import "Macros.h"
#import <Foundation/NSString.h>
#import <Foundation/NSException.h>
#import <stddef.h>
#import <zlib.h>
#import <string.h>

voidpf __ol_zalloc(voidpf alloc, uInt items, uInt size)
{
    return [((OLAllocator*)alloc) allocateBytes: items * size];
}

void __ol_zfree(voidpf alloc, voidpf address)
{
    [((OLAllocator*)alloc) deallocateBytes: address];
}

@implementation OLZlibOutStream

+ (id) streamWithOutStream: (OLOutStream*)underStream
{
    OL_BEGIN_AUTO_CTOR(OLZlibOutStream)
        initWithOutStream: underStream
    OL_END_AUTO_CTOR;
}

+ (id) streamWithOutStream: (OLOutStream*)underStream compressionLevel: (int)level
{
    OL_BEGIN_AUTO_CTOR(OLZlibOutStream)
        initWithOutStream: underStream compressionLevel: level
    OL_END_AUTO_CTOR;
}

- (id) initWithOutStream: (OLOutStream*)underStream
{
    return [self initWithOutStream: underStream compressionLevel: Z_DEFAULT_COMPRESSION];
}

- (id) initWithOutStream: (OLOutStream*)underStream compressionLevel: (int)level
{
    return [self initWithOutStream: underStream compressionLevel: level bufferSize: 1024
        writeZlibHeader: YES allocator: [OLAllocator defaultAllocator]];
}

- (id) initWithOutStream: (OLOutStream*)underStream compressionLevel: (int)level bufferSize: (unsigned)size
{
    return [self initWithOutStream: underStream compressionLevel: level bufferSize: size writeZlibHeader: YES allocator: [OLAllocator defaultAllocator]];
}

- (id) initWithOutStream: (OLOutStream*)underStream compressionLevel: (int)level bufferSize: (unsigned)size writeZlibHeader: (BOOL)zlibHeader allocator: (OLAllocator*)alloc
{
    int rc;
    char msgBuf[512];
    int windowBits = zlibHeader ? MAX_WBITS : -MAX_WBITS;

    [super initWithOutStream: underStream];
    if (level != Z_DEFAULT_COMPRESSION && (level < 0 || level > 9))
    {
        isFinished = YES;
        [NSException raise: NSInvalidArgumentException
            format: @"Compression level must be in the range [0,9]"];
    }
    allocator = OBJ_RETAIN(alloc);
    zstream = [alloc allocateBytes: sizeof(struct z_stream_s)];
    zstream->zalloc = __ol_zalloc;
    zstream->zfree = __ol_zfree;
    zstream->opaque = alloc;
    rc = deflateInit2(zstream, level, Z_DEFLATED, windowBits, MAX_MEM_LEVEL, Z_DEFAULT_STRATEGY);
    if (rc != Z_OK)
    {
        if (zstream->msg == NULL)
        {
            msgBuf[0] = 0;
        }
        else
        {
            strncpy(msgBuf, zstream->msg, 512);
            msgBuf[511] = 0;
        }
        buffer = NULL;
        bufferSize = 0;
        [alloc deallocateBytes: zstream];
        zstream = NULL;
        isFinished = YES;
        [NSException raise: OLInputOutputException
            format: @"Error initializing for deflation - %s", msgBuf];
    }
    buffer = [alloc allocateBytes: size];
    bufferSize = size;
    isFinished = NO;
    return self;
}

- (void) dealloc
{
    [self close];
    if (buffer != NULL)
        [allocator deallocateBytes: buffer];
    if (zstream != NULL)
        [allocator deallocateBytes: zstream];
    OBJ_RELEASE(allocator);
    [super dealloc];
}

- (void) close
{
    if (!isFinished)
        [self finish];
    [stream flush];
    [stream close];
}

- (unsigned) writeBytes: (const uint8_t*)bytes count: (unsigned)num
{
    zstream->next_in = (Bytef*)bytes;
    zstream->avail_in = num;
    while (zstream->avail_in > 0)
    {
        zstream->next_out = buffer;
        zstream->avail_out = bufferSize;
        if (deflate(zstream, Z_NO_FLUSH) != Z_OK)
        {
            [NSException raise: OLInputOutputException
                format: @"Error deflating - %s", zstream->msg];
        }
        [stream completelyWriteBytes: buffer count: bufferSize - zstream->avail_out];
    }
    return num;
}

@end

@implementation OLZlibOutStream (PackageMethods)

- (void) finish
{
    char msgBuf[512];
    int rc;

    // Regardless of what happens in this method we don't want to do it again
    isFinished = YES;

    zstream->next_in = NULL;
    zstream->avail_in = 0;
    do
    {
        zstream->next_out = buffer;
        zstream->avail_out = bufferSize;
        rc = deflate(zstream, Z_FINISH);
        [stream completelyWriteBytes: buffer count: bufferSize - zstream->avail_out];
    } while (rc == Z_OK);
    if (rc != Z_STREAM_END)
    {
        if (zstream->msg == NULL)
        {
            msgBuf[0] = 0;
        }
        else
        {
            strncpy(msgBuf, zstream->msg, 512);
            msgBuf[511] = 0;
        }
        deflateEnd(zstream);
        [NSException raise: OLInputOutputException
            format: @"Error closing OLZlibOutStream - %s", msgBuf];
    }
    if (deflateEnd(zstream) != Z_OK)
    {
        [NSException raise: OLInputOutputException
            format: @"Error closing OLZlibOutStream - %s", zstream->msg];
    }
}

@end

