OSSP CVS Repository

ossp - Check-in [3928]
Not logged in
[Honeypot]  [Browse]  [Home]  [Login]  [Reports
[Search]  [Ticket]  [Timeline
  [Patchset]  [Tagging/Branching

Check-in Number: 3928
Date: 2003-Jan-22 19:57:57 (local)
2003-Jan-22 18:57:57 (UTC)
User:ms
Branch:
Comment: Add our minimal random number generator for use with UUIDs and more.
Tickets:
Inspections:
Files:
ossp-pkg/as/as-gui/Makefile.in      1.31 -> 1.32     2 inserted, 2 deleted
ossp-pkg/as/as-gui/as_rand.cpp      added-> 1.1
ossp-pkg/as/as-gui/as_rand.h      added-> 1.1

ossp-pkg/as/as-gui/Makefile.in 1.31 -> 1.32

--- Makefile.in  2003/01/16 11:53:39     1.31
+++ Makefile.in  2003/01/22 18:57:57     1.32
@@ -70,9 +70,9 @@
 TARGET_PROGS    = as-gui
 TARGET_MANS     = as-gui.1 as-gui.conf.5
 
-SRCS            = as_main.cpp as_gui.cpp as_assist.cpp as_slot.cpp as_dataop.cpp as_except.cpp as_generic.cpp as_amount.cpp as_table.cpp as_pref.cpp as_uuidgen.cpp as_version.cpp
+SRCS            = as_main.cpp as_gui.cpp as_assist.cpp as_slot.cpp as_dataop.cpp as_except.cpp as_generic.cpp as_amount.cpp as_table.cpp as_pref.cpp as_rand.cpp as_uuidgen.cpp as_version.cpp
 
-OBJS            = as_main.o as_gui.o as_assist.o as_slot.o as_dataop.o as_except.o as_generic.o as_amount.o as_table.o as_pref.o as_uuidgen.o as_version.o
+OBJS            = as_main.o as_gui.o as_assist.o as_slot.o as_dataop.o as_except.o as_generic.o as_amount.o as_table.o as_pref.o as_rand.o as_uuidgen.o as_version.o
 
 GRAFX           = gfx/ossplogo.xpm
 


ossp-pkg/as/as-gui/as_rand.cpp -> 1.1

*** /dev/null    Sat Nov 23 05:46:19 2024
--- -    Sat Nov 23 05:46:21 2024
***************
*** 0 ****
--- 1,130 ----
+ //
+ //  OSSP asgui - Accounting system graphical user interface
+ //  Copyright (c) 2002-2003 The OSSP Project (http://www.ossp.org/)
+ //  Copyright (c) 2002-2003 Cable & Wireless Deutschland (http://www.cw.com/de/)
+ //  Copyright (c) 2002-2003 Ralf S. Engelschall <rse@engelschall.com>
+ //  Copyright (c) 2002-2003 Michael Schloh von Bennewitz <michael@schloh.com>
+ //
+ //  This file is part of OSSP asgui, an accounting system graphical user
+ //  interface which can be found at http://www.ossp.org/pkg/tool/asgui/.
+ //
+ //  Permission to use, copy, modify, and distribute this software for
+ //  any purpose with or without fee is hereby granted, provided that
+ //  the above copyright notice and this permission notice appear in all
+ //  copies.
+ //
+ //  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
+ //  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
+ //  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ //  IN NO EVENT SHALL THE AUTHORS AND COPYRIGHT HOLDERS AND THEIR
+ //  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ //  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ //  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
+ //  USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+ //  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ //  OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
+ //  OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ //  SUCH DAMAGE.
+ //
+ //  as_rand.cpp: ISO C++ implementation
+ //
+ 
+ #include <fcntl.h>
+ #include <cerrno>
+ 
+ #include "as_rand.h"
+ 
+ #ifdef HAVE_UNISTD_H
+ #include <unistd.h>
+ #endif // HAVE_UNISTD_H
+ #ifdef HAVE_STDLIB_H
+ #include <stdlib.h>
+ #endif // HAVE_STDLIB_H
+ 
+ #ifdef TIME_WITH_SYS_TIME
+ #include <sys/time.h>
+ #endif // TIME_WITH_SYS_TIME
+ 
+ 
+ namespace AS {
+ 
+ // Constructor
+ Rand::Rand(void)
+ {
+     this->nFd = -4; // Object fresh status
+     this->openDevice();   // Open the random device
+ //    if (this->nFd < 0)
+ //        throw Genexcept("Could not open /dev/urandom or /dev/random.");
+ }
+ 
+ // Destructor
+ Rand::~Rand()
+ {
+     this->closeDevice();  // Close the random device
+     this->nFd = -4; // Return this object to its fresh status
+ }
+ 
+ //
+ // Rand::openDevice(void)
+ // Open the random device associated with this object
+ //
+ void Rand::openDevice(void)
+ {
+     struct timeval Time;
+ 
+     if (this->nFd == -4) {  // -4 indicates a fresh object
+         gettimeofday(&Time, 0);
+         this->nFd = open("/dev/urandom", O_RDONLY);
+         if (this->nFd == -1)
+             this->nFd = open("/dev/random", O_RDONLY | O_NONBLOCK);
+         srand((getpid() << 16) ^ getuid() ^ Time.tv_sec ^ Time.tv_usec); // Seed
+     }
+ 
+     // Improve randomness by stirring in entropy
+     gettimeofday(&Time, 0);
+     for (int nIter = (Time.tv_sec ^ Time.tv_usec) & 0x1F; nIter > 0; nIter--)
+         rand();
+ }
+ 
+ //
+ // Rand::closeDevice(void)
+ // Close the random device associated with this object
+ //
+ void Rand::closeDevice(void)
+ {
+     if (this->nFd >= 0)
+         close(this->nFd);
+ }
+ 
+ //
+ // Rand::genData(void *pvBuf, int nBytes)
+ // Generate a series of random data from the system
+ // standard random devices /dev/[u|s]random
+ //
+ void Rand::genData(void *pBuf, int nBytes)
+ {
+     int nRead = 0;
+     int nLost = 0;
+     char *szOut = (char *)pBuf;
+ 
+     if (this->nFd >= 0) {
+         while (nBytes > 0) {
+             nRead = read(this->nFd, szOut, nBytes);
+             if ((nRead < 0) && ((errno == EINTR) || (errno == EAGAIN)))
+                 continue;
+             if (nRead <= 0) {
+                 if (nLost++ == 8)
+                     break;
+                 continue;
+             }
+             nBytes -= nRead;
+             szOut += nRead;
+             nLost = 0;
+         }
+     }
+ 
+     // For systems with no /dev/random, we do the next best thing
+     for (int nIter = 0;  nIter< nBytes; nIter++)
+         *szOut++ = rand() & 0xFF;
+ }
+ } // namespace AS


ossp-pkg/as/as-gui/as_rand.h -> 1.1

*** /dev/null    Sat Nov 23 05:46:19 2024
--- -    Sat Nov 23 05:46:21 2024
***************
*** 0 ****
--- 1,63 ----
+ //
+ //  OSSP asgui - Accounting system graphical user interface
+ //  Copyright (c) 2002-2003 The OSSP Project (http://www.ossp.org/)
+ //  Copyright (c) 2002-2003 Cable & Wireless Deutschland (http://www.cw.com/de/)
+ //  Copyright (c) 2002-2003 Ralf S. Engelschall <rse@engelschall.com>
+ //  Copyright (c) 2002-2003 Michael Schloh von Bennewitz <michael@schloh.com>
+ //
+ //  This file is part of OSSP asgui, an accounting system graphical user
+ //  interface which can be found at http://www.ossp.org/pkg/tool/asgui/.
+ //
+ //  Permission to use, copy, modify, and distribute this software for
+ //  any purpose with or without fee is hereby granted, provided that
+ //  the above copyright notice and this permission notice appear in all
+ //  copies.
+ //
+ //  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
+ //  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
+ //  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ //  IN NO EVENT SHALL THE AUTHORS AND COPYRIGHT HOLDERS AND THEIR
+ //  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ //  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ //  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
+ //  USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+ //  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ //  OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
+ //  OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ //  SUCH DAMAGE.
+ //
+ //  as_rand.h: ISO C++ interface
+ //
+ 
+ #ifndef RAND_H
+ #define RAND_H
+ 
+ #ifdef HAVE_CONFIG_H
+ #include "ac_config.h"
+ #endif
+ 
+ 
+ namespace AS {
+ 
+ //
+ // Generates (multiple) random numbers, using the
+ // system standard random devices or a choppy
+ // home made algorythm if no devices exist
+ //
+ class Rand
+ {
+ private:
+     int nFd;                    // File descriptor of random device
+ 
+ public:
+     Rand(void);                 // Constructor
+     ~Rand(void);                // Destructor
+     void genData(void *, int);  // Generates a number
+ 
+ private:
+     void openDevice(void);      // Opens the random device
+     void closeDevice(void);     // Closes the random device
+ };
+ }
+ 
+ #endif // RAND_H

CVSTrac 2.0.1