Пример #1
0
void UArray_setAllBitsTo_(UArray *self, uint8_t aBool)
{
	size_t i, max = UArray_sizeInBytes(self);
	uint8_t *data = self->data;
	uint8_t bits = aBool ? ~0 : 0;
	for (i = 0; i < max; i ++) { data[i] = bits; }
}
Пример #2
0
Файл: UArray.c Проект: bomma/io
void UArray_setItemType_(UArray *self, CTYPE type)
{
	size_t itemSize = CTYPE_size(type);
	div_t q = div(UArray_sizeInBytes(self), itemSize);

	if (q.rem != 0)
	{
		q.quot += 1;
		UArray_setSize_(self, (q.quot * itemSize) / self->itemSize);
	}

	self->itemType = type;

	self->itemSize = itemSize;
	self->size = q.quot;

	// ensure encoding is sane for type

	if (UArray_isFloatType(self))
	{
		self->encoding = CENCODING_NUMBER;
	}
	else if (self->encoding == CENCODING_ASCII)
	{
		switch(self->itemSize)
		{
			case 2: self->encoding = CENCODING_UCS2; break;
			case 4: self->encoding = CENCODING_UCS4; break;
			case 8: self->encoding = CENCODING_NUMBER; break;
		}
	}
}
Пример #3
0
int UArray_bitAt_(UArray *self, size_t i)
{
	size_t bytePos = i / 8;
	size_t bitPos  = i - bytePos;
	if (bytePos >= UArray_sizeInBytes(self)) return 0;
	return self->data[bytePos] = (self->data[bytePos] >> bitPos) & 0x1;
}
Пример #4
0
size_t UArray_bitCount(UArray *self)
{
	const unsigned char map[] =
	{
		0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4,
		1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
		1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
		2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
		1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
		2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
		2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
		3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
		1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
		2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
		2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
		3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
		2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
		3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
		3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
		4, 5, 5, 6, 5, 6, 6, 7, 5, 6, 6, 7, 6, 7, 7, 8
	};

	size_t i, max = UArray_sizeInBytes(self);
	uint8_t *data = self->data;
	size_t total = 0;

	for (i = 0; i < max; i ++) { total += map[data[i]]; }

	return total;
}
Пример #5
0
Datum Datum_FromUArray_(UArray *ba)
{
	Datum d;
	d.data = (unsigned char *)UArray_bytes(ba);
	d.size = UArray_sizeInBytes(ba);
	return d;
}
Пример #6
0
void UArray_bitwiseNot(UArray *self)
{
    size_t i, max = UArray_sizeInBytes(self);
    uint8_t *data = self->data;
    for (i = 0; i < max; i ++) {
        data[i] = ~(data[i]);
    }
}
Пример #7
0
IoObject *IoSeq_sizeInBytes(IoSeq *self, IoObject *locals, IoMessage *m)
{
	/*doc Sequence sizeInBytes
	Returns the length in bytes of the receiver.
	*/

	return IONUMBER(UArray_sizeInBytes(DATA(self)));
}
Пример #8
0
IO_METHOD(IoSeq, sizeInBytes)
{
	/*doc Sequence sizeInBytes
	Returns the length in bytes of the receiver.
	*/

	return IONUMBER(UArray_sizeInBytes(DATA(self)));
}
Пример #9
0
void UArray_setBit_at_(UArray *self, int aBool, size_t i)
{
	size_t bytePos = i / 8;
	size_t bitPos  = i - bytePos;
	uint8_t n = 0x1 << bitPos;
	uint8_t b;
	if (bytePos >= UArray_sizeInBytes(self)) return;
	b = self->data[bytePos];
	b ^= n;
	if (aBool) b |= (0x1 << bitPos);
	self->data[bytePos] = b;
}
Пример #10
0
IoObject *IoBlowfish_beginProcessing(IoObject *self, IoObject *locals, IoMessage *m)
{
	/*doc Blowfish beginProcessing
	Sets the key from the key slot and initializes the cipher.
	*/

	UArray *key = IoObject_rawGetUArraySlot(self, locals, m, IOSYMBOL("key"));
	blowfish_ctx *context = &(DATA(self)->context);

	blowfish_init(context, (uint8_t *)UArray_bytes(key), UArray_sizeInBytes(key));

	return self;
}
Пример #11
0
IoObject *IoBlowfish_endProcessing(IoBlowfish *self, IoObject *locals, IoMessage *m)
{
	/*doc Blowfish endProcessing
	Finish processing remaining bytes of inputBuffer.
	*/

	blowfish_ctx *context = &(DATA(self)->context);
	unsigned long lr[2];

	IoBlowfish_process(self, locals, m); // process the full blocks first


	{
		int isEncrypting = DATA(self)->isEncrypting;

		UArray *input  = IoObject_rawGetMutableUArraySlot(self, locals, m, IOSYMBOL("inputBuffer"));
		UArray *output = IoObject_rawGetMutableUArraySlot(self, locals, m, IOSYMBOL("outputBuffer"));

		IOASSERT(UArray_sizeInBytes(input) < sizeof(lr), "internal error - too many bytes left in inputBuffer");

		memset(lr, 0, sizeof(lr));
		memcpy(lr, (uint8_t *)UArray_bytes(input), UArray_sizeInBytes(input));

		if (isEncrypting)
		{
			blowfish_encrypt(context, &lr[0], &lr[1]);
		}
		else
		{
			blowfish_decrypt(context, &lr[0], &lr[1]);
		}

		UArray_appendBytes_size_(output, (unsigned char *)&lr, sizeof(lr));

		UArray_setSize_(input, 0);
	}
	return self;
}
Пример #12
0
int UArray_isMultibyte(const UArray *self)
{
    if (self->encoding == CENCODING_UTF8)
    {
        size_t i, max = UArray_sizeInBytes(self);
        const uint8_t *bytes = UArray_bytes(self);
        for (i = 0; i < max; i ++)
        {
            if (UArray_SizeOfUTF8Char(bytes + i) > 1) return 1;
        }
        //UARRAY_INTFOREACH(self, i, v, if (ismbchar((int)v)) return 1; );
    }

    return 0;
}
Пример #13
0
IoObject *IoZlibEncoder_process(IoZlibEncoder *self, IoObject *locals, IoMessage *m)
{
	/*doc ZlibEncoder process
	Process the inputBuffer and appends the result to the outputBuffer.
	The processed inputBuffer is empties except for the spare bytes at 
	the end which don't fit into a cipher block.
	*/
	
	z_stream *strm = DATA(self)->strm;

	UArray *input  = IoObject_rawGetMutableUArraySlot(self, locals, m, IOSYMBOL("inputBuffer"));
	UArray *output = IoObject_rawGetMutableUArraySlot(self, locals, m, IOSYMBOL("outputBuffer"));

	uint8_t *inputBytes = (uint8_t *)UArray_bytes(input);
	size_t inputSize = UArray_sizeInBytes(input);

	if (inputSize)
	{
		int ret;
		size_t oldOutputSize = UArray_size(output);
		size_t outputRoom = (inputSize * 2);
		uint8_t *outputBytes;

		UArray_setSize_(output, oldOutputSize + outputRoom);
		outputBytes = (uint8_t *)UArray_bytes(output) + oldOutputSize;

		strm->next_in   = inputBytes;
		strm->avail_in  = inputSize;

		strm->next_out  = outputBytes;
		strm->avail_out = outputRoom;

		ret = deflate(strm, Z_NO_FLUSH);
		//assert(ret != Z_STREAM_ERROR);
		{
		size_t outputSize = outputRoom - strm->avail_out;
		UArray_setSize_(output, oldOutputSize + outputSize);
		}

		UArray_setSize_(input, 0);
	}

	return self;
}
Пример #14
0
UArray *UArray_asBits(const UArray *self)
{
	UArray *out = UArray_new();
	size_t i, max = UArray_sizeInBytes(self);
	uint8_t *data = self->data;

	for (i = 0; i < max; i ++)
	{
		uint8_t b = data[i];
		int j;

		for (j = 0; j < 8; j ++)
		{
			int v = (b >> j) & 0x1;
			UArray_appendCString_(out, v ? "1" : "0");
		}
	}

	return out;
}
Пример #15
0
IoObject *IoLZOEncoder_process(IoLZOEncoder *self, IoObject *locals, IoMessage *m)
{
	/*doc LZOEncoder process
	Process the inputBuffer and appends the result to the outputBuffer.
	The processed inputBuffer is emptied except for the spare bytes at 
	the end which don't fit into a cipher block.
	*/
	
	lzo_align_t __LZO_MMODEL *wrkmem = DATA(self)->wrkmem;

	UArray *input  = IoObject_rawGetMutableUArraySlot(self, locals, m, IOSYMBOL("inputBuffer"));
	UArray *output = IoObject_rawGetMutableUArraySlot(self, locals, m, IOSYMBOL("outputBuffer"));

	unsigned char *inputBytes  = (uint8_t *)UArray_bytes(input);
	size_t inputSize           = UArray_sizeInBytes(input);

	if (inputSize)
	{
		int r;
		size_t oldOutputSize   = UArray_size(output);
		lzo_uint outputRoom    = (inputSize + inputSize / 64 + 16 + 3);
		unsigned char *outputBytes;

		UArray_setSize_(output, oldOutputSize + outputRoom);
		outputBytes = (uint8_t *)UArray_bytes(output) + oldOutputSize;

		r = lzo1x_1_compress(inputBytes, inputSize, outputBytes, &outputRoom, wrkmem);
		//	r = lzo1x_decompress(in, in_len, out, &out_len, wrkmem);

		if (r != LZO_E_OK)
		{
			IoState_error_(IOSTATE,  m, "LZO compression failed: %d", r);
		}


		UArray_setSize_(output, oldOutputSize + outputRoom);
		UArray_setSize_(input, 0);
	}

	return self;
}
Пример #16
0
IoObject *IoBlowfish_process(IoBlowfish *self, IoObject *locals, IoMessage *m)
{
	/*doc Blowfish process
	Process the inputBuffer and appends the result to the outputBuffer.
	The processed inputBuffer is empties except for the spare 
	bytes at the end which don't fit into a cipher block.
	*/
	blowfish_ctx *context = &(DATA(self)->context);
	int isEncrypting = DATA(self)->isEncrypting;

	UArray *input = IoObject_rawGetMutableUArraySlot(self, locals, m, IOSYMBOL("inputBuffer"));
	UArray *output = IoObject_rawGetMutableUArraySlot(self, locals, m, IOSYMBOL("outputBuffer"));

	const unsigned char *inputBytes  = (uint8_t *)UArray_bytes(input);
	size_t inputSize = UArray_sizeInBytes(input);

	unsigned long lr[2];
	size_t i, runs = inputSize / sizeof(lr);

	for (i = 0; i < runs; i ++)
	{
		memcpy(lr, inputBytes, sizeof(lr));

		inputBytes += sizeof(lr);

		if (isEncrypting)
		{
			blowfish_encrypt(context, &lr[0], &lr[1]);
		}
		else
		{
			blowfish_decrypt(context, &lr[0], &lr[1]);
		}

		UArray_appendBytes_size_(output, (unsigned char *)&lr, sizeof(lr));
	}

	UArray_removeRange(input, 0, runs * sizeof(lr));
	return self;
}
Пример #17
0
IoObject *IoTokyoCabinetPrefixCursor_jump(IoObject *self, IoObject *locals, IoMessage *m)
{
	/*doc TokyoCabinetPrefixCursor jump(key)
	Move cursor to record before key. Returns self
	*/
	
	IoSeq *key = IoMessage_locals_seqArgAt_(m, locals, 0);
	int result;
	UArray *p;
	
	IoSeq *prefix = IoObject_getSlot_(self, IOSYMBOL("prefix"));
	IOASSERT(ISSEQ(prefix), "prefix must be a sequence");

	p = UArray_clone(IoSeq_rawUArray(prefix));
	UArray_appendPath_(p, IoSeq_rawUArray(key));
	
	result = tcbdbcurjump(TokyoCabinetPrefixCursor(self), 
						  (const void *)UArray_bytes(p), 
						  (int)UArray_sizeInBytes(p));
	UArray_free(p);
	
	IOASSERT(TokyoCabinetPrefixCursor(self), "invalid TokyoCabinetPrefixCursor");
	return IOBOOL(self, result);
}
Пример #18
0
uint8_t UArray_byteAt_(UArray *self, size_t i)
{
	if (i < UArray_sizeInBytes(self)) return self->data[i];
	return 0;
}
Пример #19
0
Файл: UArray.c Проект: bomma/io
//inline 
size_t UArray_sizeRequiredToContain_(const UArray *self, const UArray *other)
{
	return (UArray_sizeInBytes(other)  + self->itemSize - 1) / self->itemSize;
}
Пример #20
0
IoSymbol *IoState_symbolWithUArray_copy_convertToFixedWidth(IoState *self, UArray *ba, int copy) 
{
	IoSymbol *r = IoState_symbolWithCString_length_(self, (const char *)UArray_bytes(ba), UArray_sizeInBytes(ba));
	if(!copy) UArray_free(ba);
	return r;
}
Пример #21
0
size_t IoSeq_rawSizeInBytes(IoSeq *self)
{
	return (size_t)(UArray_sizeInBytes(DATA(self)) );
}