blob: e2a848c3c96908203f4bfed70f7e77497079774b (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
|
// Copyright 2015 Google Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "unix_kernel_rand_impl.h"
#include <stdint.h> // uint64_t
namespace rappor {
const int kMaxBitWidth = 32; // also in encoder.cc
bool UnixKernelRand::GetMask(float prob, int num_bits, Bits* mask_out) const {
uint8_t rand_buf[kMaxBitWidth];
size_t num_elems = fread(&rand_buf, sizeof(uint8_t), num_bits, fp_);
if (num_elems != static_cast<size_t>(num_bits)) { // fread error
return false;
}
uint8_t threshold_256 = static_cast<uint8_t>(prob * 256);
Bits mask = 0;
for (int i = 0; i < num_bits; ++i) {
uint8_t bit = (rand_buf[i] < threshold_256);
mask |= (bit << i);
}
*mask_out = mask;
return true;
}
} // namespace rappor
|