DCCL v4
Loading...
Searching...
No Matches
field_codec_var_bytes.cpp
1// Copyright 2018-2023:
2// GobySoft, LLC (2013-)
3// Community contributors (see AUTHORS file)
4// File authors:
5// Toby Schneider <toby@gobysoft.org>
6//
7//
8// This file is part of the Dynamic Compact Control Language Library
9// ("DCCL").
10//
11// DCCL is free software: you can redistribute it and/or modify
12// it under the terms of the GNU Lesser General Public License as published by
13// the Free Software Foundation, either version 2.1 of the License, or
14// (at your option) any later version.
15//
16// DCCL is distributed in the hope that it will be useful,
17// but WITHOUT ANY WARRANTY; without even the implied warranty of
18// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19// GNU Lesser General Public License for more details.
20//
21// You should have received a copy of the GNU Lesser General Public License
22// along with DCCL. If not, see <http://www.gnu.org/licenses/>.
23#include "field_codec_var_bytes.h"
24#include "../field_codec_manager.h"
25#include "field_codec_default.h"
26
27using namespace dccl::logger;
28
30
31dccl::Bitset dccl::v3::VarBytesCodec::encode(const std::string& wire_value)
32{
33 std::string s = wire_value;
34 if (s.size() > dccl_field_options().max_length())
35 {
36 if (this->strict())
37 throw(dccl::OutOfRangeException(std::string("Bytes too long for field: ") +
38 FieldCodecBase::this_field()->DebugString(),
39 this->this_field(), this->this_descriptor()));
40
41 dccl::dlog.is(DEBUG2) &&
42 dccl::dlog << "Bytes " << s << " exceeds `dccl.max_length`, truncating" << std::endl;
43 s.resize(dccl_field_options().max_length());
44 }
45
46 dccl::Bitset value_bits;
47 value_bits.from_byte_string(s);
48
49 dccl::Bitset length_bits(presence_size() + prefix_size(), s.length());
50
51 if (!use_required()) // set the presence bit
52 {
53 length_bits <<= 1;
54 length_bits.set(0);
55 }
56
57 dccl::dlog.is(DEBUG2) && dccl::dlog << "dccl::v3::VarBytesCodec value_bits: " << value_bits
58 << std::endl;
59
60 dccl::dlog.is(DEBUG2) && dccl::dlog << "dccl::v3::VarBytesCodec length_bits: " << length_bits
61 << std::endl;
62
63 // adds to MSBs
64 for (int i = 0, n = value_bits.size(); i < n; ++i) length_bits.push_back(value_bits[i]);
65
66 dccl::dlog.is(DEBUG2) && dccl::dlog << "dccl::v3::VarBytesCodec created: " << length_bits
67 << std::endl;
68
69 return length_bits;
70}
71
73{
74 if (!use_required())
75 {
76 if (bits->to_ulong() == 0)
77 {
79 }
80 else
81 {
82 bits->get_more_bits(prefix_size());
83 (*bits) >>= 1;
84 }
85 }
86
87 unsigned value_length = bits->to_ulong();
88 unsigned header_length = presence_size() + prefix_size();
89
90 dccl::dlog.is(DEBUG2) && dccl::dlog << "Length of string is = " << value_length << std::endl;
91
92 dccl::dlog.is(DEBUG2) && dccl::dlog << "bits before get_more_bits " << *bits << std::endl;
93
94 // grabs more bits to add to the MSBs of `bits`
95 bits->get_more_bits(value_length * dccl::BITS_IN_BYTE);
96
97 dccl::dlog.is(DEBUG2) && dccl::dlog << "bits after get_more_bits " << *bits << std::endl;
98 dccl::Bitset string_body_bits = *bits;
99 string_body_bits >>= header_length;
100 string_body_bits.resize(bits->size() - header_length);
101
102 dccl::dlog.is(DEBUG2) && dccl::dlog << "string_body_bits " << string_body_bits << std::endl;
103
104 return string_body_bits.to_byte_string();
105}
106
107unsigned dccl::v3::VarBytesCodec::size() { return min_size(); }
108
109unsigned dccl::v3::VarBytesCodec::size(const std::string& wire_value)
110{
111 return std::min(presence_size() + prefix_size() +
112 static_cast<unsigned>(wire_value.length() * dccl::BITS_IN_BYTE),
113 max_size());
114}
115
117{
118 return presence_size() + prefix_size() + dccl_field_options().max_length() * dccl::BITS_IN_BYTE;
119}
120
122{
123 if (use_required())
124 return prefix_size();
125 else
126 return presence_size();
127}
128
130{
131 require(dccl_field_options().has_max_length(), "missing (dccl.field).max_length");
132}
A variable size container of bits (subclassed from std::deque<bool>) with an optional hierarchy....
Definition bitset.h:43
void get_more_bits(size_type num_bits)
Retrieve more bits from the parent Bitset.
Definition bitset.h:420
unsigned long to_ulong() const
Returns the value of the Bitset as an unsigned long integer. Equivalent to to<unsigned long>().
Definition bitset.h:275
void from_byte_string(const std::string &s)
Sets the value of the Bitset to the contents of a byte string, where each character represents 8 bits...
Definition bitset.h:335
std::string to_byte_string()
Returns the value of the Bitset to a byte string, where each character represents 8 bits of the Bitse...
Definition bitset.h:297
Bitset & set(size_type n, bool val=true)
Set a bit to a given value.
Definition bitset.h:175
const google::protobuf::FieldDescriptor * this_field() const
Returns the FieldDescriptor (field schema meta-data) for this field.
bool is(logger::Verbosity verbosity, logger::Group group=logger::GENERAL)
Indicates the verbosity of the Logger until the next std::flush or std::endl. The boolean return is u...
Definition logger.h:192
Exception used to signal null (non-existent) value within field codecs during decode.
Definition exception.h:62
std::string decode(dccl::Bitset *bits) override
Decode a field. If the field is empty (i.e. was encoded using the zero-argument encode()),...
unsigned max_size() override
Calculate maximum size of the field in bits.
void validate() override
Validate a field. Use require() inside your overloaded validate() to assert requirements or throw Exc...
dccl::Bitset encode() override
Encode an empty field.
unsigned min_size() override
Calculate minimum size of the field in bits.
unsigned size() override
Calculate the size (in bits) of an empty field.