forked from meganz/sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.cpp
More file actions
2444 lines (2106 loc) · 65.4 KB
/
utils.cpp
File metadata and controls
2444 lines (2106 loc) · 65.4 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
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
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* @file utils.cpp
* @brief Mega SDK various utilities and helper classes
*
* (c) 2013-2014 by Mega Limited, Auckland, New Zealand
*
* This file is part of the MEGA SDK - Client Access Engine.
*
* Applications using the MEGA API must present a valid application key
* and comply with the the rules set forth in the Terms of Service.
*
* The MEGA SDK 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.
*
* @copyright Simplified (2-clause) BSD License.
*
* You should have received a copy of the license along with this
* program.
*/
#include "mega/utils.h"
#include "mega/logging.h"
#include "mega/megaclient.h"
#include "mega/base64.h"
#include "mega/serialize64.h"
#include "mega/filesystem.h"
#include <iomanip>
#if defined(_WIN32) && defined(_MSC_VER)
#include <sys/timeb.h>
#endif
#ifdef __APPLE__
#include <sys/sysctl.h>
#endif
namespace mega {
string toNodeHandle(handle nodeHandle)
{
char base64Handle[12];
Base64::btoa((byte*)&(nodeHandle), MegaClient::NODEHANDLE, base64Handle);
return string(base64Handle);
}
string toHandle(handle h)
{
char base64Handle[14];
Base64::btoa((byte*)&(h), sizeof h, base64Handle);
return string(base64Handle);
}
CacheableWriter::CacheableWriter(string& d)
: dest(d)
{
}
void CacheableWriter::serializebinary(byte* data, size_t len)
{
dest.append((char*)data, len);
}
void CacheableWriter::serializechunkmacs(const chunkmac_map& m)
{
m.serialize(dest);
}
void CacheableWriter::serializecstr(const char* field, bool storeNull)
{
unsigned short ll = (unsigned short)(field ? strlen(field) + (storeNull ? 1 : 0) : 0);
dest.append((char*)&ll, sizeof(ll));
dest.append(field, ll);
}
void CacheableWriter::serializepstr(const string* field)
{
unsigned short ll = (unsigned short)(field ? field->size() : 0);
dest.append((char*)&ll, sizeof(ll));
if (field) dest.append(field->data(), ll);
}
void CacheableWriter::serializestring(const string& field)
{
unsigned short ll = (unsigned short)field.size();
dest.append((char*)&ll, sizeof(ll));
dest.append(field.data(), ll);
}
void CacheableWriter::serializecompressed64(int64_t field)
{
byte buf[sizeof field+1];
dest.append((const char*)buf, Serialize64::serialize(buf, field));
}
void CacheableWriter::serializei64(int64_t field)
{
dest.append((char*)&field, sizeof(field));
}
void CacheableWriter::serializeu32(uint32_t field)
{
dest.append((char*)&field, sizeof(field));
}
void CacheableWriter::serializehandle(handle field)
{
dest.append((char*)&field, sizeof(field));
}
void CacheableWriter::serializenodehandle(handle field)
{
dest.append((const char*)&field, MegaClient::NODEHANDLE);
}
void CacheableWriter::serializefsfp(fsfp_t field)
{
dest.append((char*)&field, sizeof(field));
}
void CacheableWriter::serializebool(bool field)
{
dest.append((char*)&field, sizeof(field));
}
void CacheableWriter::serializebyte(byte field)
{
dest.append((char*)&field, sizeof(field));
}
void CacheableWriter::serializedouble(double field)
{
dest.append((char*)&field, sizeof(field));
}
void CacheableWriter::serializeexpansionflags(bool b0, bool b1, bool b2, bool b3, bool b4, bool b5, bool b6, bool b7)
{
unsigned char b[8];
b[0] = b0;
b[1] = b1;
b[2] = b2;
b[3] = b3;
b[4] = b4;
b[5] = b5;
b[6] = b6;
b[7] = b7;
dest.append((char*)b, 8);
}
CacheableReader::CacheableReader(const string& d)
: ptr(d.data())
, end(ptr + d.size())
, fieldnum(0)
{
}
void CacheableReader::eraseused(string& d)
{
assert(end == d.data() + d.size());
d.erase(0, ptr - d.data());
}
bool CacheableReader::unserializecstr(string& s, bool removeNull)
{
if (ptr + sizeof(unsigned short) > end)
{
return false;
}
unsigned short len = MemAccess::get<unsigned short>(ptr);
ptr += sizeof(len);
if (ptr + len > end)
{
return false;
}
if (len)
{
s.assign(ptr, len - (removeNull ? 1 : 0));
}
ptr += len;
fieldnum += 1;
return true;
}
bool CacheableReader::unserializestring(string& s)
{
if (ptr + sizeof(unsigned short) > end)
{
return false;
}
unsigned short len = MemAccess::get<unsigned short>(ptr);
ptr += sizeof(len);
if (ptr + len > end)
{
return false;
}
if (len)
{
s.assign(ptr, len);
}
ptr += len;
fieldnum += 1;
return true;
}
bool CacheableReader::unserializebinary(byte* data, size_t len)
{
if (ptr + len > end)
{
return false;
}
memcpy(data, ptr, len);
ptr += len;
fieldnum += 1;
return true;
}
void chunkmac_map::serialize(string& d) const
{
unsigned short ll = (unsigned short)size();
d.append((char*)&ll, sizeof(ll));
for (const_iterator it = begin(); it != end(); it++)
{
d.append((char*)&it->first, sizeof(it->first));
d.append((char*)&it->second, sizeof(it->second));
}
}
bool chunkmac_map::unserialize(const char*& ptr, const char* end)
{
unsigned short ll;
if ((ptr + sizeof(ll) > end) || ptr + (ll = MemAccess::get<unsigned short>(ptr)) * (sizeof(m_off_t) + sizeof(ChunkMAC)) + sizeof(ll) > end)
{
return false;
}
ptr += sizeof(ll);
for (int i = 0; i < ll; i++)
{
m_off_t pos = MemAccess::get<m_off_t>(ptr);
ptr += sizeof(m_off_t);
memcpy(&((*this)[pos]), ptr, sizeof(ChunkMAC));
ptr += sizeof(ChunkMAC);
}
return true;
}
void chunkmac_map::calcprogress(m_off_t size, m_off_t& chunkpos, m_off_t& progresscompleted, m_off_t* lastblockprogress)
{
chunkpos = 0;
progresscompleted = 0;
for (chunkmac_map::iterator it = begin(); it != end(); ++it)
{
m_off_t chunkceil = ChunkedHash::chunkceil(it->first, size);
if (chunkpos == it->first && it->second.finished)
{
chunkpos = chunkceil;
progresscompleted = chunkceil;
}
else if (it->second.finished)
{
m_off_t chunksize = chunkceil - ChunkedHash::chunkfloor(it->first);
progresscompleted += chunksize;
}
else
{
progresscompleted += it->second.offset;
if (lastblockprogress)
{
*lastblockprogress += it->second.offset;
}
}
}
}
m_off_t chunkmac_map::nextUnprocessedPosFrom(m_off_t pos)
{
for (const_iterator it = find(ChunkedHash::chunkfloor(pos));
it != end();
it = find(ChunkedHash::chunkfloor(pos)))
{
if (it->second.finished)
{
pos = ChunkedHash::chunkceil(pos);
}
else
{
pos += it->second.offset;
break;
}
}
return pos;
}
m_off_t chunkmac_map::expandUnprocessedPiece(m_off_t pos, m_off_t npos, m_off_t fileSize, m_off_t maxReqSize)
{
for (iterator it = find(npos);
npos < fileSize && (npos - pos) <= maxReqSize && (it == end() || (!it->second.finished && !it->second.offset));
it = find(npos))
{
npos = ChunkedHash::chunkceil(npos, fileSize);
}
return npos;
}
void chunkmac_map::finishedUploadChunks(chunkmac_map& macs)
{
for (auto& m : macs)
{
m.second.finished = true;
(*this)[m.first] = m.second;
LOG_verbose << "Upload chunk completed: " << m.first;
}
}
// coalesce block macs into file mac
int64_t chunkmac_map::macsmac(SymmCipher *cipher)
{
byte mac[SymmCipher::BLOCKSIZE] = { 0 };
for (chunkmac_map::iterator it = begin(); it != end(); it++)
{
assert(it->first == ChunkedHash::chunkfloor(it->first));
// LOG_debug << "macsmac input: " << it->first << ": " << Base64Str<sizeof it->second.mac>(it->second.mac);
SymmCipher::xorblock(it->second.mac, mac);
cipher->ecb_encrypt(mac);
}
uint32_t* m = (uint32_t*)mac;
m[0] ^= m[1];
m[1] = m[2] ^ m[3];
return MemAccess::get<int64_t>((const char*)mac);
}
bool CacheableReader::unserializechunkmacs(chunkmac_map& m)
{
if (m.unserialize(ptr, end)) // ptr is adjusted by reference
{
fieldnum += 1;
return true;
}
return false;
}
bool CacheableReader::unserializecompressed64(uint64_t& field)
{
int fieldSize;
if ((fieldSize = Serialize64::unserialize((byte*)ptr, static_cast<int>(end - ptr), &field)) < 0)
{
LOG_err << "Serialize64 unserialization failed - malformed field";
return false;
}
else
{
ptr += fieldSize;
}
return true;
}
bool CacheableReader::unserializei64(int64_t& field)
{
if (ptr + sizeof(int64_t) > end)
{
return false;
}
field = MemAccess::get<int64_t>(ptr);
ptr += sizeof(int64_t);
fieldnum += 1;
return true;
}
bool CacheableReader::unserializeu32(uint32_t& field)
{
if (ptr + sizeof(uint32_t) > end)
{
return false;
}
field = MemAccess::get<uint32_t>(ptr);
ptr += sizeof(uint32_t);
fieldnum += 1;
return true;
}
bool CacheableReader::unserializehandle(handle& field)
{
if (ptr + sizeof(handle) > end)
{
return false;
}
field = MemAccess::get<handle>(ptr);
ptr += sizeof(handle);
fieldnum += 1;
return true;
}
bool CacheableReader::unserializenodehandle(handle& field)
{
if (ptr + MegaClient::NODEHANDLE > end)
{
return false;
}
field = 0;
memcpy((char*)&field, ptr, MegaClient::NODEHANDLE);
ptr += MegaClient::NODEHANDLE;
fieldnum += 1;
return true;
}
bool CacheableReader::unserializefsfp(fsfp_t& field)
{
if (ptr + sizeof(fsfp_t) > end)
{
return false;
}
field = MemAccess::get<fsfp_t>(ptr);
ptr += sizeof(fsfp_t);
fieldnum += 1;
return true;
}
bool CacheableReader::unserializebool(bool& field)
{
if (ptr + sizeof(bool) > end)
{
return false;
}
field = MemAccess::get<bool>(ptr);
ptr += sizeof(bool);
fieldnum += 1;
return true;
}
bool CacheableReader::unserializebyte(byte& field)
{
if (ptr + sizeof(byte) > end)
{
return false;
}
field = MemAccess::get<byte>(ptr);
ptr += sizeof(byte);
fieldnum += 1;
return true;
}
bool CacheableReader::unserializedouble(double& field)
{
if (ptr + sizeof(double) > end)
{
return false;
}
field = MemAccess::get<double>(ptr);
ptr += sizeof(double);
fieldnum += 1;
return true;
}
bool CacheableReader::unserializeexpansionflags(unsigned char field[8], unsigned usedFlagCount)
{
if (ptr + 8 > end)
{
return false;
}
memcpy(field, ptr, 8);
for (int i = usedFlagCount; i < 8; i++ )
{
if (field[i])
{
LOG_err << "Unserialization failed in expansion flags, invalid version detected. Fieldnum: " << fieldnum;
return false;
}
}
ptr += 8;
fieldnum += 1;
return true;
}
#ifdef ENABLE_CHAT
TextChat::TextChat()
{
id = UNDEF;
priv = PRIV_UNKNOWN;
shard = -1;
userpriv = NULL;
group = false;
ou = UNDEF;
resetTag();
ts = 0;
flags = 0;
publicchat = false;
memset(&changed, 0, sizeof(changed));
}
TextChat::~TextChat()
{
delete userpriv;
}
bool TextChat::serialize(string *d)
{
unsigned short ll;
d->append((char*)&id, sizeof id);
d->append((char*)&priv, sizeof priv);
d->append((char*)&shard, sizeof shard);
ll = (unsigned short)(userpriv ? userpriv->size() : 0);
d->append((char*)&ll, sizeof ll);
if (userpriv)
{
userpriv_vector::iterator it = userpriv->begin();
while (it != userpriv->end())
{
handle uh = it->first;
d->append((char*)&uh, sizeof uh);
privilege_t priv = it->second;
d->append((char*)&priv, sizeof priv);
it++;
}
}
d->append((char*)&group, sizeof group);
// title is a binary array
ll = (unsigned short)title.size();
d->append((char*)&ll, sizeof ll);
d->append(title.data(), ll);
d->append((char*)&ou, sizeof ou);
d->append((char*)&ts, sizeof(ts));
char hasAttachments = attachedNodes.size() != 0;
d->append((char*)&hasAttachments, 1);
d->append((char*)&flags, 1);
char mode = publicchat ? 1 : 0;
d->append((char*)&mode, 1);
char hasUnifiedKey = unifiedKey.size() ? 1 : 0;
d->append((char *)&hasUnifiedKey, 1);
d->append("\0\0\0\0\0\0", 6); // additional bytes for backwards compatibility
if (hasAttachments)
{
ll = (unsigned short)attachedNodes.size(); // number of nodes with granted access
d->append((char*)&ll, sizeof ll);
for (attachments_map::iterator it = attachedNodes.begin(); it != attachedNodes.end(); it++)
{
d->append((char*)&it->first, sizeof it->first); // nodehandle
ll = (unsigned short)it->second.size(); // number of users with granted access to the node
d->append((char*)&ll, sizeof ll);
for (set<handle>::iterator ituh = it->second.begin(); ituh != it->second.end(); ituh++)
{
d->append((char*)&(*ituh), sizeof *ituh); // userhandle
}
}
}
if (hasUnifiedKey)
{
ll = (unsigned short) unifiedKey.size();
d->append((char *)&ll, sizeof ll);
d->append((char*) unifiedKey.data(), unifiedKey.size());
}
return true;
}
TextChat* TextChat::unserialize(class MegaClient *client, string *d)
{
handle id;
privilege_t priv;
int shard;
userpriv_vector *userpriv = NULL;
bool group;
string title; // byte array
handle ou;
m_time_t ts;
byte flags;
char hasAttachments;
attachments_map attachedNodes;
bool publicchat;
string unifiedKey;
unsigned short ll;
const char* ptr = d->data();
const char* end = ptr + d->size();
if (ptr + sizeof(handle) + sizeof(privilege_t) + sizeof(int) + sizeof(short) > end)
{
return NULL;
}
id = MemAccess::get<handle>(ptr);
ptr += sizeof id;
priv = MemAccess::get<privilege_t>(ptr);
ptr += sizeof priv;
shard = MemAccess::get<int>(ptr);
ptr += sizeof shard;
ll = MemAccess::get<unsigned short>(ptr);
ptr += sizeof ll;
if (ll)
{
if (ptr + ll * (sizeof(handle) + sizeof(privilege_t)) > end)
{
return NULL;
}
userpriv = new userpriv_vector();
for (unsigned short i = 0; i < ll; i++)
{
handle uh = MemAccess::get<handle>(ptr);
ptr += sizeof uh;
privilege_t priv = MemAccess::get<privilege_t>(ptr);
ptr += sizeof priv;
userpriv->push_back(userpriv_pair(uh, priv));
}
if (priv == PRIV_RM) // clear peerlist if removed
{
delete userpriv;
userpriv = NULL;
}
}
if (ptr + sizeof(bool) + sizeof(unsigned short) > end)
{
delete userpriv;
return NULL;
}
group = MemAccess::get<bool>(ptr);
ptr += sizeof group;
ll = MemAccess::get<unsigned short>(ptr);
ptr += sizeof ll;
if (ll)
{
if (ptr + ll > end)
{
delete userpriv;
return NULL;
}
title.assign(ptr, ll);
}
ptr += ll;
if (ptr + sizeof(handle) + sizeof(m_time_t) + sizeof(char) + 9 > end)
{
delete userpriv;
return NULL;
}
ou = MemAccess::get<handle>(ptr);
ptr += sizeof ou;
ts = MemAccess::get<m_time_t>(ptr);
ptr += sizeof(m_time_t);
hasAttachments = MemAccess::get<char>(ptr);
ptr += sizeof hasAttachments;
flags = MemAccess::get<char>(ptr);
ptr += sizeof(char);
char mode = MemAccess::get<char>(ptr);
publicchat = (mode == 1);
ptr += sizeof(char);
char hasUnifiedKey = MemAccess::get<char>(ptr);
ptr += sizeof(char);
for (int i = 6; i--;)
{
if (ptr + MemAccess::get<unsigned char>(ptr) < end)
{
ptr += MemAccess::get<unsigned char>(ptr) + 1;
}
}
if (hasAttachments)
{
unsigned short numNodes = 0;
if (ptr + sizeof numNodes > end)
{
delete userpriv;
return NULL;
}
numNodes = MemAccess::get<unsigned short>(ptr);
ptr += sizeof numNodes;
for (int i = 0; i < numNodes; i++)
{
handle h = UNDEF;
unsigned short numUsers = 0;
if (ptr + sizeof h + sizeof numUsers > end)
{
delete userpriv;
return NULL;
}
h = MemAccess::get<handle>(ptr);
ptr += sizeof h;
numUsers = MemAccess::get<unsigned short>(ptr);
ptr += sizeof numUsers;
handle uh = UNDEF;
if (ptr + (numUsers * sizeof(uh)) > end)
{
delete userpriv;
return NULL;
}
for (int j = 0; j < numUsers; j++)
{
uh = MemAccess::get<handle>(ptr);
ptr += sizeof uh;
attachedNodes[h].insert(uh);
}
}
}
if (hasUnifiedKey)
{
unsigned short keylen = 0;
if (ptr + sizeof keylen > end)
{
delete userpriv;
return NULL;
}
keylen = MemAccess::get<unsigned short>(ptr);
ptr += sizeof keylen;
if (ptr + keylen > end)
{
delete userpriv;
return NULL;
}
unifiedKey.assign(ptr, keylen);
ptr += keylen;
}
if (ptr < end)
{
delete userpriv;
return NULL;
}
if (client->chats.find(id) == client->chats.end())
{
client->chats[id] = new TextChat();
}
else
{
LOG_warn << "Unserialized a chat already in RAM";
}
TextChat* chat = client->chats[id];
chat->id = id;
chat->priv = priv;
chat->shard = shard;
chat->userpriv = userpriv;
chat->group = group;
chat->title = title;
chat->ou = ou;
chat->resetTag();
chat->ts = ts;
chat->flags = flags;
chat->attachedNodes = attachedNodes;
chat->publicchat = publicchat;
chat->unifiedKey = unifiedKey;
memset(&chat->changed, 0, sizeof(chat->changed));
return chat;
}
void TextChat::setTag(int tag)
{
if (this->tag != 0) // external changes prevail
{
this->tag = tag;
}
}
int TextChat::getTag()
{
return tag;
}
void TextChat::resetTag()
{
tag = -1;
}
bool TextChat::setNodeUserAccess(handle h, handle uh, bool revoke)
{
if (revoke)
{
attachments_map::iterator uhit = attachedNodes.find(h);
if (uhit != attachedNodes.end())
{
uhit->second.erase(uh);
if (uhit->second.empty())
{
attachedNodes.erase(h);
changed.attachments = true;
}
return true;
}
}
else
{
attachedNodes[h].insert(uh);
changed.attachments = true;
return true;
}
return false;
}
bool TextChat::setFlags(byte newFlags)
{
if (flags == newFlags)
{
return false;
}
flags = newFlags;
changed.flags = true;
return true;
}
bool TextChat::isFlagSet(uint8_t offset) const
{
return (flags >> offset) & 1U;
}
bool TextChat::setMode(bool publicchat)
{
if (this->publicchat == publicchat)
{
return false;
}
this->publicchat = publicchat;
changed.mode = true;
return true;
}
bool TextChat::setFlag(bool value, uint8_t offset)
{
if (bool((flags >> offset) & 1U) == value)
{
return false;
}
flags ^= (1U << offset);
changed.flags = true;
return true;
}
#endif
/**
* @brief Encrypts a string after padding it to block length.
*
* Note: With an IV, only use the first 8 bytes.
*
* @param data Data buffer to be encrypted. Encryption is done in-place,
* so cipher text will be in `data` afterwards as well.
* @param key AES key for encryption.
* @param iv Optional initialisation vector for encryption. Will use a
* zero IV if not given. If `iv` is a zero length string, a new IV
* for encryption will be generated and available through the reference.
* @return Void.
*/
void PaddedCBC::encrypt(PrnGen &rng, string* data, SymmCipher* key, string* iv)
{
if (iv)
{
// Make a new 8-byte IV, if the one passed is zero length.
if (iv->size() == 0)
{
byte* buf = new byte[8];
rng.genblock(buf, 8);
iv->append((char*)buf);
delete [] buf;
}
// Truncate a longer IV to its first 8 bytes.
if (iv->size() > 8)
{
iv->resize(8);
}
// Bring up the IV size to BLOCKSIZE.
iv->resize(key->BLOCKSIZE);
}
// Pad to block size and encrypt.
data->append("E");
data->resize((data->size() + key->BLOCKSIZE - 1) & - key->BLOCKSIZE, 'P');
if (iv)
{
key->cbc_encrypt((byte*)data->data(), data->size(),
(const byte*)iv->data());
}
else
{
key->cbc_encrypt((byte*)data->data(), data->size());
}
// Truncate IV back to the first 8 bytes only..
if (iv)
{
iv->resize(8);
}
}
/**
* @brief Decrypts a string and strips the padding.
*
* Note: With an IV, only use the first 8 bytes.
*
* @param data Data buffer to be decrypted. Decryption is done in-place,
* so plain text will be in `data` afterwards as well.
* @param key AES key for decryption.
* @param iv Optional initialisation vector for encryption. Will use a
* zero IV if not given.
* @return Void.
*/
bool PaddedCBC::decrypt(string* data, SymmCipher* key, string* iv)
{
if (iv)
{
// Truncate a longer IV to its first 8 bytes.
if (iv->size() > 8)
{
iv->resize(8);
}
// Bring up the IV size to BLOCKSIZE.
iv->resize(key->BLOCKSIZE);
}
if ((data->size() & (key->BLOCKSIZE - 1)))
{
return false;
}
// Decrypt and unpad.
if (iv)
{
key->cbc_decrypt((byte*)data->data(), data->size(),
(const byte*)iv->data());
}
else
{
key->cbc_decrypt((byte*)data->data(), data->size());
}
size_t p = data->find_last_of('E');