-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathconversions.c
More file actions
151 lines (131 loc) · 3.9 KB
/
conversions.c
File metadata and controls
151 lines (131 loc) · 3.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
//#include <stdio.h>
//#include <stdint.h>
//#include <stdarg.h>
//#include <stdbool.h>
//#include "mbedtls/bignum.h"
static const char hexdigits[] = {
'0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 'A', 'B', 'C', 'D', 'E', 'F'
};
// convert a value from big endian variable integer to string with
// decimal point. uses 18 decimal digits
int bignum_to_string(const uint8_t *buf, int len, uint8_t *out, int outlen) {
mbedtls_mpi value;
char tmp[36];
size_t slen;
int i;
mbedtls_mpi_init(&value);
if (mbedtls_mpi_read_binary(&value, buf, len) != 0) goto loc_failed;
if (mbedtls_mpi_write_string(&value, 10, out, outlen, &slen) != 0) goto loc_failed;
mbedtls_mpi_free(&value);
// align the string to the right
snprintf(tmp, sizeof(tmp), "%32s", out);
// replace the spaces with zeros
for (i = 0; tmp[i] == ' '; i++) {
tmp[i] = '0';
slen++;
}
// add the decimal point
snprintf(out, outlen, "%.*s.%s", 32-18, tmp, tmp+32-18);
// return the size
return slen + 1;
loc_failed:
mbedtls_mpi_free(&value);
return -1;
}
// convert a value from string format to big endian variable integer.
// the string can optionally have a decimal point. uses 18 decimal digits
int string_to_bignum(const uint8_t *str, int len, uint8_t *out, int outlen) {
mbedtls_mpi value;
uint8_t integer[36], decimal[36], *p;
int i;
p = strchr(str, '.');
if (p) {
// copy the integer part
snprintf(integer, sizeof(integer), "%.*s", (int)(p-str), str);
// copy the decimal part
p++;
for (i = 0; i < 18 && *p; i++) {
decimal[i] = *p++;
}
} else {
strcpy(integer, str);
i = 0;
}
// fill the decimal with trailing zeros
for (; i < 18; i++) {
decimal[i] = '0';
}
// null terminator
decimal[18] = 0;
// contatenate both parts
strcat(integer, decimal);
// convert it to big endian variable size integer
mbedtls_mpi_init(&value);
if (mbedtls_mpi_read_string(&value, 10, integer) != 0) goto loc_failed;
len = mbedtls_mpi_size(&value);
if (mbedtls_mpi_write_binary(&value, out, len) != 0) goto loc_failed;
mbedtls_mpi_free(&value);
return len;
loc_failed:
mbedtls_mpi_free(&value);
return -1;
}
bool arguments_to_json(char *buf, int bufsize, const char *types, va_list ap){
char c, *dest;
int i, dsize;
buf[0] = '[';
dest = &buf[1];
dsize = bufsize - 2;
for(i=0; (c = types[i])!=0; i++){
if( c=='s' ){
char *limit = buf + bufsize - 4;
char *z = va_arg(ap, char*);
if (i > 0) *dest++ = ',';
*dest++ = '"';
while (*z) {
*dest++ = *z++;
if (dest > limit) goto loc_invalid;
}
*dest++ = '"';
}else if( c=='i' ){
if (dsize < 12) goto loc_invalid;
if (i > 0) *dest++ = ',';
snprintf(dest, dsize, "%d", va_arg(ap, int));
dest += strlen(dest);
}else if( c=='l' ){
if (dsize < 12) goto loc_invalid;
if (i > 0) *dest++ = ',';
snprintf(dest, dsize, "%" PRIu64, va_arg(ap, int64_t));
dest += strlen(dest);
//}else if( c=='f' ){ -- floats are promoted to double
}else if( c=='d' ){
if (dsize < 26) goto loc_invalid;
if (i > 0) *dest++ = ',';
snprintf(dest, dsize, "%f", va_arg(ap, double));
dest += strlen(dest);
}else if( c=='b' ){
char *ptr = va_arg(ap, char*);
int size = va_arg(ap, int);
if (dsize < (size * 2) + 3) goto loc_invalid;
if (i > 0) *dest++ = ',';
*dest++ = '"';
while (size) {
c = *ptr++;
*(dest++) = hexdigits[(c>>4)&0xf];
*(dest++) = hexdigits[c&0xf];
size--;
}
*dest++ = '"';
}else{
goto loc_invalid;
}
// update the remaining size of the destination buffer
dsize = bufsize - (dest - buf);
}
*dest++ = ']';
*dest++ = 0;
return true;
loc_invalid:
return false;
}