-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtd64_internal.h
More file actions
191 lines (177 loc) · 7.3 KB
/
td64_internal.h
File metadata and controls
191 lines (177 loc) · 7.3 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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
//
// td64_internal.h
// td512
//
// Created by L. Stevan Leonard on 12/9/21.
// Copyright © 2021-2022 L. Stevan Leonard. All rights reserved.
/*
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
//
#ifndef td64_internal_h
#define td64_internal_h
#define MIN_STRING_MODE_EXTENDED_VALUES 16
static const uint32_t encodingBits[64]={1,1,2,2,3,3,3,3,4,4,4,4,4,4,4,4,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6};
static const uint32_t bitMask[]={0,1,3,7,15,31,63,127,255,511};
static inline void esmOutputRemainder(unsigned char *outValsT, uint32_t *thisOutIx, uint32_t *nextOutBit, uint64_t *outBits)
{
if (*nextOutBit == 0)
return; // no bits to output
uint32_t shiftPos=0;
int32_t bitsRemaining=*nextOutBit-8;
// output bits that remain
outValsT[(*thisOutIx)++] = (unsigned char)*outBits;
while (bitsRemaining > 0)
{
shiftPos += 8;
outValsT[(*thisOutIx)++] = (unsigned char)(*outBits >> shiftPos);
bitsRemaining -= 8;
}
*nextOutBit = 0;
} // end esmOutputRemainder
static inline void esmOutputOutBits(unsigned char *outValsT, uint32_t *thisOutIx, uint64_t *outBits)
{
// copy 64 bits to output
outValsT[(*thisOutIx)++] = (unsigned char)*outBits;
outValsT[(*thisOutIx)++] = (unsigned char)(*outBits>>8);
outValsT[(*thisOutIx)++] = (unsigned char)(*outBits>>16);
outValsT[(*thisOutIx)++] = (unsigned char)(*outBits>>24);
outValsT[(*thisOutIx)++] = (unsigned char)(*outBits>>32);
outValsT[(*thisOutIx)++] = (unsigned char)(*outBits>>40);
outValsT[(*thisOutIx)++] = (unsigned char)(*outBits>>48);
outValsT[(*thisOutIx)++] = (unsigned char)(*outBits>>56);
} // end esmOutputOutBits
static inline void thisOutIx2(unsigned char *outValsT, const uint32_t nBits, const uint64_t bitVal, uint32_t *thisOutIx, uint32_t *nextOutBit, uint64_t *outBits)
{
// output 1 to 64 bits
*outBits |= bitVal << *nextOutBit;
*nextOutBit += nBits;
if (*nextOutBit >= 64)
{
esmOutputOutBits(outValsT, thisOutIx, outBits);
// init outBits with remainder of bits from current output
*nextOutBit -= 64;
*outBits = bitVal >> (nBits - *nextOutBit);
}
} // end thisOutIx2
static int32_t encode7bitsInternal(const unsigned char *inVals, unsigned char *outVals, const uint32_t nValues)
{
// for internal use: output 7 bytes for each 8-byte group, then remaining bytes
uint32_t nextOutVal=0;
uint32_t nextInVal=0;
uint32_t val1;
uint32_t val2;
if (nValues < 8)
return 0;
// process groups of 8 bytes to output 7 bytes
while (nextInVal + 7 < nValues)
{
// copy groups of 8 7-bit vals into 7 bytes
val1 = inVals[nextInVal++];
val2 = inVals[nextInVal++];
outVals[nextOutVal++] = (unsigned char)(val1 | (val2 << 7));
val1 = inVals[nextInVal++];
outVals[nextOutVal++] = (unsigned char)((val2 >> 1) | (val1 << 6));
val2 = inVals[nextInVal++];
outVals[nextOutVal++] = (unsigned char)((val1 >> 2) | (val2 << 5));
val1 = inVals[nextInVal++];
outVals[nextOutVal++] = (unsigned char)((val2 >> 3) | (val1 << 4));
val2 = inVals[nextInVal++];
outVals[nextOutVal++] = (unsigned char)((val1 >> 4) | (val2 << 3));
val1 = inVals[nextInVal++];
outVals[nextOutVal++] = (unsigned char)((val2 >> 5) | (val1 << 2));
val2 = inVals[nextInVal++];
outVals[nextOutVal++] = (unsigned char)((val1 >> 6) | (val2 << 1));
}
while (nextInVal < nValues)
{
// output final values as full bytes because no bytes saved, only bits
outVals[nextOutVal++] = inVals[nextInVal++];
if (nextInVal == nValues)
break;
outVals[nextOutVal++] = inVals[nextInVal++];
if (nextInVal == nValues)
break;
outVals[nextOutVal++] = inVals[nextInVal++];
if (nextInVal == nValues)
break;
outVals[nextOutVal++] = inVals[nextInVal++];
if (nextInVal == nValues)
break;
outVals[nextOutVal++] = inVals[nextInVal++];
if (nextInVal == nValues)
break;
outVals[nextOutVal++] = inVals[nextInVal++];
if (nextInVal == nValues)
break;
outVals[nextOutVal++] = inVals[nextInVal++];
break;
}
return (int32_t)nextOutVal;
} // end encode7bitsInternal
static int32_t decode7bitsInternal(const unsigned char *inVals, unsigned char *outVals, const uint32_t nOriginalValues, uint32_t *bytesProcessed)
{
// decode values directly into outVals
uint32_t nextOutVal=0;
uint32_t nextInVal=0;
uint32_t val1;
uint32_t val2;
if (nOriginalValues < 8)
return -33; // must have at least one group of 8 bytes to compress
// process groups of 8 bytes to output 7 bytes
while (nextOutVal + 7 < nOriginalValues)
{
// move groups of 7-bit vals into 8 bytes
val1 = inVals[nextInVal++];
outVals[nextOutVal++] = (unsigned char)(val1 & 127);
val2 = inVals[nextInVal++];
outVals[nextOutVal++] = (unsigned char)(((val2 << 1) & 127) | (val1 >> 7));
val1 = inVals[nextInVal++];
outVals[nextOutVal++] = (unsigned char)(((val1 << 2) & 127) | (val2 >> 6));
val2 = inVals[nextInVal++];
outVals[nextOutVal++] = (unsigned char)(((val2 << 3) & 127) | (val1 >> 5));
val1 = inVals[nextInVal++];
outVals[nextOutVal++] = (unsigned char)(((val1 << 4) & 127) | (val2 >> 4));
val2 = inVals[nextInVal++];
outVals[nextOutVal++] = (unsigned char)(((val2 << 5) & 127) | (val1 >> 3));
val1 = inVals[nextInVal++];
outVals[nextOutVal++] = (unsigned char)(((val1 << 6) & 127) | (val2 >> 2));
outVals[nextOutVal++] = (unsigned char)val1 >> 1;
}
while (nextOutVal < nOriginalValues)
{
// output final values as full bytes because no bytes saved, only bits
outVals[nextOutVal++] = inVals[nextInVal++];
if (nextOutVal == nOriginalValues)
break;
outVals[nextOutVal++] = inVals[nextInVal++];
if (nextOutVal == nOriginalValues)
break;
outVals[nextOutVal++] = inVals[nextInVal++];
if (nextOutVal == nOriginalValues)
break;
outVals[nextOutVal++] = inVals[nextInVal++];
if (nextOutVal == nOriginalValues)
break;
outVals[nextOutVal++] = inVals[nextInVal++];
if (nextOutVal == nOriginalValues)
break;
outVals[nextOutVal++] = inVals[nextInVal++];
if (nextOutVal == nOriginalValues)
break;
outVals[nextOutVal++] = inVals[nextInVal++];
break;
}
*bytesProcessed = nextInVal;
return (int32_t)nOriginalValues;
} // end decode7bitsInternal
#endif /* td64_internal_h */