1 |
commit: 822d7ca7cb015470e7805f9888ba5255309819ba |
2 |
Author: Fabian Groffen <grobian <AT> gentoo <DOT> org> |
3 |
AuthorDate: Wed Nov 29 21:31:26 2017 +0000 |
4 |
Commit: Fabian Groffen <grobian <AT> gentoo <DOT> org> |
5 |
CommitDate: Wed Nov 29 21:31:26 2017 +0000 |
6 |
URL: https://gitweb.gentoo.org/repo/proj/prefix.git/commit/?id=822d7ca7 |
7 |
|
8 |
hashgen: improve layout.conf parsing, return default hashes if found |
9 |
|
10 |
scripts/rsync-generation/hashgen.c | 75 +++++++++++++++++++++----------------- |
11 |
1 file changed, 42 insertions(+), 33 deletions(-) |
12 |
|
13 |
diff --git a/scripts/rsync-generation/hashgen.c b/scripts/rsync-generation/hashgen.c |
14 |
index 25ae1db70e..79d26a9ae7 100644 |
15 |
--- a/scripts/rsync-generation/hashgen.c |
16 |
+++ b/scripts/rsync-generation/hashgen.c |
17 |
@@ -32,7 +32,8 @@ enum hash_impls { |
18 |
}; |
19 |
/* default changed from sha256, sha512, whirlpool |
20 |
* to blake2b, sha512 on 2017-11-21 */ |
21 |
-static int hashes = HASH_BLAKE2B | HASH_SHA512; |
22 |
+#define HASH_DEFAULT (HASH_BLAKE2B | HASH_SHA512); |
23 |
+static int hashes = HASH_DEFAULT; |
24 |
|
25 |
static inline void |
26 |
hex_hash(char *out, const unsigned char *buf, const int length) |
27 |
@@ -196,6 +197,7 @@ parse_layout_conf(const char *path) |
28 |
char *q; |
29 |
char *tok; |
30 |
char *last_nl; |
31 |
+ char *start; |
32 |
int ret = 0; |
33 |
|
34 |
if ((f = fopen(path, "r")) == NULL) |
35 |
@@ -205,57 +207,64 @@ parse_layout_conf(const char *path) |
36 |
* if the file doesn't end with a newline, the final bit is ignored */ |
37 |
while ((sz = fread(buf + len, 1, sizeof(buf) - len, f)) > 0) { |
38 |
len += sz; |
39 |
+ start = buf; |
40 |
last_nl = NULL; |
41 |
for (p = buf; p - buf < len; p++) { |
42 |
if (*p == '\n') { |
43 |
+ if (last_nl != NULL) |
44 |
+ start = last_nl + 1; |
45 |
last_nl = p; |
46 |
- sz = strlen("manifest-hashes"); |
47 |
- if (strncmp(buf, "manifest-hashes", sz)) |
48 |
- continue; |
49 |
- if ((q = strchr(buf + sz, '=')) == NULL) |
50 |
- continue; |
51 |
- q++; |
52 |
- while (isspace((int)*q)) |
53 |
- q++; |
54 |
- /* parse the tokens, whitespace separated */ |
55 |
- tok = q; |
56 |
do { |
57 |
- while (!isspace((int)*q)) |
58 |
- q++; |
59 |
- sz = q - tok; |
60 |
- if (strncmp(tok, "SHA256", sz) == 0) { |
61 |
- ret |= HASH_SHA256; |
62 |
- } else if (strncmp(tok, "SHA512", sz) == 0) { |
63 |
- ret |= HASH_SHA512; |
64 |
- } else if (strncmp(tok, "WHIRLPOOL", sz) == 0) { |
65 |
- ret |= HASH_WHIRLPOOL; |
66 |
- } else if (strncmp(tok, "BLAKE2B", sz) == 0) { |
67 |
- ret |= HASH_BLAKE2B; |
68 |
- } else { |
69 |
- fprintf(stderr, "warning: unsupported hash from " |
70 |
- "layout.conf: %.*s\n", (int)sz, tok); |
71 |
- } |
72 |
- while (isspace((int)*q) && *q != '\n') |
73 |
+ sz = strlen("manifest-hashes"); |
74 |
+ if (strncmp(start, "manifest-hashes", sz)) |
75 |
+ break; |
76 |
+ if ((q = strchr(start + sz, '=')) == NULL) |
77 |
+ break; |
78 |
+ q++; |
79 |
+ while (isspace((int)*q)) |
80 |
q++; |
81 |
+ /* parse the tokens, whitespace separated */ |
82 |
tok = q; |
83 |
- } while (*q != '\n'); |
84 |
- /* got it, expect only once, so stop processing */ |
85 |
- fclose(f); |
86 |
- return ret; |
87 |
+ do { |
88 |
+ while (!isspace((int)*q)) |
89 |
+ q++; |
90 |
+ sz = q - tok; |
91 |
+ if (strncmp(tok, "SHA256", sz) == 0) { |
92 |
+ ret |= HASH_SHA256; |
93 |
+ } else if (strncmp(tok, "SHA512", sz) == 0) { |
94 |
+ ret |= HASH_SHA512; |
95 |
+ } else if (strncmp(tok, "WHIRLPOOL", sz) == 0) { |
96 |
+ ret |= HASH_WHIRLPOOL; |
97 |
+ } else if (strncmp(tok, "BLAKE2B", sz) == 0) { |
98 |
+ ret |= HASH_BLAKE2B; |
99 |
+ } else { |
100 |
+ fprintf(stderr, "warning: unsupported hash from " |
101 |
+ "layout.conf: %.*s\n", (int)sz, tok); |
102 |
+ } |
103 |
+ while (isspace((int)*q) && *q != '\n') |
104 |
+ q++; |
105 |
+ tok = q; |
106 |
+ } while (*q != '\n'); |
107 |
+ /* got it, expect only once, so stop processing */ |
108 |
+ fclose(f); |
109 |
+ return ret; |
110 |
+ } while (0); |
111 |
} |
112 |
} |
113 |
if (last_nl != NULL) { |
114 |
last_nl++; /* skip \n */ |
115 |
len = last_nl - buf; |
116 |
memmove(buf, last_nl, len); |
117 |
+ last_nl = buf; |
118 |
} else { |
119 |
- /* too long line, just skip */ |
120 |
+ /* skip too long line */ |
121 |
len = 0; |
122 |
} |
123 |
} |
124 |
|
125 |
fclose(f); |
126 |
- return 0; |
127 |
+ /* if we didn't find anything, return the default set */ |
128 |
+ return HASH_DEFAULT; |
129 |
} |
130 |
|
131 |
static char *str_manifest = "Manifest"; |