2 * Copyright (C) 2001-2003 by NBMK Encryption Technologies.
5 * NBMK Encryption Technologies provides no support of any kind for
6 * this software. Questions or concerns about it may be addressed to
7 * the members of the relevant open-source community at
8 * <tech-crypto@netbsd.org>.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions are
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above
18 * copyright notice, this list of conditions and the following
19 * disclaimer in the documentation and/or other materials provided
20 * with the distribution.
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
25 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
26 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
28 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
32 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 static char const n8_id
[] = "$Id: n8_user_util.c,v 1.1 2008/10/30 12:02:15 darran Exp $";
36 /*****************************************************************************/
37 /** @file n8_user_util.c
38 * @brief Utility functions that are only appropriate for user space.
40 * Some user-space utilities either don't make sense for inclusion in the kernel
41 * builds or rely on functions that are not available in the kernel.
43 *****************************************************************************/
45 /*****************************************************************************
47 * 06/05/03 brr Added functions N8_DeleteSizedBuffer & N8_DeleteBuffer.
48 * 05/09/02 bac Original version.
49 ****************************************************************************/
50 /** @defgroup user_util User-space only utilities
54 #include "n8_pub_buffer.h"
57 /*****************************************************************************
58 * N8_CreateSizedBufferFromATTR
59 *****************************************************************************/
60 /** @ingroup user_util
61 * @brief Create a sized buffer given data in an ATTR structure
63 * @param buf_p RW: pointer to the buffer to be created
64 * @param attr_p RO: pointer to the ATTR data
73 * N8_STATUS_OK, N8_MALLOC_FAILED
77 *****************************************************************************/
78 N8_Status_t
N8_CreateSizedBufferFromATTR(N8_SizedBuffer_t
*buf_p
,
81 N8_Status_t ret
= N8_STATUS_OK
;
87 PKDIGITS_TO_BYTES(attr_p
->length
* SIMON_MULTIPLIER
);
88 /* the data in an ATTR structure may be zero-padded to the left. these
89 * need to be stripped. find the first non-zero byte and start copying
93 while (i
< len
&& attr_p
->value
[i
] == 0x0)
97 buf_p
->lengthBytes
= len
- i
;
98 CREATE_UOBJECT_SIZE(buf_p
->value_p
,
101 if (buf_p
->lengthBytes
== 0)
103 ret
= N8_INVALID_VALUE
;
106 memcpy(buf_p
->value_p
, &attr_p
->value
[i
], buf_p
->lengthBytes
);
109 } /* N8_CreateSizedBufferFromATTR */
111 /*****************************************************************************
112 * N8_CreateSizedBufferFromString
113 *****************************************************************************/
114 /** @ingroup user_util
115 * @brief Create a sized buffer given a string of hex digits
117 * @param str_p RO: input string
118 * @param buf_p RW: output buffer
127 * N8_STATUS_OK, N8_MALLOC_FAILED
130 * Input string only contains hex characters ([0-9][a-f][A-F])
131 *****************************************************************************/
132 N8_Status_t
N8_CreateSizedBufferFromString(const char *str_p
,
133 N8_SizedBuffer_t
*buf_p
)
135 N8_Status_t ret
= N8_STATUS_OK
;
138 buf_p
->lengthBytes
= (strlen(str_p
)+1) / 2;
139 buf_p
->value_p
= N8_CreateBuffer(str_p
);
140 if (buf_p
->value_p
== NULL
)
142 ret
= N8_MALLOC_FAILED
;
147 ret
= N8_INVALID_OBJECT
;
150 } /* N8_CreateSizedBufferFromString */
152 /*****************************************************************************
153 * N8_DeleteSizedBuffer
154 *****************************************************************************/
155 /** @ingroup user_util
156 * @brief Delete a buffer allocated by N8_CreateSizeBuffer....
158 * @param buf_p RO: buffer
167 * N8_STATUS_OK, N8_INVALID_OBJECT
170 *****************************************************************************/
171 N8_Status_t
N8_DeleteSizedBuffer(N8_SizedBuffer_t
*buf_p
)
173 N8_Status_t ret
= N8_STATUS_OK
;
176 N8_UFREE(buf_p
->value_p
);
180 ret
= N8_INVALID_OBJECT
;
183 } /* N8_DeleteSizedBuffer */
185 /*****************************************************************************
187 *****************************************************************************/
188 /** @ingroup user_util
189 * @brief Allocate and return a N8_Buffer_t of hex values given an input string
190 * representing hex digits.
192 * @param e RO: input string
198 * Pointer to created buffer. NULL if the malloc failed.
204 * Input string only contains hex characters ([0-9][a-f][A-F])
205 *****************************************************************************/
206 N8_Buffer_t
*N8_CreateBuffer(const char *e
)
212 N8_Buffer_t
*expected
;
215 expected
= (N8_Buffer_t
*) N8_UMALLOC(size
);
217 if (expected
== NULL
)
221 for (i
= 0; i
< len
; i
+=2)
223 sscanf(&e
[i
], "%2x", &temp
);
224 expected
[j
++] = (unsigned char) (temp
& 0xff);
227 } /* N8_CreateBuffer */
229 /*****************************************************************************
231 *****************************************************************************/
232 /** @ingroup user_util
233 * @brief Delete a buffer allocated by N8_CreateBuffer
235 * @param buf_p RO: buffer
244 * N8_STATUS_OK, N8_INVALID_OBJECT
247 *****************************************************************************/
248 N8_Status_t
N8_DeleteBuffer(N8_Buffer_t
*buf_p
)
250 N8_Status_t ret
= N8_STATUS_OK
;
257 ret
= N8_INVALID_OBJECT
;
260 } /* N8_DeleteBuffer */