This repository has been archived by the owner on Feb 14, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 64
/
bitcoinAmount.c
136 lines (125 loc) · 3.92 KB
/
bitcoinAmount.c
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
/*
*******************************************************************************
* BTChip Bitcoin Hardware Wallet C test interface
* (c) 2014 BTChip - 1BTChip7VfTnrPra5jqci7ejnMguuHogTn
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
********************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <inttypes.h>
#include "bitcoinAmount.h"
// Shamelessly borrowed from Bitcoin Core to avoid rounding issues
static const int64_t CENT = 1000000;
static const int64_t COIN = 100000000;
inline int64_t atoi64(const char* psz)
{
#ifdef _MSC_VER
return _atoi64(psz);
#else
return strtoll(psz, NULL, 10);
#endif
}
int parseStringAmount(char *amount, int64_t* result) {
int strOffset = 0;
char strWhole[11];
int64_t nUnits = 0;
int64_t nWhole;
int64_t nValue;
const char* p = amount;
while (isspace(*p))
p++;
for (; *p; p++)
{
if (*p == '.')
{
p++;
int64_t nMult = CENT*10;
while (isdigit(*p) && (nMult > 0))
{
nUnits += nMult * (*p++ - '0');
nMult /= 10;
}
break;
}
if (isspace(*p))
break;
if (!isdigit(*p))
return -1;
strWhole[strOffset] = *p;
strOffset++;
if (strOffset > 10) {
return -1;
}
}
strWhole[strOffset] = '\0';
for (; *p; p++)
if (!isspace(*p))
return -1;
if (nUnits < 0 || nUnits > COIN)
return -1;
nWhole = atoi64(strWhole);
nValue = nWhole*COIN + nUnits;
*result = nValue;
return 1;
}
void writeHexAmount(int64_t amount, unsigned char *buffer) {
*(buffer) = (amount & 0xff);
*(buffer + 1) = ((amount >> 8) & 0xff);
*(buffer + 2) = ((amount >> 16) & 0xff);
*(buffer + 3) = ((amount >> 24) & 0xff);
*(buffer + 4) = ((amount >> 32) & 0xff);
*(buffer + 5) = ((amount >> 40) & 0xff);
*(buffer + 6) = ((amount >> 48) & 0xff);
*(buffer + 7) = ((amount >> 56) & 0xff);
}
void writeHexAmountBE(int64_t amount, unsigned char *buffer) {
*(buffer + 7) = (amount & 0xff);
*(buffer + 6) = ((amount >> 8) & 0xff);
*(buffer + 5) = ((amount >> 16) & 0xff);
*(buffer + 4) = ((amount >> 24) & 0xff);
*(buffer + 3) = ((amount >> 32) & 0xff);
*(buffer + 2) = ((amount >> 40) & 0xff);
*(buffer + 1) = ((amount >> 48) & 0xff);
*(buffer) = ((amount >> 56) & 0xff);
}
int64_t parseHexAmount(unsigned char *buffer) {
int64_t result = 0;
result += ((int64_t)*buffer);
result += ((int64_t)*(buffer + 1)) << 8;
result += ((int64_t)*(buffer + 2)) << 16;
result += ((int64_t)*(buffer + 3)) << 24;
result += ((int64_t)*(buffer + 4)) << 32;
result += ((int64_t)*(buffer + 5)) << 40;
result += ((int64_t)*(buffer + 6)) << 48;
result += ((int64_t)*(buffer + 7)) << 56;
return result;
}
void formatAmount(int64_t amount, char *result, size_t length) {
char tmp[20];
int i;
int64_t n_abs = (amount > 0 ? amount : -amount);
int64_t quotient = n_abs/COIN;
int64_t remainder = n_abs%COIN;
tmp[19] = '\0';
snprintf(tmp, sizeof(tmp) - 1, "%"PRId64".%08"PRId64"", quotient, remainder);
// Right-trim excess zeros before the decimal point:
int nTrim = 0;
for (i = strlen(tmp)-1; (tmp[i] == '0' && isdigit(tmp[i-2])); --i)
++nTrim;
if (nTrim)
tmp[strlen(tmp) - nTrim] = '\0';
strncpy(result, tmp, length);
}