-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathencoding.h
844 lines (771 loc) · 21.1 KB
/
encoding.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
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
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab
/*
* Ceph - scalable distributed file system
*
* Copyright (C) 2004-2006 Sage Weil <[email protected]>
*
* This is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License version 2.1, as published by the Free Software
* Foundation. See file COPYING.
*
*/
#ifndef CEPH_ENCODING_H
#define CEPH_ENCODING_H
#include <tr1/memory>
#include "inttypes.h"
#include "byteorder.h"
#include "buffer.h"
#include "assert.h"
using namespace ceph;
/*
* Notes on feature encoding:
*
* - The default encode() methods have a features argument with a default parameter
* (which goes to zero).
* - Normal classes will use WRITE_CLASS_ENCODER, with that features=0 default.
* - Classes that _require_ features will use WRITE_CLASS_ENCODER_FEATURES, which
* does not define the default. Any caller must explicitly pass it in.
* - STL container macros have two encode variants: one with a features arg, and one
* without.
*
* The result:
* - A feature encode() method will fail to compile if a value is not
* passed in.
* - The feature varianet of the STL templates will be used when the feature arg is
* provided. It will be passed through to any template arg types, but it will be
* ignored when not needed.
*/
// --------------------------------------
// base types
template<class T>
inline void encode_raw(const T& t, bufferlist& bl)
{
bl.append((char*)&t, sizeof(t));
}
template<class T>
inline void decode_raw(T& t, bufferlist::iterator &p)
{
p.copy(sizeof(t), (char*)&t);
}
#define WRITE_RAW_ENCODER(type) \
inline void encode(const type &v, bufferlist& bl, uint64_t features=0) { encode_raw(v, bl); } \
inline void decode(type &v, bufferlist::iterator& p) { __ASSERT_FUNCTION decode_raw(v, p); }
WRITE_RAW_ENCODER(__u8)
WRITE_RAW_ENCODER(__s8)
WRITE_RAW_ENCODER(char)
WRITE_RAW_ENCODER(ceph_le64)
WRITE_RAW_ENCODER(ceph_le32)
WRITE_RAW_ENCODER(ceph_le16)
// FIXME: we need to choose some portable floating point encoding here
WRITE_RAW_ENCODER(float)
WRITE_RAW_ENCODER(double)
inline void encode(const bool &v, bufferlist& bl) {
__u8 vv = v;
encode_raw(vv, bl);
}
inline void decode(bool &v, bufferlist::iterator& p) {
__u8 vv;
decode_raw(vv, p);
v = vv;
}
// -----------------------------------
// int types
#define WRITE_INTTYPE_ENCODER(type, etype) \
inline void encode(type v, bufferlist& bl, uint64_t features=0) { \
ceph_##etype e; \
e = v; \
encode_raw(e, bl); \
} \
inline void decode(type &v, bufferlist::iterator& p) { \
ceph_##etype e; \
decode_raw(e, p); \
v = e; \
}
WRITE_INTTYPE_ENCODER(uint64_t, le64)
WRITE_INTTYPE_ENCODER(int64_t, le64)
WRITE_INTTYPE_ENCODER(uint32_t, le32)
WRITE_INTTYPE_ENCODER(int32_t, le32)
WRITE_INTTYPE_ENCODER(uint16_t, le16)
WRITE_INTTYPE_ENCODER(int16_t, le16)
#ifdef ENCODE_DUMP
# include <stdio.h>
# include <sys/types.h>
# include <sys/stat.h>
# include <fcntl.h>
# define ENCODE_STR(x) #x
# define ENCODE_STRINGIFY(x) ENCODE_STR(x)
# define ENCODE_DUMP_PRE() \
unsigned pre_off = bl.length()
// NOTE: This is almost an exponential backoff, but because we count
// bits we get a better sample of things we encode later on.
# define ENCODE_DUMP_POST(cl) \
do { \
static int i = 0; \
i++; \
int bits = 0; \
for (unsigned t = i; t; bits++) \
t &= t - 1; \
if (bits > 2) \
break; \
char fn[200]; \
snprintf(fn, sizeof(fn), ENCODE_STRINGIFY(ENCODE_DUMP) "/%s__%d.%x", #cl, getpid(), i++); \
int fd = ::open(fn, O_WRONLY|O_TRUNC|O_CREAT, 0644); \
if (fd >= 0) { \
bufferlist sub; \
sub.substr_of(bl, pre_off, bl.length() - pre_off); \
sub.write_fd(fd); \
::close(fd); \
} \
} while (0)
#else
# define ENCODE_DUMP_PRE()
# define ENCODE_DUMP_POST(cl)
#endif
#define WRITE_CLASS_ENCODER(cl) \
inline void encode(const cl &c, bufferlist &bl, uint64_t features=0) { \
ENCODE_DUMP_PRE(); c.encode(bl); ENCODE_DUMP_POST(cl); } \
inline void decode(cl &c, bufferlist::iterator &p) { c.decode(p); }
#define WRITE_CLASS_MEMBER_ENCODER(cl) \
inline void encode(const cl &c, bufferlist &bl) const { \
ENCODE_DUMP_PRE(); c.encode(bl); ENCODE_DUMP_POST(cl); } \
inline void decode(cl &c, bufferlist::iterator &p) { c.decode(p); }
#define WRITE_CLASS_ENCODER_FEATURES(cl) \
inline void encode(const cl &c, bufferlist &bl, uint64_t features) { \
ENCODE_DUMP_PRE(); c.encode(bl, features); ENCODE_DUMP_POST(cl); } \
inline void decode(cl &c, bufferlist::iterator &p) { c.decode(p); }
// string
inline void encode(const std::string& s, bufferlist& bl, uint64_t features=0)
{
__u32 len = s.length();
encode(len, bl);
bl.append(s.data(), len);
}
inline void decode(std::string& s, bufferlist::iterator& p)
{
__u32 len;
decode(len, p);
s.clear();
p.copy(len, s);
}
inline void encode_nohead(const std::string& s, bufferlist& bl)
{
bl.append(s.data(), s.length());
}
inline void decode_nohead(int len, std::string& s, bufferlist::iterator& p)
{
s.clear();
p.copy(len, s);
}
// const char* (encode only, string compatible)
inline void encode(const char *s, bufferlist& bl)
{
__u32 len = strlen(s);
encode(len, bl);
bl.append(s, len);
}
// array
template<class A>
inline void encode_array_nohead(const A a[], int n, bufferlist &bl)
{
for (int i=0; i<n; i++)
encode(a[n], bl);
}
template<class A>
inline void decode_array_nohead(A a[], int n, bufferlist::iterator &p)
{
for (int i=0; i<n; i++)
decode(a[n], p);
}
// -----------------------------
// buffers
// bufferptr (encapsulated)
inline void encode(const buffer::ptr& bp, bufferlist& bl)
{
__u32 len = bp.length();
encode(len, bl);
if (len)
bl.append(bp);
}
inline void decode(buffer::ptr& bp, bufferlist::iterator& p)
{
__u32 len;
decode(len, p);
bufferlist s;
p.copy(len, s);
if (len) {
if (s.buffers().size() == 1)
bp = s.buffers().front();
else
bp = buffer::copy(s.c_str(), s.length());
}
}
// bufferlist (encapsulated)
inline void encode(const bufferlist& s, bufferlist& bl)
{
__u32 len = s.length();
encode(len, bl);
bl.append(s);
}
inline void encode_destructively(bufferlist& s, bufferlist& bl)
{
__u32 len = s.length();
encode(len, bl);
bl.claim_append(s);
}
inline void decode(bufferlist& s, bufferlist::iterator& p)
{
__u32 len;
decode(len, p);
s.clear();
p.copy(len, s);
}
inline void encode_nohead(const bufferlist& s, bufferlist& bl)
{
bl.append(s);
}
inline void decode_nohead(int len, bufferlist& s, bufferlist::iterator& p)
{
s.clear();
p.copy(len, s);
}
// full bl decoder
template<class T>
inline void decode(T &o, bufferlist& bl)
{
bufferlist::iterator p = bl.begin();
decode(o, p);
assert(p.end());
}
// -----------------------------
// STL container types
#include <set>
#include <map>
#include <deque>
#include <vector>
#include <string>
#ifndef _BACKWARD_BACKWARD_WARNING_H
#define _BACKWARD_BACKWARD_WARNING_H // make gcc 4.3 shut up about hash_*
#endif
#include <ext/hash_map>
#include <ext/hash_set>
#include "triple.h"
// pair
template<class A, class B>
inline void encode(const std::pair<A,B> &p, bufferlist &bl)
{
encode(p.first, bl);
encode(p.second, bl);
}
template<class A, class B>
inline void decode(std::pair<A,B> &pa, bufferlist::iterator &p)
{
decode(pa.first, p);
decode(pa.second, p);
}
// triple
template<class A, class B, class C>
inline void encode(const triple<A,B,C> &t, bufferlist &bl)
{
encode(t.first, bl);
encode(t.second, bl);
encode(t.third, bl);
}
template<class A, class B, class C>
inline void decode(triple<A,B,C> &t, bufferlist::iterator &p)
{
decode(t.first, p);
decode(t.second, p);
decode(t.third, p);
}
// list
template<class T>
inline void encode(const std::list<T>& ls, bufferlist& bl)
{
// should i pre- or post- count?
if (!ls.empty()) {
unsigned pos = bl.length();
unsigned n = 0;
encode(n, bl);
for (typename std::list<T>::const_iterator p = ls.begin(); p != ls.end(); ++p) {
n++;
encode(*p, bl);
}
ceph_le32 en;
en = n;
bl.copy_in(pos, sizeof(en), (char*)&en);
} else {
__u32 n = ls.size(); // FIXME: this is slow on a list.
encode(n, bl);
for (typename std::list<T>::const_iterator p = ls.begin(); p != ls.end(); ++p)
encode(*p, bl);
}
}
template<class T>
inline void decode(std::list<T>& ls, bufferlist::iterator& p)
{
__u32 n;
decode(n, p);
ls.clear();
while (n--) {
T v;
decode(v, p);
ls.push_back(v);
}
}
template<class T>
inline void encode(const std::list<std::tr1::shared_ptr<T> >& ls, bufferlist& bl)
{
// should i pre- or post- count?
if (!ls.empty()) {
unsigned pos = bl.length();
unsigned n = 0;
encode(n, bl);
for (typename std::list<std::tr1::shared_ptr<T> >::const_iterator p = ls.begin(); p != ls.end(); ++p) {
n++;
encode(**p, bl);
}
ceph_le32 en;
en = n;
bl.copy_in(pos, sizeof(en), (char*)&en);
} else {
__u32 n = ls.size(); // FIXME: this is slow on a list.
encode(n, bl);
for (typename std::list<std::tr1::shared_ptr<T> >::const_iterator p = ls.begin(); p != ls.end(); ++p)
encode(**p, bl);
}
}
template<class T>
inline void decode(std::list<std::tr1::shared_ptr<T> >& ls, bufferlist::iterator& p)
{
__u32 n;
decode(n, p);
ls.clear();
while (n--) {
std::tr1::shared_ptr<T> v(new T);
decode(*v, p);
ls.push_back(v);
}
}
// set
template<class T>
inline void encode(const std::set<T>& s, bufferlist& bl)
{
__u32 n = s.size();
encode(n, bl);
for (typename std::set<T>::const_iterator p = s.begin(); p != s.end(); ++p)
encode(*p, bl);
}
template<class T>
inline void decode(std::set<T>& s, bufferlist::iterator& p)
{
__u32 n;
decode(n, p);
s.clear();
while (n--) {
T v;
decode(v, p);
s.insert(v);
}
}
// vector (pointers)
/*template<class T>
inline void encode(const std::vector<T*>& v, bufferlist& bl)
{
__u32 n = v.size();
encode(n, bl);
for (typename std::vector<T*>::const_iterator p = v.begin(); p != v.end(); ++p)
encode(**p, bl);
}
template<class T>
inline void decode(std::vector<T*>& v, bufferlist::iterator& p)
{
__u32 n;
decode(n, p);
v.resize(n);
for (__u32 i=0; i<n; i++)
v[i] = new T(p);
}
*/
// vector
template<class T>
inline void encode(const std::vector<T>& v, bufferlist& bl, uint64_t features)
{
__u32 n = v.size();
encode(n, bl);
for (typename std::vector<T>::const_iterator p = v.begin(); p != v.end(); ++p)
encode(*p, bl, features);
}
template<class T>
inline void encode(const std::vector<T>& v, bufferlist& bl)
{
__u32 n = v.size();
encode(n, bl);
for (typename std::vector<T>::const_iterator p = v.begin(); p != v.end(); ++p)
encode(*p, bl);
}
template<class T>
inline void decode(std::vector<T>& v, bufferlist::iterator& p)
{
__u32 n;
decode(n, p);
v.resize(n);
for (__u32 i=0; i<n; i++)
decode(v[i], p);
}
template<class T>
inline void encode_nohead(const std::vector<T>& v, bufferlist& bl)
{
for (typename std::vector<T>::const_iterator p = v.begin(); p != v.end(); ++p)
encode(*p, bl);
}
template<class T>
inline void decode_nohead(int len, std::vector<T>& v, bufferlist::iterator& p)
{
v.resize(len);
for (__u32 i=0; i<v.size(); i++)
decode(v[i], p);
}
// vector (shared_ptr)
template<class T>
inline void encode(const std::vector<std::tr1::shared_ptr<T> >& v, bufferlist& bl)
{
__u32 n = v.size();
encode(n, bl);
for (typename std::vector<std::tr1::shared_ptr<T> >::const_iterator p = v.begin(); p != v.end(); ++p)
if (*p)
encode(**p, bl);
else
encode(T(), bl);
}
template<class T>
inline void decode(std::vector<std::tr1::shared_ptr<T> >& v, bufferlist::iterator& p)
{
__u32 n;
decode(n, p);
v.resize(n);
for (__u32 i=0; i<n; i++) {
v[i].reset(new T());
decode(*v[i], p);
}
}
// map (pointers)
/*
template<class T, class U>
inline void encode(const std::map<T,U*>& m, bufferlist& bl)
{
__u32 n = m.size();
encode(n, bl);
for (typename std::map<T,U*>::const_iterator p = m.begin(); p != m.end(); ++p) {
encode(p->first, bl);
encode(*p->second, bl);
}
}
template<class T, class U>
inline void decode(std::map<T,U*>& m, bufferlist::iterator& p)
{
__u32 n;
decode(n, p);
m.clear();
while (n--) {
T k;
decode(k, p);
m[k] = new U(p);
}
}*/
// map
template<class T, class U>
inline void encode(const std::map<T,U>& m, bufferlist& bl)
{
__u32 n = m.size();
encode(n, bl);
for (typename std::map<T,U>::const_iterator p = m.begin(); p != m.end(); ++p) {
encode(p->first, bl);
encode(p->second, bl);
}
}
template<class T, class U>
inline void encode(const std::map<T,U>& m, bufferlist& bl, uint64_t features)
{
__u32 n = m.size();
encode(n, bl);
for (typename std::map<T,U>::const_iterator p = m.begin(); p != m.end(); ++p) {
encode(p->first, bl, features);
encode(p->second, bl, features);
}
}
template<class T, class U>
inline void decode(std::map<T,U>& m, bufferlist::iterator& p)
{
__u32 n;
decode(n, p);
m.clear();
while (n--) {
T k;
decode(k, p);
decode(m[k], p);
}
}
template<class T, class U>
inline void decode_noclear(std::map<T,U>& m, bufferlist::iterator& p)
{
__u32 n;
decode(n, p);
while (n--) {
T k;
decode(k, p);
decode(m[k], p);
}
}
template<class T, class U>
inline void encode_nohead(const std::map<T,U>& m, bufferlist& bl)
{
for (typename std::map<T,U>::const_iterator p = m.begin(); p != m.end(); ++p) {
encode(p->first, bl);
encode(p->second, bl);
}
}
template<class T, class U>
inline void encode_nohead(const std::map<T,U>& m, bufferlist& bl, uint64_t features)
{
for (typename std::map<T,U>::const_iterator p = m.begin(); p != m.end(); ++p) {
encode(p->first, bl, features);
encode(p->second, bl, features);
}
}
template<class T, class U>
inline void decode_nohead(int n, std::map<T,U>& m, bufferlist::iterator& p)
{
m.clear();
while (n--) {
T k;
decode(k, p);
decode(m[k], p);
}
}
// multimap
template<class T, class U>
inline void encode(const std::multimap<T,U>& m, bufferlist& bl)
{
__u32 n = m.size();
encode(n, bl);
for (typename std::multimap<T,U>::const_iterator p = m.begin(); p != m.end(); ++p) {
encode(p->first, bl);
encode(p->second, bl);
}
}
template<class T, class U>
inline void decode(std::multimap<T,U>& m, bufferlist::iterator& p)
{
__u32 n;
decode(n, p);
m.clear();
while (n--) {
typename std::pair<T,U> tu = std::pair<T,U>();
decode(tu.first, p);
typename std::multimap<T,U>::iterator it = m.insert(tu);
decode(it->second, p);
}
}
// hash_map
template<class T, class U>
inline void encode(const __gnu_cxx::hash_map<T,U>& m, bufferlist& bl)
{
__u32 n = m.size();
encode(n, bl);
for (typename __gnu_cxx::hash_map<T,U>::const_iterator p = m.begin(); p != m.end(); ++p) {
encode(p->first, bl);
encode(p->second, bl);
}
}
template<class T, class U>
inline void decode(__gnu_cxx::hash_map<T,U>& m, bufferlist::iterator& p)
{
__u32 n;
decode(n, p);
m.clear();
while (n--) {
T k;
decode(k, p);
decode(m[k], p);
}
}
// hash_set
template<class T>
inline void encode(const __gnu_cxx::hash_set<T>& m, bufferlist& bl)
{
__u32 n = m.size();
encode(n, bl);
for (typename __gnu_cxx::hash_set<T>::const_iterator p = m.begin(); p != m.end(); ++p)
encode(*p, bl);
}
template<class T>
inline void decode(__gnu_cxx::hash_set<T>& m, bufferlist::iterator& p)
{
__u32 n;
decode(n, p);
m.clear();
while (n--) {
T k;
decode(k, p);
m.insert(k);
}
}
// deque
template<class T>
inline void encode(const std::deque<T>& ls, bufferlist& bl)
{
__u32 n = ls.size();
encode(n, bl);
for (typename std::deque<T>::const_iterator p = ls.begin(); p != ls.end(); ++p)
encode(*p, bl);
}
template<class T>
inline void decode(std::deque<T>& ls, bufferlist::iterator& p)
{
__u32 n;
decode(n, p);
ls.clear();
while (n--) {
T v;
decode(v, p);
ls.push_back(v);
}
}
/*
* guards
*/
/**
* start encoding block
*
* @param v current (code) version of the encoding
* @param compat oldest code version that can decode it
* @param bl bufferlist to encode to
*/
#define ENCODE_START(v, compat, bl) \
__u8 struct_v = v, struct_compat = compat; \
::encode(struct_v, bl); \
::encode(struct_compat, bl); \
ceph_le32 struct_len; \
struct_len = 0; \
::encode(struct_len, bl); \
buffer::list::iterator struct_len_it = bl.end(); \
struct_len_it.advance(-4); \
do {
/**
* finish encoding block
*
* @param bl bufferlist we were encoding to
*/
#define ENCODE_FINISH(bl) \
} while (false); \
struct_len = bl.length() - struct_len_it.get_off() - sizeof(struct_len); \
struct_len_it.copy_in(4, (char *)&struct_len);
#define DECODE_ERR_VERSION(func, v) \
"" #func " unknown encoding version > " #v
#define DECODE_ERR_OLDVERSION(func, v) \
"" #func " no longer understand old encoding version < " #v
#define DECODE_ERR_PAST(func) \
"" #func " decode past end of struct encoding"
/**
* check for very old encoding
*
* If the encoded data is older than oldestv, raise an exception.
*
* @param oldestv oldest version of the code we can successfully decode.
*/
#define DECODE_OLDEST(oldestv) \
if (struct_v < oldestv) \
throw buffer::malformed_input(DECODE_ERR_OLDVERSION(__PRETTY_FUNCTION__, v));
/**
* start a decoding block
*
* @param v current version of the encoding that the code supports/encodes
* @param bl bufferlist::iterator for the encoded data
*/
#define DECODE_START(v, bl) \
__u8 struct_v, struct_compat; \
::decode(struct_v, bl); \
::decode(struct_compat, bl); \
if (v < struct_compat) \
throw buffer::malformed_input(DECODE_ERR_VERSION(__PRETTY_FUNCTION__, v)); \
__u32 struct_len; \
::decode(struct_len, bl); \
if (struct_len > bl.get_remaining()) \
throw buffer::malformed_input(DECODE_ERR_PAST(__PRETTY_FUNCTION__)); \
unsigned struct_end = bl.get_off() + struct_len; \
do {
#define __DECODE_START_LEGACY_COMPAT_LEN(v, compatv, lenv, skip_v, bl) \
__u8 struct_v; \
::decode(struct_v, bl); \
if (struct_v >= compatv) { \
__u8 struct_compat; \
::decode(struct_compat, bl); \
if (v < struct_compat) \
throw buffer::malformed_input(DECODE_ERR_VERSION(__PRETTY_FUNCTION__, v)); \
} else if (skip_v) { \
if ((int)bl.get_remaining() < skip_v) \
throw buffer::malformed_input(DECODE_ERR_PAST(__PRETTY_FUNCTION__)); \
bl.advance(skip_v); \
} \
unsigned struct_end = 0; \
if (struct_v >= lenv) { \
__u32 struct_len; \
::decode(struct_len, bl); \
if (struct_len > bl.get_remaining()) \
throw buffer::malformed_input(DECODE_ERR_PAST(__PRETTY_FUNCTION__)); \
struct_end = bl.get_off() + struct_len; \
} \
do {
/**
* start a decoding block with legacy support for older encoding schemes
*
* The old encoding schemes has a __u8 struct_v only, or lacked either
* the compat version or length. Skip those fields conditionally.
*
* Most of the time, v, compatv, and lenv will all match the version
* where the structure was switched over to the new macros.
*
* @param v current version of the encoding that the code supports/encodes
* @param compatv oldest version that includes a __u8 compat version field
* @param lenv oldest version that includes a __u32 length wrapper
* @param bl bufferlist::iterator containing the encoded data
*/
#define DECODE_START_LEGACY_COMPAT_LEN(v, compatv, lenv, bl) \
__DECODE_START_LEGACY_COMPAT_LEN(v, compatv, lenv, 0, bl)
/**
* start a decoding block with legacy support for older encoding schemes
*
* This version of the macro assumes the legacy encoding had a 32 bit
* version
*
* The old encoding schemes has a __u8 struct_v only, or lacked either
* the compat version or length. Skip those fields conditionally.
*
* Most of the time, v, compatv, and lenv will all match the version
* where the structure was switched over to the new macros.
*
* @param v current version of the encoding that the code supports/encodes
* @param compatv oldest version that includes a __u8 compat version field
* @param lenv oldest version that includes a __u32 length wrapper
* @param bl bufferlist::iterator containing the encoded data
*/
#define DECODE_START_LEGACY_COMPAT_LEN_32(v, compatv, lenv, bl) \
__DECODE_START_LEGACY_COMPAT_LEN(v, compatv, lenv, 3, bl)
#define DECODE_START_LEGACY_COMPAT_LEN_16(v, compatv, lenv, bl) \
__DECODE_START_LEGACY_COMPAT_LEN(v, compatv, lenv, 1, bl)
/**
* finish decode block
*
* @param bl bufferlist::iterator we were decoding from
*/
#define DECODE_FINISH(bl) \
} while (false); \
if (struct_end) { \
if (bl.get_off() > struct_end) \
throw buffer::malformed_input(DECODE_ERR_PAST(__PRETTY_FUNCTION__)); \
if (bl.get_off() < struct_end) \
bl.advance(struct_end - bl.get_off()); \
}
#endif