1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 only,
10 * as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License version 2 for more details (a copy is included
16 * in the LICENSE file that accompanied this code).
18 * You should have received a copy of the GNU General Public License
19 * version 2 along with this program; If not, see
20 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
22 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23 * CA 95054 USA or visit www.sun.com if you need additional information or
29 * Copyright 2008 Sun Microsystems, Inc. All rights reserved
30 * Use is subject to license terms.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * lustre/ptlrpc/gss/gss_rawobj.c
38 * Author: Eric Mei <ericm@clusterfs.com>
42 # define EXPORT_SYMTAB
44 #define DEBUG_SUBSYSTEM S_SEC
46 #include <linux/mutex.h>
49 #include <obd_class.h>
50 #include <obd_support.h>
51 #include <lustre_sec.h>
53 #include "gss_internal.h"
55 int rawobj_empty(rawobj_t *obj)
57 LASSERT(equi(obj->len, obj->data));
58 return (obj->len == 0);
61 int rawobj_alloc(rawobj_t *obj, char *buf, int len)
68 OBD_ALLOC(obj->data, len);
73 memcpy(obj->data, buf, len);
79 void rawobj_free(rawobj_t *obj)
85 OBD_FREE(obj->data, obj->len);
92 int rawobj_equal(rawobj_t *a, rawobj_t *b)
96 return (a->len == b->len &&
97 (!a->len || !memcmp(a->data, b->data, a->len)));
100 int rawobj_dup(rawobj_t *dest, rawobj_t *src)
102 LASSERT(src && dest);
104 dest->len = src->len;
106 OBD_ALLOC(dest->data, dest->len);
111 memcpy(dest->data, src->data, dest->len);
117 int rawobj_serialize(rawobj_t *obj, __u32 **buf, __u32 *buflen)
125 len = size_round4(obj->len);
127 if (*buflen < 4 + len) {
128 CERROR("buflen %u < %u\n", *buflen, 4 + len);
132 *(*buf)++ = cpu_to_le32(obj->len);
133 memcpy(*buf, obj->data, obj->len);
135 *buflen -= (4 + len);
140 static int __rawobj_extract(rawobj_t *obj, __u32 **buf, __u32 *buflen,
141 int alloc, int local)
145 if (*buflen < sizeof(__u32)) {
146 CERROR("buflen %u\n", *buflen);
150 obj->len = *(*buf)++;
152 obj->len = le32_to_cpu(obj->len);
153 *buflen -= sizeof(__u32);
160 len = local ? obj->len : size_round4(obj->len);
162 CERROR("buflen %u < %u\n", *buflen, len);
168 obj->data = (__u8 *) *buf;
170 OBD_ALLOC(obj->data, obj->len);
172 CERROR("fail to alloc %u bytes\n", obj->len);
176 memcpy(obj->data, *buf, obj->len);
179 *((char **)buf) += len;
185 int rawobj_extract(rawobj_t *obj, __u32 **buf, __u32 *buflen)
187 return __rawobj_extract(obj, buf, buflen, 0, 0);
190 int rawobj_extract_alloc(rawobj_t *obj, __u32 **buf, __u32 *buflen)
192 return __rawobj_extract(obj, buf, buflen, 1, 0);
195 int rawobj_extract_local(rawobj_t *obj, __u32 **buf, __u32 *buflen)
197 return __rawobj_extract(obj, buf, buflen, 0, 1);
200 int rawobj_extract_local_alloc(rawobj_t *obj, __u32 **buf, __u32 *buflen)
202 return __rawobj_extract(obj, buf, buflen, 1, 1);
205 int rawobj_from_netobj(rawobj_t *rawobj, netobj_t *netobj)
207 rawobj->len = netobj->len;
208 rawobj->data = netobj->data;
212 int rawobj_from_netobj_alloc(rawobj_t *rawobj, netobj_t *netobj)
217 if (netobj->len == 0)
220 OBD_ALLOC(rawobj->data, netobj->len);
221 if (rawobj->data == NULL)
224 rawobj->len = netobj->len;
225 memcpy(rawobj->data, netobj->data, netobj->len);
229 /****************************************
231 ****************************************/
233 int buffer_extract_bytes(const void **buf, __u32 *buflen,
234 void *res, __u32 reslen)
236 if (*buflen < reslen) {
237 CERROR("buflen %u < %u\n", *buflen, reslen);
241 memcpy(res, *buf, reslen);