1 /*
2  * Copyright (C) 1998,1999,2001  Ross Combs (rocombs@cs.nmsu.edu)
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
17  */
18 #include "common/setup_before.h"
19 #ifdef HAVE_STRING_H
20 # include <string.h>
21 #endif
22 #ifdef HAVE_MEMORY_H
23 # include <memory.h>
24 #endif
25 #include "compat/memcpy.h"
26 #include "common/eventlog.h"
27 #include "common/bn_type.h"
28 #include "common/setup_after.h"
29 
30 
31 /************************************************************/
32 
33 
bn_byte_tag_get(bn_byte const * src,char * dst,unsigned int len)34 extern int bn_byte_tag_get(bn_byte const * src, char * dst, unsigned int len)
35 {
36     unsigned int i;
37 
38     if (!dst)
39     {
40 	eventlog(eventlog_level_error,__FUNCTION__,"got NULL dst");
41         return -1;
42     }
43     if (!src)
44     {
45 	eventlog(eventlog_level_error,__FUNCTION__,"got NULL src");
46         return -1;
47     }
48     if (len<1)
49     {
50 	eventlog(eventlog_level_error,__FUNCTION__,"got zero len");
51         return -1;
52     }
53 
54     for (i=0; i<len-1 && i<1; i++)
55 	dst[i] = (char)(*src)[-i];
56     dst[i] = '\0';
57 
58     return 0;
59 }
60 
61 
bn_short_tag_get(bn_short const * src,char * dst,unsigned int len)62 extern int bn_short_tag_get(bn_short const * src, char * dst, unsigned int len)
63 {
64     unsigned int i;
65 
66     if (!dst)
67     {
68 	eventlog(eventlog_level_error,__FUNCTION__,"got NULL dst");
69         return -1;
70     }
71     if (!src)
72     {
73 	eventlog(eventlog_level_error,__FUNCTION__,"got NULL src");
74         return -1;
75     }
76     if (len<1)
77     {
78 	eventlog(eventlog_level_error,__FUNCTION__,"got zero len");
79         return -1;
80     }
81 
82     for (i=0; i<len-1 && i<2; i++)
83 	dst[i] = (char)(*src)[1-i];
84     dst[i] = '\0';
85 
86     return 0;
87 }
88 
89 
bn_int_tag_get(bn_int const * src,char * dst,unsigned int len)90 extern int bn_int_tag_get(bn_int const * src, char * dst, unsigned int len)
91 {
92     unsigned int i;
93 
94     if (!dst)
95     {
96 	eventlog(eventlog_level_error,__FUNCTION__,"got NULL dst");
97         return -1;
98     }
99     if (!src)
100     {
101 	eventlog(eventlog_level_error,__FUNCTION__,"got NULL src");
102         return -1;
103     }
104     if (len<1)
105     {
106 	eventlog(eventlog_level_error,__FUNCTION__,"got zero len");
107         return -1;
108     }
109 
110     for (i=0; i<len-1 && i<4; i++)
111 	dst[i] = (char)(*src)[3-i];
112     dst[i] = '\0';
113 
114     return 0;
115 }
116 
117 
bn_long_tag_get(bn_long const * src,char * dst,unsigned int len)118 extern int bn_long_tag_get(bn_long const * src, char * dst, unsigned int len)
119 {
120     unsigned int i;
121 
122     if (!dst)
123     {
124 	eventlog(eventlog_level_error,__FUNCTION__,"got NULL dst");
125         return -1;
126     }
127     if (!src)
128     {
129 	eventlog(eventlog_level_error,__FUNCTION__,"got NULL src");
130         return -1;
131     }
132     if (len<1)
133     {
134 	eventlog(eventlog_level_error,__FUNCTION__,"got zero len");
135         return -1;
136     }
137 
138     for (i=0; i<len-1 && i<8; i++)
139 	dst[i] = (char)(*src)[7-i];
140     dst[i] = '\0';
141 
142     return 0;
143 }
144 
145 
146 /************************************************************/
147 
148 
bn_byte_tag_set(bn_byte * dst,char const * tag)149 extern int bn_byte_tag_set(bn_byte * dst, char const * tag)
150 {
151     if (!dst)
152     {
153 	eventlog(eventlog_level_error,__FUNCTION__,"got NULL dst");
154         return -1;
155     }
156     if (!tag)
157     {
158 	eventlog(eventlog_level_error,__FUNCTION__,"got NULL tag");
159         return -1;
160     }
161 
162     (*dst)[0] = (unsigned char)tag[0];
163     return 0;
164 }
165 
166 
bn_short_tag_set(bn_short * dst,char const * tag)167 extern int bn_short_tag_set(bn_short * dst, char const * tag)
168 {
169     if (!dst)
170     {
171 	eventlog(eventlog_level_error,__FUNCTION__,"got NULL dst");
172         return -1;
173     }
174     if (!tag)
175     {
176 	eventlog(eventlog_level_error,__FUNCTION__,"got NULL tag");
177         return -1;
178     }
179 
180     (*dst)[0] = (unsigned char)tag[3];
181     (*dst)[1] = (unsigned char)tag[2];
182     return 0;
183 }
184 
185 
bn_int_tag_set(bn_int * dst,char const * tag)186 extern int bn_int_tag_set(bn_int * dst, char const * tag)
187 {
188     if (!dst)
189     {
190 	eventlog(eventlog_level_error,__FUNCTION__,"got NULL dst");
191         return -1;
192     }
193     if (!tag)
194     {
195 	eventlog(eventlog_level_error,__FUNCTION__,"got NULL tag");
196         return -1;
197     }
198 
199     (*dst)[0] = (unsigned char)tag[3];
200     (*dst)[1] = (unsigned char)tag[2];
201     (*dst)[2] = (unsigned char)tag[1];
202     (*dst)[3] = (unsigned char)tag[0];
203     return 0;
204 }
205 
206 
bn_long_tag_set(bn_long * dst,char const * tag)207 extern int bn_long_tag_set(bn_long * dst, char const * tag)
208 {
209     if (!dst)
210     {
211 	eventlog(eventlog_level_error,__FUNCTION__,"got NULL dst");
212         return -1;
213     }
214     if (!tag)
215     {
216 	eventlog(eventlog_level_error,__FUNCTION__,"got NULL tag");
217         return -1;
218     }
219 
220     (*dst)[0] = (unsigned char)tag[7];
221     (*dst)[1] = (unsigned char)tag[6];
222     (*dst)[2] = (unsigned char)tag[5];
223     (*dst)[3] = (unsigned char)tag[4];
224     (*dst)[4] = (unsigned char)tag[3];
225     (*dst)[5] = (unsigned char)tag[2];
226     (*dst)[6] = (unsigned char)tag[1];
227     (*dst)[7] = (unsigned char)tag[0];
228     return 0;
229 }
230 
231 
232 /************************************************************/
233 
234 
bn_byte_get(bn_byte const src)235 extern t_uint8 bn_byte_get(bn_byte const src)
236 {
237     t_uint8 temp;
238 
239     if (!src)
240     {
241 	eventlog(eventlog_level_error,__FUNCTION__,"got NULL src");
242         return 0;
243     }
244 
245     temp = ((t_uint8)src[0])    ;
246     return temp;
247 }
248 
249 
bn_short_get(bn_short const src)250 extern t_uint16 bn_short_get(bn_short const src)
251 {
252     t_uint16 temp;
253 
254     if (!src)
255     {
256 	eventlog(eventlog_level_error,__FUNCTION__,"got NULL src");
257         return 0;
258     }
259 
260     temp =  ((t_uint16)src[0])    ;
261     temp |= ((t_uint16)src[1])<< 8;
262     return temp;
263 }
264 
265 
bn_short_nget(bn_short const src)266 extern t_uint16 bn_short_nget(bn_short const src)
267 {
268     t_uint16 temp;
269 
270     if (!src)
271     {
272 	eventlog(eventlog_level_error,__FUNCTION__,"got NULL src");
273         return 0;
274     }
275 
276     temp =  ((t_uint16)src[1])    ;
277     temp |= ((t_uint16)src[0])<< 8;
278     return temp;
279 }
280 
281 
bn_int_get(bn_int const src)282 extern t_uint32 bn_int_get(bn_int const src)
283 {
284     t_uint32 temp;
285 
286     if (!src)
287     {
288 	eventlog(eventlog_level_error,__FUNCTION__,"got NULL src");
289         return 0;
290     }
291 
292     temp =  ((t_uint32)src[0])    ;
293     temp |= ((t_uint32)src[1])<< 8;
294     temp |= ((t_uint32)src[2])<<16;
295     temp |= ((t_uint32)src[3])<<24;
296     return temp;
297 }
298 
299 
bn_int_nget(bn_int const src)300 extern t_uint32 bn_int_nget(bn_int const src)
301 {
302     t_uint32 temp;
303 
304     if (!src)
305     {
306 	eventlog(eventlog_level_error,__FUNCTION__,"got NULL src");
307         return 0;
308     }
309 
310     temp =  ((t_uint32)src[3])    ;
311     temp |= ((t_uint32)src[2])<< 8;
312     temp |= ((t_uint32)src[1])<<16;
313     temp |= ((t_uint32)src[0])<<24;
314     return temp;
315 }
316 
317 
318 #ifdef HAVE_T_LONG
bn_long_get(bn_long const src)319 extern t_uint64 bn_long_get(bn_long const src)
320 {
321     t_uint64 temp;
322 
323     if (!src)
324     {
325 	eventlog(eventlog_level_error,__FUNCTION__,"got NULL src");
326         return 0;
327     }
328 
329     temp =  ((t_uint64)src[0])    ;
330     temp |= ((t_uint64)src[1])<< 8;
331     temp |= ((t_uint64)src[2])<<16;
332     temp |= ((t_uint64)src[3])<<24;
333     temp |= ((t_uint64)src[4])<<32;
334     temp |= ((t_uint64)src[5])<<40;
335     temp |= ((t_uint64)src[6])<<48;
336     temp |= ((t_uint64)src[7])<<56;
337     return temp;
338 }
339 #endif
340 
341 
bn_long_get_a(bn_long const src)342 extern t_uint32 bn_long_get_a(bn_long const src)
343 {
344     t_uint32 temp;
345 
346     if (!src)
347     {
348 	eventlog(eventlog_level_error,__FUNCTION__,"got NULL src");
349         return 0;
350     }
351 
352     temp =  ((t_uint32)src[4])    ;
353     temp |= ((t_uint32)src[5])<< 8;
354     temp |= ((t_uint32)src[6])<<16;
355     temp |= ((t_uint32)src[7])<<24;
356     return temp;
357 }
358 
359 
bn_long_get_b(bn_long const src)360 extern t_uint32 bn_long_get_b(bn_long const src)
361 {
362     t_uint32 temp;
363 
364     if (!src)
365     {
366 	eventlog(eventlog_level_error,__FUNCTION__,"got NULL src");
367         return 0;
368     }
369 
370     temp =  ((t_uint32)src[0])    ;
371     temp |= ((t_uint32)src[1])<< 8;
372     temp |= ((t_uint32)src[2])<<16;
373     temp |= ((t_uint32)src[3])<<24;
374     return temp;
375 }
376 
377 
378 /************************************************************/
379 
380 
bn_byte_set(bn_byte * dst,t_uint8 src)381 extern int bn_byte_set(bn_byte * dst, t_uint8 src)
382 {
383     if (!dst)
384     {
385 	eventlog(eventlog_level_error,__FUNCTION__,"got NULL dst");
386 	return -1;
387     }
388 
389     (*dst)[0] = (unsigned char)((src    )     );
390     return 0;
391 }
392 
393 
bn_short_set(bn_short * dst,t_uint16 src)394 extern int bn_short_set(bn_short * dst, t_uint16 src)
395 {
396     if (!dst)
397     {
398 	eventlog(eventlog_level_error,__FUNCTION__,"got NULL dst");
399 	return -1;
400     }
401 
402     (*dst)[0] = (unsigned char)((src    )&0xff);
403     (*dst)[1] = (unsigned char)((src>> 8)     );
404     return 0;
405 }
406 
407 
bn_short_nset(bn_short * dst,t_uint16 src)408 extern int bn_short_nset(bn_short * dst, t_uint16 src)
409 {
410     if (!dst)
411     {
412 	eventlog(eventlog_level_error,__FUNCTION__,"got NULL dst");
413 	return -1;
414     }
415 
416     (*dst)[0] = (unsigned char)((src>> 8)     );
417     (*dst)[1] = (unsigned char)((src    )&0xff);
418     return 0;
419 }
420 
421 
bn_int_set(bn_int * dst,t_uint32 src)422 extern int bn_int_set(bn_int * dst, t_uint32 src)
423 {
424     if (!dst)
425     {
426 	eventlog(eventlog_level_error,__FUNCTION__,"got NULL dst");
427 	return -1;
428     }
429 
430     (*dst)[0] = (unsigned char)((src    )&0xff);
431     (*dst)[1] = (unsigned char)((src>> 8)&0xff);
432     (*dst)[2] = (unsigned char)((src>>16)&0xff);
433     (*dst)[3] = (unsigned char)((src>>24)     );
434     return 0;
435 }
436 
437 
bn_int_nset(bn_int * dst,t_uint32 src)438 extern int bn_int_nset(bn_int * dst, t_uint32 src)
439 {
440     if (!dst)
441     {
442 	eventlog(eventlog_level_error,__FUNCTION__,"got NULL dst");
443 	return -1;
444     }
445 
446     (*dst)[0] = (unsigned char)((src>>24)     );
447     (*dst)[1] = (unsigned char)((src>>16)&0xff);
448     (*dst)[2] = (unsigned char)((src>> 8)&0xff);
449     (*dst)[3] = (unsigned char)((src    )&0xff);
450     return 0;
451 }
452 
453 
454 #ifdef HAVE_T_LONG
bn_long_set(bn_long * dst,t_uint64 src)455 extern int bn_long_set(bn_long * dst, t_uint64 src)
456 {
457     if (!dst)
458     {
459 	eventlog(eventlog_level_error,__FUNCTION__,"got NULL dst");
460 	return -1;
461     }
462 
463     (*dst)[0] = (unsigned char)((src    )&0xff);
464     (*dst)[1] = (unsigned char)((src>> 8)&0xff);
465     (*dst)[2] = (unsigned char)((src>>16)&0xff);
466     (*dst)[3] = (unsigned char)((src>>24)&0xff);
467     (*dst)[4] = (unsigned char)((src>>32)&0xff);
468     (*dst)[5] = (unsigned char)((src>>40)&0xff);
469     (*dst)[6] = (unsigned char)((src>>48)&0xff);
470     (*dst)[7] = (unsigned char)((src>>56)     );
471     return 0;
472 }
473 
474 
bn_long_nset(bn_long * dst,t_uint64 src)475 extern int bn_long_nset(bn_long * dst, t_uint64 src)
476 {
477     if (!dst)
478     {
479 	eventlog(eventlog_level_error,__FUNCTION__,"got NULL dst");
480 	return -1;
481     }
482 
483     (*dst)[0] = (unsigned char)((src>>56)     );
484     (*dst)[1] = (unsigned char)((src>>48)&0xff);
485     (*dst)[2] = (unsigned char)((src>>40)&0xff);
486     (*dst)[3] = (unsigned char)((src>>32)&0xff);
487     (*dst)[4] = (unsigned char)((src>>24)&0xff);
488     (*dst)[5] = (unsigned char)((src>>16)&0xff);
489     (*dst)[6] = (unsigned char)((src>> 8)&0xff);
490     (*dst)[7] = (unsigned char)((src    )&0xff);
491     return 0;
492 }
493 #endif
494 
495 
bn_long_set_a_b(bn_long * dst,t_uint32 srca,t_uint32 srcb)496 extern int bn_long_set_a_b(bn_long * dst, t_uint32 srca, t_uint32 srcb)
497 {
498     if (!dst)
499     {
500 	eventlog(eventlog_level_error,__FUNCTION__,"got NULL dst");
501 	return -1;
502     }
503 
504     (*dst)[0] = (unsigned char)((srcb    )&0xff);
505     (*dst)[1] = (unsigned char)((srcb>> 8)&0xff);
506     (*dst)[2] = (unsigned char)((srcb>>16)&0xff);
507     (*dst)[3] = (unsigned char)((srcb>>24)&0xff);
508     (*dst)[4] = (unsigned char)((srca    )&0xff);
509     (*dst)[5] = (unsigned char)((srca>> 8)&0xff);
510     (*dst)[6] = (unsigned char)((srca>>16)&0xff);
511     (*dst)[7] = (unsigned char)((srca>>24)     );
512     return 0;
513 }
514 
515 
bn_long_nset_a_b(bn_long * dst,t_uint32 srca,t_uint32 srcb)516 extern int bn_long_nset_a_b(bn_long * dst, t_uint32 srca, t_uint32 srcb)
517 {
518     if (!dst)
519     {
520 	eventlog(eventlog_level_error,__FUNCTION__,"got NULL dst");
521 	return -1;
522     }
523 
524     (*dst)[0] = (unsigned char)((srca>>24)     );
525     (*dst)[1] = (unsigned char)((srca>>16)&0xff);
526     (*dst)[2] = (unsigned char)((srca>> 8)&0xff);
527     (*dst)[3] = (unsigned char)((srca    )&0xff);
528     (*dst)[4] = (unsigned char)((srcb>>24)&0xff);
529     (*dst)[5] = (unsigned char)((srcb>>16)&0xff);
530     (*dst)[6] = (unsigned char)((srcb>> 8)&0xff);
531     (*dst)[7] = (unsigned char)((srcb    )&0xff);
532     return 0;
533 }
534 
535 
536 /************************************************************/
537 
538 
bn_raw_set(void * dst,void const * src,unsigned int len)539 extern int bn_raw_set(void * dst, void const * src, unsigned int len)
540 {
541     if (!dst)
542     {
543 	eventlog(eventlog_level_error,__FUNCTION__,"got NULL dst");
544 	return -1;
545     }
546     if (!src)
547     {
548 	eventlog(eventlog_level_error,__FUNCTION__,"got NULL src");
549 	return -1;
550     }
551 
552     memcpy(dst,src,len);
553     return 0;
554 }
555 
556 
557 /************************************************************/
558 
559 
bn_byte_tag_eq(bn_byte const src,char const * tag)560 extern int bn_byte_tag_eq(bn_byte const src, char const * tag)
561 {
562     bn_byte temp;
563 
564     if (!src)
565     {
566 	eventlog(eventlog_level_error,__FUNCTION__,"got NULL src");
567 	return -1;
568     }
569     if (!tag)
570     {
571 	eventlog(eventlog_level_error,__FUNCTION__,"got NULL tag");
572 	return -1;
573     }
574 
575     if (bn_byte_tag_set(&temp,tag)<0)
576 	return -1;
577     if (bn_byte_get(src)==bn_byte_get(temp))
578 	return 0;
579 
580     return -1;
581 }
582 
583 
bn_short_tag_eq(bn_short const src,char const * tag)584 extern int bn_short_tag_eq(bn_short const src, char const * tag)
585 {
586     bn_short temp;
587 
588     if (!src)
589     {
590 	eventlog(eventlog_level_error,__FUNCTION__,"got NULL src");
591 	return -1;
592     }
593     if (!tag)
594     {
595 	eventlog(eventlog_level_error,__FUNCTION__,"got NULL tag");
596 	return -1;
597     }
598 
599     if (bn_short_tag_set(&temp,tag)<0)
600 	return -1;
601     if (bn_short_get(src)==bn_short_get(temp))
602 	return 0;
603 
604     return -1;
605 }
606 
607 
bn_int_tag_eq(bn_int const src,char const * tag)608 extern int bn_int_tag_eq(bn_int const src, char const * tag)
609 {
610     bn_int temp;
611 
612     if (!src)
613     {
614 	eventlog(eventlog_level_error,__FUNCTION__,"got NULL src");
615 	return -1;
616     }
617     if (!tag)
618     {
619 	eventlog(eventlog_level_error,__FUNCTION__,"got NULL tag");
620 	return -1;
621     }
622 
623     if (bn_int_tag_set(&temp,tag)<0)
624 	return -1;
625     if (bn_int_get(src)==bn_int_get(temp))
626 	return 0;
627 
628     return -1;
629 }
630 
631 
bn_long_tag_eq(bn_long const src,char const * tag)632 extern int bn_long_tag_eq(bn_long const src, char const * tag)
633 {
634     bn_long temp;
635 
636     if (!src)
637     {
638 	eventlog(eventlog_level_error,__FUNCTION__,"got NULL src");
639 	return -1;
640     }
641     if (!tag)
642     {
643 	eventlog(eventlog_level_error,__FUNCTION__,"got NULL tag");
644 	return -1;
645     }
646 
647     if (bn_long_tag_set(&temp,tag)<0)
648 	return -1;
649     if (bn_long_get_a(src)==bn_long_get_a(temp) &&
650         bn_long_get_b(src)==bn_long_get_b(temp))
651 	return 0;
652 
653     return -1;
654 }
655 
656 
657 /************************************************************/
658 
659 
uint32_to_int(t_uint32 num)660 extern int uint32_to_int(t_uint32 num)
661 {
662     if (num<(1UL<<30))
663         return (int)num;
664     return (-(int)((~(num))+1));
665 }
666