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_empty(rawobj_t *obj)
39 LASSERT(equi(obj->len, obj->data));
40 return (obj->len == 0);
43 int rawobj_alloc(rawobj_t *obj, char *buf, int len)
50 OBD_ALLOC(obj->data, len);
55 memcpy(obj->data, buf, len);
61 void rawobj_free(rawobj_t *obj)
67 OBD_FREE(obj->data, obj->len);
74 int rawobj_equal(rawobj_t *a, rawobj_t *b)
78 return (a->len == b->len &&
79 (!a->len || !memcmp(a->data, b->data, a->len)));
82 int rawobj_dup(rawobj_t *dest, rawobj_t *src)
88 OBD_ALLOC(dest->data, dest->len);
93 memcpy(dest->data, src->data, dest->len);
99 int rawobj_serialize(rawobj_t *obj, __u32 **buf, __u32 *buflen)
107 len = size_round4(obj->len);
109 if (*buflen < 4 + len) {
110 CERROR("buflen %u < %u\n", *buflen, 4 + len);
114 *(*buf)++ = cpu_to_le32(obj->len);
115 memcpy(*buf, obj->data, obj->len);
117 *buflen -= (4 + len);
122 static int __rawobj_extract(rawobj_t *obj, __u32 **buf, __u32 *buflen,
123 int alloc, int local)
127 if (*buflen < sizeof(__u32)) {
128 CERROR("buflen %u\n", *buflen);
132 obj->len = *(*buf)++;
134 obj->len = le32_to_cpu(obj->len);
135 *buflen -= sizeof(__u32);
142 len = local ? obj->len : size_round4(obj->len);
144 CERROR("buflen %u < %u\n", *buflen, len);
150 obj->data = (__u8 *) *buf;
152 OBD_ALLOC(obj->data, obj->len);
154 CERROR("fail to alloc %u bytes\n", obj->len);
158 memcpy(obj->data, *buf, obj->len);
161 *((char **)buf) += len;
167 int rawobj_extract(rawobj_t *obj, __u32 **buf, __u32 *buflen)
169 return __rawobj_extract(obj, buf, buflen, 0, 0);
172 int rawobj_extract_alloc(rawobj_t *obj, __u32 **buf, __u32 *buflen)
174 return __rawobj_extract(obj, buf, buflen, 1, 0);
177 int rawobj_extract_local(rawobj_t *obj, __u32 **buf, __u32 *buflen)
179 return __rawobj_extract(obj, buf, buflen, 0, 1);
182 int rawobj_extract_local_alloc(rawobj_t *obj, __u32 **buf, __u32 *buflen)
184 return __rawobj_extract(obj, buf, buflen, 1, 1);
187 int rawobj_from_netobj(rawobj_t *rawobj, netobj_t *netobj)
189 rawobj->len = netobj->len;
190 rawobj->data = netobj->data;
194 int rawobj_from_netobj_alloc(rawobj_t *rawobj, netobj_t *netobj)
199 if (netobj->len == 0)
202 OBD_ALLOC(rawobj->data, netobj->len);
203 if (rawobj->data == NULL)
206 rawobj->len = netobj->len;
207 memcpy(rawobj->data, netobj->data, netobj->len);
211 /****************************************
213 ****************************************/
215 int buffer_extract_bytes(const void **buf, __u32 *buflen,
216 void *res, __u32 reslen)
218 if (*buflen < reslen) {
219 CERROR("buflen %u < %u\n", *buflen, reslen);
223 memcpy(res, *buf, reslen);