SrtpSessionKeys* DtlsSocket::getSrtpSessionKeys() { // TODO(pedro): probably an exception candidate assert(mHandshakeCompleted); SrtpSessionKeys* keys = new SrtpSessionKeys(); unsigned char material[SRTP_MASTER_KEY_LEN << 1]; if (!SSL_export_keying_material(mSsl, material, sizeof(material), "EXTRACTOR-dtls_srtp", 19, NULL, 0, 0)) { return keys; } size_t offset = 0; memcpy(keys->clientMasterKey, &material[offset], SRTP_MASTER_KEY_KEY_LEN); offset += SRTP_MASTER_KEY_KEY_LEN; memcpy(keys->serverMasterKey, &material[offset], SRTP_MASTER_KEY_KEY_LEN); offset += SRTP_MASTER_KEY_KEY_LEN; memcpy(keys->clientMasterSalt, &material[offset], SRTP_MASTER_KEY_SALT_LEN); offset += SRTP_MASTER_KEY_SALT_LEN; memcpy(keys->serverMasterSalt, &material[offset], SRTP_MASTER_KEY_SALT_LEN); offset += SRTP_MASTER_KEY_SALT_LEN; keys->clientMasterKeyLen = SRTP_MASTER_KEY_KEY_LEN; keys->serverMasterKeyLen = SRTP_MASTER_KEY_KEY_LEN; keys->clientMasterSaltLen = SRTP_MASTER_KEY_SALT_LEN; keys->serverMasterSaltLen = SRTP_MASTER_KEY_SALT_LEN; return keys; }
void mbl_mw_dataprocessor_create_pulse_detector(MblMwDataSignal *source, MblMwPulseOutput output, float threshold, uint16_t width, MblMwFnDataProcessor processor_created) { MblMwDataProcessor *new_processor = new MblMwDataProcessor(*source); switch (output) { case MBL_MW_PULSE_OUTPUT_WIDTH: new_processor->set_channel_attr(1, 2); new_processor->convertor = ResponseConvertor::UINT32; new_processor->number_to_firmware = number_to_firmware_default; break; case MBL_MW_PULSE_OUTPUT_AREA: new_processor->set_channel_attr(1, 4); break; default: break; } int32_t scaled_threshold= (int32_t) source->number_to_firmware(source, threshold); PulseDetectorConfig *config = (PulseDetectorConfig*) malloc(sizeof(PulseDetectorConfig)); config->length= source->length() - 1; config->trigger_mode= 0; config->output_mode= output; memcpy(((uint8_t*)(config)) + 3, &scaled_threshold, sizeof(scaled_threshold)); memcpy(((uint8_t*)(config)) + 7, &width, sizeof(width)); create_processor(source, config, sizeof(PulseDetectorConfig), DataProcessorType::PULSE, new_processor, processor_created); }
/** @param buff address of data to be appended @param len length of data @throws std::out_of_range if there are no enough space Caller must ensure that there are enough free space in the buffer. Zero byte append is allowed. */ void CircularBuffer::add(const void * buff, size_t len){ if( len > this->availsize() ){ throw new std::out_of_range("buffer can't handle or more than availsize data"); } if ( 0 == len ){ return; } using std::memcpy; size_t end = this->lastidx(); //last used byte's index if( end < start_ ){ memcpy(buffer+end+1, buff, len); }else if(0 == currsize_){ start_=0; memcpy(buffer, buff, len); } else{ size_t free_at_end = this->capacity_ - end - 1; if( free_at_end >= len){ //will not roll over memcpy(buffer+end+1, buff, len); }else{ memcpy(buffer+end+1, buff, free_at_end); memcpy(buffer, ((char*) buff)+free_at_end, len-free_at_end); } } currsize_ += len; }
int32_t mbl_mw_dataprocessor_threshold_create(MblMwDataSignal *source, MblMwThresholdMode mode, float boundary, float hysteresis, MblMwFnDataProcessor processor_created) { if (source->length() > PROCESSOR_MAX_LENGTH) { return MBL_MW_STATUS_ERROR_UNSUPPORTED_PROCESSOR; } MblMwDataProcessor *new_processor = new MblMwDataProcessor(*source); if (mode == MBL_MW_THRESHOLD_MODE_BINARY) { new_processor->is_signed = 1; new_processor->interpreter = DataInterpreter::INT32; new_processor->set_channel_attr(1, 1); new_processor->converter = FirmwareConverter::DEFAULT; } int32_t scaled_boundary= (int32_t) number_to_firmware_converters.at(source->converter)(source, boundary); uint16_t scaled_hysteresis= (uint16_t) number_to_firmware_converters.at(source->converter)(source, hysteresis); ThresholdConfig *config = (ThresholdConfig*) malloc(sizeof(ThresholdConfig)); *((uint8_t*) config)= 0; config->length= source->length() - 1; config->is_signed= source->is_signed; config->mode= mode; memcpy(((uint8_t*)(config)) + 1, &scaled_boundary, sizeof(scaled_boundary)); memcpy(((uint8_t*)(config)) + 5, &scaled_hysteresis, sizeof(scaled_hysteresis)); create_processor(source, config, sizeof(ThresholdConfig), DataProcessorType::THRESHOLD, new_processor, processor_created); return MBL_MW_STATUS_OK; }
UString::UString(const UString &a, const UString &b) { int aSize = a.size(); int aOffset = a.m_rep->offset; int bSize = b.size(); int bOffset = b.m_rep->offset; int length = aSize + bSize; // possible cases: if (aSize == 0) { // a is empty m_rep = b.m_rep; } else if (bSize == 0) { // b is empty m_rep = a.m_rep; } else if (aOffset + aSize == a.usedCapacity() && aSize >= minShareSize && 4 * aSize >= bSize && (-bOffset != b.usedPreCapacity() || aSize >= bSize)) { // - a reaches the end of its buffer so it qualifies for shared append // - also, it's at least a quarter the length of b - appending to a much shorter // string does more harm than good // - however, if b qualifies for prepend and is longer than a, we'd rather prepend UString x(a); x.expandCapacity(aOffset + length); if (a.data() && x.data()) { memcpy(const_cast<UChar *>(a.data() + aSize), b.data(), bSize * sizeof(UChar)); m_rep = Rep::create(a.m_rep, 0, length); } else { m_rep = &Rep::null; } } else if (-bOffset == b.usedPreCapacity() && bSize >= minShareSize && 4 * bSize >= aSize) { // - b reaches the beginning of its buffer so it qualifies for shared prepend // - also, it's at least a quarter the length of a - prepending to a much shorter // string does more harm than good UString y(b); y.expandPreCapacity(-bOffset + aSize); if (b.data() && y.data()) { memcpy(const_cast<UChar *>(b.data() - aSize), a.data(), aSize * sizeof(UChar)); m_rep = Rep::create(b.m_rep, -aSize, length); } else { m_rep = &Rep::null; } } else { // a does not qualify for append, and b does not qualify for prepend, gotta make a whole new string size_t newCapacity = expandedSize(length, 0); UChar *d = allocChars(newCapacity); if (!d) { m_rep = &Rep::null; } else { memcpy(d, a.data(), aSize * sizeof(UChar)); memcpy(d + aSize, b.data(), bSize * sizeof(UChar)); m_rep = Rep::create(d, length); m_rep->capacity = newCapacity; } } }
int32_t mbl_mw_dataprocessor_pulse_detector_modify(MblMwDataProcessor *pulse, float threshold, uint16_t width) { if (pulse->type == DataProcessorType::PULSE) { int32_t scaled_threshold= (int32_t) pulse->number_to_firmware(pulse, threshold); memcpy(((uint8_t*)(pulse->config)) + 3, &scaled_threshold, sizeof(scaled_threshold)); memcpy(((uint8_t*)(pulse->config)) + 7, &width, sizeof(width)); modify_processor_configuration(pulse, sizeof(PulseDetectorConfig)); return MBL_MW_STATUS_OK; } return MBL_MW_STATUS_WARNING_INVALID_PROCESSOR_TYPE; }
void DtlsSocketContext::handshakeCompleted() { char fprint[100]; SRTP_PROTECTION_PROFILE *srtp_profile; if (mSocket->getRemoteFingerprint(fprint)) { ELOG_TRACE("Remote fingerprint == %s", fprint); bool check = mSocket->checkFingerprint(fprint, strlen(fprint)); ELOG_DEBUG("Fingerprint check == %d", check); SrtpSessionKeys* keys = mSocket->getSrtpSessionKeys(); unsigned char* cKey = (unsigned char*)malloc(keys->clientMasterKeyLen + keys->clientMasterSaltLen); unsigned char* sKey = (unsigned char*)malloc(keys->serverMasterKeyLen + keys->serverMasterSaltLen); memcpy(cKey, keys->clientMasterKey, keys->clientMasterKeyLen); memcpy(cKey + keys->clientMasterKeyLen, keys->clientMasterSalt, keys->clientMasterSaltLen); memcpy(sKey, keys->serverMasterKey, keys->serverMasterKeyLen); memcpy(sKey + keys->serverMasterKeyLen, keys->serverMasterSalt, keys->serverMasterSaltLen); // g_base64_encode must be free'd with g_free. Also, std::string's assignment operator does *not* take // ownership of the passed in ptr; under the hood it copies up to the first null character. gchar* temp = g_base64_encode((const guchar*)cKey, keys->clientMasterKeyLen + keys->clientMasterSaltLen); std::string clientKey = temp; g_free(temp); temp = NULL; temp = g_base64_encode((const guchar*)sKey, keys->serverMasterKeyLen + keys->serverMasterSaltLen); std::string serverKey = temp; g_free(temp); temp = NULL; ELOG_DEBUG("ClientKey: %s", clientKey.c_str()); ELOG_DEBUG("ServerKey: %s", serverKey.c_str()); free(cKey); free(sKey); delete keys; srtp_profile = mSocket->getSrtpProfile(); if (srtp_profile) { ELOG_DEBUG("SRTP Extension negotiated profile=%s", srtp_profile->name); } if (receiver != NULL) { receiver->onHandshakeCompleted(this, clientKey, serverKey, srtp_profile->name); } } else { ELOG_DEBUG("Peer did not authenticate"); } }
int32_t mbl_mw_dataprocessor_threshold_modify_boundary(MblMwDataProcessor *threshold, float boundary, float hysteresis) { if (threshold->type == DataProcessorType::THRESHOLD) { int32_t scaled_boundary= (int32_t) threshold->number_to_firmware(threshold, boundary); uint16_t scaled_hysteresis= (uint16_t) threshold->number_to_firmware(threshold, hysteresis); memcpy(((uint8_t*)(threshold->config)) + 1, &scaled_boundary, sizeof(scaled_boundary)); memcpy(((uint8_t*)(threshold->config)) + 5, &scaled_hysteresis, sizeof(scaled_hysteresis)); modify_processor_configuration(threshold, sizeof(ThresholdConfig)); return MBL_MW_STATUS_OK; } return MBL_MW_STATUS_WARNING_INVALID_PROCESSOR_TYPE; }
/*virtual*/ int HTTPText::read(char* buf, size_t len, size_t* pReadLen) { *pReadLen = MIN(len, m_size - 1 - m_pos); memcpy(buf, m_str + m_pos, *pReadLen); m_pos += *pReadLen; return OK; }
inline const PointGrid<R,d,q>& PointGrid<R,d,q>::operator= ( const PointGrid<R,d,q>& pointGrid ) { const size_t q_to_d = Pow<q,d>::val; memcpy( &points_[0][0], &pointGrid[0][0], q_to_d*sizeof(R) ); return *this; }
void Bytecode::Add(const UByte *data, int count) { int n = fData.size(); fData.resize(n + count); memcpy(&fData[n], data, (size_t) count); }
PassRefPtr<UString::Rep> UString::Rep::createCopying(const UChar *d, int length) { UChar *copyD = allocChars(length); memcpy(copyD, d, length * sizeof(UChar)); return create(copyD, length); }
CString::CString(const char *c, size_t len) { length = len; data = new char[len + 1]; memcpy(data, c, len); data[len] = 0; }
/*virtual*/ int HTTPText::write(const char* buf, size_t len) { size_t writeLen = MIN(len, m_size - 1 - m_pos); memcpy(m_str + m_pos, buf, writeLen); m_pos += writeLen; m_str[m_pos] = '\0'; return OK; }
float Float(char unsigned *const p) { float val; memcpy(&val,p,sizeof val); return val; }
int RtcpForwarder::addREMB(char* buf, int len, uint32_t bitrate) { buf+=len; RtcpHeader theREMB; theREMB.setPacketType(RTCP_PS_Feedback_PT); theREMB.setBlockCount(RTCP_AFB); memcpy(&theREMB.report.rembPacket.uniqueid, "REMB", 4); theREMB.setSSRC(rtcpSink_->getVideoSinkSSRC()); theREMB.setSourceSSRC(rtcpSource_->getVideoSourceSSRC()); theREMB.setLength(5); theREMB.setREMBBitRate(bitrate); theREMB.setREMBNumSSRC(1); theREMB.setREMBFeedSSRC(rtcpSource_->getVideoSourceSSRC()); int rembLength = (theREMB.getLength()+1)*4; memcpy(buf, reinterpret_cast<uint8_t*>(&theREMB), rembLength); return (len+rembLength); }
void mbl_mw_settings_set_connection_parameters(const MblMwMetaWearBoard *board, float min_conn_interval, float max_conn_interval, uint16_t latency, uint16_t timeout) { uint8_t command[10]= {MBL_MW_MODULE_SETTINGS, ORDINAL(SettingsRegister::CONNECTION_PARAMS)}; uint16_t parameters[4]= {static_cast<uint16_t>(min_conn_interval / CONN_INTERVAL_STEP), static_cast<uint16_t>(max_conn_interval / CONN_INTERVAL_STEP), latency, static_cast<uint16_t>(timeout / TIMEOUT_STEP)}; memcpy(command + 2, parameters, sizeof(parameters)); SEND_COMMAND; }
void mbl_mw_dataprocessor_create_passthrough(MblMwDataSignal *source, MblMwPassthroughMode mode, uint16_t count, MblMwFnDataProcessor processor_created) { MblMwDataProcessor *new_processor = new MblMwDataProcessor(*source); PassthroughConfig *config = (PassthroughConfig*) malloc(sizeof(PassthroughConfig)); config->mode= mode; memcpy(((uint8_t*)(config)) + 1, &count, sizeof(count)); create_processor(source, config, sizeof(PassthroughConfig), DataProcessorType::PASSTHROUGH, new_processor, processor_created); }
UString UString::spliceSubstringsWithSeparators(const Range *substringRanges, int rangeCount, const UString *separators, int separatorCount) const { if (rangeCount == 1 && separatorCount == 0) { int thisSize = size(); int position = substringRanges[0].position; int length = substringRanges[0].length; if (position <= 0 && length >= thisSize) { return *this; } return UString::Rep::create(m_rep, maxInt(0, position), minInt(thisSize, length)); } int totalLength = 0; for (int i = 0; i < rangeCount; i++) { totalLength += substringRanges[i].length; } for (int i = 0; i < separatorCount; i++) { totalLength += separators[i].size(); } if (totalLength == 0) { return ""; } UChar *buffer = allocChars(totalLength); if (!buffer) { return null(); } int maxCount = max(rangeCount, separatorCount); int bufferPos = 0; for (int i = 0; i < maxCount; i++) { if (i < rangeCount) { memcpy(buffer + bufferPos, data() + substringRanges[i].position, substringRanges[i].length * sizeof(UChar)); bufferPos += substringRanges[i].length; } if (i < separatorCount) { memcpy(buffer + bufferPos, separators[i].data(), separators[i].size() * sizeof(UChar)); bufferPos += separators[i].size(); } } return UString::Rep::create(buffer, totalLength); }
UString &UString::append(const UString &t) { int thisSize = size(); int thisOffset = m_rep->offset; int tSize = t.size(); int length = thisSize + tSize; // possible cases: if (thisSize == 0) { // this is empty *this = t; } else if (tSize == 0) { // t is empty } else if (m_rep->baseIsSelf() && m_rep->rc == 1) { // this is direct and has refcount of 1 (so we can just alter it directly) expandCapacity(thisOffset + length); if (data()) { memcpy(const_cast<UChar *>(data() + thisSize), t.data(), tSize * sizeof(UChar)); m_rep->len = length; m_rep->_hash = 0; } } else if (thisOffset + thisSize == usedCapacity() && thisSize >= minShareSize) { // this reaches the end of the buffer - extend it if it's long enough to append to expandCapacity(thisOffset + length); if (data()) { memcpy(const_cast<UChar *>(data() + thisSize), t.data(), tSize * sizeof(UChar)); m_rep = Rep::create(m_rep, 0, length); } } else { // this is shared with someone using more capacity, gotta make a whole new string size_t newCapacity = expandedSize(length, 0); UChar *d = allocChars(newCapacity); if (!d) { m_rep = &Rep::null; } else { memcpy(d, data(), thisSize * sizeof(UChar)); memcpy(const_cast<UChar *>(d + thisSize), t.data(), tSize * sizeof(UChar)); m_rep = Rep::create(d, length); m_rep->capacity = newCapacity; } } return *this; }
CString::CString(const CString &b) { length = b.length; if (length > 0 && b.data) { data = new char[length + 1]; memcpy(data, b.data, length + 1); } else { data = 0; } }
void mbl_mw_timer_create(MblMwMetaWearBoard *board, uint32_t period, uint16_t repetitions, uint8_t delay, MblMwFnTimerPtr received_timer) { auto state = GET_TIMER_STATE(board); state->pending_fns.push([=](void) -> void { uint8_t command[9]= {MBL_MW_MODULE_TIMER, ORDINAL(TimerRegister::TIMER_ENTRY)}; memcpy(command + 2, &period, sizeof(period)); memcpy(command + 6, &repetitions, sizeof(repetitions)); command[8]= (delay != 0) ? 0 : 1; state->timer_callback= received_timer; state->timeout= ThreadPool::schedule([state, received_timer](void) -> void { received_timer(nullptr); state->create_next(true); }, board->time_per_response); SEND_COMMAND; }); state->create_next(false); }
void mbl_mw_settings_set_ad_interval(const MblMwMetaWearBoard *board, uint16_t interval, uint8_t timeout) { uint8_t command[5]= {MBL_MW_MODULE_SETTINGS, ORDINAL(SettingsRegister::AD_INTERVAL), 0, 0, timeout}; auto info= board->module_info.find(MBL_MW_MODULE_SETTINGS); if (info->second.revision == 1) { interval/= AD_INTERVAL_STEP; } memcpy(command + 2, &interval, sizeof(interval)); SEND_COMMAND; }
CString &CString::operator=(const char *c) { if (data) { delete [] data; } length = strlen(c); data = new char[length + 1]; memcpy(data, c, length + 1); return *this; }
int merge(int *a, int *b, int la, int length){ int temp[length]; int i=0, j=0, k=0; int count = 0; while(i<la&&j<length-la){ if(a[i]<=b[j]) temp[k++] = a[i++]; else{ temp[k++] = b[j++]; if(la-i>m_max) m_max = la-i; count += la-i; } } if(i<la) memcpy(temp+k, a+i, (la-i)*sizeof(int)); else if(j<length-la) memcpy(temp+k, b+j, (length-la-j)*sizeof(int)); memcpy(a, temp, length*sizeof(int)); return count; }
void UString::copyForWriting() { int l = size(); if (!l) { return; // Not going to touch anything anyway. } if (m_rep->rc > 1 || !m_rep->baseIsSelf()) { UChar *n = allocChars(l); memcpy(n, data(), l * sizeof(UChar)); m_rep = Rep::create(n, l); } }
UString &UString::append(const char *t) { int thisSize = size(); int thisOffset = m_rep->offset; int tSize = static_cast<int>(strlen(t)); int length = thisSize + tSize; // possible cases: if (thisSize == 0) { // this is empty *this = t; } else if (tSize == 0) { // t is empty, we'll just return *this below. } else if (m_rep->baseIsSelf() && m_rep->rc == 1) { // this is direct and has refcount of 1 (so we can just alter it directly) expandCapacity(thisOffset + length); UChar *d = const_cast<UChar *>(data()); if (d) { for (int i = 0; i < tSize; ++i) { d[thisSize + i] = t[i]; } m_rep->len = length; m_rep->_hash = 0; } } else if (thisOffset + thisSize == usedCapacity() && thisSize >= minShareSize) { // this string reaches the end of the buffer - extend it expandCapacity(thisOffset + length); UChar *d = const_cast<UChar *>(data()); if (d) { for (int i = 0; i < tSize; ++i) { d[thisSize + i] = t[i]; } m_rep = Rep::create(m_rep, 0, length); } } else { // this is shared with someone using more capacity, gotta make a whole new string size_t newCapacity = expandedSize(length, 0); UChar *d = allocChars(newCapacity); if (!d) { m_rep = &Rep::null; } else { memcpy(d, data(), thisSize * sizeof(UChar)); for (int i = 0; i < tSize; ++i) { d[thisSize + i] = t[i]; } m_rep = Rep::create(d, length); m_rep->capacity = newCapacity; } } return *this; }
void mbl_mw_acc_mma8452q_write_acceleration_config(const MblMwMetaWearBoard *board) { uint8_t command[7]= {MBL_MW_MODULE_ACCELEROMETER, ORDINAL(AccelerometerMma8452qRegister::DATA_CONFIG)}; auto config = (Mma8452qConfig*) board->module_config.at(MBL_MW_MODULE_ACCELEROMETER); Mma8452qAccBitField bit_field; memset(&bit_field, 0, sizeof(Mma8452qAccBitField)); bit_field.dr= config->odr; bit_field.fs= config->accel_fsr; memcpy(command + 2, &bit_field, sizeof(bit_field)); SEND_COMMAND; }
void DirectSoundStreamer::Stream( void const *pSamples ) { // Verify buffer availability DWORD play, write; if( FAILED( m_pDirectSoundBuffer->GetCurrentPosition( &play, &write ) ) ) return; if( write < play ) write += DWORD( m_physical ); std::size_t begin( m_begin ); if( begin < play ) begin += m_physical; if( begin < write ) begin = std::size_t( write ); std::size_t end( begin + m_stream ); if( play + m_virtual < end ) return; begin %= m_physical; // Copy samples to buffer void *ps[ 2 ]; DWORD sizes[ 2 ]; HRESULT hResult( m_pDirectSoundBuffer->Lock( DWORD( begin ), DWORD( m_stream ), &ps[ 0 ], &sizes[ 0 ], &ps[ 1 ], &sizes[ 1 ], 0 ) ); if( FAILED( hResult ) ) { if( hResult != DSERR_BUFFERLOST ) return; m_pDirectSoundBuffer->Restore(); if( FAILED( m_pDirectSoundBuffer->Lock( DWORD( begin ), DWORD( m_stream ), &ps[ 0 ], &sizes[ 0 ], &ps[ 1 ], &sizes[ 1 ], 0 ) ) ) return; } using std::memcpy; memcpy( ps[ 0 ], pSamples, std::size_t( sizes[ 0 ] ) ); if( ps[ 1 ] ) memcpy( ps[ 1 ], static_cast< char const * >( pSamples ) + sizes[ 0 ], std::size_t( sizes[ 1 ] ) ); m_pDirectSoundBuffer->Unlock( ps[ 0 ], sizes[ 0 ], ps[ 1 ], sizes[ 1 ] ); // Select next buffer m_begin = end % m_physical; }
int32_t mbl_mw_dataprocessor_passthrough_modify(MblMwDataProcessor *passthrough, MblMwPassthroughMode mode, uint16_t count) { if (passthrough->type == DataProcessorType::PASSTHROUGH) { PassthroughConfig* current_config= (PassthroughConfig*) passthrough->config; current_config->mode= mode; memcpy(((uint8_t*)(current_config)) + 1, &count, sizeof(count)); modify_processor_configuration(passthrough, sizeof(PassthroughConfig)); return MBL_MW_STATUS_OK; } return MBL_MW_STATUS_WARNING_INVALID_PROCESSOR_TYPE; }