4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.gnu.org/licenses/gpl-2.0.html
23 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2011, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
31 * lustre/ptlrpc/gss/gss_rawobj.c
33 * Author: Eric Mei <ericm@clusterfs.com>
36 #define DEBUG_SUBSYSTEM S_SEC
38 #include <linux/mutex.h>
41 #include <obd_class.h>
42 #include <obd_support.h>
43 #include <lustre_sec.h>
45 #include "gss_internal.h"
47 int rawobj_empty(rawobj_t *obj)
49 LASSERT(equi(obj->len, obj->data));
50 return (obj->len == 0);
53 int rawobj_alloc(rawobj_t *obj, char *buf, int len)
60 OBD_ALLOC_LARGE(obj->data, len);
65 memcpy(obj->data, buf, len);
71 void rawobj_free(rawobj_t *obj)
77 OBD_FREE_LARGE(obj->data, obj->len);
84 int rawobj_equal(rawobj_t *a, rawobj_t *b)
88 return (a->len == b->len &&
89 (!a->len || !memcmp(a->data, b->data, a->len)));
92 int rawobj_dup(rawobj_t *dest, rawobj_t *src)
98 OBD_ALLOC_LARGE(dest->data, dest->len);
103 memcpy(dest->data, src->data, dest->len);
109 int rawobj_serialize(rawobj_t *obj, __u32 **buf, __u32 *buflen)
117 len = cfs_size_round4(obj->len);
119 if (*buflen < 4 + len) {
120 CERROR("buflen %u < %u\n", *buflen, 4 + len);
124 *(*buf)++ = cpu_to_le32(obj->len);
125 memcpy(*buf, obj->data, obj->len);
127 *buflen -= (4 + len);
132 static int __rawobj_extract(rawobj_t *obj, __u32 **buf, __u32 *buflen,
133 int alloc, int local)
137 if (*buflen < sizeof(__u32)) {
138 CERROR("buflen %u\n", *buflen);
142 obj->len = *(*buf)++;
144 obj->len = le32_to_cpu(obj->len);
145 *buflen -= sizeof(__u32);
152 len = local ? obj->len : cfs_size_round4(obj->len);
154 CERROR("buflen %u < %u\n", *buflen, len);
160 obj->data = (__u8 *) *buf;
162 OBD_ALLOC_LARGE(obj->data, obj->len);
164 CERROR("fail to alloc %u bytes\n", obj->len);
168 memcpy(obj->data, *buf, obj->len);
171 *((char **)buf) += len;
177 int rawobj_extract(rawobj_t *obj, __u32 **buf, __u32 *buflen)
179 return __rawobj_extract(obj, buf, buflen, 0, 0);
182 int rawobj_extract_alloc(rawobj_t *obj, __u32 **buf, __u32 *buflen)
184 return __rawobj_extract(obj, buf, buflen, 1, 0);
187 int rawobj_extract_local(rawobj_t *obj, __u32 **buf, __u32 *buflen)
189 return __rawobj_extract(obj, buf, buflen, 0, 1);
192 int rawobj_extract_local_alloc(rawobj_t *obj, __u32 **buf, __u32 *buflen)
194 return __rawobj_extract(obj, buf, buflen, 1, 1);
197 int rawobj_from_netobj(rawobj_t *rawobj, netobj_t *netobj)
199 rawobj->len = netobj->len;
200 rawobj->data = netobj->data;
204 int rawobj_from_netobj_alloc(rawobj_t *rawobj, netobj_t *netobj)
209 if (netobj->len == 0)
212 OBD_ALLOC_LARGE(rawobj->data, netobj->len);
213 if (rawobj->data == NULL)
216 rawobj->len = netobj->len;
217 memcpy(rawobj->data, netobj->data, netobj->len);
221 /****************************************
223 ****************************************/
225 int buffer_extract_bytes(const void **buf, __u32 *buflen,
226 void *res, __u32 reslen)
228 if (*buflen < reslen) {
229 CERROR("buflen %u < %u\n", *buflen, reslen);
233 memcpy(res, *buf, reslen);