1 /* Copyright (c) 2000, 2021, Oracle and/or its affiliates.
2 
3    This program is free software; you can redistribute it and/or modify
4    it under the terms of the GNU General Public License, version 2.0,
5    as published by the Free Software Foundation.
6 
7    This program is also distributed with certain software (including
8    but not limited to OpenSSL) that is licensed under separate terms,
9    as designated in a particular file or component or in included license
10    documentation.  The authors of MySQL hereby grant you an additional
11    permission to link the program and your derivative works with the
12    separately licensed software that they have included with MySQL.
13 
14    Without limiting anything contained in the foregoing, this file,
15    which is part of C Driver for MySQL (Connector/C), is also subject to the
16    Universal FOSS Exception, version 1.0, a copy of which can be found at
17    http://oss.oracle.com/licenses/universal-foss-exception.
18 
19    This program is distributed in the hope that it will be useful,
20    but WITHOUT ANY WARRANTY; without even the implied warranty of
21    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22    GNU General Public License, version 2.0, for more details.
23 
24    You should have received a copy of the GNU General Public License
25    along with this program; if not, write to the Free Software
26    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA */
27 
28 #include <my_global.h>
29 #include "m_string.h"
30 
31 /*
32   _dig_vec arrays are public because they are used in several outer places.
33 */
34 char _dig_vec_upper[] =
35   "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
36 char _dig_vec_lower[] =
37   "0123456789abcdefghijklmnopqrstuvwxyz";
38 
39 
40 /*
41   Convert integer to its string representation in given scale of notation.
42 
43   SYNOPSIS
44     int2str()
45       val     - value to convert
46       dst     - points to buffer where string representation should be stored
47       radix   - radix of scale of notation
48       upcase  - set to 1 if we should use upper-case digits
49 
50   DESCRIPTION
51     Converts the (long) integer value to its character form and moves it to
52     the destination buffer followed by a terminating NUL.
53     If radix is -2..-36, val is taken to be SIGNED, if radix is  2..36, val is
54     taken to be UNSIGNED. That is, val is signed if and only if radix is.
55     All other radixes treated as bad and nothing will be changed in this case.
56 
57     For conversion to decimal representation (radix is -10 or 10) one can use
58     optimized int10_to_str() function.
59 
60   RETURN VALUE
61     Pointer to ending NUL character or NullS if radix is bad.
62 */
63 
64 char *
int2str(long int val,char * dst,int radix,int upcase)65 int2str(long int val, char *dst, int radix,
66         int upcase)
67 {
68   char buffer[65];
69   char *p;
70   long int new_val;
71   char *dig_vec= upcase ? _dig_vec_upper : _dig_vec_lower;
72   ulong uval= (ulong) val;
73 
74   if (radix < 0)
75   {
76     if (radix < -36 || radix > -2)
77       return NullS;
78     if (val < 0)
79     {
80       *dst++ = '-';
81       /* Avoid integer overflow in (-val) for LLONG_MIN (BUG#31799). */
82       uval = (ulong)0 - uval;
83     }
84     radix = -radix;
85   }
86   else if (radix > 36 || radix < 2)
87     return NullS;
88 
89   /*
90     The slightly contorted code which follows is due to the fact that
91     few machines directly support unsigned long / and %.  Certainly
92     the VAX C compiler generates a subroutine call.  In the interests
93     of efficiency (hollow laugh) I let this happen for the first digit
94     only; after that "val" will be in range so that signed integer
95     division will do.  Sorry 'bout that.  CHECK THE CODE PRODUCED BY
96     YOUR C COMPILER.  The first % and / should be unsigned, the second
97     % and / signed, but C compilers tend to be extraordinarily
98     sensitive to minor details of style.  This works on a VAX, that's
99     all I claim for it.
100   */
101   p = &buffer[sizeof(buffer)-1];
102   *p = '\0';
103   new_val= uval / (ulong) radix;
104   *--p = dig_vec[(uchar) (uval- (ulong) new_val*(ulong) radix)];
105   val = new_val;
106   while (val != 0)
107   {
108     ldiv_t res;
109     res=ldiv(val,radix);
110     *--p = dig_vec[res.rem];
111     val= res.quot;
112   }
113   while ((*dst++ = *p++) != 0) ;
114   return dst-1;
115 }
116 
117 
118 /*
119   Converts integer to its string representation in decimal notation.
120 
121   SYNOPSIS
122     int10_to_str()
123       val     - value to convert
124       dst     - points to buffer where string representation should be stored
125       radix   - flag that shows whenever val should be taken as signed or not
126 
127   DESCRIPTION
128     This is version of int2str() function which is optimized for normal case
129     of radix 10/-10. It takes only sign of radix parameter into account and
130     not its absolute value.
131 
132   RETURN VALUE
133     Pointer to ending NUL character.
134 */
135 
int10_to_str(long int val,char * dst,int radix)136 char *int10_to_str(long int val,char *dst,int radix)
137 {
138   char buffer[65];
139   char *p;
140   long int new_val;
141   unsigned long int uval = (unsigned long int) val;
142 
143   if (radix < 0)				/* -10 */
144   {
145     if (val < 0)
146     {
147       *dst++ = '-';
148       /* Avoid integer overflow in (-val) for LLONG_MIN (BUG#31799). */
149       uval = (unsigned long int)0 - uval;
150     }
151   }
152 
153   p = &buffer[sizeof(buffer)-1];
154   *p = '\0';
155   new_val= (long) (uval / 10);
156   *--p = '0'+ (char) (uval - (unsigned long) new_val * 10);
157   val = new_val;
158 
159   while (val != 0)
160   {
161     new_val=val/10;
162     *--p = '0' + (char) (val-new_val*10);
163     val= new_val;
164   }
165   while ((*dst++ = *p++) != 0) ;
166   return dst-1;
167 }
168