Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- typedef enum
- {
- R_STORAGE_TYPE_NONE,
- R_STORAGE_TYPE_INT8,
- R_STORAGE_TYPE_INT16_LE,
- R_STORAGE_TYPE_INT16_BE,
- R_STORAGE_TYPE_INT32_LE,
- R_STORAGE_TYPE_INT32_BE,
- R_STORAGE_TYPE_INT64_LE,
- R_STORAGE_TYPE_INT64_BE,
- R_STORAGE_TYPE_UINT8,
- R_STORAGE_TYPE_UINT16_LE,
- R_STORAGE_TYPE_UINT16_BE,
- R_STORAGE_TYPE_UINT32_LE,
- R_STORAGE_TYPE_UINT32_BE,
- R_STORAGE_TYPE_UINT64_LE,
- R_STORAGE_TYPE_UINT64_BE,
- R_STORAGE_TYPE_FLOAT32,
- R_STORAGE_TYPE_FLOAT64,
- R_STORAGE_TYPE_STRING,
- R_STORAGE_TYPE_STRING16,
- R_STORAGE_TYPE_LAST
- } RStorageTypes;
- typedef struct
- {
- void * var_NONE;
- int8_t var_INT8;
- int16_t var_INT16_LE;
- int16_t var_INT16_BE;
- int32_t var_INT32_LE;
- int32_t var_INT32_BE;
- int64_t var_INT64_LE;
- int64_t var_INT64_BE;
- uint8_t var_UINT8;
- uint16_t var_UINT16_LE;
- uint16_t var_UINT16_BE;
- uint32_t var_UINT32_LE;
- uint32_t var_UINT32_BE;
- uint64_t var_UINT64_LE;
- uint64_t var_UINT64_BE;
- float var_FLOAT32;
- double var_FLOAT64;
- char * var_STRING;
- RBytes * var_STRING16;
- } RStoragePack;
- typedef struct
- {
- uint32_t size;
- uint8_t * bytes;
- } RBytes;
- void append(RBytes * bytes, uint32_t pos, RStoragePack * vars, RStorageTypes type)
- {
- struct uint16_struct
- {
- uint16_t uint16; // 2
- char dummy[6]; // 6
- } __attribute__((packed));
- struct int16_struct
- {
- int16_t int16; // 2
- char dummy[6]; // 6
- } __attribute__((packed));
- struct uint32_struct
- {
- uint32_t uint32; // 4
- char dummy[4]; // 4
- } __attribute__((packed));
- struct int32_struct
- {
- int32_t int32; // 4
- char dummy[4]; // 4
- } __attribute__((packed));
- struct uint64_struct
- {
- uint64_t uint64; // 8
- } __attribute__((packed));
- struct int64_struct
- {
- int64_t int64; // 8
- } __attribute__((packed));
- typedef union
- {
- struct int16_struct int16;
- struct uint16_struct uint16;
- struct int32_struct int32;
- struct uint32_struct uint32;
- struct int64_struct int64;
- struct uint64_struct uint64;
- uint8_t buffer[8];
- } Conv;
- Conv conv;
- ///@todo Need to check expand's result, therefore change void append to RError append
- switch (type)
- {
- case R_STORAGE_TYPE_UINT8:
- expand(bytes, bytes->size + 1);
- bytes->bytes[pos] = vars->var_UINT8;
- break;
- case R_STORAGE_TYPE_INT8 :
- expand(bytes, bytes->size + 1);
- bytes->bytes[pos] = vars->var_INT8;
- break;
- case R_STORAGE_TYPE_UINT16_BE:
- conv.uint16.uint16 = vars->var_UINT16_BE;
- expand(bytes, bytes->size + 2);
- bytes->bytes[pos ] = conv.buffer[0]; ///@todo need to check the byte order
- bytes->bytes[pos + 1] = conv.buffer[1];
- break;
- case R_STORAGE_TYPE_UINT16_LE:
- conv.uint16.uint16 = vars->var_UINT16_LE;
- expand(bytes, bytes->size + 2);
- bytes->bytes[pos ] = conv.buffer[1]; ///@todo need to check the byte order
- bytes->bytes[pos + 1] = conv.buffer[0];
- break;
- case R_STORAGE_TYPE_INT16_BE:
- conv.int16.int16 = vars->var_INT16_BE;
- expand(bytes, bytes->size + 2);
- bytes->bytes[pos ] = conv.buffer[0]; ///@todo need to check the byte order
- bytes->bytes[pos + 1] = conv.buffer[1];
- break;
- case R_STORAGE_TYPE_INT16_LE:
- conv.int16.int16 = vars->var_INT16_LE;
- expand(bytes, bytes->size + 2);
- bytes->bytes[pos ] = conv.buffer[1]; ///@todo need to check the byte order
- bytes->bytes[pos + 1] = conv.buffer[0];
- break;
- case R_STORAGE_TYPE_UINT32_BE:
- conv.uint32.uint32 = vars->var_UINT32_BE;
- expand(bytes, bytes->size + 2);
- bytes->bytes[pos ] = conv.buffer[0]; ///@todo need to check the byte order
- bytes->bytes[pos + 1] = conv.buffer[1];
- bytes->bytes[pos + 2] = conv.buffer[2];
- bytes->bytes[pos + 3] = conv.buffer[3];
- break;
- case R_STORAGE_TYPE_UINT32_LE:
- conv.uint32.uint32 = vars->var_UINT32_LE;
- expand(bytes, bytes->size + 2);
- bytes->bytes[pos ] = conv.buffer[3]; ///@todo need to check the byte order
- bytes->bytes[pos + 1] = conv.buffer[2];
- bytes->bytes[pos + 2] = conv.buffer[1];
- bytes->bytes[pos + 3] = conv.buffer[0];
- break;
- case R_STORAGE_TYPE_INT32_BE:
- conv.int32.int32 = vars->var_INT32_BE;
- expand(bytes, bytes->size + 2);
- bytes->bytes[pos ] = conv.buffer[0]; ///@todo need to check the byte order
- bytes->bytes[pos + 1] = conv.buffer[1];
- bytes->bytes[pos + 2] = conv.buffer[2];
- bytes->bytes[pos + 3] = conv.buffer[3];
- break;
- case R_STORAGE_TYPE_INT32_LE:
- conv.int32.int32 = vars->var_INT32_LE;
- expand(bytes, bytes->size + 2);
- bytes->bytes[pos ] = conv.buffer[3]; ///@todo need to check the byte order
- bytes->bytes[pos + 1] = conv.buffer[2];
- bytes->bytes[pos + 2] = conv.buffer[1];
- bytes->bytes[pos + 3] = conv.buffer[0];
- break;
- case R_STORAGE_TYPE_UINT64_BE:
- conv.uint64.uint64 = vars->var_UINT64_BE;
- expand(bytes, bytes->size + 2);
- bytes->bytes[pos ] = conv.buffer[0]; ///@todo need to check the byte order
- bytes->bytes[pos + 1] = conv.buffer[1];
- bytes->bytes[pos + 2] = conv.buffer[2];
- bytes->bytes[pos + 3] = conv.buffer[3];
- bytes->bytes[pos + 4] = conv.buffer[4];
- bytes->bytes[pos + 5] = conv.buffer[5];
- bytes->bytes[pos + 6] = conv.buffer[6];
- bytes->bytes[pos + 7] = conv.buffer[7];
- break;
- case R_STORAGE_TYPE_UINT64_LE:
- conv.uint64.uint64 = vars->var_UINT64_LE;
- expand(bytes, bytes->size + 2);
- bytes->bytes[pos ] = conv.buffer[7]; ///@todo need to check the byte order
- bytes->bytes[pos + 1] = conv.buffer[6];
- bytes->bytes[pos + 2] = conv.buffer[5];
- bytes->bytes[pos + 3] = conv.buffer[4];
- bytes->bytes[pos + 4] = conv.buffer[3];
- bytes->bytes[pos + 5] = conv.buffer[2];
- bytes->bytes[pos + 6] = conv.buffer[1];
- bytes->bytes[pos + 7] = conv.buffer[0];
- break;
- case R_STORAGE_TYPE_INT64_BE:
- conv.int64.int64 = vars->var_INT64_BE;
- expand(bytes, bytes->size + 2);
- bytes->bytes[pos ] = conv.buffer[0]; ///@todo need to check the byte order
- bytes->bytes[pos + 1] = conv.buffer[1];
- bytes->bytes[pos + 2] = conv.buffer[2];
- bytes->bytes[pos + 3] = conv.buffer[3];
- bytes->bytes[pos + 4] = conv.buffer[4];
- bytes->bytes[pos + 5] = conv.buffer[5];
- bytes->bytes[pos + 6] = conv.buffer[6];
- bytes->bytes[pos + 7] = conv.buffer[7];
- break;
- case R_STORAGE_TYPE_INT64_LE:
- conv.int64.int64 = vars->var_INT64_LE;
- expand(bytes, bytes->size + 2);
- bytes->bytes[pos ] = conv.buffer[7]; ///@todo need to check the byte order
- bytes->bytes[pos + 1] = conv.buffer[6];
- bytes->bytes[pos + 2] = conv.buffer[5];
- bytes->bytes[pos + 3] = conv.buffer[4];
- bytes->bytes[pos + 4] = conv.buffer[3];
- bytes->bytes[pos + 5] = conv.buffer[2];
- bytes->bytes[pos + 6] = conv.buffer[1];
- bytes->bytes[pos + 7] = conv.buffer[0];
- break;
- case R_STORAGE_TYPE_STRING:
- expand(bytes, strlen(vars->var_STRING));
- memcpy(bytes->bytes - strlen(vars->var_STRING),
- vars->var_STRING, strlen(vars->var_STRING));
- break;
- case R_STORAGE_TYPE_STRING16:
- expand(bytes, vars->var_STRING16->size);
- memcpy(bytes->bytes - vars->var_STRING16->size,
- vars->var_STRING16->bytes, vars->var_STRING16->size);
- default:
- return;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment