GCC Code Coverage Report


Directory: cvmfs/
File: cvmfs/nfs4/xdr.h
Date: 2026-10-18 02:40:11
Exec Total Coverage
Lines: 16 16 100.0%
Branches: 2 2 100.0%

Line Branch Exec Source
1 /**
2 * This file is part of the CernVM File System.
3 *
4 * Minimal XDR (RFC 4506) encoder/decoder for the built-in NFS server.
5 */
6
7 #ifndef CVMFS_NFS4_XDR_H_
8 #define CVMFS_NFS4_XDR_H_
9
10 #include <stdint.h>
11
12 #include <cstddef>
13 #include <string>
14 #include <vector>
15
16 namespace nfs4 {
17
18 /**
19 * Reads XDR items from a borrowed buffer. Errors are sticky: after the first
20 * short read every getter returns false and ok() stays false, so callers can
21 * decode a whole structure and check once.
22 */
23 class XdrReader {
24 public:
25 5195 XdrReader(const unsigned char *data, size_t size)
26 5195 : data_(data), size_(size), pos_(0), ok_(true) { }
27
28 bool GetU32(uint32_t *v);
29 bool GetI32(int32_t *v);
30 bool GetU64(uint64_t *v);
31 bool GetI64(int64_t *v);
32 bool GetBool(bool *v);
33 /** Variable-length opaque<max_len>; max_len 0 means unbounded. */
34 bool GetOpaque(std::string *v, uint32_t max_len = 0);
35 bool GetFixedOpaque(unsigned char *v, size_t len);
36 3407 bool GetString(std::string *v, uint32_t max_len = 0) {
37 3407 return GetOpaque(v, max_len);
38 }
39 bool Skip(size_t len);
40
41 6598 bool ok() const { return ok_; }
42 size_t pos() const { return pos_; }
43 137 size_t remaining() const { return size_ - pos_; }
44 91 const unsigned char *cursor() const { return data_ + pos_; }
45
46 private:
47 bool Take(size_t len);
48
49 const unsigned char *data_;
50 size_t size_;
51 size_t pos_;
52 bool ok_;
53 };
54
55
56 class XdrWriter {
57 public:
58 5881 XdrWriter() { }
59
60 void PutU32(uint32_t v);
61 void PutI32(int32_t v) { PutU32(static_cast<uint32_t>(v)); }
62 void PutU64(uint64_t v);
63 26 void PutI64(int64_t v) { PutU64(static_cast<uint64_t>(v)); }
64
2/2
✓ Branch 0 taken 995 times.
✓ Branch 1 taken 351 times.
1346 void PutBool(bool v) { PutU32(v ? 1 : 0); }
65 void PutOpaque(const void *data, size_t len);
66 7437 void PutOpaque(const std::string &v) { PutOpaque(v.data(), v.size()); }
67 void PutFixedOpaque(const void *data, size_t len);
68 3524 void PutString(const std::string &v) { PutOpaque(v); }
69 /** Raw bytes, already XDR encoded. */
70 void PutRaw(const void *data, size_t len);
71
72 /** Reserves a u32 slot that can be filled in later via PatchU32. */
73 size_t ReserveU32();
74 void PatchU32(size_t offset, uint32_t v);
75
76 6314 size_t size() const { return buf_.size(); }
77 338 void Truncate(size_t size) { buf_.resize(size); }
78 7990 const std::string &buffer() const { return buf_; }
79 65 std::string *mutable_buffer() { return &buf_; }
80
81 private:
82 std::string buf_;
83 };
84
85 } // namespace nfs4
86
87 #endif // CVMFS_NFS4_XDR_H_
88