PhoenixProtocolBuffer  1.0.1
Set of tools to decode offset from protocol buffer
Loading...
Searching...
No Matches
phoenix_field.cpp
Go to the documentation of this file.
1/***************************************
2 Auteur : Pierre Aubert
3 Mail : pierre.aubert@lapp.in2p3.fr
4 Licence : CeCILL-C
5****************************************/
6
7#include "dico_replace_var.h"
8#include "phoenix_varint.h"
9#include "phoenix_field.h"
10
12
16void phoenix_field(size_t & fieldId, size_t & fieldType, char*& iter){
17 char value = *iter;
18 fieldType = value & 7; //Get 3 first bits
19 fieldId = value >> 3; //Shifted of 3 bits on the left
20 iter += 1lu;
21}
22
24
27void phoenix_fieldSkip(char*& iter, size_t fieldType){
28 switch(fieldType){
29 case 0lu: //This is an enum
31 break;
32 case 1lu: //This is a uint64_t
33 iter += sizeof(uint64_t);
34 break;
35 case 2lu: //This is a binary array, which starts by a varint
36 iter += phoenix_readVarInt(iter);
37 break;
38 case 5lu: //This is a uint64_t
39 iter += sizeof(uint32_t);
40 break;
41 default:
42 break;
43 }
44}
45
47
50void phoenix_fieldPrintArray(char*& iter, const char * message){
51 size_t nbByte = phoenix_readVarInt(iter);
52 size_t dataOffset = iter - (char*)message;
53 std::cout << "ByteArray/SubMessage(data_offset = " << dataOffset << ", size = " << nbByte << ")" << std::endl;
54 iter += nbByte;
55}
56
58
62void phoenix_fieldPrint(char*& iter, size_t fieldType, const char * message){
63 switch(fieldType){
64 case 0lu: //This is an enum
65 std::cout << "Enum = " << phoenix_readVarInt(iter) << std::endl;
66 break;
67 case 1lu: //This is a uint64_t
68 std::cout << "uint64_t = " << phoenix_readValue<uint64_t>(iter) << std::endl;
69 break;
70 case 2lu: //This is a binary array, which starts by a varint
71 phoenix_fieldPrintArray(iter, message);
72 break;
73 case 5lu: //This is a uint64_t
74 std::cout << "uint32_t = " << phoenix_readValue<uint32_t>(iter) << std::endl;
75 break;
76 default:
77 std::cout << "Unknown Type" << std::endl;
78 break;
79 }
80}
81
83
89FieldConfig phoenix_createField(FieldType::FieldType type, size_t id, const PString & name, bool isArray){
90 FieldConfig field;
91 field.setType(type);
92 field.setId(id);
93 field.setName(name);
94 field.setIsArray(isArray);
95 return field;
96}
97
99
104bool phoenix_parseFieldConfig(FieldConfig & field, const PPath & fileName, const PString & fieldConfigKey){
105 DicoValue dico;
106 if(!parser_yml(dico, fileName)){
107 std::cerr << "phoenix_parseFieldConfig : cannot open yml file '"<<fileName<<"'" << std::endl;
108 return false;
109 }
110 dico_replace_var(dico);
111 if(!phoenix_parseFieldConfig(field, dico, fieldConfigKey)){
112 std::cerr << "phoenix_parseFieldConfig : cannot parse yml file '"<<fileName<<"'" << std::endl;
113 return false;
114 }else{
115 return true;
116 }
117}
118
120
125bool phoenix_parseFieldConfig(FieldConfig & field, const DicoValue & dico, const PString & fieldConfigKey){
126 const DicoValue * ymlField = dico.getMap(fieldConfigKey);
127 if(ymlField != NULL){
128 return phoenix_parseFieldConfig(field, *ymlField);
129 }else{
130 std::cerr << "phoenix_parseFieldConfig : FieldConfig definition '"<<fieldConfigKey<<"' does not exist in yml file" << std::endl;
131 return false;
132 }
133}
134
136
140bool phoenix_parseFieldConfig(FieldConfig & field, const DicoValue & dico){
141 const MapDicoValue & mapChildren = dico.getMapChild();
142 for(MapDicoValue::const_iterator it(mapChildren.begin()); it != mapChildren.end(); ++it){
143 PString name(it->first);
144 size_t id(phoenix_load_value_from_config<size_t>(it->second, "id", 0lu));
145 PString typeStr(phoenix_get_string(it->second, "type", "uint64"));
146 bool isArray(phoenix_load_value_from_config<bool>(it->second, "is_array", false));
148 if(type != FieldType::NONE){
149 FieldConfig subField = phoenix_createField(type, id, name, isArray);
150 if(type == FieldType::SUBMESSAGE){ //We check only the submessage definition with the type is FieldType::SUBMESSAGE
151 if(!phoenix_parseFieldConfig(subField, it->second, "sub_message")){
152 std::cerr << "phoenix_parseFieldConfig : cannot parse 'sub_message' of Field '"<<name<<"'" << std::endl;
153 return false;
154 }
155 }
156 field.getVecChildren()[name] = subField;
157 }else{
158 std::cerr << "phoenix_parseFieldConfig : type '"<<typeStr<<"' does not exist for Field '"<<name<<"'. Please use one of :" << std::endl;
159 std::cerr << "\t- enum" << std::endl;
160 std::cerr << "\t- submessage" << std::endl;
161 std::cerr << "\t- uint64" << std::endl;
162 std::cerr << "\t- uint32" << std::endl;
163 std::cerr << "\t- uint16" << std::endl;
164 std::cerr << "\t- uint8" << std::endl;
165 std::cerr << "\t- int64" << std::endl;
166 std::cerr << "\t- int32" << std::endl;
167 std::cerr << "\t- int16" << std::endl;
168 std::cerr << "\t- int8" << std::endl;
169 return false;
170 }
171 }
172 return true;
173}
174
176
180void phoenix_printFieldMessage(const char * message, size_t nbByte, size_t offset){
181 char* iter = (char*)message + offset;
182 size_t messageSize = nbByte - offset;
183 std::cout << "Field{" << std::endl;
184 while(iter < message + messageSize){
185 size_t fieldId(0lu), fieldType(0lu);
186 phoenix_field(fieldId, fieldType, iter);
187 size_t fieldOffset = iter - (char*)message;
188 std::cout << "\t- Field " << fieldId << " : field_offset = " << fieldOffset << ", ";
189 phoenix_fieldPrint(iter, fieldType, message);
190 }
191 std::cout << "}" << std::endl;
192}
193
195
198void phoenix_printFieldMessage(const char * message, size_t nbByte){
199 phoenix_printFieldMessage(message, nbByte, 0lu);
200}
201
FieldType::FieldType phoenix_fieldTypeFromStr(const std::string &name)
Convert a string into a FieldType.
Definition FieldType.cpp:78
Configuration to be used to create a field from a yml file.
void setType(const FieldType::FieldType &type)
Sets the type of the FieldConfig.
void setId(size_t id)
Sets the id of the FieldConfig.
const std::map< PString, FieldConfig > & getVecChildren() const
Gets the vecChildren of the FieldConfig.
void setName(const PString &name)
Sets the name of the FieldConfig.
void setIsArray(bool isArray)
Sets the isArray of the FieldConfig.
FieldType
Type of the Field.
Definition FieldType.h:14
FieldConfig phoenix_createField(FieldType::FieldType type, size_t id, const PString &name, bool isArray)
Create a FieldConfig.
bool phoenix_parseFieldConfig(FieldConfig &field, const PPath &fileName, const PString &fieldConfigKey)
Parse a yml file and search for a field_config key.
void phoenix_field(size_t &fieldId, size_t &fieldType, char *&iter)
Reads a Field header.
void phoenix_fieldPrint(char *&iter, size_t fieldType, const char *message)
Print the current field.
void phoenix_fieldPrintArray(char *&iter, const char *message)
Print the current ByteArray.
void phoenix_fieldSkip(char *&iter, size_t fieldType)
Skip the current field.
void phoenix_printFieldMessage(const char *message, size_t nbByte, size_t offset)
Print the Fields in a message.
T phoenix_readValue(char *&iter)
Read a value.
size_t phoenix_readVarInt(char *&iter)
Reads and print a varint from protocol buffer.
void phoenix_skipVarInt(char *&iter)
Skips a varint.