/* 
   NSConcreteData.m

   Copyright (C) 1995, 1996 Ovidiu Predescu and Mircea Oancea.
   All rights reserved.

   Author: Ovidiu Predescu <ovidiu@bx.logicnet.ro>

   This file is part of libFoundation.

   Permission to use, copy, modify, and distribute this software and its
   documentation for any purpose and without fee is hereby granted, provided
   that the above copyright notice appear in all copies and that both that
   copyright notice and this permission notice appear in supporting
   documentation.

   We disclaim all warranties with regard to this software, including all
   implied warranties of merchantability and fitness, in no event shall
   we be liable for any special, indirect or consequential damages or any
   damages whatsoever resulting from loss of use, data or profits, whether in
   an action of contract, negligence or other tortious action, arising out of
   or in connection with the use or performance of this software.
*/

#include <Foundation/common.h>
#include <extensions/objc-runtime.h>

#include "NSConcreteData.h"

/*
 * Subdata of a concrete data
 */

@implementation NSConcreteDataRange

- (id)initWithData:(NSData*)data range:(NSRange)range
{
    bytes  = (char*)[data bytes] + range.location;
    length = capacity = range.length;
    range  = range;
    parent = [data retain];
    return self;
}

- (id)copyWithZone:(NSZone*)zone
{
    if ([self zone] == zone)
	return [self retain];
    return [[NSConcreteData allocWithZone:zone]
	    initWithBytes:bytes length:length];
}

- (const void*)bytes
{
    return bytes;
}

- (unsigned int)length
{
    return length;
}

- (void)dealloc
{
    [parent release];
    [super dealloc];
}

@end /* NSConcreteDataRange */

/*
* Concrete data
*/

@implementation NSConcreteData

- (id)initWithBytes:(const void*)_bytes length:(unsigned int)_length
{
    void* copy_of_bytes = NSZoneMalloc([self zone], _length);

    memcpy(copy_of_bytes, _bytes, _length);
    return [self initWithBytesNoCopy:copy_of_bytes length:_length];
}

- (id)initWithBytesNoCopy:(void*)_bytes
    length:(unsigned int)_length
{
    capacity = length = _length;
    bytes = _bytes;
    return self;
}

- (id)copyWithZone:(NSZone*)zone
{
    if ([self zone] == zone)
	return [self retain];
    return [[[self class] allocWithZone:zone]
	    initWithBytes:bytes length:length];
}

- (const void*)bytes
{
    return bytes;
}

- (unsigned int)length
{
    return length;
}

- (void)dealloc
{
    Free(bytes);
    [super dealloc];
}

@end /* NSConcreteData */

/*
* Mutable data
*/

@implementation NSConcreteMutableData

+ (void)initialize
{
    static BOOL initialized = NO;
    if(!initialized) {
	initialized = YES;
	class_add_behavior(self, [NSConcreteData class]);
    }
}

- (id)initWithCapacity:(unsigned int)_capacity
{
    [self initWithLength:_capacity];
    length = 0;
    return self;
}

- (id)initWithLength:(unsigned int)_length
{
    capacity = length = _length;
    bytes = NSZoneRealloc(
	bytes ? NSZoneFromPointer(bytes) : [self zone],
	bytes, capacity);
    memset(bytes, 0, capacity);

    return self;
}

- (void)increaseLengthBy:(unsigned int)extraLength
{
    if((length += extraLength) >= capacity) {
	unsigned int extent = length - capacity;

	bytes = NSZoneRealloc(
	    bytes ? NSZoneFromPointer(bytes) : [self zone],
	    bytes, length);
	memset(bytes + capacity, 0, extent);
	capacity = length;
    }
}

- (void*)mutableBytes
{
    return bytes;
}

- (void)setLength:(unsigned int)_length
{
    if(_length <= capacity)
	length = _length;
    else [self increaseLengthBy:_length - length];
}

- (void)increaseCapacityBy:(unsigned int)extraCapacity
{
    capacity += extraCapacity;
    bytes = NSZoneRealloc(
	bytes ? NSZoneFromPointer(bytes) : [self zone],
	bytes, capacity);
}

- (void)appendBytes:(const void*)_bytes
    length:(unsigned int)_length
{
    if((length + _length) >= capacity)
	[self increaseCapacityBy:length + _length - capacity];
    memcpy(bytes + length, _bytes, _length);
    length += _length;
}

@end /* NSConcreteMutableData */
