233de651d3d72367a3b3806ccb197e4b13c99783
[yaz-moved-to-github.git] / odr / ber_bool.c
1 /*
2  * Copyright (C) 1994, Index Data I/S 
3  * All rights reserved.
4  * Sebastian Hammer, Adam Dickmeiss
5  *
6  * $Log: ber_bool.c,v $
7  * Revision 1.5  1995-04-18 08:15:14  quinn
8  * Added dynamic memory allocation on encoding (whew). Code is now somewhat
9  * neater. We'll make the same change for decoding one day.
10  *
11  * Revision 1.4  1995/03/21  10:17:27  quinn
12  * Fixed little bug in decoder.
13  *
14  * Revision 1.3  1995/03/08  12:12:06  quinn
15  * Added better error checking.
16  *
17  * Revision 1.2  1995/02/09  15:51:45  quinn
18  * Works better now.
19  *
20  * Revision 1.1  1995/02/02  16:21:51  quinn
21  * First kick.
22  *
23  */
24
25 #include <stdio.h>
26 #include <odr.h>
27
28
29 int ber_boolean(ODR o, int *val)
30 {
31     int res, len;
32
33     switch (o->direction)
34     {
35         case ODR_ENCODE:
36             if (ber_enclen(o, 1, 1, 1) != 1)
37                 return 0;
38             if (odr_putc(o, *val) < 0)
39                 return 0;
40 #ifdef ODR_DEBUG
41             fprintf(stderr, "[val=%d]\n", *val);
42 #endif
43             return 1;
44         case ODR_DECODE:
45             if ((res = ber_declen(o->bp, &len)) < 0)
46             {
47                 o->error = OPROTO;
48                 return 0;
49             }
50             if (len != 1)
51             {
52                 o->error = OPROTO;
53                 return 0;
54             }
55             o->bp+= res;
56             o->left -= res;
57             *val = *o->bp;
58             o->bp++;
59             o->left--;
60 #ifdef ODR_DEBUG
61             fprintf(stderr, "[val=%d]\n", *val);
62 #endif
63             return 1;
64         case ODR_PRINT:
65             return 1;
66         default: o->error = OOTHER; return 0;
67     }
68 }