chiark-tcl-1.1.1+nmu1/ 0000755 0000000 0000000 00000000000 12223237473 011302 5 ustar chiark-tcl-1.1.1+nmu1/hbytes/ 0000755 0000000 0000000 00000000000 12223237522 012573 5 ustar chiark-tcl-1.1.1+nmu1/hbytes/hbytes.c 0000644 0000000 0000000 00000010663 11762372314 014251 0 ustar /*
* hbytes - hex-stringrep efficient byteblocks for Tcl
* Copyright 2006-2012 Ian Jackson
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of the
* License, or (at your option) any later version.
*
* This program 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
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this library; if not, see .
*/
#include "hbytes.h"
#define COMPLEX(hb) ((HBytes_ComplexValue*)hb->begin_complex)
#define SIMPLE_LEN(hb) ((Byte*)(hb)->end_0 - (Byte*)(hb)->begin_complex)
/* enquirers */
int cht_hb_len(const HBytes_Value *hb) {
if (HBYTES_ISEMPTY(hb)) return 0;
else if (HBYTES_ISCOMPLEX(hb)) return COMPLEX(hb)->len;
else return SIMPLE_LEN(hb);
}
Byte *cht_hb_data(const HBytes_Value *hb) {
if (HBYTES_ISEMPTY(hb)) return 0;
else if (HBYTES_ISCOMPLEX(hb)) return COMPLEX(hb)->dstart;
else return hb->begin_complex;
}
int cht_hb_issentinel(const HBytes_Value *hb) {
return HBYTES_ISSENTINEL(hb);
}
/* constructors */
void cht_hb_empty(HBytes_Value *returns) {
returns->begin_complex= returns->end_0= 0;
}
void cht_hb_sentinel(HBytes_Value *returns) {
returns->begin_complex= 0;
returns->end_0= (void*)&cht_hbytes_type;
}
Byte *cht_hb_arrayspace(HBytes_Value *returns, int l) {
if (!l) { cht_hb_empty(returns); return 0; }
returns->begin_complex= TALLOC(l);
returns->end_0= returns->begin_complex + l;
return returns->begin_complex;
}
void cht_hb_array(HBytes_Value *returns, const Byte *array, int l) {
memcpy(cht_hb_arrayspace(returns,l), array, l);
}
/* destructor */
void cht_hb_free(const HBytes_Value *frees) {
if (HBYTES_ISCOMPLEX(frees)) {
HBytes_ComplexValue *cx= COMPLEX(frees);
TFREE(cx->dstart - cx->prespace);
}
TFREE(frees->begin_complex);
}
/* mutators */
static HBytes_ComplexValue *complex(HBytes_Value *hb) {
HBytes_ComplexValue *cx;
if (HBYTES_ISCOMPLEX(hb)) return hb->begin_complex;
cx= TALLOC(sizeof(*cx));
cx->dstart= hb->begin_complex;
cx->len= cx->avail= SIMPLE_LEN(hb);
cx->prespace= 0;
hb->begin_complex= cx;
hb->end_0= 0;
return cx;
}
Byte *cht_hb_prepend(HBytes_Value *hb, int el) {
HBytes_ComplexValue *cx;
int new_prespace;
Byte *old_block, *new_block, *new_dstart;
cx= complex(hb);
assert(el < INT_MAX/4 && cx->len < INT_MAX/2);
if (cx->prespace < el) {
new_prespace= el*2 + cx->len;
old_block= cx->dstart - cx->prespace;
new_block= Tcl_Realloc(old_block, new_prespace + cx->avail);
new_dstart= new_block + new_prespace;
memmove(new_dstart, new_block + cx->prespace, cx->len);
cx->prespace= new_prespace;
cx->dstart= new_dstart;
}
cx->dstart -= el;
cx->prespace -= el;
cx->len += el;
cx->avail += el;
return cx->dstart;
}
Byte *cht_hb_append(HBytes_Value *hb, int el) {
HBytes_ComplexValue *cx;
int new_len, new_avail;
Byte *newpart, *new_block, *old_block;
cx= complex(hb);
assert(el < INT_MAX/4 && cx->len < INT_MAX/4);
new_len= cx->len + el;
if (new_len > cx->avail) {
new_avail= new_len*2;
old_block= cx->dstart - cx->prespace;
new_block= Tcl_Realloc(old_block, cx->prespace + new_avail);
cx->dstart= new_block + cx->prespace;
cx->avail= new_avail;
}
newpart= cx->dstart + cx->len;
cx->len= new_len;
return newpart;
}
static HBytes_ComplexValue*
prechop(HBytes_Value *hb, int cl, const Byte **rv) {
HBytes_ComplexValue *cx;
if (cl<0) { *rv=0; return 0; }
if (cl==0) { *rv= (const void*)&cht_hbytes_type; return 0; }
cx= complex(hb);
if (cl > cx->len) { *rv=0; return 0; }
return cx;
}
const Byte *cht_hb_unprepend(HBytes_Value *hb, int pl) {
const Byte *chopped;
HBytes_ComplexValue *cx= prechop(hb,pl,&chopped);
if (!cx) return chopped;
chopped= cx->dstart;
cx->dstart += pl;
cx->prespace += pl;
cx->len -= pl;
cx->avail -= pl;
return chopped;
}
const Byte *cht_hb_unappend(HBytes_Value *hb, int sl) {
const Byte *chopped;
HBytes_ComplexValue *cx= prechop(hb,sl,&chopped);
if (!cx) return chopped;
cx->len -= sl;
return cx->dstart + cx->len;
}
void memxor(Byte *dest, const Byte *src, int l) {
while (l--) *dest++ ^= *src++;
}
chiark-tcl-1.1.1+nmu1/hbytes/hook.c 0000644 0000000 0000000 00000017543 11762403261 013713 0 ustar /*
* hbytes - hex-stringrep efficient byteblocks for Tcl
* Copyright 2006-2012 Ian Jackson
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of the
* License, or (at your option) any later version.
*
* This program 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
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this library; if not, see .
*/
#include
#include "chiark_tcl_hbytes.h"
int cht_do_hbytes_rep_info(ClientData cd, Tcl_Interp *ip,
Tcl_Obj *obj, Tcl_Obj **result) {
const char *tn;
int nums[3], i, lnl;
Tcl_Obj *objl[4];
if (obj->typePtr == &cht_hbytes_type) {
HBytes_Value *v= OBJ_HBYTES(obj);
memset(nums,0,sizeof(nums));
nums[1]= cht_hb_len(v);
if (HBYTES_ISEMPTY(v)) tn= "empty";
else if (HBYTES_ISSENTINEL(v)) tn= "sentinel!";
else if (HBYTES_ISSIMPLE(v)) tn= "simple";
else {
HBytes_ComplexValue *cx= v->begin_complex;
tn= "complex";
nums[0]= cx->prespace;
nums[2]= cx->avail - cx->len;
}
lnl= 3;
} else {
tn= "other";
lnl= 0;
}
objl[0]= Tcl_NewStringObj((char*)tn,-1);
for (i=0; itypePtr= &cht_hbytes_type;
}
static void hbytes_t_free(Tcl_Obj *o) {
cht_hb_free(OBJ_HBYTES(o));
}
void cht_obj_updatestr_array_prefix(Tcl_Obj *o, const Byte *byte,
int l, const char *prefix) {
char *str;
int pl;
pl= strlen(prefix);
assert(l < INT_MAX/2 - 1 - pl);
o->length= l*2+pl;
str= o->bytes= TALLOC(o->length+1);
memcpy(str,prefix,pl);
str += pl;
while (l>0) {
sprintf(str,"%02x",*byte);
str+=2; byte++; l--;
}
*str= 0;
}
void cht_obj_updatestr_array(Tcl_Obj *o, const Byte *byte, int l) {
cht_obj_updatestr_array_prefix(o,byte,l,"");
}
static void hbytes_t_ustr(Tcl_Obj *o) {
cht_obj_updatestr_array(o,
cht_hb_data(OBJ_HBYTES(o)),
cht_hb_len(OBJ_HBYTES(o)));
}
static int hbytes_t_sfa(Tcl_Interp *ip, Tcl_Obj *o) {
char *str, *ep;
Byte *bytes;
int l;
char cbuf[3];
if (o->typePtr == &cht_ulong_type) {
uint32_t ul;
ul= htonl(*(const uint32_t*)&o->internalRep.longValue);
cht_hb_array(OBJ_HBYTES(o), (const Byte*)&ul, 4);
} else {
str= Tcl_GetStringFromObj(o,&l); assert(str);
cht_objfreeir(o);
if (l & 1) return cht_staticerr(ip, "hbytes: conversion from hex:"
" odd length in hex", "HBYTES SYNTAX");
bytes= cht_hb_arrayspace(OBJ_HBYTES(o), l/2);
cbuf[2]= 0;
while (l>0) {
cbuf[0]= *str++;
cbuf[1]= *str++;
*bytes++= strtoul(cbuf,&ep,16);
if (ep != cbuf+2) {
cht_hb_free(OBJ_HBYTES(o));
return cht_staticerr(ip, "hbytes: conversion from hex:"
" bad hex digit", "HBYTES SYNTAX");
}
l -= 2;
}
}
o->typePtr = &cht_hbytes_type;
return TCL_OK;
}
Tcl_ObjType cht_hbytes_type = {
"hbytes",
hbytes_t_free, hbytes_t_dup, hbytes_t_ustr, hbytes_t_sfa
};
int cht_do_hbytes_raw2h(ClientData cd, Tcl_Interp *ip,
Tcl_Obj *binary, HBytes_Value *result) {
const unsigned char *str;
int l;
str= Tcl_GetByteArrayFromObj(binary,&l);
cht_hb_array(result, str, l);
return TCL_OK;
}
int cht_do_hbytes_h2raw(ClientData cd, Tcl_Interp *ip,
HBytes_Value hex, Tcl_Obj **result) {
*result= Tcl_NewByteArrayObj(cht_hb_data(&hex), cht_hb_len(&hex));
return TCL_OK;
}
int cht_do_hbytes_length(ClientData cd, Tcl_Interp *ip,
HBytes_Value v, int *result) {
*result= cht_hb_len(&v);
return TCL_OK;
}
int cht_do_hbytes_random(ClientData cd, Tcl_Interp *ip,
int length, HBytes_Value *result) {
Byte *space;
int rc;
space= cht_hb_arrayspace(result, length);
rc= cht_get_urandom(ip, space, length);
if (rc) { cht_hb_free(result); return rc; }
return TCL_OK;
}
int cht_do_hbytes_overwrite(ClientData cd, Tcl_Interp *ip,
HBytes_Var v, int start, HBytes_Value sub) {
int sub_l;
sub_l= cht_hb_len(&sub);
if (start < 0)
return cht_staticerr(ip, "hbytes overwrite start -ve",
"HBYTES LENGTH RANGE");
if (start + sub_l > cht_hb_len(v.hb))
return cht_staticerr(ip, "hbytes overwrite out of range",
"HBYTES LENGTH UNDERRUN");
memcpy(cht_hb_data(v.hb) + start, cht_hb_data(&sub), sub_l);
return TCL_OK;
}
int cht_do_hbytes_trimleft(ClientData cd, Tcl_Interp *ip, HBytes_Var v) {
const Byte *o, *p, *e;
o= p= cht_hb_data(v.hb);
e= p + cht_hb_len(v.hb);
while (p INT_MAX/sub_l) return cht_staticerr(ip, "hbytes repeat too long", 0);
data= cht_hb_arrayspace(result, sub_l*count);
sub_d= cht_hb_data(&sub);
while (count) {
memcpy(data, sub_d, sub_l);
count--; data += sub_l;
}
return TCL_OK;
}
int cht_do_hbytes_xor(ClientData cd, Tcl_Interp *ip,
HBytes_Var v, HBytes_Value d) {
int l;
Byte *dest;
const Byte *source;
l= cht_hb_len(v.hb);
if (cht_hb_len(&d) != l) return
cht_staticerr(ip, "hbytes xor lengths do not match", "HBYTES LENGTH MISMATCH");
dest= cht_hb_data(v.hb);
source= cht_hb_data(&d);
memxor(dest,source,l);
return TCL_OK;
}
int cht_do_hbytes_zeroes(ClientData cd, Tcl_Interp *ip,
int length, HBytes_Value *result) {
Byte *space;
space= cht_hb_arrayspace(result, length);
memset(space,0,length);
return TCL_OK;
}
int cht_do_hbytes_compare(ClientData cd, Tcl_Interp *ip,
HBytes_Value a, HBytes_Value b, int *result) {
int al, bl, minl, r;
al= cht_hb_len(&a);
bl= cht_hb_len(&b);
minl= al0) *result= +2;
else {
if (albl) *result= +1;
else *result= 0;
}
return TCL_OK;
}
int cht_do_hbytes_range(ClientData cd, Tcl_Interp *ip,
HBytes_Value v, int start, int size,
HBytes_Value *result) {
const Byte *data;
int l;
l= cht_hb_len(&v);
if (start<0 || size<0)
return cht_staticerr(ip,"hbytes range subscript(s) -ve","HBYTES LENGTH RANGE");
if (l2)
return cht_staticerr(ip, "hbytes h2ushort input more than 4 hex digits",
"HBYTES VALUE OVERFLOW");
data= cht_hb_data(&hex);
*result= data[l-1] | (l>1 ? data[0]<<8 : 0);
return TCL_OK;
}
int cht_do_hbytes_ushort2h(ClientData cd, Tcl_Interp *ip,
long input, HBytes_Value *result) {
uint16_t us;
if (input > 0x0ffff)
return cht_staticerr(ip, "hbytes ushort2h input >2^16",
"HBYTES VALUE OVERFLOW");
us= htons(input);
cht_hb_array(result,(const Byte*)&us,2);
return TCL_OK;
}
/* toplevel functions */
CHT_INIT(hbytes,
CHTI_TYPE(cht_hbytes_type) CHTI_TYPE(cht_ulong_type),
CHTI_COMMANDS(cht_hbytestoplevel_entries))
chiark-tcl-1.1.1+nmu1/hbytes/chop.c 0000644 0000000 0000000 00000005764 11762372314 013712 0 ustar /*
* hbytes - hex-stringrep efficient byteblocks for Tcl
* Copyright 2006-2012 Ian Jackson
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of the
* License, or (at your option) any later version.
*
* This program 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
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this library; if not, see .
*/
#include "chiark_tcl_hbytes.h"
static int strs1(Tcl_Interp *ip, int strc, Tcl_Obj *const *strv, int *l_r) {
int rc, l, i, pl;
l= 0;
for (i=1; i