Ignore:
Timestamp:
Jun 9, 2010, 2:46:02 PM (10 years ago)
Author:
Silvan Scherrer
Message:

SambaServer? 3.3: unified os2ea.c and os2path.c, rewrote randomizer

File:
1 moved

Legend:

Unmodified
Added
Removed
  • branches/samba-3.3.x/source/lib/os2helper.c

    r430 r460  
    1010#define INCL_DOSMODULEMGR
    1111#define INCL_DOSERRORS
    12 //_SMB_H
     12
    1313#include <os2.h>
    1414#include <stdio.h>
     
    1616#include <errno.h>
    1717#include <types.h>
    18 
     18#include <string.h>
    1919//YD for tmalloc
    2020#include <malloc.h>
     
    3232// Samba DEBUG() needs the following includes and defines
    3333#define PRINTF_ATTRIBUTE(a1, a2) __attribute__ ((format (__printf__, a1, a2)))
    34 #define CMD_KI_RDCNT            (0x63)
    3534#include <stdbool.h>
    3635#include "local.h"
     
    9998}
    10099
    101 /* Functions below are based on APR random code */
    102 /* Licensed to the Apache Software Foundation (ASF) under one or more
    103  * contributor license agreements.  See the NOTICE file distributed with
    104  * this work for additional information regarding copyright ownership.
    105  * The ASF licenses this file to You under the Apache License, Version 2.0
    106  * (the "License"); you may not use this file except in compliance with
    107  * the License.  You may obtain a copy of the License at
    108  *
    109  *     http://www.apache.org/licenses/LICENSE-2.0
    110  *
    111  * Unless required by applicable law or agreed to in writing, software
    112  * distributed under the License is distributed on an "AS IS" BASIS,
    113  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    114  * See the License for the specific language governing permissions and
    115  * limitations under the License.
    116  */
     100// we search the path of the .exe and return it
     101int os2_GetExePath(char *buff)
     102{
     103        APIRET rc = NO_ERROR;
     104        PPIB ppib = NULL;
     105        char sExePath [_MAX_PATH];
     106        char sDrive [_MAX_PATH], sDir [_MAX_DIR];
     107
     108        // we search for the infoblock to get the module name
     109        rc = DosGetInfoBlocks(NULL, &ppib);
     110        if (rc != NO_ERROR)
     111        {
     112                return -1;
     113        }
     114
     115        // with the module name we get the path (including the exe name)
     116        rc = DosQueryModuleName(ppib->pib_hmte, sizeof(sExePath), sExePath);
     117        if (rc != NO_ERROR)
     118        {
     119                return -1;
     120        }
     121
     122        // we split to the different values
     123        _splitpath(sExePath, sDrive, sDir, NULL, NULL);
     124        // strcat(sDrive, sDir);
     125        strncat(sDrive, sDir, strlen(sDir) -1);
     126        strcpy(buff, sDrive);
     127
     128        return 0;
     129}
     130
     131/* os2 specific random functions. this functions used to be based on APR random code.
     132   but we discovered some nasty problems with it on fast hardware (especially on quadcore) and
     133   decided to rewrite it with libc random() */
    117134
    118135void os2_randget(char * buffer, int length)
     
    126143}
    127144
    128 /* A bunch of system information like memory & process stats.
    129  * Not highly random but every bit helps....
    130  */
    131 static UCHAR randbyte_sysinfo()
    132 {
    133     UCHAR byte = 0;
    134     UCHAR SysVars[100];
    135     int b;
    136 
    137     DosQuerySysInfo(1, QSV_FOREGROUND_PROCESS, SysVars, sizeof(SysVars));
    138 
    139     for (b = 0; b < 100; b++) {
    140         byte ^= SysVars[b];
    141     }
     145UCHAR randbyte()
     146{
     147     int c;
     148     UCHAR byte;
     149     ULONG ulrandom;
     150        ulrandom = random();
     151        for (c = 0; c < sizeof(ulrandom); c++) {
     152                byte ^= ((UCHAR *)&ulrandom)[c];
     153        }
    142154
    143155    return byte;
    144 }
    145 
    146 
    147 
    148 /* Similar in concept to randbyte_hrtimer() but accesses the CPU's internal
    149  * counters which run at the CPU's MHz speed. We get separate
    150  * idle / busy / interrupt cycle counts which should provide very good
    151  * randomness due to interference of hardware events.
    152  * This only works on newer CPUs (at least PPro or K6) and newer OS/2 versions
    153  * which is why it's run-time linked.
    154  */
    155 
    156 static HMODULE hDoscalls = 0;
    157 #define   CMD_KI_RDCNT    (0x63)
    158 
    159 typedef struct _CPUUTIL {
    160     ULONG ulTimeLow;            /* Low 32 bits of time stamp      */
    161     ULONG ulTimeHigh;           /* High 32 bits of time stamp     */
    162     ULONG ulIdleLow;            /* Low 32 bits of idle time       */
    163     ULONG ulIdleHigh;           /* High 32 bits of idle time      */
    164     ULONG ulBusyLow;            /* Low 32 bits of busy time       */
    165     ULONG ulBusyHigh;           /* High 32 bits of busy time      */
    166     ULONG ulIntrLow;            /* Low 32 bits of interrupt time  */
    167     ULONG ulIntrHigh;           /* High 32 bits of interrupt time */
    168 } CPUUTIL;
    169 
    170 APIRET APIENTRY(*pfnDosPerfSysCall) (ULONG ulCommand, ULONG ulParm1, ULONG ulParm2, ULONG ulParm3) = NULL;
    171 APIRET APIENTRY(*pfnDosQuerySysState) (ULONG func, ULONG arg1, ULONG pid, ULONG _res_, PVOID buf, ULONG bufsz) = NULL;
    172 
    173 static UCHAR randbyte_perf()
    174 {
    175     UCHAR byte = 0;
    176     CPUUTIL util;
    177     int c;
    178 
    179     if (hDoscalls == 0) {
    180         char failed_module[20];
    181         ULONG rc;
    182 
    183         rc = DosLoadModule(failed_module, sizeof(failed_module), "DOSCALLS",
    184                            &hDoscalls);
    185 
    186         if (rc == 0) {
    187             rc = DosQueryProcAddr(hDoscalls, 976, NULL, (PFN *)&pfnDosPerfSysCall);
    188 
    189             if (rc) {
    190                 pfnDosPerfSysCall = NULL;
    191             }
    192         }
    193     }
    194 
    195     if (pfnDosPerfSysCall) {
    196         if ((*pfnDosPerfSysCall) (CMD_KI_RDCNT, (ULONG)&util, 0, 0) == 0) {
    197             for (c = 0; c < sizeof(util); c++) {
    198                 byte ^= ((UCHAR *)&util)[c];
    199             }
    200         }
    201         else {
    202             pfnDosPerfSysCall = NULL;
    203         }
    204     }
    205 
    206     return byte;
    207 }
    208 
    209 
    210 
    211 UCHAR randbyte()
    212 {
    213     return randbyte_sysinfo() ^ randbyte_perf();
    214156}
    215157
Note: See TracChangeset for help on using the changeset viewer.