-
Notifications
You must be signed in to change notification settings - Fork 4
/
IoTThing.h
766 lines (650 loc) · 22.6 KB
/
IoTThing.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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
/**
USE OF THIS SOFTWARE IS GOVERNED BY THE TERMS AND CONDITIONS
OF THE LICENSE STATEMENT AND LIMITED WARRANTY FURNISHED WITH
THE PRODUCT.
<p/>
IN PARTICULAR, YOU WILL INDEMNIFY AND HOLD B2N LTD., ITS
RELATED COMPANIES AND ITS SUPPLIERS, HARMLESS FROM AND AGAINST ANY
CLAIMS OR LIABILITIES ARISING OUT OF THE USE, REPRODUCTION, OR
DISTRIBUTION OF YOUR PROGRAMS, INCLUDING ANY CLAIMS OR LIABILITIES
ARISING OUT OF OR RESULTING FROM THE USE, MODIFICATION, OR
DISTRIBUTION OF PROGRAMS OR FILES CREATED FROM, BASED ON, AND/OR
DERIVED FROM THIS SOURCE CODE FILE.
*/
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wmaybe-uninitialized"
#pragma GCC diagnostic ignored "-Wunused-variable"
#pragma GCC diagnostic ignored "-Wreorder"
#pragma GCC diagnostic ignored "-Wreturn-type"
#pragma GCC diagnostic ignored "-Wunused-parameter"
#pragma GCC diagnostic ignored "-Wint-to-pointer-cast"
#pragma GCC diagnostic ignored "-Wunused-value"
#pragma GCC diagnostic ignored "-Wunused-parameter"
#pragma GCC diagnostic ignored "-Wwrite-strings"
//class IoTThing
//{
// public:
// // constructor
// IoTThing(const char * ssid_,
// const char * pass_,
// uint64_t id_,
// uint8_t key_[16],
// uint64_t gateway_id_,
// const char * gateway_key_,
// void (* receive_callback_)(uint8_t buf_[], uint8_t size, int16_t rssi) = NULL
// )
//
// // call before using
// void init();
//
// // set id ( in case after start needs to be changed)
// void set_id(uint64_t id_ );
//
// // set key ( in case after start needs to be changed)
// void set_key(uint8_t key_[16]);
//
// // set gateway id ( in case after start needs to be changed)
// void set_gateway_id(uint64_t gateway_id_ );
//
// // set gateway key ( in case after start needs to be changed)
// void set_gateway_key(uint8_t key_[16])
//
// // set ssid and pass ( in case after start needs to be changed)
// void set_ssid_pass(const char * ssid_, const char * pass_)
//
// // register to receive callback after the message has been acknowledged from the gateway
// void register_ack(void (* ack_callback_)(int16_t rssi_));
//
// // register to receive callback after the message has failed/ in msec
// void register_timeout(void (* timeout_callback_)(uint16_t timeout_));
//
// // return signal strength in %
// uint8_t signal_strength();
//
// // return total messages sent
// uint32_t total_messages();
//
// //return total acknowledged messages send
// uint32_t ack_messages();
//
// //return received messages count
// uint32_t recv_messages();
//
// //return total retransmit messages send
// uint32_t retransmit_messages()
//
// //add discrete data
// //use this if you want for example to store decimal(floating-point) temperature between -20 and +60 in one byte
// // pos is the position from which the data will be written
// // e.g. : add_discrete(buf, 0, -20.0, 60.0, 23.65, 1);
// //you need to ensure that you at least have container_size bytes available in the buffer
// //1 <= conainer_size <= 4
// //on return pos will have the value of first free byte(next postion after the data)
// static void add_discrete(uint8_t buf_[], uint8_t &pos_, double min_, double max_, double value_, uint8_t container_size_);
//
// //read discrete value
// static double get_discrete(uint8_t buf_[], uint8_t &pos_, double min_, double max_, uint8_t container_size_);
//
// //add unsigned integer data up to 62bit ( 0 - 4611686018427387904) - the container size will be adjusted automatically depending on the value
// // pos is the position from which the data will be written
// //you need to ensure that you have at least 8 bytes available in the buffer - as this is the maximum bytes that the number can occupy in worst case scenario
// //on return pos will have the value of first free byte(next postion after the data)
// static void add_uint(uint8_t buf_[], uint8_t &pos_, uint64_t value_);
//
// // get unsigned integer value
// static uint64_t get_uint(uint8_t buf_[], uint8_t &pos_)
//
// // return false if a error is logged
// // max message size in bytes is 31
// bool send(uint8_t buf_[], uint8_t size_);
//
// // return true - if all previous tasks has been completed and ready to accept new data
// bool is_ready();
//
// // call if you want to stop retry's to send the message
// void cancel();
//
// // return true - if last message sending has hit timeout and failed
// bool timeout_hit();
//
// // please call in the main loop to be able to dispatch data and menage logic
// void work();
//};
#pragma push_macro("LOG64_ENABLED")
//#undef LOG64_ENABLED
#include "IoTGateway.h"
#define IoTThing_MIN_RSSI (-150)
#define IoTThing_MAX_MESSAGE_LEN 31
#define IoTThing_MESSAGE_TIMEOUT 60000
#define IoTThing_MAGIC 8606
#define IoTThing_OPEN_GATEWAY_ID 10
#define IoTThing_WAIT_NA 0
#define IoTThing_WAIT_ACK 1
#define IoTThing_WAIT_TIMEOUT 2
class IoTThing : public IoTGatewayCallback
{
public :
IoTThing(const char * ssid_,
const char * pass_,
uint64_t id_,
uint8_t key_[16],
uint64_t gateway_id_,
const char * gateway_key_,
void (* receive_callback_)(uint8_t buf_[], uint8_t size, int16_t rssi) = NULL ) :
id(id_),
ack_callback(NULL),
timeout_callback(NULL),
wait_ack(IoTThing_WAIT_NA),
receive_callback(receive_callback_),
gateway_id(gateway_id_)
{
memcpy(key, key_, 16);
gateway = new IoTGateway(ssid_, pass_, gateway_id_, gateway_key_, this);
}
public:
// call before using
void init()
{
gateway->init();
total = 0;
total_ack = 0;
total_recv = 0;
total_retransmit = 0;
seq = random(126) + 1; // as * -1 is used for ack
q = NULL;
buf_size = 0;
#if defined (LOG64_ENABLED)
LOG64_SET(F("IoTT: INIT"));
LOG64_NEW_LINE;
#endif
}
// set id ( in case after start needs to be changed)
void set_id(uint64_t id_ )
{
id = id_;
}
// set key ( in case after start needs to be changed)
void set_key(uint8_t key_[16])
{
memcpy(key, key_, 16);
}
// set gateway id ( in case after start needs to be changed)
void set_gateway_id(uint64_t gateway_id_ )
{
gateway_id = gateway_id_;
gateway->set_gateway_id(gateway_id_);
}
// set gateway key ( in case after start needs to be changed)
void set_gateway_key(const char * gateway_key_)
{
gateway->set_gateway_key(gateway_key_);
}
// set ssid ( in case after start needs to be changed)
void set_ssid_pass(const char * ssid_, const char * pass_)
{
gateway->set_ssid_pass(ssid_, pass_);
}
// register for to receive callback after the message has been acknowledged from the gateway
void register_ack(void (* ack_callback_)(int16_t rssi))
{
ack_callback = ack_callback_;
}
// register to receive callback after the message has failed/ in msec
void register_timeout(void (* timeout_callback_)(uint16_t timeout))
{
timeout_callback = timeout_callback_;
}
public:
// return signal strength in %
uint8_t signal_strength()
{
return gateway->signal_strength();
}
// return total messages sent
uint32_t total_messages()
{
return total;
}
//return total acknowledged messages send
uint32_t ack_messages()
{
return total_ack;
}
//return received messages count for the gateway
uint32_t recv_messages()
{
return total_recv;
}
//return total acknowledged messages send
uint32_t retransmit_messages()
{
return total_retransmit;
}
public:
//add discrete data
//use this if you want for example to store decimal(floating-point) temperature between -20 and +60 in one byte
// pos is the position from which the data will be written
// e.g. : add_discrete(buf, 0, -20.0, 60.0, 23.65, 1);
//you need to ensure that you at least have container_size bytes available in the buffer
//1 <= conainer_size <= 4
//on return pos will have the value of first free byte(next postion after the data)
static void add_discrete(uint8_t buf_[], uint8_t &pos_, double min_, double max_, double value_, uint8_t container_size_)
{
double dev_;
switch (container_size_)
{
case 1 : {
dev_ = (max_ - min_) / 255.0d;
} ; break;
case 2 : {
dev_ = (max_ - min_) / 65535.0d;
} ; break;
case 3 : {
dev_ = (max_ - min_) / 16777215.0d;
} ; break;
case 4 : {
dev_ = (max_ - min_) / 4294967295.0d;
} ; break;
default : dev_ = (max_ - min_) / 255.0d;
};
uint32_t dis_ = (uint32_t)round((value_ - min_) / dev_);
memcpy((void *)buf_[pos_], (void *) &dis_, container_size_);
pos_ += container_size_;
}
static double get_discrete(uint8_t buf_[], uint8_t &pos_, double min_, double max_, uint8_t container_size_)
{
double dev_;
switch (container_size_)
{
case 1 : {
dev_ = (max_ - min_) / 255.0d;
} ; break;
case 2 : {
dev_ = (max_ - min_) / 65535.0d;
} ; break;
case 3 : {
dev_ = (max_ - min_) / 16777215.0d;
} ; break;
case 4 : {
dev_ = (max_ - min_) / 4294967295.0d;
} ; break;
default : dev_ = (max_ - min_) / 255.0d;
};
uint32_t c = 0;
memcpy((void *) &c, (void *)buf_[pos_], container_size_);
pos_ += container_size_;
return (min_ + (((double)c) * dev_));
}
//add unsigned integer data up to 62bit ( 0 - 4611686018427387904) - the container will be adjusted automatically depending on the value
//you need to ensure that you have at least 8 bytes available in the buffer - as this is the maximum bytes that the number can occupy in worst case scenario
static void add_uint(uint8_t buf_[], uint8_t &pos_, uint64_t value_)
{
put_ots(value_, buf_, pos_);
}
static uint64_t get_uint(uint8_t buf_[], uint8_t &pos_)
{
return get_ots(buf_, pos_);
}
public:
// return false if a error is logged
// format byte(len), byte(crc), byte(seq), byte[4](receiver_id), byte[4](sender_id),byte[](encrypted_payload)
// seq <=+127 - 0 is not valid sequence
// payload max size 31
bool send(uint8_t buf_[], uint8_t size_)
{
if (q != NULL)
{
#if defined (LOG64_ENABLED)
LOG64_SET(F("IoTT: NO SPACE FOR MSG."));
LOG64_NEW_LINE;
#endif
return false;
}
if (size_ > (IoTThing_MAX_MESSAGE_LEN))
{
#if defined (LOG64_ENABLED)
LOG64_SET(F("IoTT: MSG TOO BIG["));
LOG64_SET(size_);
LOG64_SET(F("]"));
LOG64_NEW_LINE;
#endif
return false;
}
uint8_t *enc;
size_t enc_size;
enc = (uint8_t *) xxtea_encrypt(buf_, size_, key, &enc_size);
uint8_t raw[IoTThing_MAX_MESSAGE_LEN + 1 + 18];
uint8_t pos_;
//raw[0] = CRC
raw[1] = seq;
pos_ = 2;
put_ots(gateway_id, raw, pos_ );
put_ots(id, raw, pos_ );
memcpy(&raw[pos_], enc , enc_size);
free(enc);
pos_ += enc_size;
raw[0] = crc(raw, pos_, 1);
memcpy(buf, raw , pos_);
buf_size = pos_;
q = buf;
wait_ack = IoTThing_WAIT_ACK;
wait_ack_start = millis();
return true;
}
// return true - if all previouse tasks has been completed and ready to accept new data
bool is_ready()
{
return (q == NULL) && ((wait_ack == IoTThing_WAIT_NA) || (wait_ack == IoTThing_WAIT_TIMEOUT));
}
// return true - if last message sending has hit timeout and failed
bool timeout_hit()
{
return (wait_ack == IoTThing_WAIT_TIMEOUT);
}
// call if you want to stop retry's to send the message
void cancel()
{
if (wait_ack == IoTThing_WAIT_ACK)
{
wait_ack = IoTThing_WAIT_TIMEOUT;
#if defined (LOG64_ENABLED)
LOG64_SET(F("IoTT: MESSAGE CANCELED"));
LOG64_NEW_LINE;
#endif
}
}
public :
virtual void received(uint8_t buf_[], uint8_t size_)
{
total_recv++;
if (receive_callback != NULL)
{
int16_t rssi_ = (IoTGateway_MIN_RSSI * (100 - ((int16_t)gateway->signal_strength()))) / 100;
//crc
//seq
uint8_t sz = 2;
get_ots(buf_, sz); // target
get_ots(buf_, sz); // source
size_t out_size = 0;
uint8_t * dec = xxtea_decrypt(&buf_[sz], size_ - sz, key, & out_size);
if ((dec != NULL) && (out_size > 0) && (dec[0] == crc(dec, (uint16_t)out_size, 1)))
{
receive_callback(&dec[1], (uint8_t)(out_size - 1), rssi_);
}
else
{
#if defined (LOG64_ENABLED)
LOG64_SET(F("IoTT: CRC CHECK FOR RECEIVED MSG FAILED"));
LOG64_NEW_LINE;
#endif
}
if (dec != NULL)
{
free(dec);
}
}
}
virtual void data_sent_successfully()
{
wait_ack = IoTThing_WAIT_NA;
// ack
total_ack++;
if (ack_callback != NULL)
{
int16_t rssi_ = (IoTGateway_MIN_RSSI * (100 - ((int16_t)gateway->signal_strength()))) / 100;
ack_callback(rssi_);
}
}
// please call in the main loop to be able to dispatch data and menage logic
void work()
{
gateway->work();
if (wait_ack == IoTThing_WAIT_ACK)
{
if ((wait_ack == IoTThing_WAIT_ACK) && (((uint32_t)(((uint32_t)millis()) - wait_ack_start)) >= IoTThing_MESSAGE_TIMEOUT))
{
wait_ack == IoTThing_WAIT_TIMEOUT;
if (timeout_callback != NULL)
{
timeout_callback(IoTThing_MESSAGE_TIMEOUT);
}
cancel();
}
}
if (q != NULL)
{
gateway->send(buf, buf_size);
total++;
q = NULL;
if ((++seq) > 126)
{
seq = 1; // as * -1 is used for ack
}
buf_size = 0;
}
};
private:
// calculate checksum
uint8_t crc(uint8_t buf_[],
uint16_t end_index_,
uint16_t start_index)
{
uint32_t res = IoTThing_MAGIC; // add magic
for (uint16_t i = start_index; i < end_index_; i++)
{
uint8_t c = buf_[i];
res = (res << 1) ^ c;
res = res & 0xFFFFFFFF;
}
return (uint8_t)(res & 0xFF);
}
private:
static uint64_t get_ots(uint8_t buf_[], uint8_t & pos_)
{
uint8_t start_ = pos_;
switch (buf_[start_] & 0x3)
{
case 0:
{
pos_ += 1;
return (uint64_t) ((buf_[start_] >> 2) & 0x3F);
}
case 1:
{
pos_ += 2;
return (uint64_t)((((buf_[start_ + 1] << 8) | (buf_[start_])) >> 2) & 0x3FFF);
}
case 2:
{
pos_ += 4;
return (uint64_t)((((((uint32_t)buf_[start_ + 3]) << 24) | (((uint32_t)buf_[start_ + 2]) << 16) | (((uint32_t)buf_[start_ + 1]) << 8) | ((uint32_t)buf_[start_])) >> 2) & 0x3FFFFFFFL);
}
case 3:
{
pos_ += 8;
return (((((uint64_t)buf_[start_ + 7]) << 56) | (((uint64_t)buf_[start_ + 6]) << 48) | (((uint64_t)buf_[start_ + 5]) << 40) | (((uint64_t)buf_[start_ + 4]) >> 32) | (((uint64_t)buf_[start_ + 3]) << 24)
| (((uint64_t)buf_[start_ + 2]) << 16) | (((uint64_t)buf_[start_ + 1]) << 8) | ((uint64_t)buf_[start_])) >> 2) & 0x3FFFFFFFFFFFFFFFLL;
}
}
}
static void put_ots(uint64_t v, uint8_t buf_[], uint8_t & pos_ )
{
v = (v << 2);
uint8_t start_ = pos_;
if (v < 0x40LL)
{
buf_[start_] = (uint8_t) (v & 0xFFLL);
pos_ += 1;
}
else if (v < 0x4000LL)
{
v = v | 1;
buf_[start_ + 1] = (uint8_t)((v >> 8) & 0xFFLL);
buf_[start_] = (uint8_t) (v & 0xFFLL);
pos_ += 2;
}
else if (v < 0x40000000LL)
{
v = v | 2;
buf_[start_ + 3] = (uint8_t)((v >> 24) & 0xFFLL);
buf_[start_ + 2] = (uint8_t)((v >> 16) & 0xFFLL);
buf_[start_ + 1] = (uint8_t)((v >> 8) & 0xFFLL);
buf_[start_] = (uint8_t) (v & 0xFFLL);
pos_ += 4;
}
else if (v < 0x4000000000000000LL)
{
v = v | 3;
buf_[start_ + 7] = (uint8_t)((v >> 56) & 0xFFLL);
buf_[start_ + 6] = (uint8_t)((v >> 48) & 0xFFLL);
buf_[start_ + 5] = (uint8_t)((v >> 40) & 0xFFLL);
buf_[start_ + 4] = (uint8_t)((v >> 32) & 0xFFLL);
buf_[start_ + 3] = (uint8_t)((v >> 24) & 0xFFLL);
buf_[start_ + 2] = (uint8_t)((v >> 16) & 0xFFLL);
buf_[start_ + 1] = (uint8_t)((v >> 8) & 0xFFLL);
buf_[start_] = (uint8_t) (v & 0xFFLL);
pos_ += 8;
}
}
private :
uint8_t buf[IoTThing_MAX_MESSAGE_LEN + 18 + 1];
uint8_t buf_size;
int8_t seq; // as * -1 is used for ack
uint8_t * q; // used as flag
uint32_t total;
uint32_t total_ack;
uint32_t total_recv;
uint32_t total_retransmit;
uint8_t resetPin;
uint8_t __attribute__ ((aligned (4))) key[16];
uint64_t id;
void (* ack_callback)(int16_t rssi_);
void (* timeout_callback)(uint16_t timeout_);
uint64_t gateway_id;
uint8_t wait_ack;
uint32_t wait_ack_start;
void (* receive_callback)(uint8_t buf_[], uint8_t size_, int16_t rssi_);
IoTGateway * gateway;
private :
/**********************************************************\
| |
| XXTEA encryption algorithm library for C. |
| |
| Encryption Algorithm Authors: |
| David J. Wheeler |
| Roger M. Needham |
| |
| Code Authors: Chen fei <[email protected]> |
| Ma Bingyao <[email protected]> |
| LastModified: Feb 7, 2016 |
| |
| With changes |
| |
| License (MIT) |
| |
\**********************************************************/
#define MX (((z >> 5) ^ (y << 2)) + ((y >> 3) ^ (z << 4))) ^ ((sum ^ y) + (key[(p & 3) ^ e] ^ z))
#define DELTA 0x9e3779b9
uint32_t * xxtea_to_uint_array_size(const uint8_t * data, size_t len, size_t * out_len) {
uint32_t *out;
++len;
size_t n = (((len & 3) == 0) ? (len >> 2) : ((len >> 2) + 1));
if (n < 2) n = 2;
--len;
out = (uint32_t *)malloc(n << 2);
if (!out) return NULL;
*out_len = n;
memcpy(&((uint8_t *)out)[1], data, len);
((uint8_t *)out)[0] = (uint8_t)len;
return out;
}
uint32_t * xxtea_to_uint_array(const uint8_t * data, size_t len, size_t * out_len) {
uint32_t *out;
out = (uint32_t *)malloc(len);
memcpy(out, data, len);
*out_len = len >> 2;
return out;
}
uint8_t * xxtea_to_ubyte_array_size(const uint32_t * data, size_t len, size_t * out_len) {
uint8_t *out;
size_t n;
n = ((uint8_t *)data)[0];
out = (uint8_t *)malloc(n);
memcpy(out, &((uint8_t *)data)[1], n);
*out_len = n;
return out;
}
uint8_t * xxtea_to_ubyte_array(const uint32_t * data, size_t len, size_t * out_len) {
uint8_t *out;
size_t n;
n = len << 2;
out = (uint8_t *)malloc(n);
memcpy(out, data, n);
*out_len = n;
return out;
}
uint32_t * xxtea_uint_decrypt(uint32_t * data, size_t len, uint32_t * key)
{
uint32_t n = (uint32_t)len - 1;
uint32_t z, y = data[0], p, q = 6 + 52 / (n + 1), sum = q * DELTA, e;
if (n < 1) return data;
while (sum != 0)
{
e = sum >> 2 & 3;
for (p = n; p > 0; p--)
{
z = data[p - 1];
y = data[p] -= MX;
}
z = data[n];
y = data[0] -= MX;
sum -= DELTA;
}
return data;
}
uint32_t * xxtea_uint_encrypt(uint32_t * data, size_t len, uint32_t * key)
{
uint32_t n = (uint32_t)len - 1;
uint32_t z = data[n], y, p, q = 6 + 52 / (n + 1), sum = 0, e;
if (n < 1) return data;
while (0 < q--)
{
sum += DELTA;
e = sum >> 2 & 3;
for (p = 0; p < n; p++)
{
y = data[p + 1];
z = data[p] += MX;
}
y = data[0];
z = data[n] += MX;
}
return data;
}
uint8_t * xxtea_encrypt(const uint8_t * data, size_t len, const uint8_t * key, size_t * out_len)
{
uint8_t *out;
uint32_t *data_array;
size_t data_len;
if (!len) return NULL;
data_array = xxtea_to_uint_array_size(data, len, &data_len);
if (!data_array) return NULL;
out = xxtea_to_ubyte_array(xxtea_uint_encrypt(data_array, data_len, (uint32_t *)key), data_len, out_len);
free(data_array);
return out;
}
uint8_t * xxtea_decrypt(const uint8_t * data, size_t len, const uint8_t * key, size_t * out_len)
{
uint8_t *out;
uint32_t *data_array;
size_t data_len;
if (!len) return NULL;
data_array = xxtea_to_uint_array(data, len, &data_len);
if (!data_array) return NULL;
out = xxtea_to_ubyte_array_size(xxtea_uint_decrypt(data_array, data_len, (uint32_t *)key), data_len, out_len);
free(data_array);
return out;
}
};
#pragma pop_macro("LOG64_ENABLED")
#pragma GCC diagnostic pop