+/* --- @mp_loadl2c@ --- *
+ *
+ * Arguments: @mp *d@ = destination
+ * @const void *pv@ = pointer to source data
+ * @size_t sz@ = size of the source data
+ *
+ * Returns: Resulting multiprecision number.
+ *
+ * Use: Loads a multiprecision number from an array of octets as
+ * two's complement. The first byte in the array is the least
+ * significant.
+ */
+
+mp *mp_loadl2c(mp *d, const void *pv, size_t sz)
+{
+ const octet *ov = pv;
+ MP_DEST(d, MPW_RQ(sz), MP_UNDEF);
+ if (!sz || !(ov[sz - 1] & 0x80)) {
+ mpx_loadl(d->v, d->vl, pv, sz);
+ d->f &= ~MP_NEG;
+ } else {
+ mpx_loadl2cn(d->v, d->vl, pv, sz);
+ d->f |= MP_NEG;
+ }
+ d->f &= ~MP_UNDEF;
+ mp_shrink(d);
+ return (d);
+}
+
+/* --- @mp_storel2c@ --- *
+ *
+ * Arguments: @const mp *m@ = source
+ * @void *pv@ = pointer to output array
+ * @size_t sz@ = size of the output array
+ *
+ * Returns: ---
+ *
+ * Use: Stores a multiprecision number in an array of octets as two's
+ * complement. The first byte in the array is the least
+ * significant. If the array is too small to represent the
+ * number, high-order bits are truncated; if the array is too
+ * large, high order bytes are sign-extended.
+ */
+
+void mp_storel2c(const mp *m, void *pv, size_t sz)
+{
+ if (m->f & MP_NEG)
+ mpx_storel2cn(m->v, m->vl, pv, sz);
+ else
+ mpx_storel(m->v, m->vl, pv, sz);
+}
+
+/* --- @mp_loadb2c@ --- *
+ *
+ * Arguments: @mp *d@ = destination
+ * @const void *pv@ = pointer to source data
+ * @size_t sz@ = size of the source data
+ *
+ * Returns: Resulting multiprecision number.
+ *
+ * Use: Loads a multiprecision number from an array of octets as
+ * two's complement. The last byte in the array is the least
+ * significant.
+ */
+
+mp *mp_loadb2c(mp *d, const void *pv, size_t sz)
+{
+ const octet *ov = pv;
+ MP_DEST(d, MPW_RQ(sz), MP_UNDEF);
+ if (!sz || !(ov[0] & 0x80)) {
+ mpx_loadl(d->v, d->vl, pv, sz);
+ d->f &= ~MP_NEG;
+ } else {
+ mpx_loadl2cn(d->v, d->vl, pv, sz);
+ d->f |= MP_NEG;
+ }
+ d->f &= ~MP_UNDEF;
+ mp_shrink(d);
+ return (d);
+}
+
+/* --- @mp_storeb2c@ --- *
+ *
+ * Arguments: @const mp *m@ = source
+ * @void *pv@ = pointer to output array
+ * @size_t sz@ = size of the output array
+ *
+ * Returns: ---
+ *
+ * Use: Stores a multiprecision number in an array of octets, as
+ * two's complement. The last byte in the array is the least
+ * significant. If the array is too small to represent the
+ * number, high-order bits are truncated; if the array is too
+ * large, high order bytes are sign-extended.
+ */
+
+void mp_storeb2c(const mp *m, void *pv, size_t sz)
+{
+ if (m->f & MP_NEG)
+ mpx_storeb2cn(m->v, m->vl, pv, sz);
+ else
+ mpx_storeb(m->v, m->vl, pv, sz);
+}
+