Embedded SDK
Embedded SDK
Loading...
Searching...
No Matches
format.c
Go to the documentation of this file.
1/*****************************************************************************
2 * (c) 2020 Ledger SAS.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *****************************************************************************/
16
17#include <stddef.h> // size_t
18#include <stdint.h> // int*_t, uint*_t
19#include <string.h> // memcpy, memset, strlen, strnlen
20#include <stdbool.h> // bool
21
22#include "format.h"
23
24bool format_i64(char *dst, size_t dst_len, const int64_t value)
25{
26 char temp[] = "-9223372036854775808";
27
28 char *ptr = temp;
29 int64_t num = value;
30 int sign = 1;
31
32 if (value < 0) {
33 sign = -1;
34 }
35
36 while (num != 0) {
37 *ptr++ = '0' + (num % 10) * sign;
38 num /= 10;
39 }
40
41 if (value < 0) {
42 *ptr++ = '-';
43 }
44 else if (value == 0) {
45 *ptr++ = '0';
46 }
47
48 int distance = (ptr - temp) + 1;
49
50 if ((int) dst_len < distance) {
51 return false;
52 }
53
54 size_t index = 0;
55
56 while (--ptr >= temp) {
57 dst[index++] = *ptr;
58 }
59
60 dst[index] = '\0';
61
62 return true;
63}
64
65bool format_u64(char *out, size_t outLen, uint64_t in)
66{
67 size_t i = 0;
68
69 if (outLen == 0) {
70 return false;
71 }
72 outLen--;
73
74 while (in > 9) {
75 out[i] = in % 10 + '0';
76 in /= 10;
77 i++;
78 if (i + 1 > outLen) {
79 return false;
80 }
81 }
82 out[i] = in + '0';
83 out[i + 1] = '\0';
84
85 uint8_t j = 0;
86 char tmp;
87
88 // revert the string
89 while (j < i) {
90 // swap out[j] and out[i]
91 tmp = out[j];
92 out[j] = out[i];
93 out[i] = tmp;
94
95 i--;
96 j++;
97 }
98 return true;
99}
100
101bool format_fpu64(char *dst, size_t dst_len, const uint64_t value, uint8_t decimals)
102{
103 char buffer[21] = {0};
104
105 if (dst_len == 0) {
106 return false;
107 }
108 // never leave a stale or partial amount in dst on failure
109 dst[0] = '\0';
110
111 if (!format_u64(buffer, sizeof(buffer), value)) {
112 return false;
113 }
114
115 size_t digits = strlen(buffer);
116
117 if (digits <= decimals) {
118 // "0." + (decimals - digits) leading zeros + digits + '\0'
119 if (dst_len <= 2 + decimals) {
120 return false;
121 }
122 *dst++ = '0';
123 *dst++ = '.';
124 memset(dst, '0', decimals - digits);
125 dst += decimals - digits;
126 memcpy(dst, buffer, digits);
127 dst[digits] = '\0';
128 }
129 else {
130 if (dst_len <= digits + 1 + decimals) {
131 return false;
132 }
133
134 const size_t shift = digits - decimals;
135 memcpy(dst, buffer, shift);
136 dst[shift] = '.';
137 memcpy(dst + shift + 1, buffer + shift, decimals);
138 dst[digits + 1] = '\0';
139 }
140
141 return true;
142}
143
144bool format_fpu64_trimmed(char *dst, size_t dst_len, const uint64_t value, uint8_t decimals)
145{
146 if (!format_fpu64(dst, dst_len, value, decimals)) {
147 return false;
148 }
149
150 size_t len = strnlen(dst, dst_len);
151 if (len == dst_len) {
152 dst[0] = '\0';
153 return false;
154 }
155
156 while (len > 0 && (dst[len - 1] == '0' || dst[len - 1] == '.')) {
157 if (dst[len - 1] == '.') {
158 dst[len - 1] = '\0';
159 return true;
160 }
161 len--;
162 }
163 dst[len] = '\0';
164 return true;
165}
166
167int format_hex(const uint8_t *in, size_t in_len, char *out, size_t out_len)
168{
169 if (out_len < 2 * in_len + 1) {
170 return -1;
171 }
172
173 const char hex[] = "0123456789ABCDEF";
174 size_t i = 0;
175 int written = 0;
176
177 while (i < in_len && (i * 2 + (2 + 1)) <= out_len) {
178 uint8_t high_nibble = (in[i] & 0xF0) >> 4;
179 *out = hex[high_nibble];
180 out++;
181
182 uint8_t low_nibble = in[i] & 0x0F;
183 *out = hex[low_nibble];
184 out++;
185
186 i++;
187 written += 2;
188 }
189
190 *out = '\0';
191
192 return written + 1;
193}
bool format_u64(char *out, size_t outLen, uint64_t in)
Definition format.c:65
int format_hex(const uint8_t *in, size_t in_len, char *out, size_t out_len)
Definition format.c:167
bool format_fpu64_trimmed(char *dst, size_t dst_len, const uint64_t value, uint8_t decimals)
Definition format.c:144
bool format_i64(char *dst, size_t dst_len, const int64_t value)
Definition format.c:24
bool format_fpu64(char *dst, size_t dst_len, const uint64_t value, uint8_t decimals)
Definition format.c:101