Avoid letting data fields overflow in output and GUI.
[pspp] / src / data / data-out.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 1997-9, 2000, 2006, 2009, 2011, 2012, 2013 Free Software Foundation, Inc.
3
4    This program is free software: you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation, either version 3 of the License, or
7    (at your option) any later version.
8
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13
14    You should have received a copy of the GNU General Public License
15    along with this program.  If not, see <http://www.gnu.org/licenses/>. */
16
17 #include <config.h>
18
19 #include "data/data-out.h"
20
21 #include <ctype.h>
22 #include <float.h>
23 #include <math.h>
24 #include <stdint.h>
25 #include <stdlib.h>
26 #include <time.h>
27 #include <unistr.h>
28
29 #include "data/calendar.h"
30 #include "data/format.h"
31 #include "data/settings.h"
32 #include "data/value.h"
33 #include "libpspp/assertion.h"
34 #include "libpspp/cast.h"
35 #include "libpspp/float-format.h"
36 #include "libpspp/i18n.h"
37 #include "libpspp/integer-format.h"
38 #include "libpspp/message.h"
39 #include "libpspp/misc.h"
40 #include "libpspp/pool.h"
41 #include "libpspp/str.h"
42
43 #include "gl/minmax.h"
44 #include "gl/c-snprintf.h"
45
46 #include "gettext.h"
47 #define _(msgid) gettext (msgid)
48 \f
49 /* A representation of a number that can be quickly rounded to
50    any desired number of decimal places (up to a specified
51    maximum). */
52 struct rounder
53   {
54     char string[64];    /* Magnitude of number with excess precision. */
55     int integer_digits; /* Number of digits before decimal point. */
56     int leading_nines;  /* Number of `9's or `.'s at start of string. */
57     int leading_zeros;  /* Number of `0's or `.'s at start of string. */
58     bool negative;      /* Is the number negative? */
59   };
60
61 static void rounder_init (struct rounder *, double number, int max_decimals);
62 static int rounder_width (const struct rounder *, int decimals,
63                           int *integer_digits, bool *negative);
64 static void rounder_format (const struct rounder *, int decimals,
65                             char *output);
66 \f
67 typedef void data_out_converter_func (const union value *,
68                                       const struct fmt_spec *,
69                                       char *);
70 #define FMT(NAME, METHOD, IMIN, OMIN, IO, CATEGORY) \
71         static data_out_converter_func output_##METHOD;
72 #include "format.def"
73
74 static bool output_decimal (const struct rounder *, const struct fmt_spec *,
75                             bool require_affixes, char *);
76 static bool output_scientific (double, const struct fmt_spec *,
77                                bool require_affixes, char *);
78
79 static double power10 (int) PURE_FUNCTION;
80 static double power256 (int) PURE_FUNCTION;
81
82 static void output_infinite (double, const struct fmt_spec *, char *);
83 static void output_missing (const struct fmt_spec *, char *);
84 static void output_overflow (const struct fmt_spec *, char *);
85 static bool output_bcd_integer (double, int digits, char *);
86 static void output_binary_integer (uint64_t, int bytes, enum integer_format,
87                                    char *);
88 static void output_hex (const void *, size_t bytes, char *);
89 \f
90
91 static data_out_converter_func *const converters[FMT_NUMBER_OF_FORMATS] =
92     {
93 #define FMT(NAME, METHOD, IMIN, OMIN, IO, CATEGORY) output_##METHOD,
94 #include "format.def"
95     };
96
97 /* Converts the INPUT value, encoded in INPUT_ENCODING, according to format
98    specification FORMAT, appending the output to OUTPUT in OUTPUT_ENCODING.
99    However, binary formats (FMT_P, FMT_PK, FMT_IB, FMT_PIB, FMT_RB) yield the
100    binary results, which may not be properly encoded for OUTPUT_ENCODING.
101
102    VALUE must be the correct width for FORMAT, that is, its width must be
103    fmt_var_width(FORMAT).
104
105    INPUT_ENCODING can normally be obtained by calling dict_get_encoding() on
106    the dictionary with which INPUT is associated.  ENCODING is only important
107    when FORMAT's type is FMT_A. */
108 void
109 data_out_recode (const union value *input, const char *input_encoding,
110                  const struct fmt_spec *format,
111                  struct string *output, const char *output_encoding)
112 {
113   assert (fmt_check_output (format));
114   if (format->type == FMT_A)
115     {
116       char *in = CHAR_CAST (char *, value_str (input, format->w));
117       char *out = recode_string (output_encoding, input_encoding,
118                                  in, format->w);
119       ds_put_cstr (output, out);
120       free (out);
121     }
122   else if (fmt_get_category (format->type) == FMT_CAT_BINARY)
123     converters[format->type] (input, format,
124                               ds_put_uninit (output, format->w));
125   else
126     {
127       char *utf8_encoded = data_out (input, input_encoding, format);
128       char *output_encoded = recode_string (output_encoding, UTF8,
129                                             utf8_encoded, -1);
130       ds_put_cstr (output, output_encoded);
131       free (output_encoded);
132       free (utf8_encoded);
133     }
134 }
135
136 static char *
137 binary_to_utf8 (const char *in, struct pool *pool)
138 {
139   uint8_t *out = pool_alloc_unaligned (pool, strlen (in) * 2 + 1);
140   uint8_t *p = out;
141
142   while (*in != '\0')
143     {
144       uint8_t byte = *in++;
145       int mblen = u8_uctomb (p, byte, 2);
146       assert (mblen > 0);
147       p += mblen;
148     }
149   *p = '\0';
150
151   return CHAR_CAST (char *, out);
152 }
153
154 /* Converts the INPUT value into a UTF-8 encoded string, according to format
155    specification FORMAT.
156
157    VALUE must be the correct width for FORMAT, that is, its width must be
158    fmt_var_width(FORMAT).
159
160    ENCODING must be the encoding of INPUT.  Normally this can be obtained by
161    calling dict_get_encoding() on the dictionary with which INPUT is
162    associated.  ENCODING is only important when FORMAT's type is FMT_A.
163
164    The return value is dynamically allocated, and must be freed by the caller.
165    If POOL is non-null, then the return value is allocated on that pool.  */
166 char *
167 data_out_pool (const union value *input, const char *encoding,
168                const struct fmt_spec *format, struct pool *pool)
169 {
170   assert (fmt_check_output (format));
171   if (format->type == FMT_A)
172     {
173       char *in = CHAR_CAST (char *, value_str (input, format->w));
174       return recode_string_pool (UTF8, encoding, in, format->w, pool);
175     }
176   else if (fmt_get_category (format->type) == FMT_CAT_BINARY)
177     {
178       char tmp[16];
179
180       assert (format->w + 1 <= sizeof tmp);
181       converters[format->type] (input, format, tmp);
182       return binary_to_utf8 (tmp, pool);
183     }
184   else
185     {
186       const struct fmt_number_style *style = settings_get_style (format->type);
187       size_t size = format->w + style->extra_bytes + 1;
188       char *output;
189
190       output = pool_alloc_unaligned (pool, size);
191       converters[format->type] (input, format, output);
192       return output;
193     }
194 }
195
196 /* Like data_out_pool(), except that for basic numeric formats (F, COMMA, DOT,
197    COLLAR, PCT, E) and custom currency formats are formatted as wide as
198    necessary to fully display the selected number of decimal places. */
199 char *
200 data_out_stretchy (const union value *input, const char *encoding,
201                    const struct fmt_spec *format, struct pool *pool)
202 {
203   struct fmt_spec wide_format;
204
205   if (fmt_get_category (format->type) & (FMT_CAT_BASIC | FMT_CAT_CUSTOM))
206     {
207       /* XXX In the common case this wastes memory for 40 bytes of mostly
208          spaces. */
209       wide_format.type = format->type;
210       wide_format.w = 40;
211       wide_format.d = format->d;
212       format = &wide_format;
213     }
214
215   return data_out_pool (input, encoding, format, pool);
216 }
217
218 char *
219 data_out (const union value *input, const char *encoding, const struct fmt_spec *format)
220 {
221   return data_out_pool (input, encoding, format, NULL);
222 }
223
224 \f
225 /* Main conversion functions. */
226
227 /* Outputs F, COMMA, DOT, DOLLAR, PCT, E, CCA, CCB, CCC, CCD, and
228    CCE formats. */
229 static void
230 output_number (const union value *input, const struct fmt_spec *format,
231                char *output)
232 {
233   double number = input->f;
234
235   if (number == SYSMIS)
236     output_missing (format, output);
237   else if (!isfinite (number))
238     output_infinite (number, format, output);
239   else
240     {
241       if (format->type != FMT_E && fabs (number) < 1.5 * power10 (format->w))
242         {
243           struct rounder r;
244           rounder_init (&r, number, format->d);
245
246           if (output_decimal (&r, format, true, output)
247               || output_scientific (number, format, true, output)
248               || output_decimal (&r, format, false, output))
249             return;
250         }
251
252       if (!output_scientific (number, format, false, output))
253         output_overflow (format, output);
254     }
255 }
256
257 /* Outputs N format. */
258 static void
259 output_N (const union value *input, const struct fmt_spec *format,
260           char *output)
261 {
262   double number = input->f * power10 (format->d);
263   if (input->f == SYSMIS || number < 0)
264     output_missing (format, output);
265   else
266     {
267       char buf[128];
268       number = fabs (round (number));
269       if (number < power10 (format->w)
270           && c_snprintf (buf, 128, "%0*.0f", format->w, number) == format->w)
271         memcpy (output, buf, format->w);
272       else
273         output_overflow (format, output);
274     }
275
276   output[format->w] = '\0';
277 }
278
279 /* Outputs Z format. */
280 static void
281 output_Z (const union value *input, const struct fmt_spec *format,
282           char *output)
283 {
284   double number = input->f * power10 (format->d);
285   char buf[128];
286   if (input->f == SYSMIS)
287     output_missing (format, output);
288   else if (fabs (number) < power10 (format->w)
289            && c_snprintf (buf, 128, "%0*.0f", format->w,
290                        fabs (round (number))) == format->w)
291     {
292       if (number < 0 && strspn (buf, "0") < format->w)
293         {
294           char *p = &buf[format->w - 1];
295           *p = "}JKLMNOPQR"[*p - '0'];
296         }
297       memcpy (output, buf, format->w);
298       output[format->w] = '\0';
299     }
300   else
301     output_overflow (format, output);
302 }
303
304 /* Outputs P format. */
305 static void
306 output_P (const union value *input, const struct fmt_spec *format,
307           char *output)
308 {
309   if (output_bcd_integer (fabs (input->f * power10 (format->d)),
310                           format->w * 2 - 1, output)
311       && input->f < 0.0)
312     output[format->w - 1] |= 0xd;
313   else
314     output[format->w - 1] |= 0xf;
315 }
316
317 /* Outputs PK format. */
318 static void
319 output_PK (const union value *input, const struct fmt_spec *format,
320            char *output)
321 {
322   output_bcd_integer (input->f * power10 (format->d), format->w * 2, output);
323 }
324
325 /* Outputs IB format. */
326 static void
327 output_IB (const union value *input, const struct fmt_spec *format,
328            char *output)
329 {
330   double number = round (input->f * power10 (format->d));
331   if (input->f == SYSMIS
332       || number >= power256 (format->w) / 2 - 1
333       || number < -power256 (format->w) / 2)
334     memset (output, 0, format->w);
335   else
336     {
337       uint64_t integer = fabs (number);
338       if (number < 0)
339         integer = -integer;
340       output_binary_integer (integer, format->w,
341                              settings_get_output_integer_format (),
342                              output);
343     }
344
345   output[format->w] = '\0';
346 }
347
348 /* Outputs PIB format. */
349 static void
350 output_PIB (const union value *input, const struct fmt_spec *format,
351             char *output)
352 {
353   double number = round (input->f * power10 (format->d));
354   if (input->f == SYSMIS
355       || number < 0 || number >= power256 (format->w))
356     memset (output, 0, format->w);
357   else
358     output_binary_integer (number, format->w,
359                            settings_get_output_integer_format (), output);
360
361   output[format->w] = '\0';
362 }
363
364 /* Outputs PIBHEX format. */
365 static void
366 output_PIBHEX (const union value *input, const struct fmt_spec *format,
367                char *output)
368 {
369   double number = round (input->f);
370   if (input->f == SYSMIS)
371     output_missing (format, output);
372   else if (input->f < 0 || number >= power256 (format->w / 2))
373     output_overflow (format, output);
374   else
375     {
376       char tmp[8];
377       output_binary_integer (number, format->w / 2, INTEGER_MSB_FIRST, tmp);
378       output_hex (tmp, format->w / 2, output);
379     }
380
381 }
382
383 /* Outputs RB format. */
384 static void
385 output_RB (const union value *input, const struct fmt_spec *format,
386            char *output)
387 {
388   double d = input->f;
389   memcpy (output, &d, format->w);
390
391   output[format->w] = '\0';
392 }
393
394 /* Outputs RBHEX format. */
395 static void
396 output_RBHEX (const union value *input, const struct fmt_spec *format,
397               char *output)
398 {
399   double d = input->f;
400
401   output_hex (&d, format->w / 2, output);
402 }
403
404 /* Outputs DATE, ADATE, EDATE, JDATE, SDATE, QYR, MOYR, WKYR,
405    DATETIME, TIME, and DTIME formats. */
406 static void
407 output_date (const union value *input, const struct fmt_spec *format,
408              char *output)
409 {
410   double number = input->f;
411   int year, month, day, yday;
412
413   const char *template = fmt_date_template (format->type, format->w);
414
415   char tmp[64];
416   char *p = tmp;
417
418   if (number == SYSMIS)
419     goto missing;
420
421   if (fmt_get_category (format->type) == FMT_CAT_DATE)
422     {
423       if (number <= 0)
424         goto missing;
425       calendar_offset_to_gregorian (number / 60. / 60. / 24.,
426                                     &year, &month, &day, &yday);
427       number = fmod (number, 60. * 60. * 24.);
428     }
429   else
430     year = month = day = yday = 0;
431
432   while (*template != '\0')
433     {
434       int excess_width;
435
436       int ch = *template;
437       int count = 1;
438       while (template[count] == ch)
439         count++;
440       template += count;
441
442       switch (ch)
443         {
444         case 'd':
445           if (count < 3)
446             p += sprintf (p, "%02d", day);
447           else
448             p += sprintf (p, "%03d", yday);
449           break;
450         case 'm':
451           if (count < 3)
452             p += sprintf (p, "%02d", month);
453           else
454             {
455               static const char *const months[12] =
456                 {
457                   "JAN", "FEB", "MAR", "APR", "MAY", "JUN",
458                   "JUL", "AUG", "SEP", "OCT", "NOV", "DEC",
459                 };
460               p = stpcpy (p, months[month - 1]);
461             }
462           break;
463         case 'y':
464           if (count >= 4)
465             {
466               if (year <= 9999)
467                 p += sprintf (p, "%04d", year);
468               else if (format->type == FMT_DATETIME)
469                 p = stpcpy (p, "****");
470               else
471                 goto overflow;
472             }
473           else
474             {
475               int epoch =  settings_get_epoch ();
476               int offset = year - epoch;
477               if (offset < 0 || offset > 99)
478                 goto overflow;
479               p += sprintf (p, "%02d", abs (year) % 100);
480             }
481           break;
482         case 'q':
483           p += sprintf (p, "%d", (month - 1) / 3 + 1);
484           break;
485         case 'w':
486           p += sprintf (p, "%2d", (yday - 1) / 7 + 1);
487           break;
488         case 'D':
489           if (number < 0)
490             *p++ = '-';
491           number = fabs (number);
492           p += c_snprintf (p, 64, "%*.0f", count, floor (number / 60. / 60. / 24.));
493           number = fmod (number, 60. * 60. * 24.);
494           break;
495         case 'H':
496           if (number < 0)
497             *p++ = '-';
498           number = fabs (number);
499           p += c_snprintf (p, 64, "%0*.0f", count, floor (number / 60. / 60.));
500           number = fmod (number, 60. * 60.);
501           break;
502         case 'M':
503           p += sprintf (p, "%02d", (int) floor (number / 60.));
504           number = fmod (number, 60.);
505           excess_width = format->w - (p - tmp);
506           if (excess_width < 0)
507             goto overflow;
508           if (excess_width == 3 || excess_width == 4
509               || (excess_width >= 5 && format->d == 0))
510             p += sprintf (p, ":%02d", (int) number);
511           else if (excess_width >= 5)
512             {
513               int d = MIN (format->d, excess_width - 4);
514               int w = d + 3;
515               c_snprintf (p, 64, ":%0*.*f", w, d, number);
516               if (settings_get_decimal_char (FMT_F) != '.')
517                 {
518                   char *cp = strchr (p, '.');
519                   if (cp != NULL)
520                     *cp = settings_get_decimal_char (FMT_F);
521                 }
522               p += strlen (p);
523             }
524           goto done;
525         default:
526           assert (count == 1);
527           *p++ = ch;
528           break;
529         }
530     }
531
532  done:
533   buf_copy_lpad (output, format->w, tmp, p - tmp, ' ');
534   output[format->w] = '\0';
535   return;
536
537  overflow:
538   output_overflow (format, output);
539   return;
540
541  missing:
542   output_missing (format, output);
543   return;
544 }
545
546 /* Outputs WKDAY format. */
547 static void
548 output_WKDAY (const union value *input, const struct fmt_spec *format,
549               char *output)
550 {
551   static const char *const weekdays[7] =
552     {
553       "SUNDAY", "MONDAY", "TUESDAY", "WEDNESDAY",
554       "THURSDAY", "FRIDAY", "SATURDAY",
555     };
556
557   if (input->f >= 1 && input->f < 8)
558     {
559       buf_copy_str_rpad (output, format->w,
560                          weekdays[(int) input->f - 1], ' ');
561       output[format->w] = '\0';
562     }
563   else
564     {
565       if (input->f != SYSMIS)
566         msg (ME, _("Weekday number %f is not between 1 and 7."), input->f);
567       output_missing (format, output);
568     }
569
570 }
571
572 /* Outputs MONTH format. */
573 static void
574 output_MONTH (const union value *input, const struct fmt_spec *format,
575               char *output)
576 {
577   static const char *const months[12] =
578     {
579       "JANUARY", "FEBRUARY", "MARCH", "APRIL", "MAY", "JUNE",
580       "JULY", "AUGUST", "SEPTEMBER", "OCTOBER", "NOVEMBER", "DECEMBER",
581     };
582
583   if (input->f >= 1 && input->f < 13)
584     {
585       buf_copy_str_rpad (output, format->w, months[(int) input->f - 1], ' ');
586       output[format->w] = '\0';
587     }
588   else
589     {
590       if (input->f != SYSMIS)
591         msg (ME, _("Month number %f is not between 1 and 12."), input->f);
592       output_missing (format, output);
593     }
594
595 }
596
597 /* Outputs A format. */
598 static void
599 output_A (const union value *input UNUSED,
600           const struct fmt_spec *format UNUSED, char *output UNUSED)
601 {
602   NOT_REACHED ();
603 }
604
605 /* Outputs AHEX format. */
606 static void
607 output_AHEX (const union value *input, const struct fmt_spec *format,
608              char *output)
609 {
610   output_hex (value_str (input, format->w), format->w / 2, output);
611 }
612 \f
613 /* Decimal and scientific formatting. */
614
615 /* If REQUEST plus the current *WIDTH fits within MAX_WIDTH,
616    increments *WIDTH by REQUEST and return true.
617    Otherwise returns false without changing *WIDTH. */
618 static bool
619 allocate_space (int request, int max_width, int *width)
620 {
621   assert (*width <= max_width);
622   if (request + *width <= max_width)
623     {
624       *width += request;
625       return true;
626     }
627   else
628     return false;
629 }
630
631 /* Tries to compose the number represented by R, in the style of
632    FORMAT, into OUTPUT.  Returns true if successful, false on
633    failure, which occurs if FORMAT's width is too narrow.  If
634    REQUIRE_AFFIXES is true, then the prefix and suffix specified
635    by FORMAT's style must be included; otherwise, they may be
636    omitted to make the number fit. */
637 static bool
638 output_decimal (const struct rounder *r, const struct fmt_spec *format,
639                 bool require_affixes, char *output)
640 {
641   const struct fmt_number_style *style =
642     settings_get_style (format->type);
643
644   int decimals;
645
646   for (decimals = format->d; decimals >= 0; decimals--)
647     {
648       /* Formatted version of magnitude of NUMBER. */
649       char magnitude[64];
650
651       /* Number of digits in MAGNITUDE's integer and fractional parts. */
652       int integer_digits;
653
654       /* Amount of space within the field width already claimed.
655          Initially this is the width of MAGNITUDE, then it is reduced
656          in stages as space is allocated to prefixes and suffixes and
657          grouping characters. */
658       int width;
659
660       /* Include various decorations? */
661       bool add_neg_prefix;
662       bool add_affixes;
663       bool add_grouping;
664
665       /* Position in output. */
666       char *p;
667
668       /* Make sure there's room for the number's magnitude, plus
669          the negative suffix, plus (if negative) the negative
670          prefix. */
671       width = rounder_width (r, decimals, &integer_digits, &add_neg_prefix);
672       width += style->neg_suffix.width;
673       if (add_neg_prefix)
674         width += style->neg_prefix.width;
675       if (width > format->w)
676         continue;
677
678       /* If there's room for the prefix and suffix, allocate
679          space.  If the affixes are required, but there's no
680          space, give up. */
681       add_affixes = allocate_space (fmt_affix_width (style),
682                                     format->w, &width);
683       if (!add_affixes && require_affixes)
684         continue;
685
686       /* Check whether we should include grouping characters.
687          We need room for a complete set or we don't insert any at all.
688          We don't include grouping characters if decimal places were
689          requested but they were all dropped. */
690       add_grouping = (style->grouping != 0
691                       && integer_digits > 3
692                       && (format->d == 0 || decimals > 0)
693                       && allocate_space ((integer_digits - 1) / 3,
694                                          format->w, &width));
695
696       /* Format the number's magnitude. */
697       rounder_format (r, decimals, magnitude);
698
699       /* Assemble number. */
700       p = output;
701       if (format->w > width)
702         p = mempset (p, ' ', format->w - width);
703       if (add_neg_prefix)
704         p = stpcpy (p, style->neg_prefix.s);
705       if (add_affixes)
706         p = stpcpy (p, style->prefix.s);
707       if (!add_grouping)
708         p = mempcpy (p, magnitude, integer_digits);
709       else
710         {
711           int i;
712           for (i = 0; i < integer_digits; i++)
713             {
714               if (i > 0 && (integer_digits - i) % 3 == 0)
715                 *p++ = style->grouping;
716               *p++ = magnitude[i];
717             }
718         }
719       if (decimals > 0)
720         {
721           *p++ = style->decimal;
722           p = mempcpy (p, &magnitude[integer_digits + 1], decimals);
723         }
724       if (add_affixes)
725         p = stpcpy (p, style->suffix.s);
726       if (add_neg_prefix)
727         p = stpcpy (p, style->neg_suffix.s);
728       else
729         p = mempset (p, ' ', style->neg_suffix.width);
730
731       assert (p >= output + format->w);
732       assert (p <= output + format->w + style->extra_bytes);
733       *p = '\0';
734
735       return true;
736     }
737   return false;
738 }
739
740 /* Formats NUMBER into OUTPUT in scientific notation according to
741    the style of the format specified in FORMAT. */
742 static bool
743 output_scientific (double number, const struct fmt_spec *format,
744                    bool require_affixes, char *output)
745 {
746   const struct fmt_number_style *style =
747     settings_get_style (format->type);
748   int width;
749   int fraction_width;
750   bool add_affixes;
751   char *p;
752
753   /* Allocate minimum required space. */
754   width = 6 + style->neg_suffix.width;
755   if (number < 0)
756     width += style->neg_prefix.width;
757   if (width > format->w)
758     return false;
759
760   /* Check for room for prefix and suffix. */
761   add_affixes = allocate_space (fmt_affix_width (style), format->w, &width);
762   if (require_affixes && !add_affixes)
763     return false;
764
765   /* Figure out number of characters we can use for the fraction,
766      if any.  (If that turns out to be 1, then we'll output a
767      decimal point without any digits following; that's what the
768      # flag does in the call to c_snprintf, below.) */
769   fraction_width = MIN (MIN (format->d + 1, format->w - width), 16);
770   if (format->type != FMT_E && fraction_width == 1)
771     fraction_width = 0;
772   width += fraction_width;
773
774   /* Format (except suffix). */
775   p = output;
776   if (width < format->w)
777     p = mempset (p, ' ', format->w - width);
778   if (number < 0)
779     p = stpcpy (p, style->neg_prefix.s);
780   if (add_affixes)
781     p = stpcpy (p, style->prefix.s);
782   if (fraction_width > 0)
783     c_snprintf (p, 64, "%#.*E", fraction_width - 1, fabs (number));
784   else
785     c_snprintf (p, 64, "%.0E", fabs (number));
786
787   /* The C locale always uses a period `.' as a decimal point.
788      Translate to comma if necessary. */
789   if (style->decimal != '.')
790     {
791       char *cp = strchr (p, '.');
792       if (cp != NULL)
793         *cp = style->decimal;
794     }
795
796   /* Make exponent have exactly three digits, plus sign. */
797   {
798     char *cp = strchr (p, 'E') + 1;
799     long int exponent = strtol (cp, NULL, 10);
800     if (abs (exponent) > 999)
801       return false;
802     sprintf (cp, "%+04ld", exponent);
803   }
804
805   /* Add suffixes. */
806   p = strchr (p, '\0');
807   if (add_affixes)
808     p = stpcpy (p, style->suffix.s);
809   if (number < 0)
810     p = stpcpy (p, style->neg_suffix.s);
811   else
812     p = mempset (p, ' ', style->neg_suffix.width);
813
814   assert (p >= output + format->w);
815   assert (p <= output + format->w + style->extra_bytes);
816   *p = '\0';
817
818   return true;
819 }
820 \f
821 /* Returns true if the magnitude represented by R should be
822    rounded up when chopped off at DECIMALS decimal places, false
823    if it should be rounded down. */
824 static bool
825 should_round_up (const struct rounder *r, int decimals)
826 {
827   int digit = r->string[r->integer_digits + decimals + 1];
828   assert (digit >= '0' && digit <= '9');
829   return digit >= '5';
830 }
831
832 /* Initializes R for formatting the magnitude of NUMBER to no
833    more than MAX_DECIMAL decimal places. */
834 static void
835 rounder_init (struct rounder *r, double number, int max_decimals)
836 {
837   assert (fabs (number) < 1e41);
838   assert (max_decimals >= 0 && max_decimals <= 16);
839   if (max_decimals == 0)
840     {
841       /* Fast path.  No rounding needed.
842
843          We append ".00" to the integer representation because
844          round_up assumes that fractional digits are present.  */
845       c_snprintf (r->string, 64, "%.0f.00", fabs (round (number)));
846     }
847   else
848     {
849       /* Slow path.
850
851          This is more difficult than it really should be because
852          we have to make sure that numbers that are exactly
853          halfway between two representations are always rounded
854          away from zero.  This is not what sprintf normally does
855          (usually it rounds to even), so we have to fake it as
856          best we can, by formatting with extra precision and then
857          doing the rounding ourselves.
858
859          We take up to two rounds to format numbers.  In the
860          first round, we obtain 2 digits of precision beyond
861          those requested by the user.  If those digits are
862          exactly "50", then in a second round we format with as
863          many digits as are significant in a "double".
864
865          It might be better to directly implement our own
866          floating-point formatting routine instead of relying on
867          the system's sprintf implementation.  But the classic
868          Steele and White paper on printing floating-point
869          numbers does not hint how to do what we want, and it's
870          not obvious how to change their algorithms to do so.  It
871          would also be a lot of work. */
872       c_snprintf (r->string, 64, "%.*f", max_decimals + 2, fabs (number));
873       if (!strcmp (r->string + strlen (r->string) - 2, "50"))
874         {
875           int binary_exponent, decimal_exponent, format_decimals;
876           frexp (number, &binary_exponent);
877           decimal_exponent = binary_exponent * 3 / 10;
878           format_decimals = (DBL_DIG + 1) - decimal_exponent;
879           if (format_decimals > max_decimals + 2)
880             c_snprintf (r->string, 64, "%.*f", format_decimals, fabs (number));
881         }
882     }
883
884   if (r->string[0] == '0')
885     memmove (r->string, &r->string[1], strlen (r->string));
886
887   r->leading_zeros = strspn (r->string, "0.");
888   r->leading_nines = strspn (r->string, "9.");
889   r->integer_digits = strchr (r->string, '.') - r->string;
890   assert (r->integer_digits < 64);
891   assert (r->integer_digits >= 0);
892   r->negative = number < 0;
893 }
894
895 /* Returns the number of characters required to format the
896    magnitude represented by R to DECIMALS decimal places.
897    The return value includes integer digits and a decimal point
898    and fractional digits, if any, but it does not include any
899    negative prefix or suffix or other affixes.
900
901    *INTEGER_DIGITS is set to the number of digits before the
902    decimal point in the output, between 0 and 40.
903
904    If R represents a negative number and its rounded
905    representation would include at least one nonzero digit,
906    *NEGATIVE is set to true; otherwise, it is set to false. */
907 static int
908 rounder_width (const struct rounder *r, int decimals,
909                int *integer_digits, bool *negative)
910 {
911   /* Calculate base measures. */
912   int width = r->integer_digits;
913   if (decimals > 0)
914     width += decimals + 1;
915   *integer_digits = r->integer_digits;
916   *negative = r->negative;
917
918   /* Rounding can cause adjustments. */
919   if (should_round_up (r, decimals))
920     {
921       /* Rounding up leading 9s adds a new digit (a 1). */
922       if (r->leading_nines >= width)
923         {
924           width++;
925           ++*integer_digits;
926         }
927     }
928   else
929     {
930       /* Rounding down. */
931       if (r->leading_zeros >= width)
932         {
933           /* All digits that remain after rounding are zeros.
934              Therefore we drop the negative sign. */
935           *negative = false;
936           if (r->integer_digits == 0 && decimals == 0)
937             {
938               /* No digits at all are left.  We need to display
939                  at least a single digit (a zero). */
940               assert (width == 0);
941               width++;
942               *integer_digits = 1;
943             }
944         }
945     }
946   return width;
947 }
948
949 /* Formats the magnitude represented by R into OUTPUT, rounding
950    to DECIMALS decimal places.  Exactly as many characters as
951    indicated by rounder_width are written.  No terminating null
952    is appended. */
953 static void
954 rounder_format (const struct rounder *r, int decimals, char *output)
955 {
956   int base_width = r->integer_digits + (decimals > 0 ? decimals + 1 : 0);
957   if (should_round_up (r, decimals))
958     {
959       if (r->leading_nines < base_width)
960         {
961           /* Rounding up.  This is the common case where rounding
962              up doesn't add an extra digit. */
963           char *p;
964           memcpy (output, r->string, base_width);
965           for (p = output + base_width - 1; ; p--)
966             {
967               assert (p >= output);
968               if (*p == '9')
969                 *p = '0';
970               else if (*p >= '0' && *p <= '8')
971                 {
972                   (*p)++;
973                   break;
974                 }
975               else
976                 assert (*p == '.');
977             }
978         }
979       else
980         {
981           /* Rounding up leading 9s causes the result to be a 1
982              followed by a number of 0s, plus a decimal point. */
983           char *p = output;
984           *p++ = '1';
985           p = mempset (p, '0', r->integer_digits);
986           if (decimals > 0)
987             {
988               *p++ = '.';
989               p = mempset (p, '0', decimals);
990             }
991           assert (p == output + base_width + 1);
992         }
993     }
994   else
995     {
996       /* Rounding down. */
997       if (r->integer_digits != 0 || decimals != 0)
998         {
999           /* Common case: just copy the digits. */
1000           memcpy (output, r->string, base_width);
1001         }
1002       else
1003         {
1004           /* No digits remain.  The output is just a zero. */
1005           output[0] = '0';
1006         }
1007     }
1008 }
1009 \f
1010 /* Helper functions. */
1011
1012 /* Returns 10**X. */
1013 static double PURE_FUNCTION
1014 power10 (int x)
1015 {
1016   static const double p[] =
1017     {
1018       1e0, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8, 1e9,
1019       1e10, 1e11, 1e12, 1e13, 1e14, 1e15, 1e16, 1e17, 1e18, 1e19,
1020       1e20, 1e21, 1e22, 1e23, 1e24, 1e25, 1e26, 1e27, 1e28, 1e29,
1021       1e30, 1e31, 1e32, 1e33, 1e34, 1e35, 1e36, 1e37, 1e38, 1e39,
1022       1e40,
1023     };
1024   return x >= 0 && x < sizeof p / sizeof *p ? p[x] : pow (10.0, x);
1025 }
1026
1027 /* Returns 256**X. */
1028 static double PURE_FUNCTION
1029 power256 (int x)
1030 {
1031   static const double p[] =
1032     {
1033       1.0,
1034       256.0,
1035       65536.0,
1036       16777216.0,
1037       4294967296.0,
1038       1099511627776.0,
1039       281474976710656.0,
1040       72057594037927936.0,
1041       18446744073709551616.0
1042     };
1043   return x >= 0 && x < sizeof p / sizeof *p ? p[x] : pow (256.0, x);
1044 }
1045
1046 /* Formats non-finite NUMBER into OUTPUT according to the width
1047    given in FORMAT. */
1048 static void
1049 output_infinite (double number, const struct fmt_spec *format, char *output)
1050 {
1051   assert (!isfinite (number));
1052
1053   if (format->w >= 3)
1054     {
1055       const char *s;
1056
1057       if (isnan (number))
1058         s = "NaN";
1059       else if (isinf (number))
1060         s = number > 0 ? "+Infinity" : "-Infinity";
1061       else
1062         s = "Unknown";
1063
1064       buf_copy_str_lpad (output, format->w, s, ' ');
1065     }
1066   else
1067     output_overflow (format, output);
1068
1069   output[format->w] = '\0';
1070 }
1071
1072 /* Formats OUTPUT as a missing value for the given FORMAT. */
1073 static void
1074 output_missing (const struct fmt_spec *format, char *output)
1075 {
1076   memset (output, ' ', format->w);
1077
1078   if (format->type != FMT_N)
1079     {
1080       int dot_ofs = (format->type == FMT_PCT ? 2
1081                      : format->type == FMT_E ? 5
1082                      : 1);
1083       output[MAX (0, format->w - format->d - dot_ofs)] = '.';
1084     }
1085   else
1086     output[format->w - 1] = '.';
1087
1088   output[format->w] = '\0';
1089 }
1090
1091 /* Formats OUTPUT for overflow given FORMAT. */
1092 static void
1093 output_overflow (const struct fmt_spec *format, char *output)
1094 {
1095   memset (output, '*', format->w);
1096   output[format->w] = '\0';
1097 }
1098
1099 /* Converts the integer part of NUMBER to a packed BCD number
1100    with the given number of DIGITS in OUTPUT.  If DIGITS is odd,
1101    the least significant nibble of the final byte in OUTPUT is
1102    set to 0.  Returns true if successful, false if NUMBER is not
1103    representable.  On failure, OUTPUT is cleared to all zero
1104    bytes. */
1105 static bool
1106 output_bcd_integer (double number, int digits, char *output)
1107 {
1108   char decimal[64];
1109
1110   assert (digits < sizeof decimal);
1111
1112   output[DIV_RND_UP (digits, 2)] = '\0';
1113   if (number != SYSMIS
1114       && number >= 0.
1115       && number < power10 (digits)
1116       && c_snprintf (decimal, 64, "%0*.0f", digits, round (number)) == digits)
1117     {
1118       const char *src = decimal;
1119       int i;
1120
1121       for (i = 0; i < digits / 2; i++)
1122         {
1123           int d0 = *src++ - '0';
1124           int d1 = *src++ - '0';
1125           *output++ = (d0 << 4) + d1;
1126         }
1127       if (digits % 2)
1128         *output = (*src - '0') << 4;
1129
1130       return true;
1131     }
1132   else
1133     {
1134       memset (output, 0, DIV_RND_UP (digits, 2));
1135       return false;
1136     }
1137 }
1138
1139 /* Writes VALUE to OUTPUT as a BYTES-byte binary integer of the
1140    given INTEGER_FORMAT. */
1141 static void
1142 output_binary_integer (uint64_t value, int bytes,
1143                        enum integer_format integer_format, char *output)
1144 {
1145   integer_put (value, integer_format, output, bytes);
1146 }
1147
1148 /* Converts the BYTES bytes in DATA to twice as many hexadecimal
1149    digits in OUTPUT. */
1150 static void
1151 output_hex (const void *data_, size_t bytes, char *output)
1152 {
1153   const uint8_t *data = data_;
1154   size_t i;
1155
1156   for (i = 0; i < bytes; i++)
1157     {
1158       static const char hex_digits[] = "0123456789ABCDEF";
1159       *output++ = hex_digits[data[i] >> 4];
1160       *output++ = hex_digits[data[i] & 15];
1161     }
1162   *output = '\0';
1163 }