소스 검색

Better skipping/padding of zeros at beginnig

Stephen Paul Weber 16 년 전
부모
커밋
7a1892563a
1개의 변경된 파일과 21개의 추가작업 그리고 1개의 파일을 삭제
  1. 21 1
      mnencode.c

+ 21 - 1
mnencode.c

@@ -5,10 +5,30 @@
 #include <ctype.h>
 #include "mnemonic.h"
 
+int counthex(const mn_byte *s, size_t len) {
+	int total = 0;
+	size_t i;
+	for(i = 0; i < len; i++) {
+		if(isxdigit(s[i])) total++;
+	}
+	return total;
+}
+
+int countLeadingCrapAndZeros(const mn_byte *in, size_t inlen) {
+	size_t i;
+	for(i = 0; i < inlen; i++) {
+		if(in[i] != '0' && isxdigit(in[i])) break; /* Find first non-zero hex digit */
+	}
+	return i;
+}
+
 int hex2bytes(int inlen, const mn_byte *in, mn_byte *out) {
 	char t[3];
 	int i, j = 0, k = 0;
-	if(inlen % 2 != 0) {
+	i = countLeadingCrapAndZeros(in, inlen);
+	inlen = inlen - i;
+	in = in + i;
+	if(counthex(in, inlen) % 2 != 0) {
 		t[j++] = '0';
 	}
 	for(i = 0; i < inlen; i++) {