Commit 680a47550f03c03f58d7899f66c7dddbe463c38e

Con Kolivas 2014-03-18T19:17:57

Fix ser_number for no remaining val byte.

diff --git a/cgminer.c b/cgminer.c
index 338f427..557f577 100644
--- a/cgminer.c
+++ b/cgminer.c
@@ -2286,13 +2286,9 @@ static bool gbt_solo_decode(struct pool *pool, json_t *res_val)
 	memset(pool->scriptsig_base, 0, 42);
 	pool->scriptsig_base[ofs++] = 49; // Template is 49 bytes
 
-	/* Put block height at start of template. Pad to 9 == size of int64_t */
-	len = ser_number(pool->scriptsig_base + ofs, height);
-	ofs += len;
-	len = 9 - len - 1;
-	if (len)
-		pool->scriptsig_base[ofs++] = len;
-	ofs += len;
+	/* Put block height at start of template. */
+	ser_number(pool->scriptsig_base + ofs, height);
+	ofs += 9;
 
 	/* Followed by flags */
 	pool->scriptsig_base[ofs++] = 7; // Flags length should be 7
diff --git a/util.c b/util.c
index 27cd8f8..0f1bb76 100644
--- a/util.c
+++ b/util.c
@@ -719,19 +719,23 @@ void address_to_pubkeyhash(unsigned char *pkh, const char *addr)
 	pkh[24] = 0xac;
 }
 
-/*  For encoding nHeight into coinbase */
-int ser_number(unsigned char *s, int64_t val)
-{
-	int i = 1;
-
-	s[0] = i;
-	while (val > 127) {
-		s[i] = val % 256;
-		s[0] = ++i;
-		val /= 256;
-	}
-	s[++i] = val;
-	return i;
+/*  For encoding nHeight into coinbase, pad out to 9 bytes */
+void ser_number(unsigned char *s, int64_t val)
+{
+	int64_t *i64 = (int64_t *)&s[1];
+	int len;
+
+	if (val < 128)
+		len = 1;
+	else if (val < 16512)
+		len = 2;
+	else if (val < 2113664)
+		len = 3;
+	else
+		len = 4;
+	*i64 = htole64(val);
+	s[0] = len++;
+	s[len] = 9 - len;
 }
 
 /* For encoding variable length strings */
diff --git a/util.h b/util.h
index 9b5d334..6df1ee9 100644
--- a/util.h
+++ b/util.h
@@ -108,7 +108,7 @@ enum dev_reason;
 struct cgpu_info;
 void b58tobin(unsigned char *b58bin, const char *b58);
 void address_to_pubkeyhash(unsigned char *pkh, const char *addr);
-int ser_number(unsigned char *s, int64_t val);
+void ser_number(unsigned char *s, int64_t val);
 unsigned char *ser_string(char *s, int *slen);
 int thr_info_create(struct thr_info *thr, pthread_attr_t *attr, void *(*start) (void *), void *arg);
 void thr_info_cancel(struct thr_info *thr);