summaryrefslogtreecommitdiffstats
path: root/generic
diff options
context:
space:
mode:
authorjan.nijtmans <nijtmans@users.sourceforge.net>2022-11-15 13:06:59 (GMT)
committerjan.nijtmans <nijtmans@users.sourceforge.net>2022-11-15 13:06:59 (GMT)
commit870cb82c96d74e93a642296f68319c777359a11d (patch)
tree2bb461b802809dbbc23c8d4c80d03f227439faa2 /generic
parent386c7ea2d345ea032e96b2f0085bbaa31b204448 (diff)
downloadtcl-870cb82c96d74e93a642296f68319c777359a11d.zip
tcl-870cb82c96d74e93a642296f68319c777359a11d.tar.gz
tcl-870cb82c96d74e93a642296f68319c777359a11d.tar.bz2
Change all mp_to_ubin() usages to mp_pack(). It makes the code much more clear
Diffstat (limited to 'generic')
-rw-r--r--generic/tclObj.c34
1 files changed, 10 insertions, 24 deletions
diff --git a/generic/tclObj.c b/generic/tclObj.c
index ce8e610..bad3f85 100644
--- a/generic/tclObj.c
+++ b/generic/tclObj.c
@@ -3124,15 +3124,12 @@ Tcl_GetLongFromObj(
{
mp_int big;
- unsigned long scratch, value = 0;
- unsigned char *bytes = (unsigned char *) &scratch;
+ unsigned long value = 0;
size_t numBytes;
TclUnpackBignum(objPtr, big);
- if (mp_to_ubin(&big, bytes, sizeof(long), &numBytes) == MP_OKAY) {
- while (numBytes-- > 0) {
- value = (value << CHAR_BIT) | *bytes++;
- }
+ if (mp_pack(&value, 1,
+ &numBytes, 0, sizeof(Tcl_WideUInt), 0, 0, &big) == MP_OKAY) {
if (big.sign) {
if (value <= 1 + (unsigned long)LONG_MAX) {
*longPtr = (long)(-value);
@@ -3364,14 +3361,10 @@ Tcl_GetWideIntFromObj(
mp_int big;
Tcl_WideUInt value = 0;
size_t numBytes;
- Tcl_WideInt scratch;
- unsigned char *bytes = (unsigned char *) &scratch;
TclUnpackBignum(objPtr, big);
- if (mp_to_ubin(&big, bytes, sizeof(Tcl_WideInt), &numBytes) == MP_OKAY) {
- while (numBytes-- > 0) {
- value = (value << CHAR_BIT) | *bytes++;
- }
+ if (mp_pack(&value, 1,
+ &numBytes, 0, sizeof(Tcl_WideUInt), 0, 0, &big) == MP_OKAY) {
if (big.sign) {
if (value <= 1 + ~(Tcl_WideUInt)WIDE_MIN) {
*wideIntPtr = (Tcl_WideInt)(-value);
@@ -3444,21 +3437,18 @@ TclGetWideBitsFromObj(
mp_int big;
mp_err err;
- Tcl_WideUInt value = 0, scratch;
+ Tcl_WideUInt value = 0;
size_t numBytes;
- unsigned char *bytes = (unsigned char *) &scratch;
Tcl_GetBignumFromObj(NULL, objPtr, &big);
err = mp_mod_2d(&big, (int) (CHAR_BIT * sizeof(Tcl_WideInt)), &big);
if (err == MP_OKAY) {
- err = mp_to_ubin(&big, bytes, sizeof(Tcl_WideInt), &numBytes);
+ err = mp_pack(&value, 1,
+ &numBytes, 0, sizeof(Tcl_WideUInt), 0, 0, &big);
}
if (err != MP_OKAY) {
return TCL_ERROR;
}
- while (numBytes-- > 0) {
- value = (value << CHAR_BIT) | *bytes++;
- }
*wideIntPtr = !big.sign ? (Tcl_WideInt)value : -(Tcl_WideInt)value;
mp_clear(&big);
return TCL_OK;
@@ -3828,19 +3818,15 @@ Tcl_SetBignumObj(
{
Tcl_WideUInt value = 0;
size_t numBytes;
- Tcl_WideUInt scratch;
- unsigned char *bytes = (unsigned char *) &scratch;
mp_int *bignumValue = (mp_int *) big;
if (Tcl_IsShared(objPtr)) {
Tcl_Panic("%s called with shared object", "Tcl_SetBignumObj");
}
- if (mp_to_ubin(bignumValue, bytes, sizeof(Tcl_WideUInt), &numBytes) != MP_OKAY) {
+ if (mp_pack(&value, 1,
+ &numBytes, 0, sizeof(Tcl_WideUInt), 0, 0, bignumValue) != MP_OKAY) {
goto tooLargeForWide;
}
- while (numBytes-- > 0) {
- value = (value << CHAR_BIT) | *bytes++;
- }
if (value > ((Tcl_WideUInt)WIDE_MAX + bignumValue->sign)) {
goto tooLargeForWide;
}