0.9.8.10
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
FixedRandomStringGenerator.cc
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2007-2015 Hypertable, Inc.
3  *
4  * This file is part of Hypertable.
5  *
6  * Hypertable is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; version 3 of the
9  * License, or any later version.
10  *
11  * Hypertable is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19  * 02110-1301, USA.
20  */
21 
22 #include <Common/Compat.h>
23 
25 
26 #include <Common/Logger.h>
27 #include <Common/Random.h>
28 
29 #include <cstdlib>
30 
31 using namespace Hypertable;
32 
33 namespace {
34  const char cb64[] =
35  "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
36 }
37 
38 
40  HT_ASSERT(n>0);
41  m_nints = ((m_nchars * 6) + 7) / 8;
42  m_ivec.resize(m_nints);
43 }
44 
45 
46 
48  size_t indexi=0, indexo=0;
49  uint8_t *in = (uint8_t *)&m_ivec[0];
50 
51  for (size_t i=0; i<m_nints; i++)
52  m_ivec[i] = Random::number32();
53 
54  indexi = 0;
55  indexo = 0;
56  while (indexo < m_nchars) {
57 
58  buf[indexo++] = cb64[ in[indexi] >> 2 ];
59  if (indexo == m_nchars)
60  break;
61 
62  buf[indexo++] = cb64[((in[indexi] & 0x03) << 4)
63  | ((in[indexi+1] & 0xf0) >> 4)];
64  if (indexo == m_nchars)
65  break;
66 
67  buf[indexo++] = cb64[((in[indexi+1] & 0x0f) << 2)
68  | ((in[indexi+2] & 0xc0) >> 6)];
69  if (indexo == m_nchars)
70  break;
71 
72  buf[indexo++] = cb64[ in[indexi+2] & 0x3f ];
73 
74  indexi += 3;
75  }
76 
77  buf[indexo] = 0;
78 }
static uint32_t number32(uint32_t maximum=0)
Returns a random 32-bit unsigned integer.
Definition: Random.cc:55
#define HT_ASSERT(_e_)
Definition: Logger.h:396
Logging routines and macros.
Compatibility Macros for C/C++.
Hypertable definitions
Random number generator for int32, int64, double and ascii arrays.