END_TEST /** * \brief Unit test of packing real32 values */ START_TEST ( test_UnPack_Real32 ) { size_t buf_pos = 0; unsigned char buffer[REAL32_BUF_SIZE] = { 0x00, 0x00, 0x00, 0x00, /* 0.0 */ 0x40, 0x00, 0x00, 0x01, /* 2.00000024 */ 0x40, 0x00, 0x00, 0x00, /* 2.0 */ 0xc0, 0x00, 0x00, 0x00, /* -2.0 */ 0x3f, 0xff, 0xff, 0xfe, /* 1.99999976 */ 0, }; real32 expected_results[REAL32_TV_SIZE] = { 0.0, 2.00000024, 2.0, -2.0, 1.99999976, 0, }; real32 results[REAL32_TV_SIZE] = {0,}; int i = 0; buf_pos += vnp_raw_unpack_real32((void*)&buffer[buf_pos], &results[i++]); buf_pos += vnp_raw_unpack_real32((void*)&buffer[buf_pos], &results[i++]); buf_pos += vnp_raw_unpack_real32((void*)&buffer[buf_pos], &results[i++]); buf_pos += vnp_raw_unpack_real32((void*)&buffer[buf_pos], &results[i++]); buf_pos += vnp_raw_unpack_real32((void*)&buffer[buf_pos], &results[i++]); fail_unless( buf_pos == 5*4, "Size of real32 buffer: %d != 20", buf_pos); for(i = 0; i < REAL32_TV_SIZE; i++) { fail_unless( results[i] == expected_results[i], "Test vector of real32 differs at position: %d (%f != %f)", i, results[i], expected_results[i]); } }
/** * \brief This function unpack negotiate commands: Change_L/R and Confirm_L/R * from the buffer * * Unpack negotiate command (CHANGE_L, CONFIRM_L, CHANGE_R, CONFIRM_R). Buffer * size has to be at least 2 bytes long (this check has to be done before * calling this function) */ int v_raw_unpack_negotiate_cmd(const char *buffer, ssize_t buffer_size, struct Negotiate_Cmd *negotiate_cmd) { int str_len=0; unsigned short buffer_pos = 0, length, i; unsigned char length8 = 0, len8, lenlen; /* Unpack command ID */ buffer_pos += vnp_raw_unpack_uint8(&buffer[buffer_pos], &negotiate_cmd->id); /* Unpack Length of values */ buffer_pos += vnp_raw_unpack_uint8(&buffer[buffer_pos], &length8); /* Check if the length of the command is stored in the second byte of * command or is it stored in two bytes after second byte of the command */ if(length8==0xFF) { buffer_pos += vnp_raw_unpack_uint16(&buffer[buffer_pos], &length); lenlen = 3; } else { length = length8; lenlen = 1; } /* Security check: check if the length of the command is not bigger * then buffer_size. If this test failed, then return buffer_size and set * count of received values to the zero. */ if(buffer_size < length) { v_print_log(VRS_PRINT_WARNING, "Buffer size: %d < command length: %d.\n", buffer_size, length); negotiate_cmd->count = 0; return buffer_size; /* Security check: check if the length of the command is equal or bigger * then minimal length of the negotiate command: ID_len + Length_len + * Feature_len*/ } else if(length < (1+lenlen+1)) { v_print_log(VRS_PRINT_WARNING, "Command length: %d < (1+%d+1).\n", length, lenlen); negotiate_cmd->count = 0; return buffer_size; } /* Unpack Feature ID */ buffer_pos += vnp_raw_unpack_uint8(&buffer[buffer_pos], &negotiate_cmd->feature); /* Compute count of values in preference list. When unknown or unsupported * feature is detected, then processing of this command is stopped and it * returns the length of command. */ switch(negotiate_cmd->feature) { case FTR_RSV_ID: v_print_log(VRS_PRINT_WARNING, "Received RESERVED feature ID\n"); negotiate_cmd->count = 0; return length; /* This feature id should never be sent or received */ case FTR_FC_ID: case FTR_CC_ID: case FTR_RWIN_SCALE: case FTR_CMD_COMPRESS: negotiate_cmd->count = length - (1+lenlen+1); break; case FTR_HOST_URL: case FTR_TOKEN: case FTR_DED: case FTR_CLIENT_NAME: case FTR_CLIENT_VERSION: negotiate_cmd->count = 0; while(str_len < (length -(1+lenlen+1))) { vnp_raw_unpack_uint8(&buffer[buffer_pos+str_len], &len8); str_len += 1 + len8; negotiate_cmd->count++; } break; case FTR_FPS: negotiate_cmd->count = (length - (1+lenlen))/4; break; default: v_print_log(VRS_PRINT_WARNING, "Received UNKNOWN feature ID\n"); negotiate_cmd->count = 0; return length; } /* Unpack values (preference list) */ for(i=0; i<negotiate_cmd->count; i++) { switch(negotiate_cmd->feature) { case FTR_FC_ID: case FTR_CC_ID: case FTR_RWIN_SCALE: case FTR_CMD_COMPRESS: buffer_pos += vnp_raw_unpack_uint8(&buffer[buffer_pos], &negotiate_cmd->value[i].uint8); break; case FTR_HOST_URL: case FTR_TOKEN: case FTR_DED: case FTR_CLIENT_NAME: case FTR_CLIENT_VERSION: buffer_pos += vnp_raw_unpack_string8_(&buffer[buffer_pos], buffer_size-buffer_pos, &negotiate_cmd->value[i].string8); break; case FTR_FPS: buffer_pos += vnp_raw_unpack_real32(&buffer[buffer_pos], &negotiate_cmd->value[i].real32); break; default: break; } } /* Check if length and buffer_pos match */ if(buffer_pos!=length) { v_print_log(VRS_PRINT_DEBUG_MSG, "%s: buffer_pos: %d != length: %d\n", __FUNCTION__, buffer_pos, length); return length; } return length; }