Advertisement
NickG

Untitled

Sep 17th, 2013
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.88 KB | None | 0 0
  1. char const* nsmp_field_unpack(
  2.     char const *const start_ptr,
  3.     nsmp_type const type,
  4.     nsmp_packet_field *const field
  5. ) {
  6.     int size;
  7.     char const *ptr = start_ptr;
  8.     field->type = type;
  9.  
  10.     if(field->type != nsmp_string) {
  11.         size = nsmp_type_size[type]
  12.     } else {
  13.             nsmp_packet_field len;
  14.             ptr = nsmp_field_upack(ptr, nsmp_uint16, &len);
  15.             size = *(uint16_t *) len.val;
  16.             free(len.val);
  17.     }
  18.     field->val = malloc(size);
  19.  
  20.     switch(field->type) {
  21.         case nsmp_string:
  22.             strcpy((char *) field->val, ptr);
  23.             break;
  24.         case nsmp_int8:
  25.         case nsmp_uint8:
  26.             *(uint8_t *) field->val = *(uint8_t *) ptr;
  27.             break;
  28.         case nsmp_int16:
  29.         case nsmp_uint16:
  30.             *(uint16_t *) field->val = ntohs(*(uint16_t *) ptr);
  31.             break;
  32.         case nsmp_int32:
  33.         case nsmp_uint32:
  34.         case nsmp_float:
  35.             *(uint32_t *) field->val = ntohl(*(uint32_t *) ptr);
  36.             break;
  37.     }
  38.  
  39.     ptr += size;
  40.     return ptr;
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement