1 /* distcache, Distributed Session Caching technology
2 * Copyright (C) 2000-2003 Geoff Thorpe, and Cryptographic Appliances, Inc.
3 * Copyright (C) 2004 The Distcache.org project
4 *
5 * This library is free software; you can redistribute it and/or modify it under
6 * the terms of the GNU Lesser General Public License as published by the Free
7 * Software Foundation; using version 2.1 of the License. The copyright holders
8 * may elect to allow the application of later versions of the License to this
9 * software, please contact the author (geoff@distcache.org) if you wish us to
10 * review any later version released by the Free Software Foundation.
11 *
12 * This library is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
15 * details.
16 *
17 * You should have received a copy of the GNU Lesser General Public License
18 * along with this library; if not, write to the Free Software Foundation, Inc.,
19 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 */
21
22 #define SYS_GENERATING_LIB
23
24 #include <libsys/pre.h>
25 #include <libnal/nal.h>
26 #include <libsys/post.h>
27
28 /*
29 * ENCODED BINARY HANDLING
30 *
31 * These decode_*** functions are to extract different kinds of primitives from
32 * a binary string. They alter the binary pointer and length counter as they go
33 * from invocation to the next, so that in the event of there being too little
34 * data, an error can be caught.
35 */
NAL_decode_uint32(const unsigned char ** bin,unsigned int * bin_len,unsigned long * val)36 int NAL_decode_uint32(const unsigned char **bin, unsigned int *bin_len,
37 unsigned long *val)
38 {
39 if(*bin_len < 4)
40 return 0;
41 *val = (unsigned long)(*((*bin)++)) << 24;
42 *val += (unsigned long)(*((*bin)++)) << 16;
43 *val += (unsigned long)(*((*bin)++)) << 8;
44 *val += (unsigned long)(*((*bin)++));
45 *bin_len -= 4;
46 return 1;
47 }
48
NAL_decode_uint16(const unsigned char ** bin,unsigned int * bin_len,unsigned int * val)49 int NAL_decode_uint16(const unsigned char **bin, unsigned int *bin_len,
50 unsigned int *val)
51 {
52 if(*bin_len < 2)
53 return 0;
54 *val = (unsigned long)*((*bin)++) << 8;
55 *val += (unsigned long)*((*bin)++);
56 *bin_len -= 2;
57 return 1;
58 }
59
NAL_decode_char(const unsigned char ** bin,unsigned int * bin_len,unsigned char * c)60 int NAL_decode_char(const unsigned char **bin, unsigned int *bin_len,
61 unsigned char *c)
62 {
63 if(*bin_len < 1)
64 return 0;
65 *c = *((*bin)++);
66 *bin_len -= 1;
67 return 1;
68 }
69
NAL_decode_bin(const unsigned char ** bin,unsigned int * bin_len,unsigned char * val,unsigned int val_len)70 int NAL_decode_bin(const unsigned char **bin, unsigned int *bin_len,
71 unsigned char *val, unsigned int val_len)
72 {
73 if(*bin_len < val_len)
74 return 0;
75 if(val_len == 0)
76 return 1;
77 SYS_memcpy_n(unsigned char, val, *bin, val_len);
78 *bin += val_len;
79 *bin_len -= val_len;
80 return 1;
81 }
82
83 /*
84 * These encode_*** functions deal with serialising primitive C types into a
85 * contiguous binary stream.
86 */
NAL_encode_uint32(unsigned char ** bin,unsigned int * cnt,const unsigned long val)87 int NAL_encode_uint32(unsigned char **bin, unsigned int *cnt,
88 const unsigned long val)
89 {
90 if(*cnt < 4) {
91 #if SYS_DEBUG_LEVEL > 3
92 if(SYS_stderr) SYS_fprintf(SYS_stderr, "encode_uint32: overflow\n");
93 #endif
94 return 0;
95 }
96 *((*bin)++) = (unsigned char)((val >> 24) & 0x0FF);
97 *((*bin)++) = (unsigned char)((val >> 16) & 0x0FF);
98 *((*bin)++) = (unsigned char)((val >> 8) & 0x0FF);
99 *((*bin)++) = (unsigned char)(val & 0x0FF);
100 *cnt -= 4;
101 return 1;
102 }
103
NAL_encode_uint16(unsigned char ** bin,unsigned int * cnt,const unsigned int val)104 int NAL_encode_uint16(unsigned char **bin, unsigned int *cnt,
105 const unsigned int val)
106 {
107 if(*cnt < 2) {
108 #if SYS_DEBUG_LEVEL > 3
109 if(SYS_stderr) SYS_fprintf(SYS_stderr, "encode_uint16: overflow\n");
110 #endif
111 return 0;
112 }
113 *((*bin)++) = (unsigned char)((val >> 8) & 0x0FF);
114 *((*bin)++) = (unsigned char)(val & 0x0FF);
115 *cnt -= 2;
116 return 1;
117 }
118
NAL_encode_char(unsigned char ** bin,unsigned int * cnt,const unsigned char c)119 int NAL_encode_char(unsigned char **bin, unsigned int *cnt,
120 const unsigned char c)
121 {
122 if(*cnt < 1) {
123 #if SYS_DEBUG_LEVEL > 3
124 if(SYS_stderr) SYS_fprintf(SYS_stderr, "encode_char: overflow\n");
125 #endif
126 return 0;
127 }
128 *((*bin)++) = c;
129 *cnt -= 1;
130 return 1;
131 }
132
NAL_encode_bin(unsigned char ** bin,unsigned int * cnt,const unsigned char * data,const unsigned int len)133 int NAL_encode_bin(unsigned char **bin, unsigned int *cnt,
134 const unsigned char *data, const unsigned int len)
135 {
136 if(*cnt < len) {
137 #if SYS_DEBUG_LEVEL > 3
138 if(SYS_stderr) SYS_fprintf(SYS_stderr, "encode_bin: overflow\n");
139 #endif
140 return 0;
141 }
142 SYS_memcpy_n(unsigned char, *bin, data, len);
143 *bin += len;
144 *cnt -= len;
145 return 1;
146 }
147
148