1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
|
/*
* mp32number.c
*
* Multiple precision numbers, code
*
* Copyright (c) 1997, 1998, 1999, 2000, 2001 Virtual Unlimited B.V.
*
* Author: Bob Deblier <bob@virtualunlimited.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
#define BEECRYPT_DLL_EXPORT
#include "mp32number.h"
#include "mp32.h"
#if HAVE_STDLIB_H
# include <stdlib.h>
#endif
#if HAVE_MALLOC_H
# include <malloc.h>
#endif
void mp32nzero(mp32number* n)
{
n->size = 0;
n->data = (uint32*) 0;
}
void mp32nsize(mp32number* n, uint32 size)
{
if (size)
{
if (n->data)
{
if (n->size != size)
n->data = (uint32*) realloc(n->data, size * sizeof(uint32));
}
else
n->data = (uint32*) malloc(size * sizeof(uint32));
if (n->data == (uint32*) 0)
n->size = 0;
else
n->size = size;
}
else if (n->data)
{
free(n->data);
n->data = (uint32*) 0;
n->size = 0;
}
}
void mp32ninit(mp32number* n, uint32 size, const uint32* data)
{
n->size = size;
n->data = (uint32*) malloc(size * sizeof(uint32));
if (n->data)
mp32copy(size, n->data, data);
}
void mp32nfree(mp32number* n)
{
if (n->data)
{
free(n->data);
n->data = (uint32*) 0;
}
n->size = 0;
}
void mp32ncopy(mp32number* n, const mp32number* copy)
{
mp32nset(n, copy->size, copy->data);
}
void mp32nwipe(mp32number* n)
{
mp32zero(n->size, n->data);
}
void mp32nset(mp32number* n, uint32 size, const uint32* data)
{
if (size)
{
if (n->data)
{
if (n->size != size)
n->data = (uint32*) realloc(n->data, size * sizeof(uint32));
}
else
n->data = (uint32*) malloc(size * sizeof(uint32));
if (n->data)
mp32copy(n->size = size, n->data, data);
else
n->size = 0;
}
else if (n->data)
{
free(n->data);
n->data = (uint32*) 0;
n->size = 0;
}
}
void mp32nsetw(mp32number* n, uint32 val)
{
if (n->data)
{
if (n->size != 1)
n->data = (uint32*) realloc(n->data, sizeof(uint32));
}
else
n->data = (uint32*) malloc(sizeof(uint32));
if (n->data)
{
n->size = 1;
n->data[0] = val;
}
else
n->size = 0;
}
void mp32nsethex(mp32number* n, const char* hex)
{
uint32 length = strlen(hex);
uint32 size = (length+7) >> 3;
uint8 rem = (uint8)(length & 0x7);
if (n->data)
{
if (n->size != size)
n->data = (uint32*) realloc(n->data, size * sizeof(uint32));
}
else
n->data = (uint32*) malloc(size * sizeof(uint32));
if (n->data)
{
register uint32 val = 0;
register uint32* dst = n->data;
register char ch;
n->size = size;
while (length-- > 0)
{
ch = *(hex++);
val <<= 4;
if (ch >= '0' && ch <= '9')
val += (ch - '0');
else if (ch >= 'A' && ch <= 'F')
val += (ch - 'A') + 10;
else if (ch >= 'a' && ch <= 'f')
val += (ch - 'a') + 10;
if ((length & 0x7) == 0)
{
*(dst++) = val;
val = 0;
}
}
if (rem)
*dst = val;
}
else
n->size = 0;
}
|