1 /*
2 Copyright (c) 2010, 2021, Oracle and/or its affiliates.
3 All rights reserved. Use is subject to license terms.
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License, version 2.0,
7 as published by the Free Software Foundation.
8
9 This program is also distributed with certain software (including
10 but not limited to OpenSSL) that is licensed under separate terms,
11 as designated in a particular file or component or in included license
12 documentation. The authors of MySQL hereby grant you an additional
13 permission to link the program and your derivative works with the
14 separately licensed software that they have included with MySQL.
15
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License, version 2.0, for more details.
20
21 You should have received a copy of the GNU General Public License
22 along with this program; if not, write to the Free Software
23 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24 */
25 /*
26 * decimal_utils.cpp
27 */
28
29 #include "my_global.h"
30 #include "m_string.h"
31 C_MODE_START
32 #include "decimal.h"
33 C_MODE_END
34
35 #include "decimal_utils.hpp"
36
decimal_str2bin(const char * str,int str_len,int prec,int scale,void * bin,int bin_len)37 int decimal_str2bin(const char *str, int str_len,
38 int prec, int scale,
39 void *bin, int bin_len)
40 {
41 register int retval; /* return value from str2dec() */
42 decimal_t dec; /* intermediate representation */
43 decimal_digit_t digits[9]; /* for dec->buf */
44 char *end = (char *) str + str_len;
45
46 assert(str != 0);
47 assert(bin != 0);
48 if(prec < 1) return E_DEC_BAD_PREC;
49 if((scale < 0) || (scale > prec)) return E_DEC_BAD_SCALE;
50
51 if(decimal_bin_size(prec, scale) > bin_len)
52 return E_DEC_OOM;
53
54 dec.len = 9; /* big enough for any decimal */
55 dec.buf = digits;
56
57 retval = string2decimal(str, &dec, &end);
58 if(retval != E_DEC_OK) return retval;
59
60 return decimal2bin(&dec, (unsigned char *) bin, prec, scale);
61 }
62
decimal_bin2str(const void * bin,int bin_len,int prec,int scale,char * str,int str_len)63 int decimal_bin2str(const void *bin, int bin_len,
64 int prec, int scale,
65 char *str, int str_len)
66 {
67 register int retval; /* return from bin2decimal() */
68 decimal_t dec; /* intermediate representation */
69 decimal_digit_t digits[9]; /* for dec->buf */
70 int to_len;
71
72 assert(bin != 0);
73 assert(str != 0);
74 if(prec < 1) return E_DEC_BAD_PREC;
75 if((scale < 0) || (scale > prec)) return E_DEC_BAD_SCALE;
76
77 dec.len = 9; /* big enough for any decimal */
78 dec.buf = digits;
79
80 // Note: bin_len is unused -- bin2decimal() does not take a length
81 retval = bin2decimal((const uchar *) bin, &dec, prec, scale);
82 if(retval != E_DEC_OK) return retval;
83
84 to_len = decimal_string_size(&dec);
85 if(to_len > str_len) return E_DEC_OOM;
86
87 return decimal2string(&dec, str, &to_len, 0, 0, 0);
88 }
89