I have to read a binary file which is usually 266 bytes but sometimes can have additional dynamic fields. The binary fields are positional and when they are not there they are inserted anyway at 00 with the declared length. The only dynamic fields are the last two (defined in the code with two vectors). I would like to do something like this:
#pragma pack(push,1)
typedef struct binary_structure{
unsigned short len;
uint8_t system;
uint8_t vers;
unsigned char data1 [6];
unsigned char data2 [4];
unsigned short data3;
unsigned char data4 [6];
unsigned char data5 [4];
unsigned short data6;
unsigned short data_ses;
unsigned char data_list [40];
unsigned char timestamp_1 [4];
unsigned char timestamp_2 [4];
int data_byte_1;
int data_byte_2;
int data_pkt_1;
int data_pkt_2;
unsigned char data_file_trace [50];
unsigned char data_file_2 [40];
unsigned char data_username [30];
unsigned char data_7 [20];
unsigned short data_num;
int data_index;
unsigned char data_value [20];
unsigned short data_url_index;
unsigned short data_len_url;
std::vector <unsigned char> data_url_value(data_len_url);
unsigned char data_info_t [2];
unsigned short data_info_len;
std::vector <unsigned char> data_info_value(data_info_len);
}binary_structure_T, *sBinary_structure_t;
#pragma pack(pop)
As you can see I'm trying to initialize the two vectors data_info_value and data_url_value dynamically only if they are present in the binary. Usually there are none and the respective data_len_url and data_info_len fields are set to 00 00. If there are, their lengths are defined in the two fields data_info_len and data_len_url, so When these fields have the two respective lengths I would like to use them as a value to define the dimensions of the respective **data_info_value **and data_url_value vectors.
The binary file is parsed into the above structure as follows:
int main(int argc, char *argv[] {
sBinary_structure_t binary = (sBinary_structure_t)(file_buffer.data());
}
Is it possible to do it dynamically like above or do I have to do it manually?