-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconverter.c
169 lines (147 loc) · 4.91 KB
/
converter.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/*
* Copyright (c) 2017 IOTA AS, IOTA Foundation & Developers (https://iotatoken.com)
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
* Adapted from https://github.com/iotaledger/ccurl/blob/master/src/lib/util/converter.c
*/
#include <string.h>
#include "converter.h"
char BYTE_TO_TRITS_MAPPINGS
[HASH_LENGTH][TRITS_IN_BYTE];
char TRYTE_TO_TRITS_MAPPINGS
[TRYTE_SPACE][TRITS_IN_TRYTE];
static const char* TRYTE_ALPHABET = TRYTE_STRING;
char long_value(int8_t* const trits, const int offset, const int size) {
int i;
char value = 0;
for (i = size; i-- > 0;) {
value = value * RADIX + trits[offset + i];
}
return value;
}
char* bytes_from_trits(int8_t* const trits, const int offset, const int size) {
int i, j;
int length =
(size + TRITS_IN_BYTE - 1) / TRITS_IN_BYTE;
char* bytes = (char*)malloc(sizeof(char) * length);
for (i = 0; i < length; i++) {
char value = 0;
for (j = (size - i * TRITS_IN_BYTE) < 5
? (size - i * TRITS_IN_BYTE)
: TRITS_IN_BYTE;
j-- > 0;) {
value = value * RADIX + trits[offset + i * TRITS_IN_BYTE + j];
}
bytes[i] = (char)value;
}
return bytes;
}
void getTrits(const char* bytes, int bytelength, int8_t* const trits,
int length) {
int i;
int offset = 0;
for (i = 0; i < bytelength && offset < length; i++) {
memcpy(
trits + offset,
BYTE_TO_TRITS_MAPPINGS
[bytes[i] < 0
? (bytes[i] +
HASH_LENGTH /* length of what? first? BYTE_TO_TRITS_MAPPINGS.length */)
: bytes[i]],
sizeof(char) * (length - offset < TRITS_IN_BYTE
? (length - offset)
: TRITS_IN_BYTE));
offset += TRITS_IN_BYTE;
}
while (offset < length) {
trits[offset++] = 0;
}
}
void trits_from_trytes(const char* trytes, int length, int8_t* trits) {
int i;
for (i = 0; i < length; i++) {
memcpy(trits + i * TRITS_IN_TRYTE,
TRYTE_TO_TRITS_MAPPINGS[strchr(TRYTE_ALPHABET, trytes[i]) -
TRYTE_ALPHABET],
sizeof(char) * TRITS_IN_TRYTE);
}
}
void copyTrits(char const value, char* const destination, const int offset,
const int size) {
int i;
char absoluteValue = value < 0 ? -value : value;
for (i = 0; i < size; i++) {
int remainder = (int)(absoluteValue % RADIX);
absoluteValue /= RADIX;
if (remainder > MAX_TRIT_VALUE) {
remainder = MIN_TRIT_VALUE;
absoluteValue++;
}
destination[offset + i] = remainder;
}
if (value < 0) {
for (i = 0; i < size; i++) {
destination[offset + i] = -destination[offset + i];
}
}
}
void trytes_from_trits(int8_t* const trits, const int size, char* trytes) {
int i;
const int length =
(size + TRITS_IN_TRYTE - 1) / TRITS_IN_TRYTE;
trytes[length] = '\0';
for (i = 0; i < length; i++) {
char j = trits[i * 3] + trits[i * 3 + 1] * 3 +
trits[i * 3 + 2] * 9;
if (j < 0) {
j += 27;
}
trytes[i] = TRYTE_ALPHABET[(size_t)j];
}
}
char tryteValue(int8_t* const trits, const int offset) {
return trits[offset] + trits[offset + 1] * 3 + trits[offset + 2] * 9;
}
static void increment(int8_t* trits, int size) {
int i;
for (i = 0; i < size; i++) {
if (++trits[i] > MAX_TRIT_VALUE) {
trits[i] = MIN_TRIT_VALUE;
} else {
break;
}
}
}
void init_converter (void) __attribute__ ((constructor));
void init_converter() {
int i;
int8_t trits[TRITS_IN_BYTE];
memset(trits, 0, TRITS_IN_BYTE * sizeof(int8_t));
for (i = 0; i < HASH_LENGTH; i++) {
memcpy(&(BYTE_TO_TRITS_MAPPINGS[i]), trits,
TRITS_IN_BYTE * sizeof(char));
increment(trits, TRITS_IN_BYTE);
}
for (i = 0; i < TRYTE_SPACE; i++) {
memcpy(&(TRYTE_TO_TRITS_MAPPINGS[i]), trits,
TRITS_IN_TRYTE * sizeof(int8_t));
increment(trits, TRITS_IN_TRYTE);
}
}