· 7 years ago · Apr 16, 2018, 12:00 AM
1#include <iostream>
2#include <inttypes.h>
3#include <cstring>
4#include <stdlib.h>
5using namespace std;
6
7struct msgStruct {
8 uint32_t secretKey;
9 uint16_t requestType;
10 uint16_t padding;
11 char * varName;
12 char * varVal;
13};
14
15
16
17int main()
18{
19 msgStruct myMsg;
20 myMsg.secretKey = 1070;
21 myMsg.requestType = 1;
22 myMsg.padding = 0;
23 myMsg.varName = "PATH\0";
24 myMsg.varVal = "/tmp/";
25
26 const uint32_t size = (sizeof(uint32_t) + (2 * sizeof(uint16_t)) + strlen(myMsg.varName) + strlen(myMsg.varVal) + 2);
27 cout << size << endl;
28 char *target = new char[size];
29 memcpy(target, (void*)&myMsg, (sizeof(uint32_t) + 2*sizeof(uint16_t)));
30 memcpy(target + sizeof(uint32_t) + 2*sizeof(uint16_t), myMsg.varName, strlen(myMsg.varName) + 1);
31 target[sizeof(uint32_t) + 2*sizeof(uint16_t) + strlen(myMsg.varName) + 1] = '\0';
32 memcpy(target+sizeof(uint32_t) + 2*sizeof(uint16_t) + strlen(myMsg.varName) + 2, myMsg.varVal, strlen(myMsg.varVal)+1);
33 target[sizeof(uint32_t) + 2*sizeof(uint16_t) + strlen(myMsg.varName) + strlen(myMsg.varVal) + 1] = '\0';
34
35 msgStruct theMsg;
36 int x = 0;
37 for (unsigned int j = 0; j < 19; j++) {
38 if (target[j] == '\0') {
39 x = j;
40 }
41 }
42 cout << x << endl;
43 cout << strlen(target) << endl;
44 for (unsigned int i = (sizeof(uint32_t) + 2*sizeof(uint16_t)); i < x; i++) {
45 cout << target[i] << endl;
46 }
47 //memcpy(&theMsg, target, sizeof(msgStruct));
48 /*cout << theMsg.secretKey << endl;
49 cout << theMsg.requestType << endl;
50 cout << theMsg.padding << endl;
51 cout << theMsg.varName << endl;
52 cout << theMsg.varVal << endl;*/
53 return 0;
54}