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 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373
|
/*
* Copyright (c) 2007-2012 Zmanda, Inc. All Rights Reserved.
* Copyright (c) 2013-2016 Carbonite, Inc. All Rights Reserved.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program 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 General Public License
* for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* Contact information: Carbonite Inc., 756 N Pastoria Ave
* Sunnyvale, CA 94085, or: http://www.zmanda.com
*/
#include "amglue.h"
#include "stdint.h"
/*
* C -> Perl
*/
/* these functions are only needed if Perl has 32-bit IV's */
/* Make sure Math::BigInt is loaded
*/
static void
load_Math_BigInt(void)
{
static int loaded = 0;
if (loaded) return;
eval_pv("use Math::BigInt; use Amanda::BigIntCompat;", 1);
loaded = 1;
}
/* Given a string, create a Math::BigInt representing its value.
*
* @param num: string representation of a number
* @returns: BigInt representation of the same number
*/
static SV *
str2bigint(char *num)
{
int count;
SV *rv;
dSP;
ENTER;
SAVETMPS;
load_Math_BigInt();
SPAGAIN;
EXTEND(SP, 2);
PUSHMARK(SP);
XPUSHs(sv_2mortal(newSVpv("Math::BigInt", 0)));
XPUSHs(sv_2mortal(newSVpv(num, 0)));
PUTBACK;
count = call_method("new", G_SCALAR);
SPAGAIN;
if (count != 1)
croak("Expected a result from Math::Bigint->new");
rv = POPs;
SvREFCNT_inc(rv);
PUTBACK;
FREETMPS;
LEAVE;
return rv;
}
SV *
amglue_newSVi64(gint64 v)
{
char numstr[25];
g_snprintf(numstr, sizeof(numstr), "%jd", (intmax_t)v);
numstr[sizeof(numstr)-1] = '\0';
return str2bigint(numstr);
}
SV *
amglue_newSVu64(guint64 v)
{
char numstr[25];
g_snprintf(numstr, sizeof(numstr), "%ju", (uintmax_t)v);
numstr[sizeof(numstr)-1] = '\0';
return str2bigint(numstr);
}
/*
* Perl -> C
*/
/* Conversion from Perl values handles BigInts regardless of whether
* Perl's IVs are 32- or 64-bit, for completeness' sake.
*/
/* Convert a bigint to a signed integer, or croak trying.
*
* @param bigint: the perl object to convert
* @returns: signed integer
*/
static gint64
bigint2int64(SV *bigint, gchar **err)
{
SV *sv;
char *str;
guint64 absval;
gboolean negative = FALSE;
int count;
dSP;
/* first, see if it's a BigInt */
if (!sv_isobject(bigint) || !sv_derived_from(bigint, "Math::BigInt")) {
*err = g_strdup("Expected an integer or a Math::BigInt; cannot convert");
return 0;
}
ENTER;
SAVETMPS;
/* get the value:
* strtoull($bigint->bstr()) */
PUSHMARK(SP);
XPUSHs(bigint);
PUTBACK;
count = call_method("Math::BigInt::bstr", G_SCALAR);
SPAGAIN;
if (count != 1)
croak("Expected a result from Math::BigInt::bstr");
sv = POPs;
str = SvPV_nolen(sv);
if (!str)
croak("Math::BigInt::bstr did not return a string");
if (str[0] == '-') {
negative = TRUE;
str++;
}
errno = 0;
absval = g_ascii_strtoull(str, NULL, 0);
/* (the last branch of this || depends on G_MININT64 = -G_MAXINT64-1) */
if ((absval == G_MAXUINT64 && errno == ERANGE)
|| (!negative && absval > (guint64)(G_MAXINT64))
|| (negative && absval > (guint64)(G_MAXINT64)+1))
croak("Expected a signed 64-bit value or smaller; value '%s' out of range", str);
if (errno)
croak("Math::BigInt->bstr returned invalid number '%s'", str);
PUTBACK;
FREETMPS;
LEAVE;
if (negative) return -absval;
return absval;
}
/* Convert bigint to an unsigned integer, or croak trying.
*
* @param bigint: the perl object to convert
* @returns: unsigned integer
*/
static guint64
bigint2uint64(SV *bigint, gchar **err)
{
SV *sv;
char *str;
guint64 rv;
int count;
dSP;
/* first, see if it's a BigInt */
if (!sv_isobject(bigint) || !sv_derived_from(bigint, "Math::BigInt")) {
*err = g_strdup("Expected an integer or a Math::BigInt; cannot convert");
return 0;
}
ENTER;
SAVETMPS;
/* make sure the bigint is positive:
* croak(..) unless $bigint->sign() eq "+"; */
PUSHMARK(SP);
XPUSHs(bigint);
PUTBACK;
count = call_method("Math::BigInt::sign", G_SCALAR);
SPAGAIN;
if (count != 1)
croak("Expected a result from Math::BigInt::sign");
sv = POPs;
str = SvPV_nolen(sv);
if (!str)
croak("Math::BigInt::sign did not return a string");
if (strcmp(str, "+") != 0)
croak("Expected a positive number; value out of range");
/* get the value:
* strtoull($bigint->bstr()) */
PUSHMARK(SP);
XPUSHs(bigint);
PUTBACK;
count = call_method("Math::BigInt::bstr", G_SCALAR);
SPAGAIN;
if (count != 1)
croak("Expected a result from Math::BigInt::bstr");
sv = POPs;
str = SvPV_nolen(sv);
if (!str)
croak("Math::BigInt::bstr did not return a string");
errno = 0;
rv = g_ascii_strtoull(str, NULL, 0);
if (rv == G_MAXUINT64 && errno == ERANGE)
croak("Expected an unsigned 64-bit value or smaller; value '%s' out of range", str);
if (errno)
croak("Math::BigInt->bstr returned invalid number '%s'", str);
PUTBACK;
FREETMPS;
LEAVE;
return rv;
}
gint64 amglue_SvI64(SV *sv, gchar **err)
{
if (SvIOK(sv)) {
if (SvIsUV(sv)) {
return SvUV(sv);
} else {
return SvIV(sv);
}
} else if (SvNOK(sv)) {
double dv = SvNV(sv);
/* preprocessor constants seem to have trouble here, so we convert to gint64 and
* back, and if the result differs, then we have lost something. Note that this will
* also error out on integer truncation .. which is probably OK */
gint64 iv = (gint64)dv;
if (dv != (double)iv) {
*err = g_strdup_printf("Expected a signed 64-bit value or smaller; value '%.0f' out of range", (float)dv);
return 0;
} else {
return iv;
}
} else {
return bigint2int64(sv, err);
}
}
guint64 amglue_SvU64(SV *sv, gchar **err)
{
if (SvIOK(sv)) {
if (SvIsUV(sv)) {
return SvUV(sv);
} else if (SvIV(sv) < 0) {
*err = g_strdup("Expected an unsigned value, got a negative integer");
return 0;
} else {
return (guint64)SvIV(sv);
}
} else if (SvNOK(sv)) {
double dv = SvNV(sv);
if (dv < 0.0) {
*err = g_strdup("Expected an unsigned value, got a negative integer");
return 0;
} else if (dv > (double)G_MAXUINT64) {
*err = g_strdup("Expected an unsigned 64-bit value or smaller; value out of range");
return 0;
} else {
return (guint64)dv;
}
} else {
return bigint2uint64(sv, err);
}
}
gint32 amglue_SvI32(SV *sv, gchar **err)
{
gint64 v64 = amglue_SvI64(sv, err);
if (v64 < G_MININT32 || v64 > G_MAXINT32) {
*err = g_strdup("Expected a 32-bit integer; value out of range");
return 0;
} else {
return (gint32)v64;
}
}
guint32 amglue_SvU32(SV *sv, gchar **err)
{
guint64 v64 = amglue_SvU64(sv, err);
if (v64 > G_MAXUINT32) {
*err = g_strdup("Expected a 32-bit unsigned integer; value out of range");
return 0;
} else {
return (guint32)v64;
}
}
gint16 amglue_SvI16(SV *sv, gchar **err)
{
gint64 v64 = amglue_SvI64(sv, err);
if (v64 < G_MININT16 || v64 > G_MAXINT16) {
*err = g_strdup("Expected a 16-bit integer; value out of range");
return 0;
} else {
return (gint16)v64;
}
}
guint16 amglue_SvU16(SV *sv, gchar **err)
{
guint64 v64 = amglue_SvU64(sv, err);
if (v64 > G_MAXUINT16) {
*err = g_strdup("Expected a 16-bit unsigned integer; value out of range");
return 0;
} else {
return (guint16)v64;
}
}
gint8 amglue_SvI8(SV *sv, gchar **err)
{
gint64 v64 = amglue_SvI64(sv, err);
if (v64 < G_MININT8 || v64 > G_MAXINT8) {
*err = g_strdup("Expected a 8-bit integer; value out of range");
return 0;
} else {
return (gint8)v64;
}
}
guint8 amglue_SvU8(SV *sv, gchar **err)
{
guint64 v64 = amglue_SvU64(sv, err);
if (v64 > G_MAXUINT8) {
*err = g_strdup("Expected a 8-bit unsigned integer; value out of range");
return 0;
} else {
return (guint8)v64;
}
}
|