lnsocket

A minimal C library for connecting to the lightning network
git clone git://jb55.com/lnsocket
Log | Files | Refs | Submodules | README | LICENSE

bigsize.c (3205B)


      1 /* Copyright Rusty Russell (Blockstream) 2015.
      2 	     William Casarin 2022
      3 
      4 Permission is hereby granted, free of charge, to any person obtaining a copy
      5 of this software and associated documentation files (the "Software"), to deal
      6 in the Software without restriction, including without limitation the rights
      7 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
      8 copies of the Software, and to permit persons to whom the Software is
      9 furnished to do so, subject to the following conditions:
     10 
     11 The above copyright notice and this permission notice shall be included in
     12 all copies or substantial portions of the Software.
     13 
     14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
     15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
     17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
     18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
     19 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
     20 THE SOFTWARE.
     21 */
     22 
     23 
     24 #include <assert.h>
     25 #include "config.h"
     26 #include "bigsize.h"
     27 
     28 #ifndef SUPERVERBOSE
     29 #define SUPERVERBOSE(...)
     30 #endif
     31 
     32 size_t bigsize_len(bigsize_t v)
     33 {
     34 	if (v < 0xfd) {
     35 		return 1;
     36 	} else if (v <= 0xffff) {
     37 		return 3;
     38 	} else if (v <= 0xffffffff) {
     39 		return 5;
     40 	} else {
     41 		return 9;
     42 	}
     43 }
     44 
     45 size_t bigsize_put(u8 buf[BIGSIZE_MAX_LEN], bigsize_t v)
     46 {
     47 	u8 *p = buf;
     48 
     49 	if (v < 0xfd) {
     50 		*(p++) = v;
     51 	} else if (v <= 0xffff) {
     52 		(*p++) = 0xfd;
     53 		(*p++) = v >> 8;
     54 		(*p++) = v;
     55 	} else if (v <= 0xffffffff) {
     56 		(*p++) = 0xfe;
     57 		(*p++) = v >> 24;
     58 		(*p++) = v >> 16;
     59 		(*p++) = v >> 8;
     60 		(*p++) = v;
     61 	} else {
     62 		(*p++) = 0xff;
     63 		(*p++) = v >> 56;
     64 		(*p++) = v >> 48;
     65 		(*p++) = v >> 40;
     66 		(*p++) = v >> 32;
     67 		(*p++) = v >> 24;
     68 		(*p++) = v >> 16;
     69 		(*p++) = v >> 8;
     70 		(*p++) = v;
     71 	}
     72 	return p - buf;
     73 }
     74 
     75 size_t bigsize_get(const u8 *p, size_t max, bigsize_t *val)
     76 {
     77 	if (max < 1) {
     78 		SUPERVERBOSE("EOF");
     79 		return 0;
     80 	}
     81 
     82 	switch (*p) {
     83 	case 0xfd:
     84 		if (max < 3) {
     85 			SUPERVERBOSE("unexpected EOF");
     86 			return 0;
     87 		}
     88 		*val = ((u64)p[1] << 8) + p[2];
     89 		if (*val < 0xfd) {
     90 			SUPERVERBOSE("decoded bigsize is not canonical");
     91 			return 0;
     92 		}
     93 		return 3;
     94 	case 0xfe:
     95 		if (max < 5) {
     96 			SUPERVERBOSE("unexpected EOF");
     97 			return 0;
     98 		}
     99 		*val = ((u64)p[1] << 24) + ((u64)p[2] << 16)
    100 			+ ((u64)p[3] << 8) + p[4];
    101 		if ((*val >> 16) == 0) {
    102 			SUPERVERBOSE("decoded bigsize is not canonical");
    103 			return 0;
    104 		}
    105 		return 5;
    106 	case 0xff:
    107 		if (max < 9) {
    108 			SUPERVERBOSE("unexpected EOF");
    109 			return 0;
    110 		}
    111 		*val = ((u64)p[1] << 56) + ((u64)p[2] << 48)
    112 			+ ((u64)p[3] << 40) + ((u64)p[4] << 32)
    113 			+ ((u64)p[5] << 24) + ((u64)p[6] << 16)
    114 			+ ((u64)p[7] << 8) + p[8];
    115 		if ((*val >> 32) == 0) {
    116 			SUPERVERBOSE("decoded bigsize is not canonical");
    117 			return 0;
    118 		}
    119 		return 9;
    120 	default:
    121 		*val = *p;
    122 		return 1;
    123 	}
    124 }
    125 
    126 int cursor_pull_bigsize(struct cursor *cur, bigsize_t *out)
    127 {
    128 	return bigsize_get(cur->p, cur->end - cur->p, out);
    129 }
    130 
    131 int cursor_push_bigsize(struct cursor *cur, const bigsize_t val)
    132 {
    133 	u8 buf[BIGSIZE_MAX_LEN];
    134 	size_t len;
    135 
    136 	len = bigsize_put(buf, val);
    137 
    138 	return cursor_push(cur, buf, len);
    139 }
    140