File: hbytes.c

package info (click to toggle)
chiark-tcl 1.3.7
  • links: PTS
  • area: main
  • in suites: forky, sid, trixie
  • size: 592 kB
  • sloc: ansic: 4,542; perl: 415; makefile: 129; tcl: 106; sh: 38
file content (175 lines) | stat: -rw-r--r-- 4,531 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/*
 * 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 <http://www.gnu.org/licenses/>.
 */


#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++;
}