//
// $Id: BzlibOutStream.m,v 1.4 2005/05/18 06:46:52 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 "BzlibOutStream.h"
#import "OutStreamPackage.h"
#import "Macros.h"
#import <Foundation/NSString.h>
#import <Foundation/NSException.h>
#import <stddef.h>
#import <bzlib.h>

void* __ol_bzalloc(void* alloc, int items, int size)
{
    return [((OLAllocator*)alloc) allocateBytes: items * size];
}

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

@implementation OLBzlibOutStream

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

+ (id) streamWithOutStream: (OLOutStream*)underStream blockSize: (int)block
{
    OL_BEGIN_AUTO_CTOR(OLBzlibOutStream)
        initWithOutStream: underStream blockSize: block
    OL_END_AUTO_CTOR;
}

- (id) initWithOutStream: (OLOutStream*)underStream
{
    return [self initWithOutStream: underStream blockSize: 9 bufferSize: 1024 allocator: [OLAllocator defaultAllocator]];
}

- (id) initWithOutStream: (OLOutStream*)underStream blockSize: (int)block
{
    return [self initWithOutStream: underStream blockSize: block bufferSize: 1024 allocator: [OLAllocator defaultAllocator]];
}

- (id) initWithOutStream: (OLOutStream*)underStream blockSize: (int)block bufferSize: (unsigned)size
{
    return [self initWithOutStream: underStream blockSize: block bufferSize: size allocator: [OLAllocator defaultAllocator]];
}

- (id) initWithOutStream: (OLOutStream*)underStream blockSize: (int)block bufferSize: (unsigned)size allocator: (OLAllocator*)alloc
{
    int rc;

    [super initWithOutStream: underStream];
    if (block < 1 || block > 9)
    {
        [NSException raise: NSInvalidArgumentException
            format: @"Block size must be in the range [1,9]"];
    }
    allocator = OBJ_RETAIN(alloc);
    bzstream = [alloc allocateBytes: sizeof(bz_stream)];
    BZSTREAM->bzalloc = __ol_bzalloc;
    BZSTREAM->bzfree = __ol_bzfree;
    BZSTREAM->opaque = alloc;
    rc = BZ2_bzCompressInit(BZSTREAM, block, 0, 0);
    if (rc != BZ_OK)
    {
        buffer = NULL;
        bufferSize = 0;
        [alloc deallocateBytes: bzstream];
        bzstream = NULL;
        [NSException raise: OLInputOutputException
            format: @"Error initializing for decompression"];
    }
    buffer = [alloc allocateBytes: size];
    bufferSize = size;
    return self;
}

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

- (void) close
{
    int rc;

    if (bzstream != NULL)
    {
        BZSTREAM->next_in = NULL;
        BZSTREAM->avail_in = 0;
        do
        {
            BZSTREAM->next_out = buffer;
            BZSTREAM->avail_out = bufferSize;
            rc = BZ2_bzCompress(BZSTREAM, BZ_FINISH);
            [stream completelyWriteBytes: buffer count: bufferSize - BZSTREAM->avail_out];
        } while (rc == BZ_FINISH_OK);
        if (rc != BZ_STREAM_END)
        {
            BZ2_bzCompressEnd(BZSTREAM);
            [NSException raise: OLInputOutputException
                format: @"Error closing OLBzlibOutStream"];
        }
        BZ2_bzCompressEnd(BZSTREAM);
        [allocator deallocateBytes: bzstream];
        bzstream = NULL;
    }
    [stream flush];
    [stream close];
}

- (unsigned) writeBytes: (const uint8_t*)bytes count: (unsigned)num
{
    BZSTREAM->next_in = (char*)bytes;
    BZSTREAM->avail_in = num;
    while (BZSTREAM->avail_in > 0)
    {
        BZSTREAM->next_out = buffer;
        BZSTREAM->avail_out = bufferSize;
        if (BZ2_bzCompress(BZSTREAM, BZ_RUN) != BZ_RUN_OK)
        {
            [NSException raise: OLInputOutputException
                format: @"Error compressing"];
        }
        [stream completelyWriteBytes: buffer count: bufferSize - BZSTREAM->avail_out];
    }
    return num;
}

@end
