No empty .Rs/.Re
[netbsd-mini2440.git] / sys / dev / pci / n8 / common / api / n8_user_util.c
blob4d027c72748c4a097c9ba93b6ef1d05e34434928
1 /*-
2 * Copyright (C) 2001-2003 by NBMK Encryption Technologies.
3 * All rights reserved.
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>.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions are
12 * met:
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 /*****************************************************************************
46 * Revision history:
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
53 #include <stdio.h>
54 #include "n8_pub_buffer.h"
55 #include "n8_util.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
66 * @par Externals
67 * None
69 * @return
70 * status
72 * @par Errors
73 * N8_STATUS_OK, N8_MALLOC_FAILED
75 * @par Assumptions
76 * None
77 *****************************************************************************/
78 N8_Status_t N8_CreateSizedBufferFromATTR(N8_SizedBuffer_t *buf_p,
79 const ATTR *attr_p)
81 N8_Status_t ret = N8_STATUS_OK;
82 unsigned int len;
83 unsigned int i;
86 len =
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
90 * from there.
92 i = 0;
93 while (i < len && attr_p->value[i] == 0x0)
95 i++;
97 buf_p->lengthBytes = len - i;
98 CREATE_UOBJECT_SIZE(buf_p->value_p,
99 buf_p->lengthBytes,
100 ret);
101 if (buf_p->lengthBytes == 0)
103 ret = N8_INVALID_VALUE;
104 break;
106 memcpy(buf_p->value_p, &attr_p->value[i], buf_p->lengthBytes);
107 } while (FALSE);
108 return ret;
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
120 * @par Externals
121 * None
123 * @return
124 * Status
126 * @par Errors
127 * N8_STATUS_OK, N8_MALLOC_FAILED
129 * @par Assumptions
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;
136 if (buf_p != NULL)
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;
145 else
147 ret = N8_INVALID_OBJECT;
149 return ret;
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
160 * @par Externals
161 * None
163 * @return
164 * Status
166 * @par Errors
167 * N8_STATUS_OK, N8_INVALID_OBJECT
169 * @par Assumptions
170 *****************************************************************************/
171 N8_Status_t N8_DeleteSizedBuffer(N8_SizedBuffer_t *buf_p)
173 N8_Status_t ret = N8_STATUS_OK;
174 if (buf_p != NULL)
176 N8_UFREE(buf_p->value_p);
178 else
180 ret = N8_INVALID_OBJECT;
182 return ret;
183 } /* N8_DeleteSizedBuffer */
185 /*****************************************************************************
186 * N8_CreateBuffer
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
194 * @par Externals
195 * None
197 * @return
198 * Pointer to created buffer. NULL if the malloc failed.
200 * @par Errors
201 * None
203 * @par Assumptions
204 * Input string only contains hex characters ([0-9][a-f][A-F])
205 *****************************************************************************/
206 N8_Buffer_t *N8_CreateBuffer(const char *e)
208 int i,j;
209 unsigned int temp;
210 int len;
211 int size;
212 N8_Buffer_t *expected;
213 len = strlen(e);
214 size = (len+1) / 2;
215 expected = (N8_Buffer_t *) N8_UMALLOC(size);
217 if (expected == NULL)
218 return NULL;
220 j = 0;
221 for (i = 0; i < len; i+=2)
223 sscanf(&e[i], "%2x", &temp);
224 expected[j++] = (unsigned char) (temp & 0xff);
226 return expected;
227 } /* N8_CreateBuffer */
229 /*****************************************************************************
230 * N8_DeleteBuffer
231 *****************************************************************************/
232 /** @ingroup user_util
233 * @brief Delete a buffer allocated by N8_CreateBuffer
235 * @param buf_p RO: buffer
237 * @par Externals
238 * None
240 * @return
241 * Status
243 * @par Errors
244 * N8_STATUS_OK, N8_INVALID_OBJECT
246 * @par Assumptions
247 *****************************************************************************/
248 N8_Status_t N8_DeleteBuffer(N8_Buffer_t *buf_p)
250 N8_Status_t ret = N8_STATUS_OK;
251 if (buf_p != NULL)
253 N8_UFREE(buf_p);
255 else
257 ret = N8_INVALID_OBJECT;
259 return ret;
260 } /* N8_DeleteBuffer */