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)
66 struct tigertreehash tth;
79 while((c = getopt(argc, argv, "phf456F:s:")) != -1) {
102 fprintf(stderr, "usage: tigersum [-h456] FILE...\n");
103 fprintf(stderr, " tigersum [-h456] [-F OUTFD] [-s STATEFILE] -f\n");
104 exit((c == 'h')?0:1);
109 if(statefile != NULL)
111 if(((state = fopen(statefile, "r")) == NULL) && (errno != ENOENT)) {
112 fprintf(stderr, "tigersum: %s: %s\n", statefile, strerror(errno));
116 if(fgets(buf, sizeof(buf), state) == NULL) {
117 fprintf(stderr, "tigersum: %s: could not read entire state\n", statefile);
120 tth.blocks = atoi(buf);
121 if(fgets(buf, sizeof(buf), state) == NULL) {
122 fprintf(stderr, "tigersum: %s: could not read entire state\n", statefile);
125 tth.depth = atoi(buf);
126 for(i = 0; i < tth.depth; i++) {
127 if(fgets(buf, sizeof(buf), state) == NULL) {
128 fprintf(stderr, "tigersum: %s: could not read entire state\n", statefile);
131 dec = base64dec2(buf, &ret);
133 fprintf(stderr, "tigersum: %s: illegal state\n", statefile);
136 memcpy(tth.stack[i], dec, 24);
138 if(fgets(buf, sizeof(buf), state) == NULL) {
139 fprintf(stderr, "tigersum: %s: could not read entire state\n", statefile);
142 tth.offset = atoi(buf);
143 if(fgets(buf, sizeof(buf), state) == NULL) {
144 fprintf(stderr, "tigersum: %s: could not read entire state\n", statefile);
147 dec = base64dec2(buf, &ret);
148 if(ret != tth.offset) {
149 fprintf(stderr, "tigersum: %s: illegal state\n", statefile);
152 memcpy(&tth.block, dec, tth.offset);
158 ret = read(0, buf, sizeof(buf));
160 perror("tigersum: read");
166 for(len2 = 0; len2 < len; len2 += ret) {
167 if((ret = write(1, buf, ret)) < 0) {
168 perror("tigersum: write");
172 dotigertree(&tth, buf, len);
174 if(statefile != NULL) {
175 if((state = fopen(statefile, "w")) == NULL) {
176 fprintf(stderr, "tigersum: %s: %s\n", statefile, strerror(errno));
179 fprintf(state, "%i\n", tth.blocks);
180 fprintf(state, "%i\n", tth.depth);
181 for(i = 0; i < tth.depth; i++) {
182 enc = base64enc2(tth.stack[i], 24);
183 fprintf(state, "%s\n", enc);
185 fprintf(state, "%i\n", tth.offset);
186 enc = base64enc2(tth.block, tth.offset);
192 restigertree(&tth, res);
194 enc = hexencode(res, 24);
196 enc = base32encode(res, 24);
198 enc = base64encode(res, 24);
199 for(len = 0; len < strlen(enc); len += ret) {
200 if((ret = write(outfd, enc + len, strlen(enc) - len)) < 0) {
201 perror("tigersum: output");
206 write(outfd, "\n", 1);
208 for(i = optind; i < argc; i++) {
209 if(!strcmp(argv[i], "-")) {
212 if((fd = open(argv[i], O_RDONLY)) < 0) {
213 fprintf(stderr, "tigersum: %s: %s\n", argv[i], strerror(errno));
219 if(!S_ISREG(sb.st_mode))
225 ret = read(fd, buf, sizeof(buf));
227 perror("tigersum: read");
231 if((bytes == 0) || ((bytes & ~0xFFFFF) != ((bytes + ret) & ~0xFFFFF))) {
233 fprintf(stderr, "\033[1G");
234 if(argc - optind > 1)
235 fprintf(stderr, "%s: ", argv[i]);
237 fprintf(stderr, "%i", bytes);
239 fprintf(stderr, "%i%%", (int)(((float)bytes / (float)sb.st_size) * 100.0));
241 fprintf(stderr, "\033[K");
249 dotigertree(&tth, buf, ret);
252 fprintf(stderr, "\n");
254 restigertree(&tth, res);
256 enc = hexencode(res, 24);
258 enc = base32encode(res, 24);
260 enc = base64encode(res, 24);
261 if(argc - optind > 1)
262 printf("%s %s\n", enc, argv[i]);