OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2004 The WebRTC Project Authors. All rights reserved. | 2 * Copyright 2004 The WebRTC Project Authors. All rights reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 4 * Use of this source code is governed by a BSD-style license |
5 * that can be found in the LICENSE file in the root of the source | 5 * that can be found in the LICENSE file in the root of the source |
6 * tree. An additional intellectual property rights grant can be found | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
(...skipping 146 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
157 return true; | 157 return true; |
158 } | 158 } |
159 | 159 |
160 private: | 160 private: |
161 int GetRandom() { | 161 int GetRandom() { |
162 return ((seed_ = seed_ * 214013L + 2531011L) >> 16) & 0x7fff; | 162 return ((seed_ = seed_ * 214013L + 2531011L) >> 16) & 0x7fff; |
163 } | 163 } |
164 int seed_; | 164 int seed_; |
165 }; | 165 }; |
166 | 166 |
| 167 // TODO: Use Base64::Base64Table instead. |
| 168 static const char BASE64[64] = { |
| 169 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', |
| 170 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', |
| 171 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', |
| 172 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', |
| 173 '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/' |
| 174 }; |
| 175 |
167 namespace { | 176 namespace { |
168 | 177 |
169 // TODO: Use Base64::Base64Table instead. | |
170 static const char kBase64[64] = { | |
171 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', | |
172 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', | |
173 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', | |
174 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', | |
175 '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/'}; | |
176 | |
177 static const char kHex[16] = {'0', '1', '2', '3', '4', '5', '6', '7', | |
178 '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'}; | |
179 | |
180 static const char kUuidDigit17[4] = {'8', '9', 'a', 'b'}; | |
181 | |
182 // This round about way of creating a global RNG is to safe-guard against | 178 // This round about way of creating a global RNG is to safe-guard against |
183 // indeterminant static initialization order. | 179 // indeterminant static initialization order. |
184 scoped_ptr<RandomGenerator>& GetGlobalRng() { | 180 scoped_ptr<RandomGenerator>& GetGlobalRng() { |
185 RTC_DEFINE_STATIC_LOCAL(scoped_ptr<RandomGenerator>, global_rng, | 181 RTC_DEFINE_STATIC_LOCAL(scoped_ptr<RandomGenerator>, global_rng, |
186 (new SecureRandomGenerator())); | 182 (new SecureRandomGenerator())); |
187 return global_rng; | 183 return global_rng; |
188 } | 184 } |
189 | 185 |
190 RandomGenerator& Rng() { | 186 RandomGenerator& Rng() { |
191 return *GetGlobalRng(); | 187 return *GetGlobalRng(); |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
229 return false; | 225 return false; |
230 } | 226 } |
231 str->reserve(len); | 227 str->reserve(len); |
232 for (size_t i = 0; i < len; ++i) { | 228 for (size_t i = 0; i < len; ++i) { |
233 str->push_back(table[bytes[i] % table_size]); | 229 str->push_back(table[bytes[i] % table_size]); |
234 } | 230 } |
235 return true; | 231 return true; |
236 } | 232 } |
237 | 233 |
238 bool CreateRandomString(size_t len, std::string* str) { | 234 bool CreateRandomString(size_t len, std::string* str) { |
239 return CreateRandomString(len, kBase64, 64, str); | 235 return CreateRandomString(len, BASE64, 64, str); |
240 } | 236 } |
241 | 237 |
242 bool CreateRandomString(size_t len, const std::string& table, | 238 bool CreateRandomString(size_t len, const std::string& table, |
243 std::string* str) { | 239 std::string* str) { |
244 return CreateRandomString(len, table.c_str(), | 240 return CreateRandomString(len, table.c_str(), |
245 static_cast<int>(table.size()), str); | 241 static_cast<int>(table.size()), str); |
246 } | 242 } |
247 | 243 |
248 // Version 4 UUID is of the form: | |
249 // xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx | |
250 // Where 'x' is a hex digit, and 'y' is 8, 9, a or b. | |
251 std::string CreateRandomUuid() { | |
252 std::string str; | |
253 scoped_ptr<uint8_t[]> bytes(new uint8_t[31]); | |
254 if (!Rng().Generate(bytes.get(), 31)) { | |
255 LOG(LS_ERROR) << "Failed to generate random string!"; | |
256 return str; | |
257 } | |
258 str.reserve(36); | |
259 for (size_t i = 0; i < 8; ++i) { | |
260 str.push_back(kHex[bytes[i] % 16]); | |
261 } | |
262 str.push_back('-'); | |
263 for (size_t i = 8; i < 12; ++i) { | |
264 str.push_back(kHex[bytes[i] % 16]); | |
265 } | |
266 str.push_back('-'); | |
267 str.push_back('4'); | |
268 for (size_t i = 12; i < 15; ++i) { | |
269 str.push_back(kHex[bytes[i] % 16]); | |
270 } | |
271 str.push_back('-'); | |
272 str.push_back(kUuidDigit17[bytes[15] % 4]); | |
273 for (size_t i = 16; i < 19; ++i) { | |
274 str.push_back(kHex[bytes[i] % 16]); | |
275 } | |
276 str.push_back('-'); | |
277 for (size_t i = 19; i < 31; ++i) { | |
278 str.push_back(kHex[bytes[i] % 16]); | |
279 } | |
280 return str; | |
281 } | |
282 | |
283 uint32_t CreateRandomId() { | 244 uint32_t CreateRandomId() { |
284 uint32_t id; | 245 uint32_t id; |
285 if (!Rng().Generate(&id, sizeof(id))) { | 246 if (!Rng().Generate(&id, sizeof(id))) { |
286 LOG(LS_ERROR) << "Failed to generate random id!"; | 247 LOG(LS_ERROR) << "Failed to generate random id!"; |
287 } | 248 } |
288 return id; | 249 return id; |
289 } | 250 } |
290 | 251 |
291 uint64_t CreateRandomId64() { | 252 uint64_t CreateRandomId64() { |
292 return static_cast<uint64_t>(CreateRandomId()) << 32 | CreateRandomId(); | 253 return static_cast<uint64_t>(CreateRandomId()) << 32 | CreateRandomId(); |
293 } | 254 } |
294 | 255 |
295 uint32_t CreateRandomNonZeroId() { | 256 uint32_t CreateRandomNonZeroId() { |
296 uint32_t id; | 257 uint32_t id; |
297 do { | 258 do { |
298 id = CreateRandomId(); | 259 id = CreateRandomId(); |
299 } while (id == 0); | 260 } while (id == 0); |
300 return id; | 261 return id; |
301 } | 262 } |
302 | 263 |
303 double CreateRandomDouble() { | 264 double CreateRandomDouble() { |
304 return CreateRandomId() / (std::numeric_limits<uint32_t>::max() + | 265 return CreateRandomId() / (std::numeric_limits<uint32_t>::max() + |
305 std::numeric_limits<double>::epsilon()); | 266 std::numeric_limits<double>::epsilon()); |
306 } | 267 } |
307 | 268 |
308 } // namespace rtc | 269 } // namespace rtc |
OLD | NEW |