Path: blob/main/system/lib/wasmfs/fuzzer/random.cpp
6175 views
// Copyright 2021 The Emscripten Authors. All rights reserved.1// Emscripten is available under two separate licenses, the MIT license and the2// University of Illinois/NCSA Open Source License. Both these licenses can be3// found in the LICENSE file.45// Original Source that was modified:6// https://github.com/WebAssembly/binaryen/blob/main/src/tools/fuzzing/random.cpp78#include "random.h"910namespace wasmfs {1112int8_t Random::get() {13if (pos == bytes.size()) {14// We ran out of input; go back to the start for more.15finishedInput = true;16pos = 0;17xorFactor++;18}19return bytes[pos++] ^ xorFactor;20}2122int16_t Random::get16() {23auto temp = uint16_t(get()) << 8;24return temp | uint16_t(get());25}2627int32_t Random::get32() {28auto temp = uint32_t(get16()) << 16;29return temp | uint32_t(get16());30}3132int64_t Random::get64() {33auto temp = uint64_t(get32()) << 32;34return temp | uint64_t(get32());35}3637uint32_t Random::upTo(uint32_t x) {38if (x == 0) {39return 0;40}41uint32_t raw;42if (x <= 255) {43raw = get();44} else if (x <= 65535) {45raw = get16();46} else {47raw = get32();48}49auto ret = raw % x;50// use extra bits as "noise" for later51xorFactor += raw / x;52return ret;53}5455// Returns a random length string of random characters.56std::string Random::getString(int8_t size) {57static const char alphanum[] = "0123456789"58"ABCDEFGHIJKLMNOPQRSTUVWXYZ"59"abcdefghijklmnopqrstuvwxyz";60std::string ret;61ret.reserve(size);62for (int i = 0; i < size; ++i) {63ret += alphanum[upTo(sizeof(alphanum) - 1)];64}65return ret;66}6768// Returns a random length string of the same character.69std::string Random::getSingleSymbolString(uint32_t length) {70return std::string(length, getString(1)[0]);71}7273} // namespace wasmfs747576