line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
/** |
2
|
|
|
|
|
|
|
* @file pstm_str.c |
3
|
|
|
|
|
|
|
* @version 950bba4 (HEAD -> master) |
4
|
|
|
|
|
|
|
* |
5
|
|
|
|
|
|
|
* Multiprecision number implementation. |
6
|
|
|
|
|
|
|
*/ |
7
|
|
|
|
|
|
|
/* |
8
|
|
|
|
|
|
|
* Copyright (c) 2017 INSIDE Secure Corporation |
9
|
|
|
|
|
|
|
* All Rights Reserved |
10
|
|
|
|
|
|
|
* |
11
|
|
|
|
|
|
|
* The latest version of this code is available at http://www.matrixssl.org |
12
|
|
|
|
|
|
|
* |
13
|
|
|
|
|
|
|
* This software is open source; you can redistribute it and/or modify |
14
|
|
|
|
|
|
|
* it under the terms of the GNU General Public License as published by |
15
|
|
|
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or |
16
|
|
|
|
|
|
|
* (at your option) any later version. |
17
|
|
|
|
|
|
|
* |
18
|
|
|
|
|
|
|
* This General Public License does NOT permit incorporating this software |
19
|
|
|
|
|
|
|
* into proprietary programs. If you are unable to comply with the GPL, a |
20
|
|
|
|
|
|
|
* commercial license for this software may be purchased from INSIDE at |
21
|
|
|
|
|
|
|
* http://www.insidesecure.com/ |
22
|
|
|
|
|
|
|
* |
23
|
|
|
|
|
|
|
* This program is distributed in WITHOUT ANY WARRANTY; without even the |
24
|
|
|
|
|
|
|
* implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
25
|
|
|
|
|
|
|
* See the GNU General Public License for more details. |
26
|
|
|
|
|
|
|
* |
27
|
|
|
|
|
|
|
* You should have received a copy of the GNU General Public License |
28
|
|
|
|
|
|
|
* along with this program; if not, write to the Free Software |
29
|
|
|
|
|
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
30
|
|
|
|
|
|
|
* http://www.gnu.org/copyleft/gpl.html |
31
|
|
|
|
|
|
|
*/ |
32
|
|
|
|
|
|
|
/******************************************************************************/ |
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
#include "../cryptoImpl.h" |
35
|
|
|
|
|
|
|
#include "pstm.h" |
36
|
|
|
|
|
|
|
#include "pstm_str.h" |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
#ifndef NO_PSTM_STR |
39
|
|
|
|
|
|
|
/* Static storage for pstm_str preallocated values. */ |
40
|
|
|
|
|
|
|
const char *pstm_str_null = "(null)"; |
41
|
|
|
|
|
|
|
const char *pstm_str_memfail = "(memory_error)"; |
42
|
|
|
|
|
|
|
|
43
|
0
|
|
|
|
|
|
pstm_str pstm_str_from(psPool_t *pool, const pstm_int *a) |
44
|
|
|
|
|
|
|
{ |
45
|
|
|
|
|
|
|
static unsigned char hex[16] = { |
46
|
|
|
|
|
|
|
'0', '1', '2', '3', '4', '5', '6', '7', |
47
|
|
|
|
|
|
|
'8', '9', 'a', 'b', 'c', 'd', 'e', 'f' |
48
|
|
|
|
|
|
|
}; |
49
|
|
|
|
|
|
|
char *b; |
50
|
|
|
|
|
|
|
unsigned char *bh; |
51
|
|
|
|
|
|
|
int neg; |
52
|
0
|
|
|
|
|
|
uint16_t sz = pstm_unsigned_bin_size(a); |
53
|
|
|
|
|
|
|
uint16_t i; |
54
|
|
|
|
|
|
|
int32_t err; |
55
|
|
|
|
|
|
|
|
56
|
0
|
0
|
|
|
|
|
if (a == NULL) |
57
|
|
|
|
|
|
|
{ |
58
|
0
|
|
|
|
|
|
return PSTM_STR_NULL; |
59
|
|
|
|
|
|
|
} |
60
|
0
|
|
|
|
|
|
b = psMalloc(pool, sz * 2 + 4); |
61
|
0
|
0
|
|
|
|
|
if (!b) |
62
|
|
|
|
|
|
|
{ |
63
|
0
|
|
|
|
|
|
return PSTM_STR_MEMFAIL; |
64
|
|
|
|
|
|
|
} |
65
|
0
|
|
|
|
|
|
neg = a->sign == PSTM_NEG; |
66
|
0
|
0
|
|
|
|
|
if (neg) |
67
|
|
|
|
|
|
|
{ |
68
|
0
|
|
|
|
|
|
b[0] = '-'; |
69
|
0
|
|
|
|
|
|
b[1] = '0'; |
70
|
0
|
0
|
|
|
|
|
b[2] = sz > 0 ? 'x' : 0; |
71
|
0
|
|
|
|
|
|
bh = (unsigned char *) &b[3]; |
72
|
0
|
|
|
|
|
|
bh[sz * 2] = 0; |
73
|
|
|
|
|
|
|
} |
74
|
|
|
|
|
|
|
else |
75
|
|
|
|
|
|
|
{ |
76
|
0
|
|
|
|
|
|
b[0] = '0'; |
77
|
0
|
0
|
|
|
|
|
b[1] = sz > 0 ? 'x' : 0; |
78
|
0
|
|
|
|
|
|
bh = (unsigned char *) &b[2]; |
79
|
0
|
|
|
|
|
|
bh[sz * 2] = 0; |
80
|
|
|
|
|
|
|
} |
81
|
0
|
|
|
|
|
|
err = pstm_to_unsigned_bin(pool, a, bh); |
82
|
0
|
0
|
|
|
|
|
if (err < 0) |
83
|
|
|
|
|
|
|
{ |
84
|
0
|
|
|
|
|
|
psFree(b, pool); |
85
|
0
|
|
|
|
|
|
return PSTM_STR_MEMFAIL; |
86
|
|
|
|
|
|
|
} |
87
|
0
|
0
|
|
|
|
|
for (i = sz; i-- > 0; ) |
88
|
|
|
|
|
|
|
{ |
89
|
0
|
|
|
|
|
|
bh[i * 2 + 1] = hex[bh[i] & 15]; |
90
|
0
|
|
|
|
|
|
bh[i * 2] = hex[bh[i] / 16]; |
91
|
|
|
|
|
|
|
} |
92
|
0
|
|
|
|
|
|
return b; |
93
|
|
|
|
|
|
|
} |
94
|
|
|
|
|
|
|
|
95
|
0
|
|
|
|
|
|
void pstm_str_free(psPool_t *pool, pstm_str str) |
96
|
|
|
|
|
|
|
{ |
97
|
0
|
0
|
|
|
|
|
if (str != PSTM_STR_NULL && str != PSTM_STR_MEMFAIL) |
|
|
0
|
|
|
|
|
|
98
|
|
|
|
|
|
|
{ |
99
|
0
|
|
|
|
|
|
psFree(str, pool); |
100
|
|
|
|
|
|
|
} |
101
|
0
|
|
|
|
|
|
} |
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
#endif /* NO_PSTM_STR */ |
104
|
|
|
|
|
|
|
|