1 /*
2  * Copyright (c) 2012-2014 ARM Ltd
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. The name of the company may not be used to endorse or promote
14  *    products derived from this software without specific prior written
15  *    permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY ARM LTD ``AS IS'' AND ANY EXPRESS OR IMPLIED
18  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL ARM LTD BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
22  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
23  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
24  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
25  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #include <newlib.h>
30 
31 #define _DEFAULT_SOURCE
32 #include <_ansi.h>
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <string.h>
36 #include <limits.h>
37 #include <stdint.h>
38 #include <wchar.h>
39 #include <sys/lock.h>
40 #include <stdarg.h>
41 #include "local.h"
42 #include "../stdlib/local.h"
43 #include "fvwrite.h"
44 #include "vfieeefp.h"
45 #include "nano-vfprintf_local.h"
46 
47 /* Decode and print non-floating point data.  */
48 int
_printf_common(struct _prt_data_t * pdata,int * realsz,FILE * fp,int (* pfunc)(FILE *,const char *,size_t len))49 _printf_common (
50 		struct _prt_data_t *pdata,
51 		int *realsz,
52 		FILE *fp,
53 		int (*pfunc)(FILE *,
54 			     const char *, size_t len))
55 {
56   int n;
57   /*
58    * All reasonable formats wind up here.  At this point, `cp'
59    * points to a string which (if not flags&LADJUST) should be
60    * padded out to `width' places.  If flags&ZEROPAD, it should
61    * first be prefixed by any sign or other prefix; otherwise,
62    * it should be blank padded before the prefix is emitted.
63    * After any left-hand padding and prefixing, emit zeroes
64    * required by a decimal [diouxX] precision, then print the
65    * string proper, then emit zeroes required by any leftover
66    * floating precision; finally, if LADJUST, pad with blanks.
67    * If flags&FPT, ch must be in [aAeEfg].
68    *
69    * Compute actual size, so we know how much to pad.
70    * size excludes decimal prec; realsz includes it.
71    */
72   *realsz = pdata->dprec > pdata->size ? pdata->dprec : pdata->size;
73   if (pdata->l_buf[0])
74     (*realsz)++;
75 
76   if (pdata->flags & HEXPREFIX)
77     *realsz += 2;
78 
79   /* Right-adjusting blank padding.  */
80   if ((pdata->flags & (LADJUST|ZEROPAD)) == 0)
81     PAD (pdata->width - *realsz, pdata->blank);
82 
83   /* Prefix.  */
84   n = 0;
85   if (pdata->l_buf[0])
86     n++;
87 
88   if (pdata->flags & HEXPREFIX)
89     {
90       pdata->l_buf[n++] = '0';
91       pdata->l_buf[n++] = pdata->l_buf[2];
92     }
93 
94   PRINT (pdata->l_buf, n);
95   n = pdata->width - *realsz;
96   if ((pdata->flags & (LADJUST|ZEROPAD)) != ZEROPAD || n < 0)
97     n = 0;
98 
99   if (pdata->dprec > pdata->size)
100     n += pdata->dprec - pdata->size;
101 
102   PAD (n, pdata->zero);
103   return 0;
104 error:
105   return -1;
106 }
107 int
_printf_i(struct _prt_data_t * pdata,FILE * fp,int (* pfunc)(FILE *,const char *,size_t len),va_list * ap)108 _printf_i (struct _prt_data_t *pdata, FILE *fp,
109 	   int (*pfunc)(FILE *, const char *, size_t len),
110 	   va_list *ap)
111 {
112   /* Field size expanded by dprec.  */
113   int realsz;
114   u_quad_t _uquad;
115   int base;
116   int n;
117   char *cp = pdata->buf + BUF;
118   char *xdigs = "0123456789ABCDEF";
119 
120   /* Decoding the conversion specifier.  */
121   switch (pdata->code)
122     {
123     case 'c':
124       *--cp = GET_ARG (N, *ap, int);
125       pdata->size = 1;
126       goto non_number_nosign;
127     case 'd':
128     case 'i':
129       _uquad = SARG (pdata->flags);
130       if ((long) _uquad < 0)
131 	{
132 	  _uquad = -_uquad;
133 	  pdata->l_buf[0] = '-';
134 	}
135       base = 10;
136       goto number;
137     case 'u':
138     case 'o':
139       _uquad = UARG (pdata->flags);
140       base = (pdata->code == 'o') ? 8 : 10;
141       goto nosign;
142     case 'X':
143       pdata->l_buf[2] = 'X';
144       goto hex;
145     case 'p':
146       /*
147        * ``The argument shall be a pointer to void.  The
148        * value of the pointer is converted to a sequence
149        * of printable characters, in an implementation-
150        * defined manner.''
151        *	-- ANSI X3J11
152        */
153       pdata->flags |= HEXPREFIX;
154       if (sizeof (void*) > sizeof (int))
155 	pdata->flags |= LONGINT;
156       FALLTHROUGH;
157       /* NOSTRICT.  */
158     case 'x':
159       pdata->l_buf[2] = 'x';
160       xdigs = "0123456789abcdef";
161 hex:
162       _uquad = UARG (pdata->flags);
163       base = 16;
164       if (pdata->flags & ALT)
165 	pdata->flags |= HEXPREFIX;
166 
167       /* Leading 0x/X only if non-zero.  */
168       if (_uquad == 0)
169 	pdata->flags &= ~HEXPREFIX;
170 
171       /* Unsigned conversions.  */
172 nosign:
173       pdata->l_buf[0] = '\0';
174       /*
175        * ``... diouXx conversions ... if a precision is
176        * specified, the 0 flag will be ignored.''
177        *	-- ANSI X3J11
178        */
179 number:
180       if ((pdata->dprec = pdata->prec) >= 0)
181 	pdata->flags &= ~ZEROPAD;
182 
183       /*
184        * ``The result of converting a zero value with an
185        * explicit precision of zero is no characters.''
186        *	-- ANSI X3J11
187        */
188       if (_uquad != 0 || pdata->prec != 0)
189 	{
190 	  do
191 	    {
192 	      *--cp = xdigs[_uquad % base];
193 	      _uquad /= base;
194 	    }
195 	  while (_uquad);
196 	}
197       /* For 'o' conversion, '#' increases the precision to force the first
198 	 digit of the result to be zero.  */
199       if (base == 8 && (pdata->flags & ALT) && pdata->prec <= pdata->size)
200 	*--cp = '0';
201 
202       pdata->size = pdata->buf + BUF - cp;
203       break;
204     case 'n':
205       if (pdata->flags & LONGINT)
206 	*GET_ARG (N, *ap, long_ptr_t) = pdata->ret;
207       else if (pdata->flags & SHORTINT)
208 	*GET_ARG (N, *ap, short_ptr_t) = pdata->ret;
209       else
210 	*GET_ARG (N, *ap, int_ptr_t) = pdata->ret;
211       FALLTHROUGH;
212     case '\0':
213       pdata->size = 0;
214       break;
215     case 's':
216       cp = GET_ARG (N, *ap, char_ptr_t);
217       /* Precision gives the maximum number of chars to be written from a
218 	 string, and take prec == -1 into consideration.
219 	 Use normal Newlib approach here to support case where cp is not
220 	 nul-terminated.  */
221       if (cp == NULL)
222           cp = "(null)";
223       char *p = memchr (cp, 0, pdata->prec);
224 
225       if (p != NULL)
226 	pdata->prec = p - cp;
227 
228       pdata->size = pdata->prec;
229       goto non_number_nosign;
230     default:
231       /* "%?" prints ?, unless ? is NUL.  */
232       /* Pretend it was %c with argument ch.  */
233       *--cp = pdata->code;
234       pdata->size = 1;
235 non_number_nosign:
236       pdata->l_buf[0] = '\0';
237       break;
238     }
239 
240     /* Output.  */
241     n = _printf_common (pdata, &realsz, fp, pfunc);
242     if (n == -1)
243       goto error;
244 
245     PRINT (cp, pdata->size);
246     /* Left-adjusting padding (always blank).  */
247     if (pdata->flags & LADJUST)
248       PAD (pdata->width - realsz, pdata->blank);
249 
250     return (pdata->width > realsz ? pdata->width : realsz);
251 error:
252     return -1;
253 }
254 
255