Refactor
[mech_eap.orig] / util_buffer.c
1 /*
2  * Copyright (c) 2010, JANET(UK)
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  *
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * 3. Neither the name of JANET(UK) nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  */
32
33 #include "gssapiP_eap.h"
34
35 OM_uint32
36 makeStringBuffer(OM_uint32 *minor,
37                  const char *string,
38                  gss_buffer_t buffer)
39 {
40     size_t len = strlen(string);
41
42     buffer->value = GSSEAP_MALLOC(len + 1);
43     if (buffer->value == NULL) {
44         *minor = ENOMEM;
45         return GSS_S_FAILURE;
46     }
47     memcpy(buffer->value, string, len + 1);
48     buffer->length = len;
49
50     *minor = 0;
51     return GSS_S_COMPLETE;
52 }
53
54 OM_uint32
55 bufferToString(OM_uint32 *minor,
56                const gss_buffer_t buffer,
57                char **pString)
58 {
59     char *s;
60
61     s = GSSEAP_MALLOC(buffer->length + 1);
62     if (s == NULL) {
63         *minor = ENOMEM;
64         return GSS_S_FAILURE;
65     }
66     memcpy(s, buffer->value, buffer->length);
67     s[buffer->length] = '\0';
68
69     *pString = s;
70
71     *minor = 0;
72     return GSS_S_COMPLETE;
73 }
74
75 OM_uint32
76 duplicateBuffer(OM_uint32 *minor,
77                 const gss_buffer_t src,
78                 gss_buffer_t dst)
79 {
80     dst->length = 0;
81     dst->value = NULL;
82
83     if (src == GSS_C_NO_BUFFER)
84         return GSS_S_COMPLETE;
85
86     dst->value = GSSEAP_MALLOC(src->length + 1);
87     if (dst->value == NULL) {
88         *minor = ENOMEM;
89         return GSS_S_FAILURE;
90     }
91
92     dst->length = src->length;
93     memcpy(dst->value, src->value, dst->length);
94
95     ((unsigned char *)dst->value)[dst->length] = '\0';
96
97     *minor = 0;
98     return GSS_S_COMPLETE;
99 }