Debian Codegraph @

generic/tclTomMathInterface.c

raw
1 /*
2 *----------------------------------------------------------------------
3 *
4 * tclTomMathInterface.c --
5 *
6 * This file contains procedures that are used as a 'glue' layer between
7 * Tcl and libtommath.
8 *
9 * Copyright (c) 2005 by Kevin B. Kenny. All rights reserved.
10 *
11 * See the file "license.terms" for information on usage and redistribution of
12 * this file, and for a DISCLAIMER OF ALL WARRANTIES.
13 */
14
15 #include "tclInt.h"
16 #include "tommath.h"
17
18 MODULE_SCOPE const TclTomMathStubs tclTomMathStubs;
19
20 /*
21 *----------------------------------------------------------------------
22 *
23 * TclTommath_Init --
24 *
25 * Initializes the TclTomMath 'package', which exists as a
26 * placeholder so that the package data can be used to hold
27 * a stub table pointer.
28 *
29 * Results:
30 * Returns a standard Tcl result.
31 *
32 * Side effects:
33 * Installs the stub table for tommath.
34 *
35 *----------------------------------------------------------------------
36 */
37
38 int
39 TclTommath_Init(
40 Tcl_Interp *interp) /* Tcl interpreter */
41 {
42 /* TIP #268: Full patchlevel instead of just major.minor */
43
44 if (Tcl_PkgProvideEx(interp, "tcl::tommath", TCL_PATCH_LEVEL,
45 &tclTomMathStubs) != TCL_OK) {
46 return TCL_ERROR;
47 }
48 return TCL_OK;
49 }
50
51 /*
52 *----------------------------------------------------------------------
53 *
54 * TclBN_epoch --
55 *
56 * Return the epoch number of the TclTomMath stubs table
57 *
58 * Results:
59 * Returns an arbitrary integer that does not decrease with
60 * release. Stubs tables with different epochs are incompatible.
61 *
62 *----------------------------------------------------------------------
63 */
64
65 int
66 TclBN_epoch(void)
67 {
68 return TCLTOMMATH_EPOCH;
69 }
70
71 /*
72 *----------------------------------------------------------------------
73 *
74 * TclBN_revision --
75 *
76 * Returns the revision level of the TclTomMath stubs table
77 *
78 * Results:
79 * Returns an arbitrary integer that increases with revisions.
80 * If a client requires a given epoch and revision, any Stubs table
81 * with the same epoch and an equal or higher revision satisfies
82 * the request.
83 *
84 *----------------------------------------------------------------------
85 */
86
87 int
88 TclBN_revision(void)
89 {
90 return TCLTOMMATH_REVISION;
91 }
92
93 /*
94 *----------------------------------------------------------------------
95 *
96 * TclBNInitBignumFromLong --
97 *
98 * Allocate and initialize a 'bignum' from a native 'long'.
99 *
100 * Results:
101 * None.
102 *
103 * Side effects:
104 * The 'bignum' is constructed.
105 *
106 *----------------------------------------------------------------------
107 */
108
109 int
110 TclBNInitBignumFromLong(
111 mp_int *a,
112 long initVal)
113 {
114 unsigned long v;
115 mp_digit *p;
116
117 /*
118 * Allocate enough memory to hold the largest possible long
119 */
120
121 if (mp_init(a) != MP_OKAY) {
122 Tcl_Panic("initialization failure in TclBNInitBignumFromLong");
123 }
124
125 /*
126 * Convert arg to sign and magnitude.
127 */
128
129 if (initVal < 0) {
130 a->sign = MP_NEG;
131 v = -(unsigned long)initVal;
132 } else {
133 a->sign = MP_ZPOS;
134 v = initVal;
135 }
136
137 /*
138 * Store the magnitude in the bignum.
139 */
140
141 p = a->dp;
142 while (v) {
143 *p++ = (mp_digit) (v & MP_MASK);
144 v >>= MP_DIGIT_BIT;
145 }
146 a->used = p - a->dp;
147 return MP_OKAY;
148 }
149
150 /*
151 *----------------------------------------------------------------------
152 *
153 * TclBNInitBignumFromWideInt --
154 *
155 * Allocate and initialize a 'bignum' from a Tcl_WideInt
156 *
157 * Results:
158 * None.
159 *
160 * Side effects:
161 * The 'bignum' is constructed.
162 *
163 *----------------------------------------------------------------------
164 */
165
166 int
167 TclBNInitBignumFromWideInt(
168 mp_int *a, /* Bignum to initialize */
169 Tcl_WideInt v) /* Initial value */
170 {
171 if (v < 0) {
172 (void)TclBNInitBignumFromWideUInt(a, -(Tcl_WideUInt)v);
173 return mp_neg(a, a);
174 }
175 (void)TclBNInitBignumFromWideUInt(a, (Tcl_WideUInt)v);
176 return MP_OKAY;
177 }
178
179 /*
180 *----------------------------------------------------------------------
181 *
182 * TclBNInitBignumFromWideUInt --
183 *
184 * Allocate and initialize a 'bignum' from a Tcl_WideUInt
185 *
186 * Results:
187 * None.
188 *
189 * Side effects:
190 * The 'bignum' is constructed.
191 *
192 *----------------------------------------------------------------------
193 */
194
195 int
196 TclBNInitBignumFromWideUInt(
197 mp_int *a, /* Bignum to initialize */
198 Tcl_WideUInt v) /* Initial value */
199 {
200 mp_digit *p;
201
202 /*
203 * Allocate enough memory to hold the largest possible Tcl_WideUInt.
204 */
205
206 if (mp_init(a) != MP_OKAY) {
207 Tcl_Panic("initialization failure in TclBNInitBignumFromWideUInt");
208 }
209
210 a->sign = 0;
211
212 /*
213 * Store the magnitude in the bignum.
214 */
215
216 p = a->dp;
217 while (v) {
218 *p++ = (mp_digit) (v & MP_MASK);
219 v >>= MP_DIGIT_BIT;
220 }
221 a->used = p - a->dp;
222 return MP_OKAY;
223 }
224
225 /*
226 * Local Variables:
227 * mode: c
228 * c-basic-offset: 4
229 * fill-column: 78
230 * End:
231 */