+/* --- @mpx_storel2cn@ --- *
+ *
+ * Arguments: @const mpw *v, *vl@ = base and limit of source vector
+ * @void *pp@ = pointer to octet array
+ * @size_t sz@ = size of octet array
+ *
+ * Returns: ---
+ *
+ * Use: Stores a negative MP in an octet array, least significant
+ * octet first, as two's complement. High-end octets are
+ * silently discarded if there isn't enough space for them.
+ * This obviously makes the output bad.
+ */
+
+void mpx_storel2cn(const mpw *v, const mpw *vl, void *pp, size_t sz)
+{
+ unsigned c = 1;
+ unsigned b = 0;
+ mpw n, w = 0;
+ octet *p = pp, *q = p + sz;
+ unsigned bits = 0;
+
+ while (p < q) {
+ if (bits < 8) {
+ if (v >= vl) {
+ b = w;
+ break;
+ }
+ n = *v++;
+ b = w | n << bits;
+ w = n >> (8 - bits);
+ bits += MPW_BITS - 8;
+ } else {
+ b = w;
+ w >>= 8;
+ bits -= 8;
+ }
+ b = U8(~b + c);
+ c = c && !b;
+ *p++ = b;
+ }
+ while (p < q) {
+ b = U8(~b + c);
+ c = c && !b;
+ *p++ = b;
+ b = 0;
+ }
+}
+
+/* --- @mpx_loadl2cn@ --- *
+ *
+ * Arguments: @mpw *v, *vl@ = base and limit of destination vector
+ * @const void *pp@ = pointer to octet array
+ * @size_t sz@ = size of octet array
+ *
+ * Returns: ---
+ *
+ * Use: Loads a negative MP in an octet array, least significant
+ * octet first, as two's complement. High-end octets are
+ * ignored if there isn't enough space for them. This probably
+ * means you made the wrong choice coming here.
+ */
+
+void mpx_loadl2cn(mpw *v, mpw *vl, const void *pp, size_t sz)
+{
+ unsigned n;
+ unsigned c = 1;
+ mpw w = 0;
+ const octet *p = pp, *q = p + sz;
+ unsigned bits = 0;
+
+ if (v >= vl)
+ return;
+ while (p < q) {
+ n = U8(~(*p++) + c);
+ c = c && !n;
+ w |= n << bits;
+ bits += 8;
+ if (bits >= MPW_BITS) {
+ *v++ = MPW(w);
+ w = n >> (MPW_BITS - bits + 8);
+ bits -= MPW_BITS;
+ if (v >= vl)
+ return;
+ }
+ }
+ *v++ = w;
+ MPX_ZERO(v, vl);
+}
+
+/* --- @mpx_storeb2cn@ --- *
+ *
+ * Arguments: @const mpw *v, *vl@ = base and limit of source vector
+ * @void *pp@ = pointer to octet array
+ * @size_t sz@ = size of octet array
+ *
+ * Returns: ---
+ *
+ * Use: Stores a negative MP in an octet array, most significant
+ * octet first, as two's complement. High-end octets are
+ * silently discarded if there isn't enough space for them,
+ * which probably isn't what you meant.
+ */
+
+void mpx_storeb2cn(const mpw *v, const mpw *vl, void *pp, size_t sz)
+{
+ mpw n, w = 0;
+ unsigned b = 0;
+ unsigned c = 1;
+ octet *p = pp, *q = p + sz;
+ unsigned bits = 0;
+
+ while (q > p) {
+ if (bits < 8) {
+ if (v >= vl) {
+ b = w;
+ break;
+ }
+ n = *v++;
+ b = w | n << bits;
+ w = n >> (8 - bits);
+ bits += MPW_BITS - 8;
+ } else {
+ b = w;
+ w >>= 8;
+ bits -= 8;
+ }
+ b = U8(~b + c);
+ c = c && !b;
+ *--q = b;
+ }
+ while (q > p) {
+ b = ~b + c;
+ c = c && !(b & 0xff);
+ *--q = b;
+ b = 0;
+ }
+}
+
+/* --- @mpx_loadb2cn@ --- *
+ *
+ * Arguments: @mpw *v, *vl@ = base and limit of destination vector
+ * @const void *pp@ = pointer to octet array
+ * @size_t sz@ = size of octet array
+ *
+ * Returns: ---
+ *
+ * Use: Loads a negative MP in an octet array, most significant octet
+ * first as two's complement. High-end octets are ignored if
+ * there isn't enough space for them. This probably means you
+ * chose this function wrongly.
+ */
+
+void mpx_loadb2cn(mpw *v, mpw *vl, const void *pp, size_t sz)
+{
+ unsigned n;
+ unsigned c = 1;
+ mpw w = 0;
+ const octet *p = pp, *q = p + sz;
+ unsigned bits = 0;
+
+ if (v >= vl)
+ return;
+ while (q > p) {
+ n = U8(~(*--q) + c);
+ c = c && !n;
+ w |= n << bits;
+ bits += 8;
+ if (bits >= MPW_BITS) {
+ *v++ = MPW(w);
+ w = n >> (MPW_BITS - bits + 8);
+ bits -= MPW_BITS;
+ if (v >= vl)
+ return;
+ }
+ }
+ *v++ = w;
+ MPX_ZERO(v, vl);
+}
+