-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmsg.h
222 lines (176 loc) · 4.47 KB
/
msg.h
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
typedef struct{
uint8_t *Data;
uintptr_t Size;
}String_t;
typedef struct{
uint8_t Data[64];
uintptr_t Size;
}BitString_t;
sint32_t MSG_ReadString(uint8_t **Data, uintptr_t *Size, String_t *String){
String->Data = *Data;
String->Size = 0;
while(String->Size != *Size){
if((*Data)[String->Size] == 0){
*Data += String->Size + 1;
*Size -= String->Size + 1;
return 0;
}
String->Size++;
}
*Data += *Size;
*Size = 0;
return -1;
}
sint32_t MSG_ReadBytes(
uint8_t **Source, uintptr_t *SourceSize,
void *Destination, uintptr_t DestinationSize
){
if(*SourceSize < DestinationSize){
return -1;
}
for(uintptr_t i = 0; i < DestinationSize; i++){
((uint8_t *)Destination)[i] = (*Source)[i];
}
*Source += DestinationSize;
*SourceSize -= DestinationSize;
return 0;
}
sint32_t MSG_ReadBits(
uint8_t *Source, uintptr_t *SourceIndex, uintptr_t SourceSize,
void *Destination, uintptr_t DestinationSize
){
if(SourceSize * 8 - *SourceIndex < DestinationSize){
return -1;
}
uintptr_t DestinationIndex = 0;
while(DestinationSize){
uint8_t m = *SourceIndex % 8;
uint8_t l = 8 - m;
if(l > DestinationSize){
l = DestinationSize;
}
DestinationSize -= l;
uint8_t n = Source[*SourceIndex / 8];
*SourceIndex += l;
n >>= m;
n &= (uint8_t)(((uint16_t)1 << l) - 1);
while(l){
uint8_t dm = DestinationIndex % 8;
uint8_t dl = 8 - dm;
if(dl > l){
dl = l;
}
((uint8_t *)Destination)[DestinationIndex / 8] |= n << dm;
l -= dl;
n >>= dl;
DestinationIndex += dl;
}
}
return 0;
}
sint32_t MSG_ReadBitString(
uint8_t *Source, uintptr_t *SourceIndex, uintptr_t SourceSize,
BitString_t *BitString
){
BitString->Size = 0;
while(1){
if(BitString->Size == sizeof(BitString->Data)){
PR_abort();
}
BitString->Data[BitString->Size] = 0;
if(MSG_ReadBits(Source, SourceIndex, SourceSize, &BitString->Data[BitString->Size], 8) != 0){
return -1;
}
if(BitString->Data[BitString->Size] == 0){
break;
}
BitString->Size++;
}
return 0;
}
sint32_t MSG_ReadBitCoord(
uint8_t *Source, uintptr_t *SourceIndex, uintptr_t SourceSize,
f32_t *f
){
uint16_t intval = 0;
if(MSG_ReadBits(Source, SourceIndex, SourceSize, &intval, 1)){return -1;}
uint8_t fractval = 0;
if(MSG_ReadBits(Source, SourceIndex, SourceSize, &fractval, 1)){return -1;}
if(intval || fractval){
uint8_t signbit = 0;
if(MSG_ReadBits(Source, SourceIndex, SourceSize, &signbit, 1)){return -1;}
if(intval){
intval = 0;
if(MSG_ReadBits(Source, SourceIndex, SourceSize, &intval, 12)){return -1;}
}
if(fractval){
fractval = 0;
if(MSG_ReadBits(Source, SourceIndex, SourceSize, &fractval, 3)){return -1;}
}
*f = (f32_t)(fractval / 8.0 + intval);
if(signbit){
*f = -*f;
}
}
return 0;
}
sint32_t MSG_ReadBitVec3Coord(
uint8_t *Source, uintptr_t *SourceIndex, uintptr_t SourceSize,
f32_t *x, f32_t *y, f32_t *z
){
*x = 0;
*y = 0;
*z = 0;
uint8_t xflag = 0;
uint8_t yflag = 0;
uint8_t zflag = 0;
if(MSG_ReadBits(Source, SourceIndex, SourceSize, &xflag, 1)){return -1;}
if(MSG_ReadBits(Source, SourceIndex, SourceSize, &yflag, 1)){return -1;}
if(MSG_ReadBits(Source, SourceIndex, SourceSize, &zflag, 1)){return -1;}
if(xflag){
if(MSG_ReadBitCoord(Source, SourceIndex, SourceSize, x)){return -1;}
}
if(yflag){
if(MSG_ReadBitCoord(Source, SourceIndex, SourceSize, y)){return -1;}
}
if(zflag){
if(MSG_ReadBitCoord(Source, SourceIndex, SourceSize, z)){return -1;}
}
return 0;
}
sint32_t MSG_WriteBits(
void *Source, uintptr_t SourceSize,
uint8_t *Destination, uintptr_t *DestinationIndex, uintptr_t DestinationSize
){
if(SourceSize > DestinationSize * 8 - *DestinationIndex){
return -1;
}
uintptr_t SourceIndex = 0;
while(SourceSize){
uint8_t m = SourceIndex % 8;
uint8_t l = 8 - m;
if(l > SourceSize){
l = SourceSize;
}
SourceSize -= l;
uint8_t n = ((uint8_t *)Source)[SourceIndex / 8];
SourceIndex += l;
n >>= m;
n &= (uint8_t)(((uint16_t)1 << l) - 1);
while(l){
uint8_t dm = *DestinationIndex % 8;
uint8_t dl = 8 - dm;
if(dl > l){
dl = l;
}
if(dm == 0){
Destination[*DestinationIndex / 8] = 0;
}
Destination[*DestinationIndex / 8] |= n << dm;
l -= dl;
n >>= dl;
*DestinationIndex += dl;
}
}
return 0;
}