chiark / gitweb /
5daecd1c5038437357fb896f1bd9d4e4b9044005
[chiark-tcl.git] / hbytes / hbytes.c
1 /*
2  * hbytes - hex-stringrep efficient byteblocks for Tcl
3  * Copyright 2006 Ian Jackson
4  *
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.
9  *
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.
14  *
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
18  * 02110-1301, USA.
19  */
20
21
22 #include "hbytes.h"
23
24 #define COMPLEX(hb) ((HBytes_ComplexValue*)hb->begin_complex)
25 #define SIMPLE_LEN(hb) ((Byte*)(hb)->end_0 - (Byte*)(hb)->begin_complex)
26
27 /* enquirers */
28
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);
33 }
34
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;
39 }
40
41 int cht_hb_issentinel(const HBytes_Value *hb) {
42   return HBYTES_ISSENTINEL(hb);
43 }
44
45 /* constructors */
46
47 void cht_hb_empty(HBytes_Value *returns) {
48   returns->begin_complex= returns->end_0= 0;
49 }
50
51 void cht_hb_sentinel(HBytes_Value *returns) {
52   returns->begin_complex= 0;
53   returns->end_0= (void*)&cht_hbytes_type;
54 }
55
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;
61 }
62   
63 void cht_hb_array(HBytes_Value *returns, const Byte *array, int l) {
64   memcpy(cht_hb_arrayspace(returns,l), array, l);
65 }
66
67 /* destructor */
68
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);
73   }
74   TFREE(frees->begin_complex);
75 }
76
77 /* mutators */
78
79 static HBytes_ComplexValue *complex(HBytes_Value *hb) {
80   HBytes_ComplexValue *cx;
81
82   if (HBYTES_ISCOMPLEX(hb)) return hb->begin_complex;
83
84   cx= TALLOC(sizeof(*cx));
85   cx->dstart= hb->begin_complex;
86   cx->len= cx->avail= SIMPLE_LEN(hb);
87   cx->prespace= 0;
88
89   hb->begin_complex= cx;
90   hb->end_0= 0;
91
92   return cx;
93 }
94
95 Byte *cht_hb_prepend(HBytes_Value *hb, int el) {
96   HBytes_ComplexValue *cx;
97   int new_prespace;
98   Byte *old_block, *new_block, *new_dstart;
99
100   cx= complex(hb);
101   
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;
110   }
111   cx->dstart -= el;
112   cx->prespace -= el;
113   cx->len += el;
114   cx->avail += el;
115   return cx->dstart;
116 }
117
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;
122
123   cx= complex(hb);
124
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;
132   }
133   newpart= cx->dstart + cx->len;
134   cx->len= new_len;
135   return newpart;
136 }
137
138 static HBytes_ComplexValue*
139 prechop(HBytes_Value *hb, int cl, const Byte **rv) {
140   HBytes_ComplexValue *cx;
141
142   if (cl<0) { *rv=0; return 0; }
143   if (cl==0) { *rv= (const void*)&cht_hbytes_type; return 0; }
144   
145   cx= complex(hb);
146   if (cl > cx->len) { *rv=0; return 0; }
147   return cx;
148 }
149
150 const Byte *cht_hb_unprepend(HBytes_Value *hb, int pl) {
151   const Byte *chopped;
152   HBytes_ComplexValue *cx= prechop(hb,pl,&chopped);
153   if (!cx) return chopped;
154
155   chopped= cx->dstart;
156   cx->dstart += pl;
157   cx->prespace += pl;
158   cx->len -= pl;
159   cx->avail -= pl;
160   return chopped;
161 }
162
163 const Byte *cht_hb_unappend(HBytes_Value *hb, int sl) {
164   const Byte *chopped;
165   HBytes_ComplexValue *cx= prechop(hb,sl,&chopped);
166   if (!cx) return chopped;
167
168   cx->len -= sl;
169   return cx->dstart + cx->len;
170 }
171
172 void memxor(Byte *dest, const Byte *src, int l) {
173   while (l--) *dest++ ^= *src++;
174 }