0
0
mirror of https://github.com/netwide-assembler/nasm.git synced 2025-09-22 10:43:39 -04:00

preproc: actually use uleb128 format for encoding macros

The format wasn't actually uleb128 because it was accidentally
bigendian (like UTF-8). That is just begging for confusion in the
future, if and when the uleb128 code gets librarized.

Fix it now.

Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
This commit is contained in:
H. Peter Anvin
2025-08-15 17:07:15 -07:00
parent 2bf3585547
commit 06f7ce2dba
2 changed files with 33 additions and 17 deletions

View File

@@ -1206,13 +1206,29 @@ static void inject_predefs(void)
do_predef = false;
}
static uint64_t get_uleb128(const char **pp)
{
const char *p = *pp;
unsigned int shcnt = 0;
uint8_t c;
uint64_t v = 0;
do {
c = *p++;
v += ((uint64_t)(c & 127)) << shcnt;
shcnt += 7;
} while (c & 128);
*pp = p;
return v;
}
static char *line_from_stdmac(void)
{
static const char *stdmacpos = NULL;
static char *stdmacbuf = NULL;
char *line;
size_t len = 0;
uint8_t c;
if (!stdmacpos || !*stdmacpos) {
macros_t *next = *stdmaclist;
@@ -1234,11 +1250,7 @@ static char *line_from_stdmac(void)
}
/* Length encoded using uleb128 encoding */
while ((c = *stdmacpos++) >= 128) {
len += c - 128;
len <<= 7;
}
len += c;
len = get_uleb128(&stdmacpos);
line = nasm_malloc(len + 1);
memcpy(line, stdmacpos, len);

View File

@@ -70,22 +70,26 @@ sub print_data($$) {
#
# Prefix a string with its length in uleb128 encoding
#
sub uleb128($)
{
my($n) = @_;
my $o = '';
do {
my $nn = $n >> 7;
$o .= pack('C', ($n & 127) | ($nn ? 128 : 0));
$n = $nn;
} while ($n);
return $o;
}
sub addstringlen($)
{
my($s) = @_;
my $l = length($s);
return '' if (!$l); # Drop empty line
my $lc = '';
for (my $shcnt = 0; $l >> $shcnt; $shcnt += 7) {
my $b = ($l >> $shcnt) & 127;
$b += 128 if ($shcnt);
$lc = pack('C', $b) . $lc;
}
return $lc.$s;
return $l ? uleb128($l).$s : '';
}
sub init_mac() {