X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ian/git?p=chiark-tcl.git;a=blobdiff_plain;f=hbytes%2Fhbytes.c;h=15980dcd6024e7518c8f60a7c1ba72bfb295f3d3;hp=4bbf8f67663b54d904512f7f98c4c1921f006829;hb=ceed4cf646a34245b3bc88089a2187ebf7a41f0f;hpb=2cf1bfc63e73a424f9f3899c204025a8346b38e0 diff --git a/hbytes/hbytes.c b/hbytes/hbytes.c index 4bbf8f6..15980dc 100644 --- a/hbytes/hbytes.c +++ b/hbytes/hbytes.c @@ -1,59 +1,72 @@ /* + * hbytes - hex-stringrep efficient byteblocks for Tcl + * Copyright 2006 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, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA + * 02110-1301, USA. */ -#include #include "hbytes.h" -#include "tables.h" -#include "hbintern.h" #define COMPLEX(hb) ((HBytes_ComplexValue*)hb->begin_complex) #define SIMPLE_LEN(hb) ((Byte*)(hb)->end_0 - (Byte*)(hb)->begin_complex) /* enquirers */ -int hbytes_len(const HBytes_Value *hb) { +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 *hbytes_data(const HBytes_Value *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 hbytes_issentinel(const HBytes_Value *hb) { - return HBYTES_ISCOMPLEX(hb); +int cht_hb_issentinel(const HBytes_Value *hb) { + return HBYTES_ISSENTINEL(hb); } /* constructors */ -void hbytes_empty(HBytes_Value *returns) { +void cht_hb_empty(HBytes_Value *returns) { returns->begin_complex= returns->end_0= 0; } -void hbytes_sentinel(HBytes_Value *returns) { +void cht_hb_sentinel(HBytes_Value *returns) { returns->begin_complex= 0; - returns->end_0= (void*)&hbytes_type; + returns->end_0= (void*)&cht_hbytes_type; } -Byte *hbytes_arrayspace(HBytes_Value *returns, int l) { - if (!l) { hbytes_empty(returns); return 0; } +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 hbytes_array(HBytes_Value *returns, const Byte *array, int l) { - memcpy(hbytes_arrayspace(returns,l), array, l); +void cht_hb_array(HBytes_Value *returns, const Byte *array, int l) { + memcpy(cht_hb_arrayspace(returns,l), array, l); } /* destructor */ -void hbytes_free(HBytes_Value *frees) { +void cht_hb_free(const HBytes_Value *frees) { if (HBYTES_ISCOMPLEX(frees)) { HBytes_ComplexValue *cx= COMPLEX(frees); TFREE(cx->dstart - cx->prespace); @@ -79,12 +92,14 @@ static HBytes_ComplexValue *complex(HBytes_Value *hb) { return cx; } -Byte *hbytes_prepend(HBytes_Value *hb, int el) { +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; @@ -102,12 +117,13 @@ Byte *hbytes_prepend(HBytes_Value *hb, int el) { return cx->dstart; } -Byte *hbytes_append(HBytes_Value *hb, int el) { +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) { @@ -122,18 +138,19 @@ Byte *hbytes_append(HBytes_Value *hb, int el) { return newpart; } -HBytes_ComplexValue *prechop(HBytes_Value *hb, int cl, const Byte **rv) { +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*)&hbytes_type; 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 *hbytes_unprepend(HBytes_Value *hb, int pl) { +const Byte *cht_hb_unprepend(HBytes_Value *hb, int pl) { const Byte *chopped; HBytes_ComplexValue *cx= prechop(hb,pl,&chopped); if (!cx) return chopped; @@ -146,7 +163,7 @@ const Byte *hbytes_unprepend(HBytes_Value *hb, int pl) { return chopped; } -const Byte *hbytes_unappend(HBytes_Value *hb, int sl) { +const Byte *cht_hb_unappend(HBytes_Value *hb, int sl) { const Byte *chopped; HBytes_ComplexValue *cx= prechop(hb,sl,&chopped); if (!cx) return chopped; @@ -154,3 +171,7 @@ const Byte *hbytes_unappend(HBytes_Value *hb, int sl) { cx->len -= sl; return cx->dstart + cx->len; } + +void memxor(Byte *dest, const Byte *src, int l) { + while (l--) *dest++ ^= *src++; +}