webrtc_m130/webrtc/test/libtest/helpers/bit_flip_encryption.cc
pbos@webrtc.org 12dc1a38ca Switch C++-style C headers with their C equivalents.
The C++ headers define the C functions within the std:: namespace, but
we mainly don't use the std:: namespace for C functions. Therefore we
should include the C headers.

BUG=1833
R=tommi@webrtc.org

Review URL: https://webrtc-codereview.appspot.com/1917004

git-svn-id: http://webrtc.googlecode.com/svn/trunk@4486 4adac7df-926f-26a2-2b94-8c16560cd09d
2013-08-05 16:22:53 +00:00

74 lines
2.6 KiB
C++

/*
* Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
*
* Use of this source code is governed by a BSD-style license
* that can be found in the LICENSE file in the root of the source
* tree. An additional intellectual property rights grant can be found
* in the file PATENTS. All contributing project authors may
* be found in the AUTHORS file in the root of the source tree.
*/
#include "webrtc/test/libtest/include/bit_flip_encryption.h"
#include <stdlib.h>
float NormalizedRand() {
return static_cast<float>(rand()) /
static_cast<float>(RAND_MAX);
}
BitFlipEncryption::BitFlipEncryption(unsigned int rand_seed,
float flip_probability)
: flip_probability_(flip_probability),
flip_count_(0) {
srand(rand_seed);
}
void BitFlipEncryption::FlipSomeBitsInData(const unsigned char* in_data,
unsigned char* out_data,
int bytes_in, int* bytes_out) {
for (int i = 0; i < bytes_in; i++) {
out_data[i] = in_data[i];
if (NormalizedRand() < flip_probability_) {
int bit_to_flip = rand() % 8;
out_data[i] ^= 1 << bit_to_flip;
flip_count_++;
}
}
*bytes_out = bytes_in;
}
void BitFlipEncryption::encrypt(int channel_no,
unsigned char* in_data,
unsigned char* out_data,
int bytes_in,
int* bytes_out) {
FlipSomeBitsInData(in_data, out_data, bytes_in, bytes_out);
}
void BitFlipEncryption::decrypt(int channel_no,
unsigned char* in_data,
unsigned char* out_data,
int bytes_in,
int* bytes_out) {
FlipSomeBitsInData(in_data, out_data, bytes_in, bytes_out);
}
void BitFlipEncryption::encrypt_rtcp(int channel_no,
unsigned char* in_data,
unsigned char* out_data,
int bytes_in,
int* bytes_out) {
FlipSomeBitsInData(in_data, out_data, bytes_in, bytes_out);
}
void BitFlipEncryption::decrypt_rtcp(int channel_no,
unsigned char* in_data,
unsigned char* out_data,
int bytes_in,
int* bytes_out) {
FlipSomeBitsInData(in_data, out_data, bytes_in, bytes_out);
}