2 * hbytes - hex-stringrep efficient byteblocks for Tcl
3 * Copyright 2006 Ian Jackson
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation; either version 2 of the
8 * License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
24 #define COMPLEX(hb) ((HBytes_ComplexValue*)hb->begin_complex)
25 #define SIMPLE_LEN(hb) ((Byte*)(hb)->end_0 - (Byte*)(hb)->begin_complex)
29 int cht_hb_len(const HBytes_Value *hb) {
30 if (HBYTES_ISEMPTY(hb)) return 0;
31 else if (HBYTES_ISCOMPLEX(hb)) return COMPLEX(hb)->len;
32 else return SIMPLE_LEN(hb);
35 Byte *cht_hb_data(const HBytes_Value *hb) {
36 if (HBYTES_ISEMPTY(hb)) return 0;
37 else if (HBYTES_ISCOMPLEX(hb)) return COMPLEX(hb)->dstart;
38 else return hb->begin_complex;
41 int cht_hb_issentinel(const HBytes_Value *hb) {
42 return HBYTES_ISSENTINEL(hb);
47 void cht_hb_empty(HBytes_Value *returns) {
48 returns->begin_complex= returns->end_0= 0;
51 void cht_hb_sentinel(HBytes_Value *returns) {
52 returns->begin_complex= 0;
53 returns->end_0= (void*)&cht_hbytes_type;
56 Byte *cht_hb_arrayspace(HBytes_Value *returns, int l) {
57 if (!l) { cht_hb_empty(returns); return 0; }
58 returns->begin_complex= TALLOC(l);
59 returns->end_0= returns->begin_complex + l;
60 return returns->begin_complex;
63 void cht_hb_array(HBytes_Value *returns, const Byte *array, int l) {
64 memcpy(cht_hb_arrayspace(returns,l), array, l);
69 void cht_hb_free(const HBytes_Value *frees) {
70 if (HBYTES_ISCOMPLEX(frees)) {
71 HBytes_ComplexValue *cx= COMPLEX(frees);
72 TFREE(cx->dstart - cx->prespace);
74 TFREE(frees->begin_complex);
79 static HBytes_ComplexValue *complex(HBytes_Value *hb) {
80 HBytes_ComplexValue *cx;
82 if (HBYTES_ISCOMPLEX(hb)) return hb->begin_complex;
84 cx= TALLOC(sizeof(*cx));
85 cx->dstart= hb->begin_complex;
86 cx->len= cx->avail= SIMPLE_LEN(hb);
89 hb->begin_complex= cx;
95 Byte *cht_hb_prepend(HBytes_Value *hb, int el) {
96 HBytes_ComplexValue *cx;
98 Byte *old_block, *new_block, *new_dstart;
102 if (cx->prespace < el) {
103 new_prespace= el*2 + cx->len;
104 old_block= cx->dstart - cx->prespace;
105 new_block= Tcl_Realloc(old_block, new_prespace + cx->avail);
106 new_dstart= new_block + new_prespace;
107 memmove(new_dstart, new_block + cx->prespace, cx->len);
108 cx->prespace= new_prespace;
109 cx->dstart= new_dstart;
118 Byte *cht_hb_append(HBytes_Value *hb, int el) {
119 HBytes_ComplexValue *cx;
120 int new_len, new_avail;
121 Byte *newpart, *new_block, *old_block;
125 new_len= cx->len + el;
126 if (new_len > cx->avail) {
127 new_avail= new_len*2;
128 old_block= cx->dstart - cx->prespace;
129 new_block= Tcl_Realloc(old_block, cx->prespace + new_avail);
130 cx->dstart= new_block + cx->prespace;
131 cx->avail= new_avail;
133 newpart= cx->dstart + cx->len;
138 static HBytes_ComplexValue*
139 prechop(HBytes_Value *hb, int cl, const Byte **rv) {
140 HBytes_ComplexValue *cx;
142 if (cl<0) { *rv=0; return 0; }
143 if (cl==0) { *rv= (const void*)&cht_hbytes_type; return 0; }
146 if (cl > cx->len) { *rv=0; return 0; }
150 const Byte *cht_hb_unprepend(HBytes_Value *hb, int pl) {
152 HBytes_ComplexValue *cx= prechop(hb,pl,&chopped);
153 if (!cx) return chopped;
163 const Byte *cht_hb_unappend(HBytes_Value *hb, int sl) {
165 HBytes_ComplexValue *cx= prechop(hb,sl,&chopped);
166 if (!cx) return chopped;
169 return cx->dstart + cx->len;
172 void memxor(Byte *dest, const Byte *src, int l) {
173 while (l--) *dest++ ^= *src++;