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 (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
30 * Use is subject to license terms.
32 * Copyright (c) 2011, Whamcloud, Inc.
35 * This file is part of Lustre, http://www.lustre.org/
36 * Lustre is a trademark of Sun Microsystems, Inc.
38 * lustre/ptlrpc/gss/gss_rawobj.c
40 * Author: Eric Mei <ericm@clusterfs.com>
44 # define EXPORT_SYMTAB
46 #define DEBUG_SUBSYSTEM S_SEC
48 #include <linux/mutex.h>
51 #include <obd_class.h>
52 #include <obd_support.h>
53 #include <lustre_sec.h>
55 #include "gss_internal.h"
57 int rawobj_empty(rawobj_t *obj)
59 LASSERT(equi(obj->len, obj->data));
60 return (obj->len == 0);
63 int rawobj_alloc(rawobj_t *obj, char *buf, int len)
70 OBD_ALLOC_LARGE(obj->data, len);
75 memcpy(obj->data, buf, len);
81 void rawobj_free(rawobj_t *obj)
87 OBD_FREE_LARGE(obj->data, obj->len);
94 int rawobj_equal(rawobj_t *a, rawobj_t *b)
98 return (a->len == b->len &&
99 (!a->len || !memcmp(a->data, b->data, a->len)));
102 int rawobj_dup(rawobj_t *dest, rawobj_t *src)
104 LASSERT(src && dest);
106 dest->len = src->len;
108 OBD_ALLOC_LARGE(dest->data, dest->len);
113 memcpy(dest->data, src->data, dest->len);
119 int rawobj_serialize(rawobj_t *obj, __u32 **buf, __u32 *buflen)
127 len = cfs_size_round4(obj->len);
129 if (*buflen < 4 + len) {
130 CERROR("buflen %u < %u\n", *buflen, 4 + len);
134 *(*buf)++ = cpu_to_le32(obj->len);
135 memcpy(*buf, obj->data, obj->len);
137 *buflen -= (4 + len);
142 static int __rawobj_extract(rawobj_t *obj, __u32 **buf, __u32 *buflen,
143 int alloc, int local)
147 if (*buflen < sizeof(__u32)) {
148 CERROR("buflen %u\n", *buflen);
152 obj->len = *(*buf)++;
154 obj->len = le32_to_cpu(obj->len);
155 *buflen -= sizeof(__u32);
162 len = local ? obj->len : cfs_size_round4(obj->len);
164 CERROR("buflen %u < %u\n", *buflen, len);
170 obj->data = (__u8 *) *buf;
172 OBD_ALLOC_LARGE(obj->data, obj->len);
174 CERROR("fail to alloc %u bytes\n", obj->len);
178 memcpy(obj->data, *buf, obj->len);
181 *((char **)buf) += len;
187 int rawobj_extract(rawobj_t *obj, __u32 **buf, __u32 *buflen)
189 return __rawobj_extract(obj, buf, buflen, 0, 0);
192 int rawobj_extract_alloc(rawobj_t *obj, __u32 **buf, __u32 *buflen)
194 return __rawobj_extract(obj, buf, buflen, 1, 0);
197 int rawobj_extract_local(rawobj_t *obj, __u32 **buf, __u32 *buflen)
199 return __rawobj_extract(obj, buf, buflen, 0, 1);
202 int rawobj_extract_local_alloc(rawobj_t *obj, __u32 **buf, __u32 *buflen)
204 return __rawobj_extract(obj, buf, buflen, 1, 1);
207 int rawobj_from_netobj(rawobj_t *rawobj, netobj_t *netobj)
209 rawobj->len = netobj->len;
210 rawobj->data = netobj->data;
214 int rawobj_from_netobj_alloc(rawobj_t *rawobj, netobj_t *netobj)
219 if (netobj->len == 0)
222 OBD_ALLOC_LARGE(rawobj->data, netobj->len);
223 if (rawobj->data == NULL)
226 rawobj->len = netobj->len;
227 memcpy(rawobj->data, netobj->data, netobj->len);
231 /****************************************
233 ****************************************/
235 int buffer_extract_bytes(const void **buf, __u32 *buflen,
236 void *res, __u32 reslen)
238 if (*buflen < reslen) {
239 CERROR("buflen %u < %u\n", *buflen, reslen);
243 memcpy(res, *buf, reslen);