aboutsummaryrefslogtreecommitdiffstats
path: root/c/sha3sum.c
diff options
context:
space:
mode:
authorMattias Andrée <maandree@operamail.com>2013-02-07 20:15:30 +0100
committerMattias Andrée <maandree@operamail.com>2013-02-07 20:15:30 +0100
commitf69073bfe9b8566e456c9e1771db9da9ddd7182d (patch)
tree3e3d320b609b5f60aed0521735326387242d145c /c/sha3sum.c
parentresolve pedantic warnings (diff)
downloadsha3sum-f69073bfe9b8566e456c9e1771db9da9ddd7182d.tar.gz
sha3sum-f69073bfe9b8566e456c9e1771db9da9ddd7182d.tar.bz2
sha3sum-f69073bfe9b8566e456c9e1771db9da9ddd7182d.tar.xz
error in java version detected (c is identical) + c implemention other wise complete but not optimised (and misses blksize reading)
Signed-off-by: Mattias Andrée <maandree@operamail.com>
Diffstat (limited to 'c/sha3sum.c')
-rw-r--r--c/sha3sum.c37
1 files changed, 29 insertions, 8 deletions
diff --git a/c/sha3sum.c b/c/sha3sum.c
index 4e2198b..993cf94 100644
--- a/c/sha3sum.c
+++ b/c/sha3sum.c
@@ -26,6 +26,8 @@
#define true 1
#define null 0
+#define HEXADECA "0123456789ABCDEF"
+
/**
* String equality comparator
@@ -53,8 +55,8 @@ long parseInt(char* str)
{
long rc = 0;
while (*str)
- rc = rc * 10 - (*str & 15);
- return rc;
+ rc = rc * 10 - (*str++ & 15);
+ return -rc;
}
@@ -77,6 +79,8 @@ int main(int argc, char** argv)
long a = 0, an = argc - 1;
char** args = argv + 1;
+ *linger = 0;
+
s = -1;
for (i = 0; *(cmd + i); i++)
@@ -162,6 +166,10 @@ int main(int argc, char** argv)
printf("\n");
fflush(stdout);
fflush(stderr);
+ if (freelinger)
+ free(*linger);
+ free(linger);
+ free(files);
return 2;
}
else
@@ -188,12 +196,16 @@ int main(int argc, char** argv)
fprintf(stderr, "%s: unrecognised option: %s\n", cmd, *linger);
fflush(stdout);
fflush(stderr);
+ if (freelinger)
+ free(*linger);
+ free(linger);
+ free(files);
return 1;
}
}
if (freelinger)
free(*linger);
- freelinger = true;
+ freelinger = false;
*linger = null;
if (arg == null)
continue;
@@ -245,7 +257,7 @@ int main(int argc, char** argv)
{
char* _ = (char*)malloc(3);
*_++ = '-'; *_++ = *arg; *_ = 0;
- linger[0] = _ - 3;
+ linger[0] = _ - 2;
}
{
long _ = 0;
@@ -269,6 +281,7 @@ int main(int argc, char** argv)
fprintf(stderr, "%s: sorry, I will only do at least one iteration!\n", cmd);
fflush(stdout);
fflush(stderr);
+ free(files);
return 3;
}
@@ -301,7 +314,7 @@ int main(int argc, char** argv)
initialise(r, c, o);
blksize = 4096; /** XXX os.stat(os.path.realpath(fn)).st_size; **/
- chunk = malloc(blksize);
+ chunk = (char*)malloc(blksize);
for (;;)
{
long read = fread(chunk, 1, blksize, file);
@@ -311,11 +324,15 @@ int main(int argc, char** argv)
}
free(chunk);
bs = digest(null, 0);
+ dispose();
bn = (o + 7) >> 3;
for (_ = 1; _ < i; _++)
{
+ char* _ = bs;
initialise(r, c, o);
bs = digest(bs, bn);
+ free(_);
+ dispose();
}
if (binary)
@@ -340,8 +357,9 @@ int main(int argc, char** argv)
rc = malloc((bn << 1) + 3 + (filename == null ? 1 : 0) + flen);
for (b = 0; b < bn; b++)
{
- *(rc + rcptr++) = "0123456789ABCDEF"[(bs[b] >> 4) & 15];
- *(rc + rcptr++) = "0123456789ABCDEF"[bs[b] & 15];
+ char v = bs[b];
+ *(rc + rcptr++) = HEXADECA[(v >> 4) & 15];
+ *(rc + rcptr++) = HEXADECA[v & 15];
}
*(rc + rcptr++) = ' ';
if (filename == null)
@@ -373,7 +391,10 @@ int main(int argc, char** argv)
fflush(stdout);
fflush(stderr);
if (fail)
- return 5;
+ {
+ free(files);
+ return 5;
+ }
}
free(files);