2121
2222longlong Item_func_inet_aton::val_int ()
2323{
24- DBUG_ASSERT (fixed == 1 );
25- uint byte_result = 0 ;
26- ulonglong result = 0 ; // We are ready for 64 bit addresses
24+ DBUG_ASSERT (fixed);
25+
26+ uint byte_result= 0 ;
27+ ulonglong result= 0 ;
2728 const char *p,* end;
28- char c = ' .' ; // we mark c to indicate invalid IP in case length is 0
29+ char c= ' .' ; // we mark c to indicate invalid IP in case length is 0
2930 char buff[36 ];
3031 int dot_count= 0 ;
3132
32- String *s, tmp (buff, sizeof (buff), &my_charset_latin1);
33- if (!(s = args[0 ]->val_str_ascii (&tmp))) // If null value
33+ String tmp (buff, sizeof (buff), &my_charset_latin1);
34+ String *s= args[0 ]->val_str_ascii (&tmp);
35+
36+ if (!s) // If null value
3437 goto err;
35- null_value=0 ;
3638
37- end= (p = s->ptr ()) + s->length ();
39+ null_value= 0 ;
40+
41+ p= s->ptr ();
42+ end= p + s->length ();
3843 while (p < end)
3944 {
40- c = *p++;
41- int digit = (int ) (c - ' 0' );
45+ c= *p++;
46+ int digit= (int ) (c - ' 0' );
4247 if (digit >= 0 && digit <= 9 )
4348 {
44- if ((byte_result = byte_result * 10 + digit) > 255 )
45- goto err; // Wrong address
49+ byte_result= byte_result * 10 + digit;
50+ if (byte_result > 255 )
51+ goto err; // Wrong address
4652 }
4753 else if (c == ' .' )
4854 {
4955 dot_count++;
5056 result= (result << 8 ) + (ulonglong) byte_result;
51- byte_result = 0 ;
57+ byte_result= 0 ;
5258 }
5359 else
54- goto err; // Invalid character
60+ goto err; // Invalid character
5561 }
56- if (c != ' .' ) // IP number can't end on '.'
62+ if (c != ' .' ) // IP number can't end on '.'
5763 {
5864 /*
59- Handle short-forms addresses according to standard. Examples:
60- 127 -> 0.0.0.127
61- 127.1 -> 127.0.0.1
62- 127.2.1 -> 127.2.0.1
65+ Attempt to support short-form addresses (i.e. classful addresses).
66+ The current code does not support full range of classful addresses.
67+ Examples:
68+ 127 -> 0.0.0.127
69+ 127.255 -> 127.0.0.255
70+ 127.256 -> NULL (should have been 127.0.1.0)
71+ 127.2.1 -> 127.2.0.1
6372 */
6473 switch (dot_count) {
6574 case 1 : result<<= 8 ; /* Fall through */
@@ -77,42 +86,48 @@ longlong Item_func_inet_aton::val_int()
7786
7887String* Item_func_inet_ntoa::val_str (String* str)
7988{
80- DBUG_ASSERT (fixed == 1 );
81- uchar buf[8 ], *p;
82- ulonglong n = (ulonglong) args[0 ]->val_int ();
83- char num[4 ];
89+ DBUG_ASSERT (fixed);
90+
91+ ulonglong n= (ulonglong) args[0 ]->val_int ();
8492
8593 /*
8694 We do not know if args[0] is NULL until we have called
8795 some val function on it if args[0] is not a constant!
8896
8997 Also return null if n > 255.255.255.255
9098 */
91- if ((null_value= (args[0 ]->null_value || n > (ulonglong) LL (4294967295 ))))
92- return 0 ; // Null value
99+ null_value= args[0 ]->null_value || n > (ulonglong) LL (4294967295 );
100+
101+ if (null_value)
102+ return 0 ; // Null value
93103
94104 str->set_charset (collation.collation );
95105 str->length (0 );
96- int4store (buf,n);
106+
107+ uchar buf[8 ];
108+ int4store (buf, n);
97109
98110 /* Now we can assume little endian. */
99111
100- num[3 ]=' .' ;
101- for (p=buf+4 ; p-- > buf ; )
112+ char num[4 ];
113+ num[3 ]= ' .' ;
114+
115+ for (uchar *p= buf + 4 ; p-- > buf;)
102116 {
103- uint c = *p;
104- uint n1,n2; // Try to avoid divisions
105- n1= c / 100 ; // 100 digits
106- c-= n1* 100 ;
107- n2= c / 10 ; // 10 digits
108- c-=n2* 10 ; // last digit
109- num[0 ]=(char ) n1+ ' 0' ;
110- num[1 ]=(char ) n2+ ' 0' ;
111- num[2 ]=(char ) c+ ' 0' ;
117+ uint c= *p;
118+ uint n1, n2; // Try to avoid divisions
119+ n1= c / 100 ; // 100 digits
120+ c -= n1 * 100 ;
121+ n2= c / 10 ; // 10 digits
122+ c -= n2 * 10 ; // last digit
123+ num[0 ]= (char ) n1 + ' 0' ;
124+ num[1 ]= (char ) n2 + ' 0' ;
125+ num[2 ]= (char ) c + ' 0' ;
112126 uint length= (n1 ? 4 : n2 ? 3 : 2 ); // Remove pre-zero
113127 uint dot_length= (p <= buf) ? 1 : 0 ;
114- ( void ) str-> append (num + 4 - length, length - dot_length,
115- &my_charset_latin1);
128+
129+ str-> append (num + 4 - length, length - dot_length, &my_charset_latin1);
116130 }
131+
117132 return str;
118133}
0 commit comments