1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * Copyright (C) 2004 Cluster File Systems, Inc.
5 * Author: Eric Mei <ericm@clusterfs.com>
7 * This file is part of Lustre, http://www.lustre.org.
9 * Lustre is free software; you can redistribute it and/or
10 * modify it under the terms of version 2 of the GNU General Public
11 * License as published by the Free Software Foundation.
13 * Lustre is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with Lustre; if not, write to the Free Software
20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 # define EXPORT_SYMTAB
26 #define DEBUG_SUBSYSTEM S_SEC
28 #include <linux/mutex.h>
31 #include <obd_class.h>
32 #include <obd_support.h>
33 #include <lustre_sec.h>
35 #include "gss_internal.h"
37 int rawobj_alloc(rawobj_t *obj, char *buf, int len)
44 OBD_ALLOC(obj->data, len);
49 memcpy(obj->data, buf, len);
55 void rawobj_free(rawobj_t *obj)
61 OBD_FREE(obj->data, obj->len);
68 int rawobj_equal(rawobj_t *a, rawobj_t *b)
72 return (a->len == b->len &&
73 (!a->len || !memcmp(a->data, b->data, a->len)));
76 int rawobj_dup(rawobj_t *dest, rawobj_t *src)
82 OBD_ALLOC(dest->data, dest->len);
87 memcpy(dest->data, src->data, dest->len);
93 int rawobj_serialize(rawobj_t *obj, __u32 **buf, __u32 *buflen)
101 len = size_round4(obj->len);
103 if (*buflen < 4 + len) {
104 CERROR("buflen %u < %u\n", *buflen, 4 + len);
108 *(*buf)++ = cpu_to_le32(obj->len);
109 memcpy(*buf, obj->data, obj->len);
111 *buflen -= (4 + len);
116 static int __rawobj_extract(rawobj_t *obj, __u32 **buf, __u32 *buflen,
117 int alloc, int local)
121 if (*buflen < sizeof(__u32)) {
122 CERROR("buflen %u\n", *buflen);
126 obj->len = *(*buf)++;
128 obj->len = le32_to_cpu(obj->len);
129 *buflen -= sizeof(__u32);
136 len = local ? obj->len : size_round4(obj->len);
138 CERROR("buflen %u < %u\n", *buflen, len);
144 obj->data = (__u8 *) *buf;
146 OBD_ALLOC(obj->data, obj->len);
148 CERROR("fail to alloc %u bytes\n", obj->len);
152 memcpy(obj->data, *buf, obj->len);
155 *((char **)buf) += len;
161 int rawobj_extract(rawobj_t *obj, __u32 **buf, __u32 *buflen)
163 return __rawobj_extract(obj, buf, buflen, 0, 0);
166 int rawobj_extract_alloc(rawobj_t *obj, __u32 **buf, __u32 *buflen)
168 return __rawobj_extract(obj, buf, buflen, 1, 0);
171 int rawobj_extract_local(rawobj_t *obj, __u32 **buf, __u32 *buflen)
173 return __rawobj_extract(obj, buf, buflen, 0, 1);
176 int rawobj_extract_local_alloc(rawobj_t *obj, __u32 **buf, __u32 *buflen)
178 return __rawobj_extract(obj, buf, buflen, 1, 1);
181 int rawobj_from_netobj(rawobj_t *rawobj, netobj_t *netobj)
183 rawobj->len = netobj->len;
184 rawobj->data = netobj->data;
188 int rawobj_from_netobj_alloc(rawobj_t *rawobj, netobj_t *netobj)
193 if (netobj->len == 0)
196 OBD_ALLOC(rawobj->data, netobj->len);
197 if (rawobj->data == NULL)
200 rawobj->len = netobj->len;
201 memcpy(rawobj->data, netobj->data, netobj->len);
205 /****************************************
207 ****************************************/
209 int buffer_extract_bytes(const void **buf, __u32 *buflen,
210 void *res, __u32 reslen)
212 if (*buflen < reslen) {
213 CERROR("buflen %u < %u\n", *buflen, reslen);
217 memcpy(res, *buf, reslen);