Whamcloud - gitweb
99facc7ab5b8f00a2f8ae72c2d8a9452cd9654f8
[fs/lustre-release.git] / lustre / ptlrpc / gss / gss_rawobj.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * Copyright (C) 2004 Cluster File Systems, Inc.
5  *
6  *   This file is part of Lustre, http://www.lustre.org.
7  *
8  *   Lustre is free software; you can redistribute it and/or
9  *   modify it under the terms of version 2 of the GNU General Public
10  *   License as published by the Free Software Foundation.
11  *
12  *   Lustre is distributed in the hope that it will be useful,
13  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *   GNU General Public License for more details.
16  *
17  *   You should have received a copy of the GNU General Public License
18  *   along with Lustre; if not, write to the Free Software
19  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20  */
21
22 #ifndef EXPORT_SYMTAB
23 # define EXPORT_SYMTAB
24 #endif
25 #define DEBUG_SUBSYSTEM S_SEC
26
27 #include <linux/mutex.h>
28
29 #include <obd.h>
30 #include <obd_class.h>
31 #include <obd_support.h>
32 #include <lustre_sec.h>
33
34 #include "gss_internal.h"
35
36 int rawobj_alloc(rawobj_t *obj, char *buf, int len)
37 {
38         LASSERT(obj);
39         LASSERT(len >= 0);
40
41         obj->len = len;
42         if (len) {
43                 OBD_ALLOC(obj->data, len);
44                 if (!obj->data) {
45                         obj->len = 0;
46                         RETURN(-ENOMEM);
47                 }
48                 memcpy(obj->data, buf, len);
49         } else
50                 obj->data = NULL;
51         return 0;
52 }
53
54 void rawobj_free(rawobj_t *obj)
55 {
56         LASSERT(obj);
57
58         if (obj->len) {
59                 LASSERT(obj->data);
60                 OBD_FREE(obj->data, obj->len);
61                 obj->len = 0;
62                 obj->data = NULL;
63         } else
64                 LASSERT(!obj->data);
65 }
66
67 int rawobj_equal(rawobj_t *a, rawobj_t *b)
68 {
69         LASSERT(a && b);
70
71         return (a->len == b->len &&
72                 (!a->len || !memcmp(a->data, b->data, a->len)));
73 }
74
75 int rawobj_dup(rawobj_t *dest, rawobj_t *src)
76 {
77         LASSERT(src && dest);
78
79         dest->len = src->len;
80         if (dest->len) {
81                 OBD_ALLOC(dest->data, dest->len);
82                 if (!dest->data) {
83                         dest->len = 0;
84                         return -ENOMEM;
85                 }
86                 memcpy(dest->data, src->data, dest->len);
87         } else
88                 dest->data = NULL;
89         return 0;
90 }
91
92 int rawobj_serialize(rawobj_t *obj, __u32 **buf, __u32 *buflen)
93 {
94         __u32 len;
95
96         LASSERT(obj);
97         LASSERT(buf);
98         LASSERT(buflen);
99
100         len = size_round4(obj->len);
101
102         if (*buflen < 4 + len) {
103                 CERROR("buflen %u <  %u\n", *buflen, 4 + len);
104                 return -EINVAL;
105         }
106
107         *(*buf)++ = cpu_to_le32(obj->len);
108         memcpy(*buf, obj->data, obj->len);
109         *buf += (len >> 2);
110         *buflen -= (4 + len);
111
112         return 0;
113 }
114
115 static int __rawobj_extract(rawobj_t *obj, __u32 **buf, __u32 *buflen,
116                             int alloc, int local)
117 {
118         __u32 len;
119
120         if (*buflen < sizeof(__u32)) {
121                 CERROR("buflen %u\n", *buflen);
122                 return -EINVAL;
123         }
124
125         obj->len = *(*buf)++;
126         if (!local)
127                 obj->len = le32_to_cpu(obj->len);
128         *buflen -= sizeof(__u32);
129
130         if (!obj->len) {
131                 obj->data = NULL;
132                 return 0;
133         }
134
135         len = local ? obj->len : size_round4(obj->len);
136         if (*buflen < len) {
137                 CERROR("buflen %u < %u\n", *buflen, len);
138                 obj->len = 0;
139                 return -EINVAL;
140         }
141
142         if (!alloc)
143                 obj->data = (__u8 *) *buf;
144         else {
145                 OBD_ALLOC(obj->data, obj->len);
146                 if (!obj->data) {
147                         CERROR("fail to alloc %u bytes\n", obj->len);
148                         obj->len = 0;
149                         return -ENOMEM;
150                 }
151                 memcpy(obj->data, *buf, obj->len);
152         }
153
154         *((char **)buf) += len;
155         *buflen -= len;
156
157         return 0;
158 }
159
160 int rawobj_extract(rawobj_t *obj, __u32 **buf, __u32 *buflen)
161 {
162         return __rawobj_extract(obj, buf, buflen, 0, 0);
163 }
164
165 int rawobj_extract_alloc(rawobj_t *obj, __u32 **buf, __u32 *buflen)
166 {
167         return __rawobj_extract(obj, buf, buflen, 1, 0);
168 }
169
170 int rawobj_extract_local(rawobj_t *obj, __u32 **buf, __u32 *buflen)
171 {
172         return __rawobj_extract(obj, buf, buflen, 0, 1);
173 }
174
175 int rawobj_extract_local_alloc(rawobj_t *obj, __u32 **buf, __u32 *buflen)
176 {
177         return __rawobj_extract(obj, buf, buflen, 1, 1);
178 }
179
180 int rawobj_from_netobj(rawobj_t *rawobj, netobj_t *netobj)
181 {
182         rawobj->len = netobj->len;
183         rawobj->data = netobj->data;
184         return 0;
185 }
186
187 int rawobj_from_netobj_alloc(rawobj_t *rawobj, netobj_t *netobj)
188 {
189         rawobj->len = 0;
190         rawobj->data = NULL;
191
192         if (netobj->len == 0)
193                 return 0;
194                 
195         OBD_ALLOC(rawobj->data, netobj->len);
196         if (rawobj->data == NULL)
197                 return -ENOMEM;
198
199         rawobj->len = netobj->len;
200         memcpy(rawobj->data, netobj->data, netobj->len);
201         return 0;
202 }
203
204 /****************************************
205  * misc more                            *
206  ****************************************/
207
208 int buffer_extract_bytes(const void **buf, __u32 *buflen,
209                          void *res, __u32 reslen)
210 {
211         if (*buflen < reslen) {
212                 CERROR("buflen %u < %u\n", *buflen, reslen);
213                 return -EINVAL;
214         }
215
216         memcpy(res, *buf, reslen);
217         *buf += reslen;
218         *buflen -= reslen;
219         return 0;
220 }