File: bn_type.c

package info (click to toggle)
bnetd 0.4.19-1
  • links: PTS
  • area: main
  • in suites: potato
  • size: 1,552 kB
  • ctags: 2,109
  • sloc: ansic: 23,811; sh: 1,704; makefile: 295
file content (505 lines) | stat: -rw-r--r-- 11,082 bytes parent folder | download
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
/*
 * Copyright (C) 1998,1999  Ross Combs (rocombs@cs.nmsu.edu)
 * 
 * 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 program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */
#include "config.h"
#include "setup.h"
#ifdef HAVE_STRING_H
# include <string.h>
#else
# ifdef HAVE_STRINGS_H
#  include <strings.h>
# endif
#endif
#include "eventlog.h"
#include "bn_type.h"


/************************************************************/


extern int bn_byte_tag_set(bn_byte * dst, char const * tag)
{
    if (!dst)
    {
	eventlog(eventlog_level_error,"bn_byte_tag_set","got NULL dst");
        return -1;
    }
    if (!tag)
    {
	eventlog(eventlog_level_error,"bn_byte_tag_set","got NULL tag");
        return -1;
    }
    
    (*dst)[0] = (unsigned char)tag[0];
    return 0;
}


extern int bn_short_tag_set(bn_short * dst, char const * tag)
{
    if (!dst)
    {
	eventlog(eventlog_level_error,"bn_short_tag_set","got NULL dst");
        return -1;
    }
    if (!tag)
    {
	eventlog(eventlog_level_error,"bn_short_tag_set","got NULL tag");
        return -1;
    }
    
    (*dst)[0] = (unsigned char)tag[3];
    (*dst)[1] = (unsigned char)tag[2];
    return 0;
}


extern int bn_int_tag_set(bn_int * dst, char const * tag)
{
    if (!dst)
    {
	eventlog(eventlog_level_error,"bn_int_tag_set","got NULL dst");
        return -1;
    }
    if (!tag)
    {
	eventlog(eventlog_level_error,"bn_int_tag_set","got NULL tag");
        return -1;
    }
    
    (*dst)[0] = (unsigned char)tag[3];
    (*dst)[1] = (unsigned char)tag[2];
    (*dst)[2] = (unsigned char)tag[1];
    (*dst)[3] = (unsigned char)tag[0];
    return 0;
}


extern int bn_long_tag_set(bn_long * dst, char const * tag)
{
    if (!dst)
    {
	eventlog(eventlog_level_error,"bn_long_tag_set","got NULL dst");
        return -1;
    }
    if (!tag)
    {
	eventlog(eventlog_level_error,"bn_long_tag_set","got NULL tag");
        return -1;
    }
    
    (*dst)[0] = (unsigned char)tag[7];
    (*dst)[1] = (unsigned char)tag[6];
    (*dst)[2] = (unsigned char)tag[5];
    (*dst)[3] = (unsigned char)tag[4];
    (*dst)[4] = (unsigned char)tag[3];
    (*dst)[5] = (unsigned char)tag[2];
    (*dst)[6] = (unsigned char)tag[1];
    (*dst)[7] = (unsigned char)tag[0];
    return 0;
}


/************************************************************/


extern t_byte bn_byte_get(bn_byte const src)
{
    t_byte temp;
    
    if (!src)
    {
	eventlog(eventlog_level_error,"bn_byte_get","got NULL src");
        return 0;
    }
    
    temp = ((t_byte)src[0])    ;
    return temp;
}


extern t_short bn_short_get(bn_short const src)
{
    t_short temp;
    
    if (!src)
    {
	eventlog(eventlog_level_error,"bn_short_get","got NULL src");
        return 0;
    }
    
    temp =  ((t_short)src[0])    ;
    temp |= ((t_short)src[1])<< 8;
    return temp;
}


extern t_int bn_int_get(bn_int const src)
{
    t_int temp;
    
    if (!src)
    {
	eventlog(eventlog_level_error,"bn_int_get","got NULL src");
        return 0;
    }
    
    temp =  ((t_int)src[0])    ;
    temp |= ((t_int)src[1])<< 8;
    temp |= ((t_int)src[2])<<16;
    temp |= ((t_int)src[3])<<24;
    return temp;
}


#ifdef HAVE_T_LONG
extern t_long bn_long_get(bn_long const src)
{
    t_long temp;
    
    if (!src)
    {
	eventlog(eventlog_level_error,"bn_long_get","got NULL src");
        return 0;
    }
    
    temp =  ((t_long)src[0])    ;
    temp |= ((t_long)src[1])<< 8;
    temp |= ((t_long)src[2])<<16;
    temp |= ((t_long)src[3])<<24;
    temp |= ((t_long)src[4])<<32;
    temp |= ((t_long)src[5])<<40;
    temp |= ((t_long)src[6])<<48;
    temp |= ((t_long)src[7])<<56;
    return temp;
}
#endif


extern t_int bn_long_get_a(bn_long const src)
{
    t_int temp;
    
    if (!src)
    {
	eventlog(eventlog_level_error,"bn_long_get_a","got NULL src");
        return 0;
    }
    
    temp =  ((t_int)src[4])    ;
    temp |= ((t_int)src[5])<< 8;
    temp |= ((t_int)src[6])<<16;
    temp |= ((t_int)src[7])<<24;
    return temp;
}


extern t_int bn_long_get_b(bn_long const src)
{
    t_int temp;
    
    if (!src)
    {
	eventlog(eventlog_level_error,"bn_long_get_b","got NULL src");
        return 0;
    }
    
    temp =  ((t_int)src[0])    ;
    temp |= ((t_int)src[1])<< 8;
    temp |= ((t_int)src[2])<<16;
    temp |= ((t_int)src[3])<<24;
    return temp;
}


/************************************************************/


extern int bn_byte_set(bn_byte * dst, t_byte src)
{
    if (!dst)
    {
	eventlog(eventlog_level_error,"bn_byte_set","got NULL dst");
	return -1;
    }
    
    (*dst)[0] = (unsigned char)((src    )     );
    return 0;
}


extern int bn_short_set(bn_short * dst, t_short src)
{
    if (!dst)
    {
	eventlog(eventlog_level_error,"bn_short_set","got NULL dst");
	return -1;
    }
    
    (*dst)[0] = (unsigned char)((src    )&0xff);
    (*dst)[1] = (unsigned char)((src>> 8)     );
    return 0;
}


extern int bn_short_nset(bn_short * dst, t_short src)
{
    if (!dst)
    {
	eventlog(eventlog_level_error,"bn_short_nset","got NULL dst");
	return -1;
    }
    
    (*dst)[0] = (unsigned char)((src>> 8)     );
    (*dst)[1] = (unsigned char)((src    )&0xff);
    return 0;
}


extern int bn_int_set(bn_int * dst, t_int src)
{
    if (!dst)
    {
	eventlog(eventlog_level_error,"bn_int_set","got NULL dst");
	return -1;
    }
    
    (*dst)[0] = (unsigned char)((src    )&0xff);
    (*dst)[1] = (unsigned char)((src>> 8)&0xff);
    (*dst)[2] = (unsigned char)((src>>16)&0xff);
    (*dst)[3] = (unsigned char)((src>>24)     );
    return 0;
}


extern int bn_int_nset(bn_int * dst, t_int src)
{
    if (!dst)
    {
	eventlog(eventlog_level_error,"bn_int_nset","got NULL dst");
	return -1;
    }
    
    (*dst)[0] = (unsigned char)((src>>24)     );
    (*dst)[1] = (unsigned char)((src>>16)&0xff);
    (*dst)[2] = (unsigned char)((src>> 8)&0xff);
    (*dst)[3] = (unsigned char)((src    )&0xff);
    return 0;
}


#ifdef HAVE_T_LONG
extern int bn_long_set(bn_long * dst, t_long src)
{
    if (!dst)
    {
	eventlog(eventlog_level_error,"bn_long_set","got NULL dst");
	return -1;
    }
    
    (*dst)[0] = (unsigned char)((src    )&0xff);
    (*dst)[1] = (unsigned char)((src>> 8)&0xff);
    (*dst)[2] = (unsigned char)((src>>16)&0xff);
    (*dst)[3] = (unsigned char)((src>>24)&0xff);
    (*dst)[4] = (unsigned char)((src>>32)&0xff);
    (*dst)[5] = (unsigned char)((src>>40)&0xff);
    (*dst)[6] = (unsigned char)((src>>48)&0xff);
    (*dst)[7] = (unsigned char)((src>>56)     );
    return 0;
}


extern int bn_long_nset(bn_long * dst, t_long src)
{
    if (!dst)
    {
	eventlog(eventlog_level_error,"bn_long_nset","got NULL dst");
	return -1;
    }
    
    (*dst)[0] = (unsigned char)((src>>56)     );
    (*dst)[1] = (unsigned char)((src>>48)&0xff);
    (*dst)[2] = (unsigned char)((src>>40)&0xff);
    (*dst)[3] = (unsigned char)((src>>32)&0xff);
    (*dst)[4] = (unsigned char)((src>>24)&0xff);
    (*dst)[5] = (unsigned char)((src>>16)&0xff);
    (*dst)[6] = (unsigned char)((src>> 8)&0xff);
    (*dst)[7] = (unsigned char)((src    )&0xff);
    return 0;
}
#endif


extern int bn_long_set_a_b(bn_long * dst, t_int srca, t_int srcb)
{
    if (!dst)
    {
	eventlog(eventlog_level_error,"bn_long_set_a_b","got NULL dst");
	return -1;
    }
    
    (*dst)[0] = (unsigned char)((srcb    )&0xff);
    (*dst)[1] = (unsigned char)((srcb>> 8)&0xff);
    (*dst)[2] = (unsigned char)((srcb>>16)&0xff);
    (*dst)[3] = (unsigned char)((srcb>>24)&0xff);
    (*dst)[4] = (unsigned char)((srca    )&0xff);
    (*dst)[5] = (unsigned char)((srca>> 8)&0xff);
    (*dst)[6] = (unsigned char)((srca>>16)&0xff);
    (*dst)[7] = (unsigned char)((srca>>24)     );
    return 0;
}


extern int bn_long_nset_a_b(bn_long * dst, t_int srca, t_int srcb)
{
    if (!dst)
    {
	eventlog(eventlog_level_error,"bn_long_nset_a_b","got NULL dst");
	return -1;
    }
    
    (*dst)[0] = (unsigned char)((srca>>24)     );
    (*dst)[1] = (unsigned char)((srca>>16)&0xff);
    (*dst)[2] = (unsigned char)((srca>> 8)&0xff);
    (*dst)[3] = (unsigned char)((srca    )&0xff);
    (*dst)[4] = (unsigned char)((srcb>>24)&0xff);
    (*dst)[5] = (unsigned char)((srcb>>16)&0xff);
    (*dst)[6] = (unsigned char)((srcb>> 8)&0xff);
    (*dst)[7] = (unsigned char)((srcb    )&0xff);
    return 0;
}


/************************************************************/


extern int bn_raw_set(void * dst, void const * src, unsigned int len)
{
    if (!dst)
    {
	eventlog(eventlog_level_error,"bn_raw_set","got NULL dst");
	return -1;
    }
    if (!src)
    {
	eventlog(eventlog_level_error,"bn_raw_set","got NULL src");
	return -1;
    }
    
    memcpy(dst,src,len);
    return 0;
}


/************************************************************/


extern int bn_byte_tag_eq(bn_byte const src, char const * tag)
{
    bn_byte temp;
    
    if (!src)
    {
	eventlog(eventlog_level_error,"bn_byte_tag_eq","got NULL src");
	return -1;
    }
    if (!tag)
    {
	eventlog(eventlog_level_error,"bn_byte_tag_eq","got NULL tag");
	return -1;
    }
    
    if (bn_byte_tag_set(&temp,tag)<0)
	return -1;
    if (bn_byte_get(src)==bn_byte_get(temp))
	return 0;
    
    return -1;
}


extern int bn_short_tag_eq(bn_short const src, char const * tag)
{
    bn_short temp;
    
    if (!src)
    {
	eventlog(eventlog_level_error,"bn_short_tag_eq","got NULL src");
	return -1;
    }
    if (!tag)
    {
	eventlog(eventlog_level_error,"bn_short_tag_eq","got NULL tag");
	return -1;
    }
    
    if (bn_short_tag_set(&temp,tag)<0)
	return -1;
    if (bn_short_get(src)==bn_short_get(temp))
	return 0;
    
    return -1;
}


extern int bn_int_tag_eq(bn_int const src, char const * tag)
{
    bn_int temp;
    
    if (!src)
    {
	eventlog(eventlog_level_error,"bn_int_tag_eq","got NULL src");
	return -1;
    }
    if (!tag)
    {
	eventlog(eventlog_level_error,"bn_int_tag_eq","got NULL tag");
	return -1;
    }
    
    if (bn_int_tag_set(&temp,tag)<0)
	return -1;
    if (bn_int_get(src)==bn_int_get(temp))
	return 0;
    
    return -1;
}


extern int bn_long_tag_eq(bn_long const src, char const * tag)
{
    bn_long temp;
    
    if (!src)
    {
	eventlog(eventlog_level_error,"bn_long_tag_eq","got NULL src");
	return -1;
    }
    if (!tag)
    {
	eventlog(eventlog_level_error,"bn_long_tag_eq","got NULL tag");
	return -1;
    }
    
    if (bn_long_tag_set(&temp,tag)<0)
	return -1;
    if (bn_long_get_a(src)==bn_long_get_a(temp) &&
        bn_long_get_b(src)==bn_long_get_b(temp))
	return 0;
    
    return -1;
}