Archive-Lha
view release on metacpan or search on metacpan
lib/Archive/Lha.xs view on Meta::CPAN
/*
XS for Archive::Lha
This XS is, though largely modified, based on LHa for UNIX.
See lib/Archive/Lha.pm for Authors/Copyright/License information.
*/
#include "EXTERN.h"
#include "perl.h"
#include "XSUB.h"
#include "ppport.h"
#include "Lha.h"
#include <time.h>
/*
these are not from LHa for UNIX
*/
void
destroy_stash(LhaStash * stash)
{
Safefree(stash->tree->left);
Safefree(stash->tree->right);
Safefree(stash->tree);
Safefree(stash->pt->table);
Safefree(stash->pt->length);
Safefree(stash->pt);
Safefree(stash->c->table);
Safefree(stash->c->length);
Safefree(stash->c);
Safefree(stash->bit);
Safefree(stash->queue);
Safefree(stash);
}
void
safe_croak(LhaStash * stash, char * dying_message)
{
destroy_stash(stash);
croak("%s", dying_message);
}
void
output(LhaStash * stash, unsigned char * queue, int len)
{
dSP;
ENTER;
SAVETMPS;
PUSHMARK(SP);
XPUSHs(sv_2mortal(newSVpv(queue, len)));
PUTBACK;
call_sv(stash->write, G_VOID);
SPAGAIN;
PUTBACK;
FREETMPS;
LEAVE;
}
void
input(LhaStash * stash, int len)
{
int n;
SV *sv;
STRLEN got;
const char *ptr;
dSP;
ENTER;
SAVETMPS;
PUSHMARK(SP);
XPUSHs(sv_2mortal(newSViv(len)));
PUTBACK;
n = call_sv(stash->read, G_SCALAR);
SPAGAIN;
if (n != 1)
safe_croak(stash, "There's something wrong in 'read' callback");
sv = POPs;
ptr = SvPVbyte(sv, got);
Copy(ptr, stash->bit->readbuf, (STRLEN)len <= got ? (STRLEN)len : got, unsigned char);
PUTBACK;
FREETMPS;
LEAVE;
}
/*
modified from LHa for UNIX: bitio.c ver 1.14
original authors:
Source All chagned 1995.01.14 N.Watazaki
Separated from crcio.c 2002.10.26 Koji Arai
*/
unsigned short
shiftbits(LhaBitstream * bit, unsigned char n)
{
return (bit->value << n)
+ (bit->buf >> (char_bit - n));
}
void
fillbuf(LhaStash * stash, unsigned char n)
{
unsigned short len;
LhaBitstream * bit = stash->bit;
while (n > bit->pos) {
n -= bit->pos;
bit->value = shiftbits(bit, bit->pos);
if (stash->encoded_size > 0) {
if (bit->readpos == 0) {
if (stash->encoded_size > readbuf_size)
len = readbuf_size;
else
len = stash->encoded_size;
input(stash, len);
}
bit->buf = bit->readbuf[bit->readpos++];
if (bit->readpos == readbuf_size)
bit->readpos = 0;
stash->encoded_size--;
}
else
bit->buf = 0;
bit->pos = char_bit;
}
bit->pos -= n;
bit->value = shiftbits(bit, n);
bit->buf <<= n;
}
unsigned short
peekbits(LhaStash * stash, unsigned char n)
{
return (stash->bit->value >> (ushort_bit - n));
}
unsigned short
getbits(LhaStash * stash, unsigned char n)
{
unsigned short bits = peekbits(stash, n);
fillbuf(stash, n);
return bits;
lib/Archive/Lha.xs view on Meta::CPAN
if ( !hash_exists(self, "read") )
croak("'read' callback is missing");
if ( !hash_exists(self, "write") )
croak("'write' callback is missing");
dicsize = self_uint("DICSIZE");
dicsize1 = dicsize - 1;
Newxz(queue, dicsize, unsigned char);
Newxz(stash, 1, LhaStash);
stash->queue = queue;
stash->read = self_sv("read");
stash->write = self_sv("write");
stash->original_size = self_uint("original_size");
stash->encoded_size = self_uint("encoded_size");
init_tables(self, stash);
init_bitstream(stash);
adjust = (1u << uchar_bit) - self_uchar("THRESHOLD");
crc16 = 0;
loc = 0;
total = 0;
while ( total < stash->original_size ) {
c = decode_c(stash);
if (c <= uchar_max) {
queue[loc++] = c;
if (loc == dicsize) {
output(stash, queue, dicsize);
crc16 = calc_crc16(crc16, queue, dicsize);
loc = 0;
}
total++;
}
else {
matchlen = c - adjust;
matchoff = decode_p(stash) + 1;
matchpos = (loc - matchoff) & dicsize1;
total += matchlen;
for(i = 0; i < matchlen; i++) {
queue[loc++] = queue[(matchpos + i) & dicsize1];
if (loc == dicsize) {
output(stash, queue, dicsize);
crc16 = calc_crc16(crc16, queue, dicsize);
loc = 0;
}
}
}
}
if (loc) {
output(stash, queue, loc);
crc16 = calc_crc16(crc16, queue, loc);
}
destroy_stash(stash);
RETVAL = crc16;
OUTPUT:
RETVAL
MODULE = Archive::Lha PACKAGE = Archive::Lha::CRC PREFIX = xs_
PROTOTYPES: DISABLE
#/* this is not from LHa for UNIX */
unsigned short
xs_update(unsigned short crc, SV * str, STRLEN len)
CODE:
RETVAL = calc_crc16(crc, SvPV(str, len), len);
OUTPUT:
RETVAL
MODULE = Archive::Lha PACKAGE = Archive::Lha::Header::Utils PREFIX = xs_
PROTOTYPES: DISABLE
unsigned char
xs_checksum(SV * buf, STRLEN offset)
CODE:
STRLEN len;
unsigned char * s = (unsigned char *) SvPV(buf, len);
unsigned char sum = 0;
STRLEN i;
for (i = offset; i < len; i++)
sum += s[i];
RETVAL = sum;
OUTPUT:
RETVAL
IV
xs_dostime2utime(U32 v)
CODE:
struct tm t;
time_t result;
if (v == 0) {
RETVAL = 0;
} else {
t.tm_sec = (v & 0x1F) * 2;
t.tm_min = (v >> 5) & 0x3F;
t.tm_hour = (v >> 11) & 0x1F;
t.tm_mday = (v >> 16) & 0x1F;
t.tm_mon = ((v >> 21) & 0x0F) - 1;
t.tm_year = ((v >> 25) & 0x7F) + 80;
t.tm_isdst = -1;
result = mktime(&t);
RETVAL = (result == (time_t)-1) ? 0 : (IV)result;
}
OUTPUT:
RETVAL
MODULE = Archive::Lha PACKAGE = Archive::Lha PREFIX = xs_
PROTOTYPES: DISABLE
( run in 1.406 second using v1.01-cache-2.11-cpan-e1769b4cff6 )