From bec7bfb29d76f995dbf223c20f5d04ae1e33cee6 Mon Sep 17 00:00:00 2001
From: Paul Sokolovsky <pfalcon@users.sourceforge.net>
Date: Sat, 21 Jan 2017 20:14:18 +0300
Subject: [PATCH] py/objint: from_bytes(): Implement "byteorder" param and
 arbitrary precision.

If result guaranteedly fits in a small int, it is handled in objint.c.
Otherwise, it is delegated to mp_obj_int_from_bytes_impl(), which should
be implemented by individual objint_*.c, similar to
mp_obj_int_to_bytes_impl().
---
 py/objint.c     | 31 +++++++++++++++++++------------
 py/objint.h     |  1 +
 py/objint_mpz.c |  6 ++++++
 3 files changed, 26 insertions(+), 12 deletions(-)

diff --git a/py/objint.c b/py/objint.c
index 5842a00a4d..a61ed8e275 100644
--- a/py/objint.c
+++ b/py/objint.c
@@ -374,26 +374,33 @@ mp_obj_t mp_obj_int_binary_op_extra_cases(mp_uint_t op, mp_obj_t lhs_in, mp_obj_
 
 // this is a classmethod
 STATIC mp_obj_t int_from_bytes(size_t n_args, const mp_obj_t *args) {
-    // TODO: Support long ints
-    // TODO: Support byteorder param
     // TODO: Support signed param (assumes signed=False at the moment)
     (void)n_args;
 
-    if (args[2] != MP_OBJ_NEW_QSTR(MP_QSTR_little)) {
-        mp_not_implemented("");
-    }
-
     // get the buffer info
     mp_buffer_info_t bufinfo;
     mp_get_buffer_raise(args[1], &bufinfo, MP_BUFFER_READ);
 
-    // convert the bytes to an integer
-    mp_uint_t value = 0;
-    for (const byte* buf = (const byte*)bufinfo.buf + bufinfo.len - 1; buf >= (byte*)bufinfo.buf; buf--) {
-        value = (value << 8) | *buf;
-    }
+    #if MICROPY_LONGINT_IMPL != MICROPY_LONGINT_IMPL_NONE
+    // If result guaranteedly fits in small int, use that
+    if (!MP_SMALL_INT_FITS(1 << (bufinfo.len * 8 - 1))) {
+        return mp_obj_int_from_bytes_impl(args[2] != MP_OBJ_NEW_QSTR(MP_QSTR_little), bufinfo.len, bufinfo.buf);
+    } else
+    #endif
+    {
+        const byte* buf = (const byte*)bufinfo.buf;
+        int delta = 1;
+        if (args[2] == MP_OBJ_NEW_QSTR(MP_QSTR_little)) {
+            buf += bufinfo.len - 1;
+            delta = -1;
+        }
 
-    return mp_obj_new_int_from_uint(value);
+        mp_uint_t value = 0;
+        for (; bufinfo.len--; buf += delta) {
+            value = (value << 8) | *buf;
+        }
+        return mp_obj_new_int_from_uint(value);
+    }
 }
 
 STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(int_from_bytes_fun_obj, 3, 4, int_from_bytes);
diff --git a/py/objint.h b/py/objint.h
index f418c329e9..a84a33fa50 100644
--- a/py/objint.h
+++ b/py/objint.h
@@ -59,6 +59,7 @@ char *mp_obj_int_formatted(char **buf, size_t *buf_size, size_t *fmt_size, mp_co
 char *mp_obj_int_formatted_impl(char **buf, size_t *buf_size, size_t *fmt_size, mp_const_obj_t self_in,
                                 int base, const char *prefix, char base_char, char comma);
 mp_int_t mp_obj_int_hash(mp_obj_t self_in);
+mp_obj_t mp_obj_int_from_bytes_impl(bool big_endian, size_t len, const byte *buf);
 void mp_obj_int_to_bytes_impl(mp_obj_t self_in, bool big_endian, size_t len, byte *buf);
 int mp_obj_int_sign(mp_obj_t self_in);
 mp_obj_t mp_obj_int_abs(mp_obj_t self_in);
diff --git a/py/objint_mpz.c b/py/objint_mpz.c
index 5ca2bf53b3..d465ef9650 100644
--- a/py/objint_mpz.c
+++ b/py/objint_mpz.c
@@ -107,6 +107,12 @@ char *mp_obj_int_formatted_impl(char **buf, size_t *buf_size, size_t *fmt_size,
     return str;
 }
 
+mp_obj_t mp_obj_int_from_bytes_impl(bool big_endian, size_t len, const byte *buf) {
+    mp_obj_int_t *o = mp_obj_int_new_mpz();
+    mpz_set_from_bytes(&o->mpz, big_endian, len, buf);
+    return MP_OBJ_FROM_PTR(o);
+}
+
 void mp_obj_int_to_bytes_impl(mp_obj_t self_in, bool big_endian, size_t len, byte *buf) {
     assert(MP_OBJ_IS_TYPE(self_in, &mp_type_int));
     mp_obj_int_t *self = MP_OBJ_TO_PTR(self_in);