rand.cc
Go to the documentation of this file.
1 /* Copyright (c) 2015, Google Inc.
2  *
3  * Permission to use, copy, modify, and/or distribute this software for any
4  * purpose with or without fee is hereby granted, provided that the above
5  * copyright notice and this permission notice appear in all copies.
6  *
7  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
8  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
10  * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
11  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
12  * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
13  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
14 
15 #include <string>
16 #include <vector>
17 
18 #include <stdint.h>
19 #include <stdio.h>
20 #include <stdlib.h>
21 
22 #include <openssl/rand.h>
23 
24 #include "internal.h"
25 
26 
27 static const struct argument kArguments[] = {
28  {
29  "-hex", kBooleanArgument,
30  "Hex encoded output."
31  },
32  {
33  "", kOptionalArgument, "",
34  },
35 };
36 
37 bool Rand(const std::vector<std::string> &args) {
38  bool forever = true, hex = false;
39  size_t len = 0;
40 
41  if (!args.empty()) {
42  std::vector<std::string> args_copy(args);
43  const std::string &last_arg = args.back();
44 
45  if (!last_arg.empty() && last_arg[0] != '-') {
46  char *endptr;
47  unsigned long long num = strtoull(last_arg.c_str(), &endptr, 10);
48  if (*endptr == 0) {
49  len = num;
50  forever = false;
51  args_copy.pop_back();
52  }
53  }
54 
55  std::map<std::string, std::string> args_map;
56  if (!ParseKeyValueArguments(&args_map, args_copy, kArguments)) {
58  return false;
59  }
60 
61  hex = args_map.count("-hex") > 0;
62  }
63 
64  uint8_t buf[4096];
65  uint8_t hex_buf[8192];
66 
67  size_t done = 0;
68  while (forever || done < len) {
69  size_t todo = sizeof(buf);
70  if (!forever && todo > len - done) {
71  todo = len - done;
72  }
74  if (hex) {
75  static const char hextable[16 + 1] = "0123456789abcdef";
76  for (unsigned i = 0; i < todo; i++) {
77  hex_buf[i*2] = hextable[buf[i] >> 4];
78  hex_buf[i*2 + 1] = hextable[buf[i] & 0xf];
79  }
80  if (fwrite(hex_buf, todo*2, 1, stdout) != 1) {
81  return false;
82  }
83  } else {
84  if (fwrite(buf, todo, 1, stdout) != 1) {
85  return false;
86  }
87  }
88  done += todo;
89  }
90 
91  if (hex && fwrite("\n", 1, 1, stdout) != 1) {
92  return false;
93  }
94 
95  return true;
96 }
ParseKeyValueArguments
bool ParseKeyValueArguments(std::map< std::string, std::string > *out_args, const std::vector< std::string > &args, const struct argument *templates)
Definition: args.cc:27
hextable
static const char hextable[]
Definition: boringssl-with-bazel/src/crypto/bn_extra/convert.c:77
RAND_bytes
#define RAND_bytes
Definition: boringssl_prefix_symbols.h:2060
internal.h
demumble_test.stdout
stdout
Definition: demumble_test.py:38
buf
voidpf void * buf
Definition: bloaty/third_party/zlib/contrib/minizip/ioapi.h:136
testing::internal::string
::std::string string
Definition: bloaty/third_party/protobuf/third_party/googletest/googletest/include/gtest/internal/gtest-port.h:881
uint8_t
unsigned char uint8_t
Definition: stdint-msvc2008.h:78
asyncio_get_stats.args
args
Definition: asyncio_get_stats.py:40
argument
Definition: third_party/boringssl-with-bazel/src/tool/internal.h:108
done
struct tab * done
Definition: bloaty/third_party/zlib/examples/enough.c:176
Rand
bool Rand(const std::vector< std::string > &args)
Definition: rand.cc:37
kBooleanArgument
@ kBooleanArgument
Definition: third_party/boringssl-with-bazel/src/tool/internal.h:105
stdint.h
PrintUsage
void PrintUsage(const struct argument *templates)
Definition: args.cc:75
rand.h
kArguments
static const struct argument kArguments[]
Definition: rand.cc:27
xds_manager.num
num
Definition: xds_manager.py:56
kOptionalArgument
@ kOptionalArgument
Definition: third_party/boringssl-with-bazel/src/tool/internal.h:104
len
int len
Definition: abseil-cpp/absl/base/internal/low_level_alloc_test.cc:46
mkowners.todo
todo
Definition: mkowners.py:209
i
uint64_t i
Definition: abseil-cpp/absl/container/btree_benchmark.cc:230


grpc
Author(s):
autogenerated on Thu Mar 13 2025 03:00:59