2 * Dolda Connect - Modular multiuser Direct Connect-style client
3 * Copyright (C) 2004 Fredrik Tolf <fredrik@dolda2000.com>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 /* This frankenfile is built from bits and pieces of the daemon. The
21 * copying and pasting is very ugly, but it doesn't *really*
40 static char *base64enc2(char *data, size_t datalen)
42 static char *res = NULL;
46 res = base64encode(data, datalen);
50 static char *base64dec2(char *data, size_t *datalen)
52 static char *res = NULL;
56 res = base64decode(data, datalen);
60 int main(int argc, char **argv)
67 struct tigertreehash tth;
80 while((c = getopt(argc, argv, "phf456F:s:")) != -1) {
103 fprintf(stderr, "usage: tigersum [-h456] FILE...\n");
104 fprintf(stderr, " tigersum [-h456] [-F OUTFD] [-s STATEFILE] -f\n");
105 exit((c == 'h')?0:1);
110 if(statefile != NULL)
112 if(((state = fopen(statefile, "r")) == NULL) && (errno != ENOENT)) {
113 fprintf(stderr, "tigersum: %s: %s\n", statefile, strerror(errno));
117 if(fgets(buf, sizeof(buf), state) == NULL) {
118 fprintf(stderr, "tigersum: %s: could not read entire state\n", statefile);
121 tth.blocks = atoi(buf);
122 if(fgets(buf, sizeof(buf), state) == NULL) {
123 fprintf(stderr, "tigersum: %s: could not read entire state\n", statefile);
126 tth.depth = atoi(buf);
127 for(i = 0; i < tth.depth; i++) {
128 if(fgets(buf, sizeof(buf), state) == NULL) {
129 fprintf(stderr, "tigersum: %s: could not read entire state\n", statefile);
132 dec = base64dec2(buf, &size);
134 fprintf(stderr, "tigersum: %s: illegal state\n", statefile);
137 memcpy(tth.stack[i], dec, 24);
139 if(fgets(buf, sizeof(buf), state) == NULL) {
140 fprintf(stderr, "tigersum: %s: could not read entire state\n", statefile);
143 tth.offset = atoi(buf);
144 if(fgets(buf, sizeof(buf), state) == NULL) {
145 fprintf(stderr, "tigersum: %s: could not read entire state\n", statefile);
148 dec = base64dec2(buf, &size);
149 if(size != tth.offset) {
150 fprintf(stderr, "tigersum: %s: illegal state\n", statefile);
153 memcpy(&tth.block, dec, tth.offset);
159 ret = read(0, buf, sizeof(buf));
161 perror("tigersum: read");
167 for(len2 = 0; len2 < len; len2 += ret) {
168 if((ret = write(1, buf, ret)) < 0) {
169 perror("tigersum: write");
173 dotigertree(&tth, buf, len);
175 if(statefile != NULL) {
176 if((state = fopen(statefile, "w")) == NULL) {
177 fprintf(stderr, "tigersum: %s: %s\n", statefile, strerror(errno));
180 fprintf(state, "%i\n", tth.blocks);
181 fprintf(state, "%i\n", tth.depth);
182 for(i = 0; i < tth.depth; i++) {
183 enc = base64enc2(tth.stack[i], 24);
184 fprintf(state, "%s\n", enc);
186 fprintf(state, "%i\n", tth.offset);
187 enc = base64enc2(tth.block, tth.offset);
193 restigertree(&tth, res);
195 enc = hexencode(res, 24);
197 enc = base32encode(res, 24);
199 enc = base64encode(res, 24);
200 for(len = 0; len < strlen(enc); len += ret) {
201 if((ret = write(outfd, enc + len, strlen(enc) - len)) < 0) {
202 perror("tigersum: output");
207 write(outfd, "\n", 1);
209 for(i = optind; i < argc; i++) {
210 if(!strcmp(argv[i], "-")) {
213 if((fd = open(argv[i], O_RDONLY)) < 0) {
214 fprintf(stderr, "tigersum: %s: %s\n", argv[i], strerror(errno));
220 if(!S_ISREG(sb.st_mode))
226 ret = read(fd, buf, sizeof(buf));
228 perror("tigersum: read");
232 if((bytes == 0) || ((bytes & ~0xFFFFF) != ((bytes + ret) & ~0xFFFFF))) {
234 fprintf(stderr, "\033[1G");
235 if(argc - optind > 1)
236 fprintf(stderr, "%s: ", argv[i]);
238 fprintf(stderr, "%i", bytes);
240 fprintf(stderr, "%i%%", (int)(((float)bytes / (float)sb.st_size) * 100.0));
242 fprintf(stderr, "\033[K");
250 dotigertree(&tth, buf, ret);
253 fprintf(stderr, "\n");
255 restigertree(&tth, res);
257 enc = hexencode(res, 24);
259 enc = base32encode(res, 24);
261 enc = base64encode(res, 24);
262 if(argc - optind > 1)
263 printf("%s %s\n", enc, argv[i]);