📄 pgprngpriv.c
字号:
/*
* Private helper functions for keyring manipulation.
*
* Written by Colin Plumb.
*
* $Id: pgpRngPriv.c,v 1.45 1999/04/26 23:27:38 hal Exp $
*/
#include "pgpConfig.h"
#include <errno.h>
#include <string.h>
#include <stdio.h>
#include "pgpDebug.h"
#include "pgpPktByte.h"
#include "pgpRngPriv.h"
#include "pgpRngPars.h"
#include "pgpRngRead.h" /* For ringFilePurgeTrouble */
#include "pgpTrstPkt.h"
#include "pgpHashPriv.h"
#include "pgpEnv.h"
#include "pgpErrors.h"
#include "pgpMem.h"
#include "pgpSigSpec.h" /* for PGP_SIGTYPE */
#include "pgpTrust.h"
#include "pgpContext.h"
#include "pgpRegExp.h"
#ifndef NULL
#define NULL 0
#endif
#define PGP_TRUST_DECADE_INTERNAL (PGP_TRUST_DECADE >> 6)
#define PGP_TRUST_OCTAVE_INTERNAL (PGP_TRUST_OCTAVE >> 6)
/*
* Virtual ring mask functions
*/
#if !VIRTMASK
#if !MULTIMASK
/* Use old data structure for backwards compatibility */
PGPError
pgpVirtMaskInit (RingPool const *pool, PGPVirtMask *mask)
{
(void) pool;
(void) mask;
*mask = (PGPVirtMask)0;
return kPGPError_NoErr;
}
/* Need a lighter weight memory allocation package! */
PGPError
pgpVirtMaskCleanup (RingPool const *pool, PGPVirtMask *mask)
{
(void) pool;
(void) mask;
*mask = (PGPVirtMask)0;
return kPGPError_NoErr;
}
PGPError
pgpVirtMaskOR (RingPool const *pool, PGPVirtMask const *imask,
PGPVirtMask *omask)
{
(void) pool;
*omask |= *imask;
return kPGPError_NoErr;
}
PGPError
pgpVirtMaskAND (RingPool const *pool, PGPVirtMask const *imask,
PGPVirtMask *omask)
{
(void) pool;
*omask &= *imask;
return kPGPError_NoErr;
}
PGPError
pgpVirtMaskANDNOT (RingPool const *pool, PGPVirtMask const *imask,
PGPVirtMask *omask)
{
(void) pool;
*omask &= ~*imask;
return kPGPError_NoErr;
}
PGPError
pgpVirtMaskSetBit (RingPool const *pool, PGPVirtMask *mask,
PGPUInt32 bitnumber)
{
(void) pool;
*mask |= (1 << bitnumber);
return kPGPError_NoErr;
}
PGPError
pgpVirtMaskClearBit (RingPool const *pool, PGPVirtMask *mask,
PGPUInt32 bitnumber)
{
(void) pool;
*mask &= ~(1 << bitnumber);
return kPGPError_NoErr;
}
PGPError
pgpVirtMaskClearGreaterBits (RingPool const *pool, PGPVirtMask *mask,
PGPUInt32 firstbitnumber)
{
(void) pool;
if (firstbitnumber < 32)
*mask &= (1 << firstbitnumber) - 1;
return kPGPError_NoErr;
}
PGPError
pgpVirtMaskCopy (RingPool const *pool, PGPVirtMask const *imask,
PGPVirtMask *omask)
{
(void) pool;
*omask = *imask;
return kPGPError_NoErr;
}
PGPError
pgpVirtMaskNOT (RingPool const *pool, PGPVirtMask *mask,
PGPUInt32 highbitnumber)
{
(void) pool;
*mask = ~*mask;
if (highbitnumber < 32)
*mask &= (1 << highbitnumber) - 1;
return kPGPError_NoErr;
}
PGPInt32
pgpVirtMaskLSBit (PGPVirtMask const *mask)
{
return ringLsBitFind (*mask);
}
PGPBoolean
pgpVirtMaskIsEmpty (PGPVirtMask const *mask)
{
return !*mask;
}
PGPBoolean
pgpVirtMaskIsEqual (PGPVirtMask const *mask1, PGPVirtMask const *mask2)
{
return *mask1 == *mask2;
}
PGPBoolean
pgpVirtMaskIsOverlapping (PGPVirtMask const *mask1, PGPVirtMask const *mask2)
{
return (*mask1 & *mask2) != 0;
}
#else /* MULTIMASK */
/* Use MULTIMASK words of data */
PGPError
pgpVirtMaskInit (RingPool const *pool, PGPVirtMask *mask)
{
PGPUInt32 i;
(void)pool;
for (i=0; i<MULTIMASK; ++i)
mask->words[i] = 0;
return kPGPError_NoErr;
}
PGPError
pgpVirtMaskCleanup (RingPool const *pool, PGPVirtMask *mask)
{
PGPUInt32 i;
(void)pool;
for (i=0; i<MULTIMASK; ++i)
mask->words[i] = 0;
return kPGPError_NoErr;
}
PGPError
pgpVirtMaskOR (RingPool const *pool, PGPVirtMask const *imask,
PGPVirtMask *omask)
{
PGPUInt32 i;
(void) pool;
for (i=0; i<MULTIMASK; ++i)
omask->words[i] |= imask->words[i];
return kPGPError_NoErr;
}
PGPError
pgpVirtMaskAND (RingPool const *pool, PGPVirtMask const *imask,
PGPVirtMask *omask)
{
PGPUInt32 i;
(void) pool;
for (i=0; i<MULTIMASK; ++i)
omask->words[i] &= imask->words[i];
return kPGPError_NoErr;
}
PGPError
pgpVirtMaskANDNOT (RingPool const *pool, PGPVirtMask const *imask,
PGPVirtMask *omask)
{
PGPUInt32 i;
(void)pool;
for (i=0; i<MULTIMASK; ++i)
omask->words[i] &= ~imask->words[i];
return kPGPError_NoErr;
}
PGPError
pgpVirtMaskSetBit (RingPool const *pool, PGPVirtMask *mask,
PGPUInt32 bitnumber)
{
PGPUInt32 bitword;
(void) pool;
bitword = bitnumber / 32;
mask->words[bitword] |= 1 << (bitnumber % 32);
return kPGPError_NoErr;
}
PGPError
pgpVirtMaskClearBit (RingPool const *pool, PGPVirtMask *mask,
PGPUInt32 bitnumber)
{
PGPUInt32 bitword;
(void) pool;
bitword = bitnumber / 32;
mask->words[bitword] &= ~ (1 << (bitnumber % 32) );
return kPGPError_NoErr;
}
PGPError
pgpVirtMaskClearGreaterBits (RingPool const *pool, PGPVirtMask *mask,
PGPUInt32 firstbitnumber)
{
PGPUInt32 bitword;
(void) pool;
bitword = firstbitnumber / 32;
mask->words[bitword] &= (1 << (firstbitnumber % 32) ) - 1;
for (++bitword; bitword < MULTIMASK; ++bitword) {
mask->words[bitword] = 0;
}
return kPGPError_NoErr;
}
/* Complements a mask up to one less than the specified number of bits */
PGPError
pgpVirtMaskNOT (RingPool const *pool, PGPVirtMask *mask,
PGPUInt32 highbitnumber)
{
PGPUInt32 highbitword;
PGPUInt32 i;
(void) pool;
highbitword = highbitnumber / 32;
for (i=0; i<pgpMin(highbitword, MULTIMASK); ++i)
mask->words[i] = ~mask->words[i];
if (highbitword < MULTIMASK)
mask->words[highbitword] = ((1 << (highbitnumber % 32)) - 1) &
~mask->words[highbitword];
return kPGPError_NoErr;
}
PGPError
pgpVirtMaskCopy (RingPool const *pool, PGPVirtMask const *imask,
PGPVirtMask *omask)
{
PGPUInt32 i;
(void) pool;
for (i=0; i<MULTIMASK; ++i)
omask->words[i] = imask->words[i];
return kPGPError_NoErr;
}
PGPInt32
pgpVirtMaskLSBit (PGPVirtMask const *mask)
{
PGPUInt32 i;
for (i=0; i<MULTIMASK; ++i) {
if (mask->words[i])
return 32*i + ringLsBitFind (mask->words[i]);
}
return -1;
}
PGPBoolean
pgpVirtMaskIsEmpty (PGPVirtMask const *mask)
{
PGPUInt32 i;
for (i=0; i<MULTIMASK; ++i)
if (mask->words[i])
return FALSE;
return TRUE;
}
PGPBoolean
pgpVirtMaskIsEqual (PGPVirtMask const *mask1, PGPVirtMask const *mask2)
{
PGPUInt32 i;
for (i=0; i<MULTIMASK; ++i)
if (mask1->words[i] != mask2->words[i])
return FALSE;
return TRUE;
}
PGPBoolean
pgpVirtMaskIsOverlapping (PGPVirtMask const *mask1, PGPVirtMask const *mask2)
{
PGPUInt32 i;
for (i=0; i<MULTIMASK; ++i)
if (mask1->words[i] & mask2->words[i])
return TRUE;
return FALSE;
}
#endif /* MULTIMASK */
#else /* VIRTMASK */
/*
* The original design included a fixed size mask with a bit for
* each ringset in the system. However this tended to become
* exhausted with sufficient user windows open. This package
* provides an (almost) unlimited size virtual ring mask function
* to be compatible with the previous logic but to remove the limits
* on the number of ringsets being manipulated.
*/
PGPError
pgpVirtMaskInit (RingPool const *pool, PGPVirtMask *mask)
{
(void)pool;
mask->nwords = 0;
mask->words = NULL;
return kPGPError_NoErr;
}
#define PVM_LEAKS 0
#if PVM_LEAKS
/* leak detection */
static struct pvm {
struct pvm *next;
int num;
void *addr;
} *pvm_head;
static int pvm_index;
static void
pvm_remember (PGPContextRef c, void *addr)
{
struct pvm *n;
n = (struct pvm *)pgpContextMemAlloc (c, sizeof(struct pvm), 0);
n->next = pvm_head;
n->num = pvm_index++;
n->addr = addr;
pvm_head = n;
}
static void
pvm_forget (PGPContextRef c, void *addr)
{
struct pvm *n = pvm_head;
struct pvm *pn = NULL;
while (n) {
if (n->addr == addr) {
if (pn)
pn->next = n->next;
else
pvm_head = n->next;
pgpContextMemFree (c, n);
return;
}
pn = n;
n = n->next;
}
pgpAssert(0);
}
#endif
/* Need a lighter weight memory allocation package! */
PGPError
pgpVirtMaskCleanup (RingPool const *pool, PGPVirtMask *mask)
{
PGPError err = kPGPError_NoErr;
if (mask->nwords != 0) {
err = pgpContextMemFree (pool->context, mask->words);
#if PVM_LEAKS
pvm_forget (pool->context, mask->words);
#endif
}
mask->words = NULL;
mask->nwords = 0;
return err;
}
static PGPError
pgpVirtMaskSizeUp (RingPool const *pool, PGPVirtMask *mask,
PGPUInt32 minwords)
{
PGPError err;
/* Make minwords a multiple of 4 to reduce number of resizes */
minwords += (-minwords) & 3;
if (mask->nwords < minwords) {
if (IsntNull (mask->words)) {
void *vptr = mask->words;
#if PVM_LEAKS
pvm_forget (pool->context, vptr);
#endif
err = pgpContextMemRealloc (pool->context, &vptr,
minwords * sizeof(*mask->words), kPGPMemoryMgrFlags_Clear);
#if PVM_LEAKS
pvm_remember (pool->context, vptr);
#endif
if (IsPGPError(err))
return err;
mask->words = vptr;
mask->nwords = minwords;
} else {
mask->words = pgpContextMemAlloc (pool->context,
minwords * sizeof(*mask->words), kPGPMemoryMgrFlags_Clear);
if (IsNull(mask->words))
return kPGPError_OutOfMemory;
#if PVM_LEAKS
pvm_remember(pool->context, mask->words);
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -