~evarlast/ubuntu/utopic/mongodb/upstart-workaround-debian-bug-718702

« back to all changes in this revision

Viewing changes to .pc/0003-kfreebsd-has-dev-urandom.patch/src/mongo/platform/random.cpp

  • Committer: Package Import Robot
  • Author(s): James Page, James Page, Robie Basak
  • Date: 2013-05-29 17:44:42 UTC
  • mfrom: (44.1.7 sid)
  • Revision ID: package-import@ubuntu.com-20130529174442-z0a4qmoww4y0t458
Tags: 1:2.4.3-1ubuntu1
[ James Page ]
* Merge from Debian unstable, remaining changes:
  - Enable SSL support:
    + d/control: Add libssl-dev to BD's.
    + d/rules: Enabled --ssl option.
    + d/mongodb.conf: Add example SSL configuration options.
  - d/mongodb-server.mongodb.upstart: Add upstart configuration.
  - d/rules: Don't strip binaries during scons build for Ubuntu.
  - d/control: Add armhf to target archs.
  - d/p/SConscript.client.patch: fixup install of client libraries.
  - d/p/0010-install-libs-to-usr-lib-not-usr-lib64-Closes-588557.patch:
    Install libraries to lib not lib64.
* Dropped changes:
  - d/p/arm-support.patch: Included in Debian.
  - d/p/double-alignment.patch: Included in Debian.
  - d/rules,control: Debian also builds with avaliable system libraries
    now.
* Fix FTBFS due to gcc and boost upgrades in saucy:
  - d/p/0008-ignore-unused-local-typedefs.patch: Add -Wno-unused-typedefs
    to unbreak building with g++-4.8.
  - d/p/0009-boost-1.53.patch: Fixup signed/unsigned casting issue.

[ Robie Basak ]
* d/p/0011-Use-a-signed-char-to-store-BSONType-enumerations.patch: Fixup
  build failure on ARM due to missing signed'ness of char cast.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// random.cpp
 
2
 
 
3
/*    Copyright 2012 10gen Inc.
 
4
 *
 
5
 *    Licensed under the Apache License, Version 2.0 (the "License");
 
6
 *    you may not use this file except in compliance with the License.
 
7
 *    You may obtain a copy of the License at
 
8
 *
 
9
 *    http://www.apache.org/licenses/LICENSE-2.0
 
10
 *
 
11
 *    Unless required by applicable law or agreed to in writing, software
 
12
 *    distributed under the License is distributed on an "AS IS" BASIS,
 
13
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
14
 *    See the License for the specific language governing permissions and
 
15
 *    limitations under the License.
 
16
 */
 
17
 
 
18
#include "mongo/platform/random.h"
 
19
 
 
20
#include <stdio.h>
 
21
#include <string.h>
 
22
 
 
23
#ifndef _WIN32
 
24
#include <errno.h>
 
25
#endif
 
26
 
 
27
#define _CRT_RAND_S
 
28
#include <cstdlib>
 
29
#include <iostream>
 
30
#include <fstream>
 
31
 
 
32
#include "mongo/platform/basic.h"
 
33
 
 
34
namespace mongo {
 
35
 
 
36
    // ---- PseudoRandom  -----
 
37
 
 
38
    int32_t PseudoRandom::nextInt32() {
 
39
        int32_t t = _x ^ (_x << 11);
 
40
        _x = _y;
 
41
        _y = _z;
 
42
        _z = _w;
 
43
        return _w = _w ^ (_w >> 19) ^ (t ^ (t >> 8));
 
44
    }
 
45
 
 
46
    namespace {
 
47
        const int32_t default_y = 362436069;
 
48
        const int32_t default_z = 521288629;
 
49
        const int32_t default_w = 88675123;
 
50
    }
 
51
 
 
52
    PseudoRandom::PseudoRandom( int32_t seed ) {
 
53
        _x = seed;
 
54
        _y = default_y;
 
55
        _z = default_z;
 
56
        _w = default_w;
 
57
    }
 
58
 
 
59
 
 
60
    PseudoRandom::PseudoRandom( uint32_t seed ) {
 
61
        _x = static_cast<int32_t>(seed);
 
62
        _y = default_y;
 
63
        _z = default_z;
 
64
        _w = default_w;
 
65
    }
 
66
 
 
67
 
 
68
    PseudoRandom::PseudoRandom( int64_t seed ) {
 
69
        int32_t high = seed >> 32;
 
70
        int32_t low = seed & 0xFFFFFFFF;
 
71
 
 
72
        _x = high ^ low;
 
73
        _y = default_y;
 
74
        _z = default_z;
 
75
        _w = default_w;
 
76
    }
 
77
 
 
78
    int64_t PseudoRandom::nextInt64() {
 
79
        int64_t a = nextInt32();
 
80
        int64_t b = nextInt32();
 
81
        return ( a << 32 ) | b;
 
82
    }
 
83
 
 
84
    // --- SecureRandom ----
 
85
 
 
86
    SecureRandom::~SecureRandom() {
 
87
    }
 
88
 
 
89
#ifdef _WIN32
 
90
    class WinSecureRandom : public SecureRandom {
 
91
        virtual ~WinSecureRandom(){}
 
92
        int64_t nextInt64() {
 
93
            uint32_t a, b;
 
94
            if ( rand_s(&a) ) {
 
95
                abort();
 
96
            }
 
97
            if ( rand_s(&b) ) {
 
98
                abort();
 
99
            }
 
100
            return ( static_cast<int64_t>(a) << 32 ) | b;
 
101
        }
 
102
    };
 
103
 
 
104
    SecureRandom* SecureRandom::create() {
 
105
        return new WinSecureRandom();
 
106
    }
 
107
 
 
108
#elif defined(__linux__) || defined(__sunos__) || defined(__APPLE__)
 
109
 
 
110
    class InputStreamSecureRandom : public SecureRandom {
 
111
    public:
 
112
        InputStreamSecureRandom( const char* fn ) {
 
113
            _in = new std::ifstream( fn, std::ios::binary | std::ios::in );
 
114
            if ( !_in->is_open() ) {
 
115
                std::cerr << "can't open " << fn << " " << strerror(errno) << std::endl;
 
116
                abort();
 
117
            }
 
118
        }
 
119
 
 
120
        ~InputStreamSecureRandom() {
 
121
            delete _in;
 
122
        }
 
123
 
 
124
        int64_t nextInt64() {
 
125
            int64_t r;
 
126
            _in->read( reinterpret_cast<char*>( &r ), sizeof(r) );
 
127
            if ( _in->fail() ) {
 
128
                abort();
 
129
            }
 
130
            return r;
 
131
        }
 
132
 
 
133
    private:
 
134
        std::ifstream* _in;
 
135
    };
 
136
 
 
137
    SecureRandom* SecureRandom::create() {
 
138
        return new InputStreamSecureRandom( "/dev/urandom" );
 
139
    }
 
140
 
 
141
#else
 
142
    class SRandSecureRandom : public SecureRandom {
 
143
    public:
 
144
        SRandSecureRandom() {
 
145
            srandomdev();
 
146
        }
 
147
 
 
148
        int64_t nextInt64() {
 
149
            long a, b;
 
150
            a = random();
 
151
            b = random();
 
152
            return ( static_cast<int64_t>(a) << 32 ) | b;
 
153
        }
 
154
    };
 
155
 
 
156
    SecureRandom* SecureRandom::create() {
 
157
        return new SRandSecureRandom();
 
158
    }
 
159
 
 
160
 
 
161
#endif
 
162
 
 
163
}