Warning: Attempt to read property "date" on null in /usr/local/www/websvn.planix.org/blame.php on line 247

Warning: Attempt to read property "msg" on null in /usr/local/www/websvn.planix.org/blame.php on line 247
WebSVN – tendra.SVN – Blame – //branches/tendra4/src/tools/pl/streams.c – Rev 2

Subversion Repositories tendra.SVN

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
2 7u83 1
/*
2
    		 Crown Copyright (c) 1997
3
 
4
    This TenDRA(r) Computer Program is subject to Copyright
5
    owned by the United Kingdom Secretary of State for Defence
6
    acting through the Defence Evaluation and Research Agency
7
    (DERA).  It is made available to Recipients with a
8
    royalty-free licence for its use, reproduction, transfer
9
    to other parties and amendment for any purpose not excluding
10
    product development provided that any such use et cetera
11
    shall be deemed to be acceptance of the following conditions:-
12
 
13
        (1) Its Recipients shall ensure that this Notice is
14
        reproduced upon any copies or amended versions of it;
15
 
16
        (2) Any amended version of it shall be clearly marked to
17
        show both the nature of and the organisation responsible
18
        for the relevant amendment or amendments;
19
 
20
        (3) Its onward transfer from a recipient to another
21
        party shall be deemed to be that party's acceptance of
22
        these conditions;
23
 
24
        (4) DERA gives no warranty or assurance as to its
25
        quality or suitability for any purpose and DERA accepts
26
        no liability whatsoever in relation to any use to which
27
        it may be put.
28
*/
29
 
30
 
31
#include "config.h"
32
#include "util.h"
33
#include "defs.h"
34
#include "streams.h"
35
#include "enc_nos.h"
36
#include "errors.h"
37
 
38
 
39
TDF * current_TDF;
40
 
41
static Chunk * free_chunks = (Chunk*)0;
42
 
43
 
44
Chunk * create_chunk
45
    PROTO_Z ()
46
{
47
    Chunk * ans;
48
    if (free_chunks != (Chunk*)0) {
49
	ans = free_chunks;
50
	free_chunks = free_chunks->next;
51
    } else {
52
	ans = MALLOC(Chunk);
53
    }
54
    ans->next = (Chunk*)0;
55
    ans->usage = 0;
56
    ans->offst = 0;  ans->aligned = 0;
57
    ans->data[0] = 0;
58
    return ans;
59
}
60
 
61
 
62
static void free_chunk
63
    PROTO_N ( (x) )
64
    PROTO_T ( Chunk * x )
65
{
66
    x->next = free_chunks;
67
    free_chunks = x;
68
}
69
 
70
 
71
void out_basic_int
72
    PROTO_N ( (num, bts) )
73
    PROTO_T ( unsigned long num X unsigned int bts )
74
    /* outputs num onto current stream in bts bits */
75
{
76
    Chunk * t_ch = current_TDF->last;
77
    unsigned disp = t_ch->offst;
78
    unsigned space = 8 - disp;
79
    for(;bts>=space;bts-=space,space=8,disp=0){
80
	(t_ch->data)[t_ch->usage]|=UC((num>>(bts-space))&255);
81
	t_ch->usage+=1;t_ch->offst=0;
82
	if(t_ch->usage == DATA_SIZE) {
83
	    Chunk * nch = create_chunk();
84
	    t_ch->next = nch;
85
	    current_TDF->last = nch;
86
	    t_ch = nch;
87
	}
88
	else (t_ch->data)[t_ch->usage] = 0;
89
    }
90
    if(bts){
91
	unsigned garbage=8-bts;
92
	(t_ch->data)[t_ch->usage]|=UC(((num<<garbage)&255)>>disp);
93
	t_ch->offst+=UC(bts);
94
    }
95
}
96
 
97
 
98
void append_TDF
99
    PROTO_N ( (tdf, free_it) )
100
    PROTO_T ( TDF * tdf X Bool free_it )
101
    /* appends the stream tdf onto current stream;
102
       if free_it, chunks in tdf will be reused */
103
{
104
    Chunk * t_ch = current_TDF->last;
105
    Chunk * a_ch = tdf->first;
106
 
107
    if (t_ch->usage < DATA_SIZE -1 - a_ch->usage ) {
108
	/* room to put it in current chunk */
109
	int i;
110
	int offst = a_ch->offst;
111
	Chunk * nextch = a_ch->next;
112
	for(i=0; i<a_ch->usage; i++) {
113
	    out_basic_int(UL(a_ch->data[i]),UI(8));
114
	}
115
	if (a_ch ->offst != 0) {
116
	    out_basic_int(UL(a_ch->data[i])>>(8-offst), UI(offst));
117
	}
118
	if (free_it) free_chunk(a_ch);
119
	a_ch = nextch;
120
    }
121
    if (a_ch != (Chunk*)0) {
122
	if (free_it) {	/* use existing chunks */
123
	    t_ch->next = a_ch;
124
	    current_TDF->last = tdf->last;
125
	} else {  /* copy chunks */
126
	    while( a_ch != (Chunk*)0 ) {
127
		Chunk *nch = create_chunk();
128
		t_ch->next = nch;
129
		t_ch = current_TDF->last = nch;
130
		*nch = *a_ch;
131
		a_ch = a_ch->next;
132
	    }
133
	}
134
    }
135
    SET_RSORT(tdf->sort);
136
    if (free_it) {
137
	tdf->first = tdf->last = (Chunk*)0; /* Don't use it again! */
138
    }
139
    return;
140
}
141
 
142
 
143
unsigned long bits_in_TDF
144
    PROTO_N ( (tdf) )
145
    PROTO_T ( TDF * tdf )
146
{
147
    Chunk * t_ch = tdf->first;
148
    unsigned long ans = 0;
149
    while (t_ch != (Chunk*)0) {
150
	Assert(!t_ch->aligned);
151
	ans+= (UL(t_ch->usage)<<3) + UL(t_ch->offst);
152
	t_ch = t_ch->next;
153
    }
154
    return ans;
155
}
156
 
157
 
158
void out_extendable_int
159
    PROTO_N ( (num, bts) )
160
    PROTO_T ( unsigned long num X unsigned int bts )
161
{
162
    if (num < (UL(1)<<bts)) {
163
	out_basic_int(num,bts);
164
    } else {
165
	out_basic_int(UL(0), bts);
166
	out_extendable_int(num-(UL(1)<<bts)+1, bts);
167
    }
168
}
169
 
170
 
171
void out_tdfint32
172
    PROTO_N ( (n) )
173
    PROTO_T ( unsigned long n )
174
    /* output 32 bit int as tdfint */
175
{
176
    int sh;
177
    Bool sig = 0;
178
    for (sh=30; sh>0; sh-=3) {
179
	unsigned long i = (n>>sh)&7;
180
	if (sig || i!=0) {
181
	    out_basic_int(i, UI(4));
182
	    sig = 1;
183
	}
184
    }
185
    out_basic_int((n &7)+8, 4);
186
    SET_RSORT(s_tdfint);
187
}
188
 
189
 
190
void out_tdfbool
191
    PROTO_N ( (b) )
192
    PROTO_T ( Bool b )
193
{
194
    out_basic_int(UL(b), UI(1));
195
    SET_RSORT(s_tdfbool);
196
}
197
 
198
 
199
void out_tdfstring_bytes
200
    PROTO_N ( (s, k, n) )
201
    PROTO_T ( char * s X unsigned int k X unsigned int n )
202
{
203
    unsigned int i;
204
    out_tdfint32(UL(k));
205
    out_tdfint32(UL(n));
206
    for (i=0; i<n; i++) {
207
	out_basic_int(UL(s[i]), k);
208
    }
209
    SET_RSORT(s_tdfstring);
210
}
211
 
212
 
213
void byte_align
214
    PROTO_Z ()
215
{
216
    Chunk * ch = current_TDF->last;
217
    if (ch->aligned !=0) {
218
      if (ch->offst !=0) out_basic_int(UL(0), UI(8-ch->offst));
219
      Assert(ch->offst == 0);
220
    } else {
221
       Chunk * nch = create_chunk();
222
       nch->aligned = 1;
223
       ch->next = nch;
224
       current_TDF->last = nch;
225
    }
226
}
227
 
228
 
229
void out_tdfident_bytes
230
    PROTO_N ( (s) )
231
    PROTO_T ( char *s )
232
{
233
    unsigned long i;
234
    unsigned long n = (unsigned long) strlen(s);
235
    byte_align();
236
    out_tdfint32(UL(8));
237
    out_tdfint32(n);
238
    byte_align();
239
    for (i=0; i<n; i++) {
240
	out_basic_int(UL(s[i]), UI(8));
241
    }
242
    SET_RSORT(s_tdfident);
243
}
244
 
245
 
246
void append_bytestream
247
    PROTO_N ( (tdf, free_it) )
248
    PROTO_T ( TDF * tdf X Bool free_it )
249
{
250
    unsigned long len = bits_in_TDF(tdf);
251
    unsigned long lenb = (len+7)>>3;
252
    out_tdfint32(lenb);
253
    byte_align();
254
    append_TDF(tdf, free_it);
255
    if ((lenb<<3) != len ) {
256
	out_basic_int(UL(0), UI((lenb<<3)-len));
257
    }
258
    SET_RSORT(s_bytestream);
259
}