1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause
3 *
4 * Copyright (c) 2001 Alexey Zelkin <phantom@FreeBSD.org>
5 * All rights reserved.
6 *
7 * Copyright (c) 2011 The FreeBSD Foundation
8 *
9 * Portions of this software were developed by David Chisnall
10 * under sponsorship from the FreeBSD Foundation.
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 *
33 */
34
35 #include <sys/cdefs.h>
36 #include <sys/types.h>
37
38 #include <ctype.h>
39 #include <errno.h>
40 #include <limits.h>
41 #include <locale.h>
42 #include <monetary.h>
43 #include <stdarg.h>
44 #include <stdio.h>
45 #include <stdlib.h>
46 #include <string.h>
47
48 #include "xlocale_private.h"
49
50 /* internal flags */
51 #define NEED_GROUPING 0x01 /* print digits grouped (default) */
52 #define SIGN_POSN_USED 0x02 /* '+' or '(' usage flag */
53 #define LOCALE_POSN 0x04 /* use locale defined +/- (default) */
54 #define PARENTH_POSN 0x08 /* enclose negative amount in () */
55 #define SUPPRESS_CURR_SYMBOL 0x10 /* suppress the currency from output */
56 #define LEFT_JUSTIFY 0x20 /* left justify */
57 #define USE_INTL_CURRENCY 0x40 /* use international currency symbol */
58 #define IS_NEGATIVE 0x80 /* is argument value negative ? */
59
60 /* internal macros */
61 #define PRINT(CH) do { \
62 if (dst >= s + maxsize) \
63 goto e2big_error; \
64 *dst++ = CH; \
65 } while (0)
66
67 #define PRINTS(STR) do { \
68 char *tmps = STR; \
69 while (*tmps != '\0') \
70 PRINT(*tmps++); \
71 } while (0)
72
73 #define GET_NUMBER(VAR, LOC) do { \
74 VAR = 0; \
75 while (isdigit_l((unsigned char)*fmt, LOC)) { \
76 if (VAR > INT_MAX / 10) \
77 goto e2big_error; \
78 VAR *= 10; \
79 VAR += *fmt - '0'; \
80 if (VAR < 0) \
81 goto e2big_error; \
82 fmt++; \
83 } \
84 } while (0)
85
86 #define GRPCPY(howmany) do { \
87 int i = howmany; \
88 while (i-- > 0) { \
89 avalue_size--; \
90 *--bufend = *(avalue + avalue_size + padded); \
91 } \
92 } while (0)
93
94 #define GRPSEP do { \
95 bufend -= thousands_sep_size; \
96 memcpy(bufend, thousands_sep, thousands_sep_size); \
97 groups++; \
98 } while (0)
99
100 static void __setup_vars(int, char *, char *, char *, char **, struct lconv *);
101 static int __calc_left_pad(int, char *, struct lconv *);
102 static char *__format_grouped_double(double, int *, int, int, int,
103 struct lconv *, locale_t);
104
105 static ssize_t
vstrfmon_l(char * __restrict s,size_t maxsize,locale_t loc,const char * __restrict format,va_list ap)106 vstrfmon_l(char *__restrict s, size_t maxsize, locale_t loc,
107 const char *__restrict format, va_list ap)
108 {
109 char *dst; /* output destination pointer */
110 const char *fmt; /* current format poistion pointer */
111 struct lconv *lc; /* pointer to lconv structure */
112 char *asciivalue; /* formatted double pointer */
113
114 int flags; /* formatting options */
115 int pad_char; /* padding character */
116 int pad_size; /* pad size */
117 int width; /* field width */
118 int left_prec; /* left precision */
119 int right_prec; /* right precision */
120 double value; /* just value */
121 char space_char = ' '; /* space after currency */
122
123 char cs_precedes, /* values gathered from struct lconv */
124 sep_by_space,
125 sign_posn,
126 *signstr,
127 *currency_symbol;
128
129 char *tmpptr; /* temporary vars */
130 int sverrno;
131
132 FIX_LOCALE(loc);
133
134 lc = localeconv_l(loc);
135 dst = s;
136 fmt = format;
137 asciivalue = NULL;
138 currency_symbol = NULL;
139
140 while (*fmt != 0) {
141 /* pass nonformating characters AS IS */
142 if (*fmt != '%')
143 goto literal;
144
145 /* '%' found ! */
146
147 /* "%%" mean just '%' */
148 if (*(fmt + 1) == '%') {
149 fmt++;
150 literal:
151 PRINT(*fmt++);
152 continue;
153 }
154
155 /* set up initial values */
156 flags = NEED_GROUPING | LOCALE_POSN;
157 pad_char = ' '; /* padding character is "space" */
158 pad_size = 0; /* no padding initially */
159 left_prec = -1; /* no left precision specified */
160 right_prec = -1; /* no right precision specified */
161 width = -1; /* no width specified */
162
163 /* Flags */
164 while (1) {
165 switch (*++fmt) {
166 case '=': /* fill character */
167 pad_char = *++fmt;
168 if (pad_char == '\0')
169 goto format_error;
170 continue;
171 case '^': /* not group currency */
172 flags &= ~(NEED_GROUPING);
173 continue;
174 case '+': /* use locale defined signs */
175 if (flags & SIGN_POSN_USED)
176 goto format_error;
177 flags |= (SIGN_POSN_USED | LOCALE_POSN);
178 continue;
179 case '(': /* enclose negatives with () */
180 if (flags & SIGN_POSN_USED)
181 goto format_error;
182 flags |= (SIGN_POSN_USED | PARENTH_POSN);
183 continue;
184 case '!': /* suppress currency symbol */
185 flags |= SUPPRESS_CURR_SYMBOL;
186 continue;
187 case '-': /* alignment (left) */
188 flags |= LEFT_JUSTIFY;
189 continue;
190 default:
191 break;
192 }
193 break;
194 }
195
196 /* field Width */
197 if (isdigit_l((unsigned char)*fmt, loc)) {
198 GET_NUMBER(width, loc);
199 /*
200 * Do we have enough space to put number with
201 * required width ?
202 */
203 if ((unsigned int)width >= maxsize - (dst - s))
204 goto e2big_error;
205 }
206
207 /* Left precision */
208 if (*fmt == '#') {
209 if (!isdigit_l((unsigned char)*++fmt, loc))
210 goto format_error;
211 GET_NUMBER(left_prec, loc);
212 if ((unsigned int)left_prec >= maxsize - (dst - s))
213 goto e2big_error;
214 }
215
216 /* Right precision */
217 if (*fmt == '.') {
218 if (!isdigit_l((unsigned char)*++fmt, loc))
219 goto format_error;
220 GET_NUMBER(right_prec, loc);
221 if ((unsigned int)right_prec >=
222 maxsize - (dst - s) - left_prec)
223 goto e2big_error;
224 }
225
226 /* Conversion Characters */
227 switch (*fmt++) {
228 case 'i': /* use international currency format */
229 flags |= USE_INTL_CURRENCY;
230 break;
231 case 'n': /* use national currency format */
232 flags &= ~(USE_INTL_CURRENCY);
233 break;
234 default: /*
235 * required character is missing or
236 * premature EOS
237 */
238 goto format_error;
239 }
240
241 if (currency_symbol != NULL)
242 free(currency_symbol);
243 if (flags & USE_INTL_CURRENCY) {
244 currency_symbol = strdup(lc->int_curr_symbol);
245 if (currency_symbol != NULL &&
246 strlen(currency_symbol) > 3) {
247 space_char = currency_symbol[3];
248 currency_symbol[3] = '\0';
249 }
250 } else
251 currency_symbol = strdup(lc->currency_symbol);
252
253 if (currency_symbol == NULL)
254 goto end_error; /* ENOMEM. */
255
256 /* value itself */
257 value = va_arg(ap, double);
258
259 /* detect sign */
260 if (value < 0) {
261 flags |= IS_NEGATIVE;
262 value = -value;
263 }
264
265 /* fill left_prec with amount of padding chars */
266 if (left_prec >= 0) {
267 pad_size = __calc_left_pad((flags ^ IS_NEGATIVE),
268 currency_symbol, lc) -
269 __calc_left_pad(flags, currency_symbol, lc);
270 if (pad_size < 0)
271 pad_size = 0;
272 }
273
274 if (asciivalue != NULL)
275 free(asciivalue);
276 asciivalue = __format_grouped_double(value, &flags, left_prec,
277 right_prec, pad_char, lc, loc);
278 if (asciivalue == NULL)
279 goto end_error; /*
280 * errno already set to ENOMEM by
281 * malloc()
282 */
283
284 /* set some variables for later use */
285 __setup_vars(flags, &cs_precedes, &sep_by_space, &sign_posn,
286 &signstr, lc);
287
288 /*
289 * Description of some LC_MONETARY's values:
290 *
291 * p_cs_precedes & n_cs_precedes
292 *
293 * = 1 - $currency_symbol precedes the value
294 * for a monetary quantity with a non-negative value
295 * = 0 - symbol succeeds the value
296 *
297 * p_sep_by_space & n_sep_by_space
298 *
299 * = 0 - no space separates $currency_symbol
300 * from the value for a monetary quantity with a
301 * non-negative value
302 * = 1 - space separates the symbol from the value
303 * = 2 - space separates the symbol and the sign string,
304 * if adjacent; otherwise, a space separates
305 * the sign string from the value
306 *
307 * p_sign_posn & n_sign_posn
308 *
309 * = 0 - parentheses enclose the quantity and the
310 * $currency_symbol
311 * = 1 - the sign string precedes the quantity and the
312 * $currency_symbol
313 * = 2 - the sign string succeeds the quantity and the
314 * $currency_symbol
315 * = 3 - the sign string precedes the $currency_symbol
316 * = 4 - the sign string succeeds the $currency_symbol
317 */
318
319 tmpptr = dst;
320
321 while (pad_size-- > 0)
322 PRINT(' ');
323
324 if (sign_posn == 0 && (flags & IS_NEGATIVE))
325 PRINT('(');
326
327 if (cs_precedes == 1) {
328 if (sign_posn == 1 || sign_posn == 3) {
329 PRINTS(signstr);
330 if (sep_by_space == 2)
331 PRINT(' ');
332 }
333
334 if (!(flags & SUPPRESS_CURR_SYMBOL)) {
335 PRINTS(currency_symbol);
336
337 if (sign_posn == 4) {
338 if (sep_by_space == 2)
339 PRINT(space_char);
340 PRINTS(signstr);
341 if (sep_by_space == 1)
342 PRINT(' ');
343 } else if (sep_by_space == 1)
344 PRINT(space_char);
345 }
346 } else if (sign_posn == 1) {
347 PRINTS(signstr);
348 if (sep_by_space == 2)
349 PRINT(' ');
350 }
351
352 PRINTS(asciivalue);
353
354 if (cs_precedes == 0) {
355 if (sign_posn == 3) {
356 if (sep_by_space == 1)
357 PRINT(' ');
358 PRINTS(signstr);
359 }
360
361 if (!(flags & SUPPRESS_CURR_SYMBOL)) {
362 if ((sign_posn == 3 && sep_by_space == 2) ||
363 (sep_by_space == 1 &&
364 (sign_posn == 0 || sign_posn == 1 ||
365 sign_posn == 2 || sign_posn == 4)))
366 PRINT(space_char);
367 PRINTS(currency_symbol);
368 if (sign_posn == 4) {
369 if (sep_by_space == 2)
370 PRINT(' ');
371 PRINTS(signstr);
372 }
373 }
374 }
375
376 if (sign_posn == 2) {
377 if (sep_by_space == 2)
378 PRINT(' ');
379 PRINTS(signstr);
380 }
381
382 if (sign_posn == 0) {
383 if (flags & IS_NEGATIVE)
384 PRINT(')');
385 else if (left_prec >= 0)
386 PRINT(' ');
387 }
388
389 if (dst - tmpptr < width) {
390 if (flags & LEFT_JUSTIFY) {
391 while (dst - tmpptr < width)
392 PRINT(' ');
393 } else {
394 pad_size = dst - tmpptr;
395 memmove(tmpptr + width - pad_size, tmpptr,
396 pad_size);
397 memset(tmpptr, ' ', width - pad_size);
398 dst += width - pad_size;
399 }
400 }
401 }
402
403 PRINT('\0');
404 free(asciivalue);
405 free(currency_symbol);
406 return (dst - s - 1); /* size of put data except trailing '\0' */
407
408 e2big_error:
409 errno = E2BIG;
410 goto end_error;
411
412 format_error:
413 errno = EINVAL;
414
415 end_error:
416 sverrno = errno;
417 if (asciivalue != NULL)
418 free(asciivalue);
419 if (currency_symbol != NULL)
420 free(currency_symbol);
421 errno = sverrno;
422 return (-1);
423 }
424
425 static void
__setup_vars(int flags,char * cs_precedes,char * sep_by_space,char * sign_posn,char ** signstr,struct lconv * lc)426 __setup_vars(int flags, char *cs_precedes, char *sep_by_space, char *sign_posn,
427 char **signstr, struct lconv *lc)
428 {
429 if ((flags & IS_NEGATIVE) && (flags & USE_INTL_CURRENCY)) {
430 *cs_precedes = lc->int_n_cs_precedes;
431 *sep_by_space = lc->int_n_sep_by_space;
432 *sign_posn = (flags & PARENTH_POSN) ? 0 : lc->int_n_sign_posn;
433 *signstr = (lc->negative_sign[0] == '\0') ? "-" :
434 lc->negative_sign;
435 } else if (flags & USE_INTL_CURRENCY) {
436 *cs_precedes = lc->int_p_cs_precedes;
437 *sep_by_space = lc->int_p_sep_by_space;
438 *sign_posn = (flags & PARENTH_POSN) ? 0 : lc->int_p_sign_posn;
439 *signstr = lc->positive_sign;
440 } else if (flags & IS_NEGATIVE) {
441 *cs_precedes = lc->n_cs_precedes;
442 *sep_by_space = lc->n_sep_by_space;
443 *sign_posn = (flags & PARENTH_POSN) ? 0 : lc->n_sign_posn;
444 *signstr = (lc->negative_sign[0] == '\0') ? "-" :
445 lc->negative_sign;
446 } else {
447 *cs_precedes = lc->p_cs_precedes;
448 *sep_by_space = lc->p_sep_by_space;
449 *sign_posn = (flags & PARENTH_POSN) ? 0 : lc->p_sign_posn;
450 *signstr = lc->positive_sign;
451 }
452
453 /* Set default values for unspecified information. */
454 if (*cs_precedes != 0)
455 *cs_precedes = 1;
456 if (*sep_by_space == CHAR_MAX)
457 *sep_by_space = 0;
458 if (*sign_posn == CHAR_MAX)
459 *sign_posn = 0;
460 }
461
462 static int
__calc_left_pad(int flags,char * cur_symb,struct lconv * lc)463 __calc_left_pad(int flags, char *cur_symb, struct lconv *lc)
464 {
465 char cs_precedes, sep_by_space, sign_posn, *signstr;
466 int left_chars = 0;
467
468 __setup_vars(flags, &cs_precedes, &sep_by_space, &sign_posn, &signstr,
469 lc);
470
471 if (cs_precedes != 0) {
472 left_chars += strlen(cur_symb);
473 if (sep_by_space != 0)
474 left_chars++;
475 }
476
477 switch (sign_posn) {
478 case 0:
479 if (flags & IS_NEGATIVE)
480 left_chars++;
481 break;
482 case 1:
483 left_chars += strlen(signstr);
484 break;
485 case 3:
486 case 4:
487 if (cs_precedes != 0)
488 left_chars += strlen(signstr);
489 }
490 return (left_chars);
491 }
492
493 static int
get_groups(int size,char * grouping)494 get_groups(int size, char *grouping)
495 {
496 int chars = 0;
497
498 if (*grouping == CHAR_MAX || *grouping <= 0) /* no grouping ? */
499 return (0);
500
501 while (size > (int)*grouping) {
502 chars++;
503 size -= (int)*grouping++;
504 /* no more grouping ? */
505 if (*grouping == CHAR_MAX)
506 break;
507 /* rest grouping with same value ? */
508 if (*grouping == 0) {
509 chars += (size - 1) / *(grouping - 1);
510 break;
511 }
512 }
513 return (chars);
514 }
515
516 /* convert double to locale-encoded string */
517 static char *
__format_grouped_double(double value,int * flags,int left_prec,int right_prec,int pad_char,struct lconv * lc,locale_t loc)518 __format_grouped_double(double value, int *flags, int left_prec, int right_prec,
519 int pad_char, struct lconv *lc, locale_t loc)
520 {
521
522 char *rslt;
523 char *avalue;
524 int avalue_size;
525
526 size_t bufsize;
527 char *bufend;
528
529 int padded;
530
531 char *grouping;
532 const char *decimal_point;
533 const char *thousands_sep;
534 size_t decimal_point_size;
535 size_t thousands_sep_size;
536
537 int groups = 0;
538
539 grouping = lc->mon_grouping;
540 decimal_point = lc->mon_decimal_point;
541 if (*decimal_point == '\0')
542 decimal_point = lc->decimal_point;
543 thousands_sep = lc->mon_thousands_sep;
544 if (*thousands_sep == '\0')
545 thousands_sep = lc->thousands_sep;
546
547 decimal_point_size = strlen(decimal_point);
548 thousands_sep_size = strlen(thousands_sep);
549
550 /* fill left_prec with default value */
551 if (left_prec == -1)
552 left_prec = 0;
553
554 /* fill right_prec with default value */
555 if (right_prec == -1) {
556 if (*flags & USE_INTL_CURRENCY)
557 right_prec = lc->int_frac_digits;
558 else
559 right_prec = lc->frac_digits;
560
561 if (right_prec == CHAR_MAX) /* POSIX locale ? */
562 right_prec = 2;
563 }
564
565 if (*flags & NEED_GROUPING)
566 left_prec += get_groups(left_prec, grouping);
567
568 /* convert to string */
569 avalue_size = asprintf_l(&avalue, loc, "%*.*f",
570 left_prec + right_prec + 1, right_prec, value);
571 if (avalue_size < 0)
572 return (NULL);
573
574 /* make sure that we've enough space for result string */
575 bufsize = avalue_size * (1 + thousands_sep_size) + decimal_point_size +
576 1;
577 rslt = calloc(1, bufsize);
578 if (rslt == NULL) {
579 free(avalue);
580 return (NULL);
581 }
582 bufend = rslt + bufsize - 1; /* reserve space for trailing '\0' */
583
584 /* skip spaces at beginning */
585 padded = 0;
586 while (avalue[padded] == ' ') {
587 padded++;
588 avalue_size--;
589 }
590
591 if (right_prec > 0) {
592 bufend -= right_prec;
593 memcpy(bufend, avalue + avalue_size + padded - right_prec,
594 right_prec);
595 bufend -= decimal_point_size;
596 memcpy(bufend, decimal_point, decimal_point_size);
597 avalue_size -= (right_prec + 1);
598 }
599
600 if ((*flags & NEED_GROUPING) && thousands_sep_size > 0 &&
601 *grouping != CHAR_MAX && *grouping > 0) {
602 while (avalue_size > (int)*grouping) {
603 GRPCPY(*grouping);
604 GRPSEP;
605 grouping++;
606
607 /* no more grouping ? */
608 if (*grouping == CHAR_MAX)
609 break;
610
611 /* rest grouping with same value ? */
612 if (*grouping == 0) {
613 grouping--;
614 while (avalue_size > *grouping) {
615 GRPCPY(*grouping);
616 GRPSEP;
617 }
618 }
619 }
620 if (avalue_size != 0)
621 GRPCPY(avalue_size);
622 padded -= groups;
623 } else {
624 bufend -= avalue_size;
625 memcpy(bufend, avalue + padded, avalue_size);
626 /* decrease assumed $decimal_point */
627 if (right_prec == 0)
628 padded -= decimal_point_size;
629 }
630
631 /* do padding with pad_char */
632 if (padded > 0) {
633 bufend -= padded;
634 memset(bufend, pad_char, padded);
635 }
636
637 bufsize = rslt + bufsize - bufend;
638 memmove(rslt, bufend, bufsize);
639 free(avalue);
640 return (rslt);
641 }
642
643 ssize_t
strfmon(char * restrict s,size_t maxsize,const char * restrict format,...)644 strfmon(char *restrict s, size_t maxsize, const char *restrict format, ...)
645 {
646 ssize_t ret;
647 va_list ap;
648
649 va_start(ap, format);
650 ret = vstrfmon_l(s, maxsize, __get_locale(), format, ap);
651 va_end(ap);
652
653 return (ret);
654 }
655
656 ssize_t
strfmon_l(char * restrict s,size_t maxsize,locale_t loc,const char * restrict format,...)657 strfmon_l(char *restrict s, size_t maxsize, locale_t loc,
658 const char *restrict format, ...)
659 {
660 ssize_t ret;
661 va_list ap;
662
663 va_start(ap, format);
664 ret = vstrfmon_l(s, maxsize, loc, format, ap);
665 va_end(ap);
666
667 return (ret);
668 }
669