1/*
2 * Copyright (c) 2011-12 Apple Inc. All Rights Reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23/*
24 * Copyright (c) 1997 - 2001 Kungliga Tekniska Högskolan
25 * (Royal Institute of Technology, Stockholm, Sweden).
26 * All rights reserved.
27 *
28 * Redistribution and use in source and binary forms, with or without
29 * modification, are permitted provided that the following conditions
30 * are met:
31 *
32 * 1. Redistributions of source code must retain the above copyright
33 *    notice, this list of conditions and the following disclaimer.
34 *
35 * 2. Redistributions in binary form must reproduce the above copyright
36 *    notice, this list of conditions and the following disclaimer in the
37 *    documentation and/or other materials provided with the distribution.
38 *
39 * 3. Neither the name of the Institute nor the names of its contributors
40 *    may be used to endorse or promote products derived from this software
41 *    without specific prior written permission.
42 *
43 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
44 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
45 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
46 * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
47 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
48 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
49 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
50 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
51 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
52 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
53 * SUCH DAMAGE.
54 */
55
56#include "ossl-config.h"
57
58#include <stdio.h>
59#include <ctype.h>
60#include <stdlib.h>
61#include <string.h>
62
63/* #include "rk-roken.h" */
64#include <parse_units.h>
65
66#ifndef max
67#define	max(x, y)	((x) > (y) ? (x) : (y))
68#endif
69
70/*
71 * Parse string in `s' according to `units' and return value.
72 * def_unit defines the default unit.
73 */
74
75static int
76parse_something (const char *s, const struct units *units,
77		 const char *def_unit,
78		 int (*func)(int res, int val, unsigned mult),
79		 int init,
80		 int accept_no_val_p)
81{
82    const char *p;
83    int res = init;
84    unsigned def_mult = 1;
85
86    if (def_unit != NULL) {
87	const struct units *u;
88
89	for (u = units; u->name; ++u) {
90	    if (strcasecmp (u->name, def_unit) == 0) {
91		def_mult = u->mult;
92		break;
93	    }
94	}
95	if (u->name == NULL)
96	    return -1;
97    }
98
99    p = s;
100    while (*p) {
101	int val;
102	char *next;
103	const struct units *u, *partial_unit;
104	size_t u_len;
105	unsigned partial;
106	int no_val_p = 0;
107
108	while(isspace((unsigned char)*p) || *p == ',')
109	    ++p;
110
111	val = strtol(p, &next, 0);
112	if (p == next) {
113	    val = 0;
114	    if(!accept_no_val_p)
115		return -1;
116	    no_val_p = 1;
117	}
118	p = next;
119	while (isspace((unsigned char)*p))
120	    ++p;
121	if (*p == '\0') {
122	    res = (*func)(res, val, def_mult);
123	    if (res < 0)
124		return res;
125	    break;
126	} else if (*p == '+') {
127	    ++p;
128	    val = 1;
129	} else if (*p == '-') {
130	    ++p;
131	    val = -1;
132	}
133	if (no_val_p && val == 0)
134	    val = 1;
135	u_len = strcspn (p, ", \t");
136	partial = 0;
137	partial_unit = NULL;
138	if (u_len > 1 && p[u_len - 1] == 's')
139	    --u_len;
140	for (u = units; u->name; ++u) {
141	    if (strncasecmp (p, u->name, u_len) == 0) {
142		if (u_len == strlen (u->name)) {
143		    p += u_len;
144		    res = (*func)(res, val, u->mult);
145		    if (res < 0)
146			return res;
147		    break;
148		} else {
149		    ++partial;
150		    partial_unit = u;
151		}
152	    }
153	}
154	if (u->name == NULL) {
155	    if (partial == 1) {
156		p += u_len;
157		res = (*func)(res, val, partial_unit->mult);
158		if (res < 0)
159		    return res;
160	    } else {
161		return -1;
162	    }
163	}
164	if (*p == 's')
165	    ++p;
166    }
167    return res;
168}
169
170/*
171 * The string consists of a sequence of `n unit'
172 */
173
174static int
175acc_units(int res, int val, unsigned mult)
176{
177    return res + val * mult;
178}
179
180ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL
181parse_units (const char *s, const struct units *units,
182	     const char *def_unit)
183{
184    return parse_something (s, units, def_unit, acc_units, 0, 0);
185}
186
187/*
188 * The string consists of a sequence of `[+-]flag'.  `orig' consists
189 * the original set of flags, those are then modified and returned as
190 * the function value.
191 */
192
193static int
194acc_flags(int res, int val, unsigned mult)
195{
196    if(val == 1)
197	return res | mult;
198    else if(val == -1)
199	return res & ~mult;
200    else if (val == 0)
201	return mult;
202    else
203	return -1;
204}
205
206ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL
207parse_flags (const char *s, const struct units *units,
208	     int orig)
209{
210    return parse_something (s, units, NULL, acc_flags, orig, 1);
211}
212
213/*
214 * Return a string representation according to `units' of `num' in `s'
215 * with maximum length `len'.  The actual length is the function value.
216 */
217
218static int
219unparse_something (int num, const struct units *units, char *s, size_t len,
220		   int (*print) (char *, size_t, int, const char *, int),
221		   int (*update) (int, unsigned),
222		   const char *zero_string)
223{
224    const struct units *u;
225    int ret = 0, tmp;
226
227    if (num == 0)
228	return snprintf (s, len, "%s", zero_string);
229
230    for (u = units; num > 0 && u->name; ++u) {
231	int divisor;
232
233	divisor = num / u->mult;
234	if (divisor) {
235	    num = (*update) (num, u->mult);
236	    tmp = (*print) (s, len, divisor, u->name, num);
237	    if (tmp < 0)
238		return tmp;
239	    if (tmp > (int) len) {
240		len = 0;
241		s = NULL;
242	    } else {
243		len -= tmp;
244		s += tmp;
245	    }
246	    ret += tmp;
247	}
248    }
249    return ret;
250}
251
252static int
253print_unit (char *s, size_t len, int divisor, const char *name, int rem)
254{
255    return snprintf (s, len, "%u %s%s%s",
256		     divisor, name,
257		     divisor == 1 ? "" : "s",
258		     rem > 0 ? " " : "");
259}
260
261static int
262update_unit (int in, unsigned mult)
263{
264    return in % mult;
265}
266
267static int
268update_unit_approx (int in, unsigned mult)
269{
270    if (in / mult > 0)
271	return 0;
272    else
273	return update_unit (in, mult);
274}
275
276ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL
277unparse_units (int num, const struct units *units, char *s, size_t len)
278{
279    return unparse_something (num, units, s, len,
280			      print_unit,
281			      update_unit,
282			      "0");
283}
284
285ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL
286unparse_units_approx (int num, const struct units *units, char *s, size_t len)
287{
288    return unparse_something (num, units, s, len,
289			      print_unit,
290			      update_unit_approx,
291			      "0");
292}
293
294ROKEN_LIB_FUNCTION void ROKEN_LIB_CALL
295print_units_table (const struct units *units, FILE *f)
296{
297    const struct units *u, *u2;
298    size_t max_sz = 0;
299
300    for (u = units; u->name; ++u) {
301	max_sz = max(max_sz, strlen(u->name));
302    }
303
304    for (u = units; u->name;) {
305	char buf[1024];
306	const struct units *next;
307
308	for (next = u + 1; next->name && next->mult == u->mult; ++next)
309	    ;
310
311	if (next->name) {
312	    for (u2 = next;
313		 u2->name && u->mult % u2->mult != 0;
314		 ++u2)
315		;
316	    if (u2->name == NULL)
317		--u2;
318	    unparse_units (u->mult, u2, buf, sizeof(buf));
319	    fprintf (f, "1 %*s = %s\n", (int)max_sz, u->name, buf);
320	} else {
321	    fprintf (f, "1 %s\n", u->name);
322	}
323	u = next;
324    }
325}
326
327static int
328print_flag (char *s, size_t len, int divisor, const char *name, int rem)
329{
330    return snprintf (s, len, "%s%s", name, rem > 0 ? ", " : "");
331}
332
333static int
334update_flag (int in, unsigned mult)
335{
336    return in - mult;
337}
338
339ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL
340unparse_flags (int num, const struct units *units, char *s, size_t len)
341{
342    return unparse_something (num, units, s, len,
343			      print_flag,
344			      update_flag,
345			      "");
346}
347
348ROKEN_LIB_FUNCTION void ROKEN_LIB_CALL
349print_flags_table (const struct units *units, FILE *f)
350{
351    const struct units *u;
352
353    for(u = units; u->name; ++u)
354	fprintf(f, "%s%s", u->name, (u+1)->name ? ", " : "\n");
355}
356