Skip to contentSkip to conversation

base85: Two tiny fixes

PATCH In review Andreas Gruenbacher 5 participants 11 messages Jun 15, 2016

lore
Share & tools
lore
#0 lore raw

While looking at the base85 code I found a bug in the debug code and an unnecessary call. You may want to have a look at the two fixes here:

http://www.kernel.org/pub/scm/linux/kernel/git/agruen/git.git

There is another little oddity in the way the de85 table is set up: 0 indicates an invalid entry; to avoid this from clashing with a valid entry, valid entries are incremented by one and decremented again while decoding. This leads to slightly worse code than using a negative number to indicate invalid values (and avoiding to increment/decrement).

Andreas

#1 lore raw
Quoted 4 lines · Andreas Gruenbacher

While looking at the base85 code I found a bug in the debug code and an unnecessary call. You may want to have a look at the two fixes here:

http://www.kernel.org/pub/scm/linux/kernel/git/agruen/git.git

ACK. Please post them to this list.

Quoted 5 lines

There is another little oddity in the way the de85 table is set up: 0 indicates an invalid entry; to avoid this from clashing with a valid entry, valid entries are incremented by one and decremented again while decoding. This leads to slightly worse code than using a negative number to indicate invalid values (and avoiding to increment/decrement).

You can make a patch to modify that as well if you wish. And in that case don't forget to make de85 explicitly signed as a char is unsigned by default on some platforms.

Nicolas

#2 lore raw

PATCH 1/3 base85 debug code: Fix length byte calculation

---
 base85.c |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
base85.c +1 -1
index b417a15..1d165d9 100644
@@ -118,7 +118,7 @@ int main(int ac, char **av)
 		int len = strlen(av[2]);
 		encode_85(buf, av[2], len);
 		if (len <= 26) len = len + 'A' - 1;
-		else len = len + 'a' - 26 + 1;
+		else len = len + 'a' - 26 - 1;
 		printf("encoded: %c%s\n", len, buf);
 		return 0;
 	}
Signature · 1 line
1.6.6.75.g37bae
#3 lore raw

PATCH 2/3 base85: No need to initialize the decode table in encode_85

In reply to Nicolas Pitre
---
 base85.c |    2 --
 1 files changed, 0 insertions(+), 2 deletions(-)
base85.c +0 -2
index 1d165d9..7204ce2 100644
@@ -84,8 +84,6 @@ int decode_85(char *dst, const char *buffer, int len)
 
 void encode_85(char *buf, const unsigned char *data, int bytes)
 {
-	prep_base85();
-
 	say("encode 85");
 	while (bytes) {
 		unsigned acc = 0;
Signature · 1 line
1.6.6.75.g37bae
#4 lore raw

Re: [PATCH 2/3] base85: No need to initialize the decode table in encode_85

Andreas Gruenbacher venit, vidit, dixit 08.01.2010 14:39:

Quoted 2 lines

---

For the less informed it may be worthwhile to have an explanation in the commit message why encode_85() does not need to initialize the table. (I strongly suspect it's a matter of de vs. en, i.e. "because it only encodes but does not decode."...)

Quoted 16 lines
 base85.c |    2 --
 1 files changed, 0 insertions(+), 2 deletions(-)
base85.c +0 -2
index 1d165d9..7204ce2 100644
@@ -84,8 +84,6 @@ int decode_85(char *dst, const char *buffer, int len)
 
 void encode_85(char *buf, const unsigned char *data, int bytes)
 {
-	prep_base85();
-
 	say("encode 85");
 	while (bytes) {
 		unsigned acc = 0;
#5 lore raw

Re: [PATCH 2/3] base85: No need to initialize the decode table in encode_85

Quoted 8 lines · Michael J Gruber

Andreas Gruenbacher venit, vidit, dixit 08.01.2010 14:39:

Quoted 2 lines

---

For the less informed it may be worthwhile to have an explanation in the commit message why encode_85() does not need to initialize the table. (I strongly suspect it's a matter of de vs. en, i.e. "because it only encodes but does not decode."...)

The title can be reworded to

base85: encode85() does not use the decode table

#6 lore raw

PATCH 1/1 base85: encode85() does not use the decode table

---
 base85.c |    2 --
 1 files changed, 0 insertions(+), 2 deletions(-)
base85.c +0 -2
index 1d165d9..7204ce2 100644
@@ -84,8 +84,6 @@ int decode_85(char *dst, const char *buffer, int len)
 
 void encode_85(char *buf, const unsigned char *data, int bytes)
 {
-	prep_base85();
-
 	say("encode 85");
 	while (bytes) {
 		unsigned acc = 0;
Signature · 1 line
1.6.6.75.g37bae
#7 lore raw

PATCH 1/1 base85: encode_85() does not use the decode table

In reply to Junio C Hamano
---
 base85.c |    2 --
 1 files changed, 0 insertions(+), 2 deletions(-)
base85.c +0 -2
index 1d165d9..7204ce2 100644
@@ -84,8 +84,6 @@ int decode_85(char *dst, const char *buffer, int len)
 
 void encode_85(char *buf, const unsigned char *data, int bytes)
 {
-	prep_base85();
-
 	say("encode 85");
 	while (bytes) {
 		unsigned acc = 0;
Signature · 1 line
1.6.6.75.g37bae
#8 lore raw

PATCH 3/3 base85: Make the code more obvious instead of explaining the non-obvious

In reply to Nicolas Pitre

Here is another cleanup ...

---
 base85.c |   10 ++--------
 1 files changed, 2 insertions(+), 8 deletions(-)
base85.c +2 -8
index 7204ce2..e459fee 100644
@@ -57,14 +57,8 @@ int decode_85(char *dst, const char *buffer, int len)
 		de = de85[ch];
 		if (--de < 0)
 			return error("invalid base85 alphabet %c", ch);
-		/*
-		 * Detect overflow.  The largest
-		 * 5-letter possible is "|NsC0" to
-		 * encode 0xffffffff, and "|NsC" gives
-		 * 0x03030303 at this point (i.e.
-		 * 0xffffffff = 0x03030303 * 85).
-		 */
-		if (0x03030303 < acc ||
+		/* Detect overflow. */
+		if (0xffffffff / 85 < acc ||
 		    0xffffffff - de < (acc *= 85))
 			return error("invalid base85 sequence %.5s", buffer-5);
 		acc += de;
Signature · 1 line
1.6.6.75.g37bae
#9 lore raw

Re: [PATCH 3/3] base85: Make the code more obvious instead of explaining the non-obvious

Quoted 2 lines · Andreas Gruenbacher

Here is another cleanup ...

I LOVE the subject line of this commit!

#10 lore raw
In reply to Nicolas Pitre
Quoted 1 line · On Thursday 07 January 2010 18:58:01 Nicolas Pitre wrote:

ACK. Please post them to this list.

Okay, done.

Quoted 8 lines · Andreas Gruenbacher
Quoted 5 lines

There is another little oddity in the way the de85 table is set up: 0 indicates an invalid entry; to avoid this from clashing with a valid entry, valid entries are incremented by one and decremented again while decoding. This leads to slightly worse code than using a negative number to indicate invalid values (and avoiding to increment/decrement).

You can make a patch to modify that as well if you wish.

Nah, it's not worth the noise.

Quoted 2 lines

And in that case don't forget to make de85 explicitly signed as a char is unsigned by default on some platforms.

I would have forgotten this; thanks for pointing it out!

Thanks,
Andreas