Main Page   Modules   Data Structures   File List   Data Fields   Globals   Related Pages  

beecrypt/mp32number.c

Go to the documentation of this file.
00001 /*@-sizeoftype@*/
00008 /*
00009  *
00010  * Copyright (c) 1997, 1998, 1999, 2000, 2001 Virtual Unlimited B.V.
00011  *
00012  * Author: Bob Deblier <bob@virtualunlimited.com>
00013  *
00014  * This library is free software; you can redistribute it and/or
00015  * modify it under the terms of the GNU Lesser General Public
00016  * License as published by the Free Software Foundation; either
00017  * version 2.1 of the License, or (at your option) any later version.
00018  *
00019  * This library is distributed in the hope that it will be useful,
00020  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00021  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00022  * Lesser General Public License for more details.
00023  *
00024  * You should have received a copy of the GNU Lesser General Public
00025  * License along with this library; if not, write to the Free Software
00026  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00027  *
00028  */
00029 
00030 #include "system.h"
00031 #include "mp32number.h"
00032 #include "mp32.h"
00033 #include "debug.h"
00034 
00035 void mp32nzero(mp32number* n)
00036 {
00037         n->size = 0;
00038         n->data = (uint32*) 0;
00039 }
00040 
00041 /*@-compdef @*/ /* n->data not initialized */
00042 void mp32nsize(mp32number* n, uint32 size)
00043 {
00044         if (size)
00045         {
00046                 if (n->data)
00047                 {
00048                         if (n->size != size)
00049                                 n->data = (uint32*) realloc(n->data, size * sizeof(uint32));
00050                 }
00051                 else
00052                         n->data = (uint32*) malloc(size * sizeof(uint32));
00053 
00054                 if (n->data)
00055                         n->size = size;
00056                 else
00057                 {
00058                         n->size = 0;
00059                         n->data = (uint32*) 0;
00060                 }
00061 
00062         }
00063         else if (n->data)
00064         {
00065                 free(n->data);
00066                 n->size = 0;
00067                 n->data = (uint32*) 0;
00068         }
00069         else
00070                 {};
00071 }
00072 /*@=compdef @*/
00073 
00074 /*@-boundswrite@*/
00075 void mp32ninit(mp32number* n, uint32 size, const uint32* data)
00076 {
00077         n->size = size;
00078         if (n->data)
00079         {
00080                 free(n->data);
00081                 n->data = (uint32*) 0;
00082         }
00083         n->data = (uint32*) malloc(size * sizeof(uint32));
00084 
00085         if (n->data && data)
00086                 mp32copy(size, n->data, data);
00087 }
00088 /*@=boundswrite@*/
00089 
00090 void mp32nfree(mp32number* n)
00091 {
00092         if (n->data)
00093         {
00094                 free(n->data);
00095                 n->data = (uint32*) 0;
00096         }
00097         n->size = 0;
00098 }
00099 
00100 void mp32ncopy(mp32number* n, const mp32number* copy)
00101 {
00102         mp32nset(n, copy->size, copy->data);
00103 }
00104 
00105 void mp32nwipe(mp32number* n)
00106 {
00107         if (n->data)
00108                 mp32zero(n->size, n->data);
00109 }
00110 
00111 /*@-boundswrite@*/
00112 void mp32nset(mp32number* n, uint32 size, const uint32* data)
00113 {
00114         if (size)
00115         {
00116                 if (n->data)
00117                 {
00118                         if (n->size != size)
00119                                 n->data = (uint32*) realloc(n->data, size * sizeof(uint32));
00120                 }
00121                 else
00122                         n->data = (uint32*) malloc(size * sizeof(uint32));
00123 
00124                 if (n->data && data)
00125                         /*@-nullpass@*/ /* data is notnull */
00126                         mp32copy(n->size = size, n->data, data);
00127                         /*@=nullpass@*/
00128                 else
00129                 {
00130                         n->size = 0;
00131                         n->data = (uint32*) 0;
00132                 }
00133         }
00134         else if (n->data)
00135         {
00136                 free(n->data);
00137                 n->data = (uint32*) 0;
00138                 n->size = 0;
00139         }
00140         else
00141                 {};
00142 }
00143 /*@=boundswrite@*/
00144 
00145 /*@-boundswrite@*/
00146 void mp32nsetw(mp32number* n, uint32 val)
00147 {
00148         if (n->data)
00149         {
00150                 if (n->size != 1)
00151                         n->data = (uint32*) realloc(n->data, sizeof(uint32));
00152         }
00153         else
00154                 n->data = (uint32*) malloc(sizeof(uint32));
00155 
00156         if (n->data)
00157         {
00158                 n->size = 1;
00159                 n->data[0] = val;
00160         }
00161         else
00162         {
00163                 n->size = 0;
00164                 n->data = (uint32*) 0;
00165         }
00166 }
00167 /*@=boundswrite@*/
00168 
00169 /*@-boundswrite@*/
00170 /*@-usedef @*/  /* n->data may be NULL */
00171 void mp32nsethex(mp32number* n, const char* hex)
00172 {
00173         uint32 length = strlen(hex);
00174         uint32 size = (length+7) >> 3;
00175         uint8 rem = (uint8)(length & 0x7);
00176 
00177         if (n->data)
00178         {
00179                 if (n->size != size)
00180                         n->data = (uint32*) realloc(n->data, size * sizeof(uint32));
00181         }
00182         else
00183                 n->data = (uint32*) malloc(size * sizeof(uint32));
00184 
00185         if (n->data)
00186         {
00187                 register uint32  val = 0;
00188                 register uint32* dst = n->data;
00189                 register char ch;
00190 
00191                 n->size = size;
00192 
00193                 while (length-- > 0)
00194                 {
00195                         ch = *(hex++);
00196                         val <<= 4;
00197                         if (ch >= '0' && ch <= '9')
00198                                 val += (ch - '0');
00199                         else if (ch >= 'A' && ch <= 'F')
00200                                 val += (ch - 'A') + 10;
00201                         else if (ch >= 'a' && ch <= 'f')
00202                                 val += (ch - 'a') + 10;
00203                         else
00204                                 {};
00205 
00206                         if ((length & 0x7) == 0)
00207                         {
00208                                 *(dst++) = val;
00209                                 val = 0;
00210                                 rem = 0;
00211                         } else
00212                                 rem = 1;
00213                 }
00214                 if (rem != 0) {
00215                         *dst = val;
00216                 }
00217         }
00218         else {
00219                 n->size = 0;
00220                 n->data = (uint32*)0;
00221         }
00222 }
00223 /*@=usedef @*/
00224 /*@=sizeoftype@*/
00225 /*@=boundswrite@*/

Generated on Tue Sep 17 15:56:37 2002 for rpm by doxygen1.2.14 written by Dimitri van Heesch, © 1997-2002