Whamcloud - gitweb
- make HEAD from b_post_cmd3
[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 <obd.h>
28 #include <obd_class.h>
29 #include <obd_support.h>
30 #include <lustre_sec.h>
31
32 #include "gss_internal.h"
33
34 int rawobj_alloc(rawobj_t *obj, char *buf, int len)
35 {
36         LASSERT(obj);
37         LASSERT(len >= 0);
38
39         obj->len = len;
40         if (len) {
41                 OBD_ALLOC(obj->data, len);
42                 if (!obj->data) {
43                         obj->len = 0;
44                         RETURN(-ENOMEM);
45                 }
46                 memcpy(obj->data, buf, len);
47         } else
48                 obj->data = NULL;
49         return 0;
50 }
51
52 void rawobj_free(rawobj_t *obj)
53 {
54         LASSERT(obj);
55
56         if (obj->len) {
57                 LASSERT(obj->data);
58                 OBD_FREE(obj->data, obj->len);
59                 obj->len = 0;
60                 obj->data = NULL;
61         } else
62                 LASSERT(!obj->data);
63 }
64
65 int rawobj_equal(rawobj_t *a, rawobj_t *b)
66 {
67         LASSERT(a && b);
68
69         return (a->len == b->len &&
70                 (!a->len || !memcmp(a->data, b->data, a->len)));
71 }
72
73 int rawobj_dup(rawobj_t *dest, rawobj_t *src)
74 {
75         LASSERT(src && dest);
76
77         dest->len = src->len;
78         if (dest->len) {
79                 OBD_ALLOC(dest->data, dest->len);
80                 if (!dest->data) {
81                         dest->len = 0;
82                         return -ENOMEM;
83                 }
84                 memcpy(dest->data, src->data, dest->len);
85         } else
86                 dest->data = NULL;
87         return 0;
88 }
89
90 int rawobj_serialize(rawobj_t *obj, __u32 **buf, __u32 *buflen)
91 {
92         __u32 len;
93
94         LASSERT(obj);
95         LASSERT(buf);
96         LASSERT(buflen);
97
98         len = size_round4(obj->len);
99
100         if (*buflen < 4 + len) {
101                 CERROR("buflen %u <  %u\n", *buflen, 4 + len);
102                 return -EINVAL;
103         }
104
105         *(*buf)++ = cpu_to_le32(obj->len);
106         memcpy(*buf, obj->data, obj->len);
107         *buf += (len >> 2);
108         *buflen -= (4 + len);
109
110         return 0;
111 }
112
113 static int __rawobj_extract(rawobj_t *obj, __u32 **buf, __u32 *buflen,
114                             int alloc, int local)
115 {
116         __u32 len;
117
118         if (*buflen < sizeof(__u32)) {
119                 CERROR("buflen %u\n", *buflen);
120                 return -EINVAL;
121         }
122
123         obj->len = *(*buf)++;
124         if (!local)
125                 obj->len = le32_to_cpu(obj->len);
126         *buflen -= sizeof(__u32);
127
128         if (!obj->len) {
129                 obj->data = NULL;
130                 return 0;
131         }
132
133         len = local ? obj->len : size_round4(obj->len);
134         if (*buflen < len) {
135                 CERROR("buflen %u < %u\n", *buflen, len);
136                 obj->len = 0;
137                 return -EINVAL;
138         }
139
140         if (!alloc)
141                 obj->data = (__u8 *) *buf;
142         else {
143                 OBD_ALLOC(obj->data, obj->len);
144                 if (!obj->data) {
145                         CERROR("fail to alloc %u bytes\n", obj->len);
146                         obj->len = 0;
147                         return -ENOMEM;
148                 }
149                 memcpy(obj->data, *buf, obj->len);
150         }
151
152         *((char **)buf) += len;
153         *buflen -= len;
154
155         return 0;
156 }
157
158 int rawobj_extract(rawobj_t *obj, __u32 **buf, __u32 *buflen)
159 {
160         return __rawobj_extract(obj, buf, buflen, 0, 0);
161 }
162
163 int rawobj_extract_alloc(rawobj_t *obj, __u32 **buf, __u32 *buflen)
164 {
165         return __rawobj_extract(obj, buf, buflen, 1, 0);
166 }
167
168 int rawobj_extract_local(rawobj_t *obj, __u32 **buf, __u32 *buflen)
169 {
170         return __rawobj_extract(obj, buf, buflen, 0, 1);
171 }
172
173 int rawobj_from_netobj(rawobj_t *rawobj, netobj_t *netobj)
174 {
175         rawobj->len = netobj->len;
176         rawobj->data = netobj->data;
177         return 0;
178 }
179
180 int rawobj_from_netobj_alloc(rawobj_t *rawobj, netobj_t *netobj)
181 {
182         rawobj->len = 0;
183         rawobj->data = NULL;
184
185         if (netobj->len == 0)
186                 return 0;
187                 
188         OBD_ALLOC(rawobj->data, netobj->len);
189         if (rawobj->data == NULL)
190                 return -ENOMEM;
191
192         rawobj->len = netobj->len;
193         memcpy(rawobj->data, netobj->data, netobj->len);
194         return 0;
195 }