Whamcloud - gitweb
LU-8648 all: remove all Sun license and URL references
[fs/lustre-release.git] / lustre / ptlrpc / gss / gss_rawobj.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.gnu.org/licenses/gpl-2.0.html
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Use is subject to license terms.
25  *
26  * Copyright (c) 2011, Intel Corporation.
27  */
28 /*
29  * This file is part of Lustre, http://www.lustre.org/
30  * Lustre is a trademark of Sun Microsystems, Inc.
31  *
32  * lustre/ptlrpc/gss/gss_rawobj.c
33  *
34  * Author: Eric Mei <ericm@clusterfs.com>
35  */
36
37 #define DEBUG_SUBSYSTEM S_SEC
38
39 #include <linux/mutex.h>
40
41 #include <obd.h>
42 #include <obd_class.h>
43 #include <obd_support.h>
44 #include <lustre_sec.h>
45
46 #include "gss_internal.h"
47
48 int rawobj_empty(rawobj_t *obj)
49 {
50         LASSERT(equi(obj->len, obj->data));
51         return (obj->len == 0);
52 }
53
54 int rawobj_alloc(rawobj_t *obj, char *buf, int len)
55 {
56         LASSERT(obj);
57         LASSERT(len >= 0);
58
59         obj->len = len;
60         if (len) {
61                 OBD_ALLOC_LARGE(obj->data, len);
62                 if (!obj->data) {
63                         obj->len = 0;
64                         RETURN(-ENOMEM);
65                 }
66                 memcpy(obj->data, buf, len);
67         } else
68                 obj->data = NULL;
69         return 0;
70 }
71
72 void rawobj_free(rawobj_t *obj)
73 {
74         LASSERT(obj);
75
76         if (obj->len) {
77                 LASSERT(obj->data);
78                 OBD_FREE_LARGE(obj->data, obj->len);
79                 obj->len = 0;
80                 obj->data = NULL;
81         } else
82                 LASSERT(!obj->data);
83 }
84
85 int rawobj_equal(rawobj_t *a, rawobj_t *b)
86 {
87         LASSERT(a && b);
88
89         return (a->len == b->len &&
90                 (!a->len || !memcmp(a->data, b->data, a->len)));
91 }
92
93 int rawobj_dup(rawobj_t *dest, rawobj_t *src)
94 {
95         LASSERT(src && dest);
96
97         dest->len = src->len;
98         if (dest->len) {
99                 OBD_ALLOC_LARGE(dest->data, dest->len);
100                 if (!dest->data) {
101                         dest->len = 0;
102                         return -ENOMEM;
103                 }
104                 memcpy(dest->data, src->data, dest->len);
105         } else
106                 dest->data = NULL;
107         return 0;
108 }
109
110 int rawobj_serialize(rawobj_t *obj, __u32 **buf, __u32 *buflen)
111 {
112         __u32 len;
113
114         LASSERT(obj);
115         LASSERT(buf);
116         LASSERT(buflen);
117
118         len = cfs_size_round4(obj->len);
119
120         if (*buflen < 4 + len) {
121                 CERROR("buflen %u <  %u\n", *buflen, 4 + len);
122                 return -EINVAL;
123         }
124
125         *(*buf)++ = cpu_to_le32(obj->len);
126         memcpy(*buf, obj->data, obj->len);
127         *buf += (len >> 2);
128         *buflen -= (4 + len);
129
130         return 0;
131 }
132
133 static int __rawobj_extract(rawobj_t *obj, __u32 **buf, __u32 *buflen,
134                             int alloc, int local)
135 {
136         __u32 len;
137
138         if (*buflen < sizeof(__u32)) {
139                 CERROR("buflen %u\n", *buflen);
140                 return -EINVAL;
141         }
142
143         obj->len = *(*buf)++;
144         if (!local)
145                 obj->len = le32_to_cpu(obj->len);
146         *buflen -= sizeof(__u32);
147
148         if (!obj->len) {
149                 obj->data = NULL;
150                 return 0;
151         }
152
153         len = local ? obj->len : cfs_size_round4(obj->len);
154         if (*buflen < len) {
155                 CERROR("buflen %u < %u\n", *buflen, len);
156                 obj->len = 0;
157                 return -EINVAL;
158         }
159
160         if (!alloc)
161                 obj->data = (__u8 *) *buf;
162         else {
163                 OBD_ALLOC_LARGE(obj->data, obj->len);
164                 if (!obj->data) {
165                         CERROR("fail to alloc %u bytes\n", obj->len);
166                         obj->len = 0;
167                         return -ENOMEM;
168                 }
169                 memcpy(obj->data, *buf, obj->len);
170         }
171
172         *((char **)buf) += len;
173         *buflen -= len;
174
175         return 0;
176 }
177
178 int rawobj_extract(rawobj_t *obj, __u32 **buf, __u32 *buflen)
179 {
180         return __rawobj_extract(obj, buf, buflen, 0, 0);
181 }
182
183 int rawobj_extract_alloc(rawobj_t *obj, __u32 **buf, __u32 *buflen)
184 {
185         return __rawobj_extract(obj, buf, buflen, 1, 0);
186 }
187
188 int rawobj_extract_local(rawobj_t *obj, __u32 **buf, __u32 *buflen)
189 {
190         return __rawobj_extract(obj, buf, buflen, 0, 1);
191 }
192
193 int rawobj_extract_local_alloc(rawobj_t *obj, __u32 **buf, __u32 *buflen)
194 {
195         return __rawobj_extract(obj, buf, buflen, 1, 1);
196 }
197
198 int rawobj_from_netobj(rawobj_t *rawobj, netobj_t *netobj)
199 {
200         rawobj->len = netobj->len;
201         rawobj->data = netobj->data;
202         return 0;
203 }
204
205 int rawobj_from_netobj_alloc(rawobj_t *rawobj, netobj_t *netobj)
206 {
207         rawobj->len = 0;
208         rawobj->data = NULL;
209
210         if (netobj->len == 0)
211                 return 0;
212                 
213         OBD_ALLOC_LARGE(rawobj->data, netobj->len);
214         if (rawobj->data == NULL)
215                 return -ENOMEM;
216
217         rawobj->len = netobj->len;
218         memcpy(rawobj->data, netobj->data, netobj->len);
219         return 0;
220 }
221
222 /****************************************
223  * misc more                            *
224  ****************************************/
225
226 int buffer_extract_bytes(const void **buf, __u32 *buflen,
227                          void *res, __u32 reslen)
228 {
229         if (*buflen < reslen) {
230                 CERROR("buflen %u < %u\n", *buflen, reslen);
231                 return -EINVAL;
232         }
233
234         memcpy(res, *buf, reslen);
235         *buf += reslen;
236         *buflen -= reslen;
237         return 0;
238 }