]> git.lizzy.rs Git - plan9front.git/blob - sys/src/cmd/hgfs/hash.c
hgfs: honor x-bit in manifest
[plan9front.git] / sys / src / cmd / hgfs / hash.c
1 #include <u.h>
2 #include <libc.h>
3 #include <thread.h>
4 #include "dat.h"
5 #include "fns.h"
6
7 #include <mp.h>
8 #include <libsec.h>
9
10 int
11 Hfmt(Fmt *f)
12 {
13         uchar *p;
14
15         p = va_arg(f->args, uchar*);
16         return fmtprint(f, 
17                 "%.2x%.2x%.2x%.2x%.2x%.2x",
18                 p[0], p[1], p[2], p[3], p[4], p[5]);
19 }
20
21 int
22 fhash(int fd, uchar p1[], uchar p2[], uchar h[])
23 {
24         DigestState *ds;
25         uchar buf[BUFSZ];
26         int n;
27
28         ds = nil;
29         memset(h, 0, HASHSZ);
30         if(memcmp(p1, p2, HASHSZ) > 0){
31                 ds = sha1(p2, HASHSZ, nil, ds);
32                 sha1(p1, HASHSZ, nil, ds);
33         } else {
34                 ds = sha1(p1, HASHSZ, nil, ds);
35                 sha1(p2, HASHSZ, nil, ds);
36         }
37         while((n = read(fd, buf, BUFSZ)) > 0)
38                 sha1(buf, n, nil, ds);
39         sha1(buf, 0, h, ds);
40
41         return 0;
42 }
43
44 int
45 strhash(char *s, uchar *h)
46 {
47         uchar *b;
48         int n;
49
50         b = h;
51         memset(h, 0, HASHSZ);
52         n = HASHSZ*2;
53         while(*s && n > 0){
54                 if(*s >= '0' && *s <= '9')
55                         *h |= *s - '0';
56                 else if(*s >= 'a' && *s <= 'f')
57                         *h |= 10 + *s - 'a';
58                 else if(*s >= 'A' && *s <= 'F')
59                         *h |= 10 + *s - 'A';
60                 else
61                         break;
62                 if(n-- & 1)
63                         h++;
64                 else
65                         *h <<= 4;
66                 s++;
67         }
68         return h - b;
69 }