Guest User

Untitled

a guest
Mar 23rd, 2016
406
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 7.89 KB | None | 0 0
  1. typedef enum
  2. {
  3.     R_STORAGE_TYPE_NONE,
  4.     R_STORAGE_TYPE_INT8,
  5.     R_STORAGE_TYPE_INT16_LE,
  6.     R_STORAGE_TYPE_INT16_BE,
  7.     R_STORAGE_TYPE_INT32_LE,
  8.     R_STORAGE_TYPE_INT32_BE,
  9.     R_STORAGE_TYPE_INT64_LE,
  10.     R_STORAGE_TYPE_INT64_BE,
  11.     R_STORAGE_TYPE_UINT8,
  12.     R_STORAGE_TYPE_UINT16_LE,
  13.     R_STORAGE_TYPE_UINT16_BE,
  14.     R_STORAGE_TYPE_UINT32_LE,
  15.     R_STORAGE_TYPE_UINT32_BE,
  16.     R_STORAGE_TYPE_UINT64_LE,
  17.     R_STORAGE_TYPE_UINT64_BE,
  18.     R_STORAGE_TYPE_FLOAT32,
  19.     R_STORAGE_TYPE_FLOAT64,
  20.     R_STORAGE_TYPE_STRING,
  21.     R_STORAGE_TYPE_STRING16,
  22.     R_STORAGE_TYPE_LAST
  23. } RStorageTypes;
  24.  
  25. typedef struct
  26. {
  27.     void * var_NONE;
  28. int8_t var_INT8;
  29. int16_t var_INT16_LE;
  30. int16_t var_INT16_BE;
  31. int32_t var_INT32_LE;
  32. int32_t var_INT32_BE;
  33. int64_t var_INT64_LE;
  34. int64_t var_INT64_BE;
  35. uint8_t var_UINT8;
  36. uint16_t var_UINT16_LE;
  37. uint16_t var_UINT16_BE;
  38. uint32_t var_UINT32_LE;
  39. uint32_t var_UINT32_BE;
  40. uint64_t var_UINT64_LE;
  41. uint64_t var_UINT64_BE;
  42. float var_FLOAT32;
  43. double var_FLOAT64;
  44. char * var_STRING;
  45. RBytes * var_STRING16;
  46. } RStoragePack;
  47.  
  48. typedef struct
  49. {
  50.     uint32_t  size;
  51.     uint8_t * bytes;
  52. } RBytes;
  53.  
  54. void append(RBytes * bytes, uint32_t pos, RStoragePack * vars, RStorageTypes type)
  55. {
  56.     struct uint16_struct
  57.     {
  58.         uint16_t   uint16; // 2
  59.             char dummy[6]; // 6
  60.     } __attribute__((packed));
  61.  
  62.     struct int16_struct
  63.     {
  64.          int16_t    int16; // 2
  65.             char dummy[6]; // 6
  66.     } __attribute__((packed));
  67.  
  68.     struct uint32_struct
  69.     {
  70.         uint32_t   uint32; // 4
  71.             char dummy[4]; // 4
  72.     } __attribute__((packed));
  73.  
  74.     struct int32_struct
  75.     {
  76.          int32_t    int32; // 4
  77.             char dummy[4]; // 4
  78.     } __attribute__((packed));
  79.  
  80.     struct uint64_struct
  81.     {
  82.         uint64_t   uint64; // 8
  83.     } __attribute__((packed));
  84.  
  85.     struct int64_struct
  86.     {
  87.          int64_t    int64; // 8
  88.     } __attribute__((packed));
  89.  
  90.     typedef union
  91.     {
  92.         struct  int16_struct  int16;
  93.         struct uint16_struct uint16;
  94.         struct  int32_struct  int32;
  95.         struct uint32_struct uint32;
  96.         struct  int64_struct  int64;
  97.         struct uint64_struct uint64;
  98.                      uint8_t buffer[8];
  99.     } Conv;
  100.  
  101.     Conv conv;
  102.  
  103.     ///@todo Need to check expand's result, therefore change void append to RError append
  104.  
  105.     switch (type)
  106.     {
  107.     case R_STORAGE_TYPE_UINT8:
  108.         expand(bytes, bytes->size + 1);
  109.         bytes->bytes[pos] = vars->var_UINT8;
  110.         break;
  111.     case R_STORAGE_TYPE_INT8 :
  112.         expand(bytes, bytes->size + 1);
  113.         bytes->bytes[pos] = vars->var_INT8;
  114.         break;
  115.  
  116.     case R_STORAGE_TYPE_UINT16_BE:
  117.         conv.uint16.uint16 = vars->var_UINT16_BE;
  118.         expand(bytes, bytes->size + 2);
  119.         bytes->bytes[pos    ] = conv.buffer[0]; ///@todo need to check the byte order
  120.         bytes->bytes[pos + 1] = conv.buffer[1];
  121.         break;
  122.     case R_STORAGE_TYPE_UINT16_LE:
  123.         conv.uint16.uint16 = vars->var_UINT16_LE;
  124.         expand(bytes, bytes->size + 2);
  125.         bytes->bytes[pos    ] = conv.buffer[1]; ///@todo need to check the byte order
  126.         bytes->bytes[pos + 1] = conv.buffer[0];
  127.         break;
  128.     case R_STORAGE_TYPE_INT16_BE:
  129.         conv.int16.int16 = vars->var_INT16_BE;
  130.         expand(bytes, bytes->size + 2);
  131.         bytes->bytes[pos    ] = conv.buffer[0]; ///@todo need to check the byte order
  132.         bytes->bytes[pos + 1] = conv.buffer[1];
  133.         break;
  134.     case R_STORAGE_TYPE_INT16_LE:
  135.         conv.int16.int16 = vars->var_INT16_LE;
  136.         expand(bytes, bytes->size + 2);
  137.         bytes->bytes[pos    ] = conv.buffer[1]; ///@todo need to check the byte order
  138.         bytes->bytes[pos + 1] = conv.buffer[0];
  139.         break;
  140.  
  141.     case R_STORAGE_TYPE_UINT32_BE:
  142.         conv.uint32.uint32 = vars->var_UINT32_BE;
  143.         expand(bytes, bytes->size + 2);
  144.         bytes->bytes[pos    ] = conv.buffer[0]; ///@todo need to check the byte order
  145.         bytes->bytes[pos + 1] = conv.buffer[1];
  146.         bytes->bytes[pos + 2] = conv.buffer[2];
  147.         bytes->bytes[pos + 3] = conv.buffer[3];
  148.         break;
  149.     case R_STORAGE_TYPE_UINT32_LE:
  150.         conv.uint32.uint32 = vars->var_UINT32_LE;
  151.         expand(bytes, bytes->size + 2);
  152.         bytes->bytes[pos    ] = conv.buffer[3]; ///@todo need to check the byte order
  153.         bytes->bytes[pos + 1] = conv.buffer[2];
  154.         bytes->bytes[pos + 2] = conv.buffer[1];
  155.         bytes->bytes[pos + 3] = conv.buffer[0];
  156.         break;
  157.     case R_STORAGE_TYPE_INT32_BE:
  158.         conv.int32.int32 = vars->var_INT32_BE;
  159.         expand(bytes, bytes->size + 2);
  160.         bytes->bytes[pos    ] = conv.buffer[0]; ///@todo need to check the byte order
  161.         bytes->bytes[pos + 1] = conv.buffer[1];
  162.         bytes->bytes[pos + 2] = conv.buffer[2];
  163.         bytes->bytes[pos + 3] = conv.buffer[3];
  164.         break;
  165.     case R_STORAGE_TYPE_INT32_LE:
  166.         conv.int32.int32 = vars->var_INT32_LE;
  167.         expand(bytes, bytes->size + 2);
  168.         bytes->bytes[pos    ] = conv.buffer[3]; ///@todo need to check the byte order
  169.         bytes->bytes[pos + 1] = conv.buffer[2];
  170.         bytes->bytes[pos + 2] = conv.buffer[1];
  171.         bytes->bytes[pos + 3] = conv.buffer[0];
  172.         break;
  173.  
  174.     case R_STORAGE_TYPE_UINT64_BE:
  175.         conv.uint64.uint64 = vars->var_UINT64_BE;
  176.         expand(bytes, bytes->size + 2);
  177.         bytes->bytes[pos    ] = conv.buffer[0]; ///@todo need to check the byte order
  178.         bytes->bytes[pos + 1] = conv.buffer[1];
  179.         bytes->bytes[pos + 2] = conv.buffer[2];
  180.         bytes->bytes[pos + 3] = conv.buffer[3];
  181.         bytes->bytes[pos + 4] = conv.buffer[4];
  182.         bytes->bytes[pos + 5] = conv.buffer[5];
  183.         bytes->bytes[pos + 6] = conv.buffer[6];
  184.         bytes->bytes[pos + 7] = conv.buffer[7];
  185.         break;
  186.     case R_STORAGE_TYPE_UINT64_LE:
  187.         conv.uint64.uint64 = vars->var_UINT64_LE;
  188.         expand(bytes, bytes->size + 2);
  189.         bytes->bytes[pos    ] = conv.buffer[7]; ///@todo need to check the byte order
  190.         bytes->bytes[pos + 1] = conv.buffer[6];
  191.         bytes->bytes[pos + 2] = conv.buffer[5];
  192.         bytes->bytes[pos + 3] = conv.buffer[4];
  193.         bytes->bytes[pos + 4] = conv.buffer[3];
  194.         bytes->bytes[pos + 5] = conv.buffer[2];
  195.         bytes->bytes[pos + 6] = conv.buffer[1];
  196.         bytes->bytes[pos + 7] = conv.buffer[0];
  197.         break;
  198.     case R_STORAGE_TYPE_INT64_BE:
  199.         conv.int64.int64 = vars->var_INT64_BE;
  200.         expand(bytes, bytes->size + 2);
  201.         bytes->bytes[pos    ] = conv.buffer[0]; ///@todo need to check the byte order
  202.         bytes->bytes[pos + 1] = conv.buffer[1];
  203.         bytes->bytes[pos + 2] = conv.buffer[2];
  204.         bytes->bytes[pos + 3] = conv.buffer[3];
  205.         bytes->bytes[pos + 4] = conv.buffer[4];
  206.         bytes->bytes[pos + 5] = conv.buffer[5];
  207.         bytes->bytes[pos + 6] = conv.buffer[6];
  208.         bytes->bytes[pos + 7] = conv.buffer[7];
  209.         break;
  210.     case R_STORAGE_TYPE_INT64_LE:
  211.         conv.int64.int64 = vars->var_INT64_LE;
  212.         expand(bytes, bytes->size + 2);
  213.         bytes->bytes[pos    ] = conv.buffer[7]; ///@todo need to check the byte order
  214.         bytes->bytes[pos + 1] = conv.buffer[6];
  215.         bytes->bytes[pos + 2] = conv.buffer[5];
  216.         bytes->bytes[pos + 3] = conv.buffer[4];
  217.         bytes->bytes[pos + 4] = conv.buffer[3];
  218.         bytes->bytes[pos + 5] = conv.buffer[2];
  219.         bytes->bytes[pos + 6] = conv.buffer[1];
  220.         bytes->bytes[pos + 7] = conv.buffer[0];
  221.         break;
  222.  
  223.     case R_STORAGE_TYPE_STRING:
  224.         expand(bytes, strlen(vars->var_STRING));
  225.         memcpy(bytes->bytes - strlen(vars->var_STRING),
  226.                vars->var_STRING, strlen(vars->var_STRING));
  227.         break;
  228.  
  229.     case R_STORAGE_TYPE_STRING16:
  230.         expand(bytes, vars->var_STRING16->size);
  231.         memcpy(bytes->bytes - vars->var_STRING16->size,
  232.                vars->var_STRING16->bytes, vars->var_STRING16->size);
  233.     default:
  234.         return;
  235.     }
  236. }
Advertisement
Add Comment
Please, Sign In to add comment