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 | |
176 namespace { | 167 namespace { |
177 | 168 |
| 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 |
178 // This round about way of creating a global RNG is to safe-guard against | 182 // This round about way of creating a global RNG is to safe-guard against |
179 // indeterminant static initialization order. | 183 // indeterminant static initialization order. |
180 scoped_ptr<RandomGenerator>& GetGlobalRng() { | 184 scoped_ptr<RandomGenerator>& GetGlobalRng() { |
181 RTC_DEFINE_STATIC_LOCAL(scoped_ptr<RandomGenerator>, global_rng, | 185 RTC_DEFINE_STATIC_LOCAL(scoped_ptr<RandomGenerator>, global_rng, |
182 (new SecureRandomGenerator())); | 186 (new SecureRandomGenerator())); |
183 return global_rng; | 187 return global_rng; |
184 } | 188 } |
185 | 189 |
186 RandomGenerator& Rng() { | 190 RandomGenerator& Rng() { |
187 return *GetGlobalRng(); | 191 return *GetGlobalRng(); |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
225 return false; | 229 return false; |
226 } | 230 } |
227 str->reserve(len); | 231 str->reserve(len); |
228 for (size_t i = 0; i < len; ++i) { | 232 for (size_t i = 0; i < len; ++i) { |
229 str->push_back(table[bytes[i] % table_size]); | 233 str->push_back(table[bytes[i] % table_size]); |
230 } | 234 } |
231 return true; | 235 return true; |
232 } | 236 } |
233 | 237 |
234 bool CreateRandomString(size_t len, std::string* str) { | 238 bool CreateRandomString(size_t len, std::string* str) { |
235 return CreateRandomString(len, BASE64, 64, str); | 239 return CreateRandomString(len, kBase64, 64, str); |
236 } | 240 } |
237 | 241 |
238 bool CreateRandomString(size_t len, const std::string& table, | 242 bool CreateRandomString(size_t len, const std::string& table, |
239 std::string* str) { | 243 std::string* str) { |
240 return CreateRandomString(len, table.c_str(), | 244 return CreateRandomString(len, table.c_str(), |
241 static_cast<int>(table.size()), str); | 245 static_cast<int>(table.size()), str); |
242 } | 246 } |
243 | 247 |
| 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 |
244 uint32_t CreateRandomId() { | 283 uint32_t CreateRandomId() { |
245 uint32_t id; | 284 uint32_t id; |
246 if (!Rng().Generate(&id, sizeof(id))) { | 285 if (!Rng().Generate(&id, sizeof(id))) { |
247 LOG(LS_ERROR) << "Failed to generate random id!"; | 286 LOG(LS_ERROR) << "Failed to generate random id!"; |
248 } | 287 } |
249 return id; | 288 return id; |
250 } | 289 } |
251 | 290 |
252 uint64_t CreateRandomId64() { | 291 uint64_t CreateRandomId64() { |
253 return static_cast<uint64_t>(CreateRandomId()) << 32 | CreateRandomId(); | 292 return static_cast<uint64_t>(CreateRandomId()) << 32 | CreateRandomId(); |
254 } | 293 } |
255 | 294 |
256 uint32_t CreateRandomNonZeroId() { | 295 uint32_t CreateRandomNonZeroId() { |
257 uint32_t id; | 296 uint32_t id; |
258 do { | 297 do { |
259 id = CreateRandomId(); | 298 id = CreateRandomId(); |
260 } while (id == 0); | 299 } while (id == 0); |
261 return id; | 300 return id; |
262 } | 301 } |
263 | 302 |
264 double CreateRandomDouble() { | 303 double CreateRandomDouble() { |
265 return CreateRandomId() / (std::numeric_limits<uint32_t>::max() + | 304 return CreateRandomId() / (std::numeric_limits<uint32_t>::max() + |
266 std::numeric_limits<double>::epsilon()); | 305 std::numeric_limits<double>::epsilon()); |
267 } | 306 } |
268 | 307 |
269 } // namespace rtc | 308 } // namespace rtc |
OLD | NEW |