Added better error checking.
[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.3  1995-03-08 12:12:06  quinn
8  * Added better error checking.
9  *
10  * Revision 1.2  1995/02/09  15:51:45  quinn
11  * Works better now.
12  *
13  * Revision 1.1  1995/02/02  16:21:51  quinn
14  * First kick.
15  *
16  */
17
18 #include <stdio.h>
19 #include <odr.h>
20
21
22 int ber_boolean(ODR o, int *val)
23 {
24     unsigned char *b = o->bp;
25     int res, len;
26
27     switch (o->direction)
28     {
29         case ODR_ENCODE:
30             if (!o->left)
31             {
32                 o->error = OSPACE;
33                 return 0;
34             }
35             if (ber_enclen(o->bp, 1, 1, 1) != 1)
36             {
37                 o->error = OOTHER;
38                 return 0;
39             }
40             o->bp++;
41             o->left--;
42             *(o->bp++) = (unsigned char) *val;
43 #ifdef ODR_DEBUG
44             fprintf(stderr, "[val=%d]\n", *val);
45 #endif
46             o->left--;
47             return 1;
48         case ODR_DECODE:
49             if ((res = ber_declen(b, &len)) < 0)
50             {
51                 o->error = OPROTO;
52                 return 0;
53             }
54             if (len != 1)
55             {
56                 o->error = OPROTO;
57                 return 0;
58             }
59             o->bp+= res;
60             o->left -= res;
61             *val = *b;
62             o->bp++;
63             o->left--;
64 #ifdef ODR_DEBUG
65             fprintf(stderr, "[val=%d]\n", *val);
66 #endif
67             return 1;
68         case ODR_PRINT:
69             return 1;
70         default: o->error = OOTHER; return 0;
71     }
72 }