diff options
Diffstat (limited to 'misc/metainfo.c')
| -rw-r--r-- | misc/metainfo.c | 200 |
1 files changed, 100 insertions, 100 deletions
diff --git a/misc/metainfo.c b/misc/metainfo.c index 61da727..261f60a 100644 --- a/misc/metainfo.c +++ b/misc/metainfo.c @@ -44,7 +44,7 @@ print_metainfo(struct metainfo *tp) printf("Info hash: "); for (i = 0; i < 20; i++) - printf("%.2x", tp->info_hash[i]); + printf("%.2x", tp->info_hash[i]); printf("\n"); printf("Tracker URL: %s\n", tp->announce); printf("Piece length: %" PRId64 "\n", (int64_t)tp->piece_length); @@ -53,8 +53,8 @@ print_metainfo(struct metainfo *tp) printf("Advisory name: %s\n", tp->name); printf("Files:\n"); for (i = 0; i < tp->nfiles; i++) { - printf("%s (%" PRId64 ")\n", - tp->files[i].path, (int64_t)tp->files[i].length); + printf("%s (%" PRId64 ")\n", + tp->files[i].path, (int64_t)tp->files[i].length); } printf("Total length: %" PRId64 "\n\n", (int64_t)tp->total_length); } @@ -63,13 +63,13 @@ static int check_path(const char *path, size_t len) { if (len == 0) - return 0; + return 0; else if (len == 1 && path[0] == '.') - return 0; + return 0; else if (len == 2 && path[0] == '.' && path[1] == '.') - return 0; + return 0; else if (memchr(path, '/', len) != NULL) - return 0; + return 0; return 1; } @@ -81,27 +81,27 @@ fill_fileinfo(const char *fdct, struct fileinfo *tfp) const char *plst, *iter, *str; if ((err = benc_dget_off(fdct, "length", &tfp->length)) != 0) - return err; + return err; if ((err = benc_dget_lst(fdct, "path", &plst)) != 0) - return err; + return err; npath = plen = 0; iter = benc_first(plst); while (iter != NULL) { - if (!benc_isstr(iter)) - return EINVAL; - benc_str(iter, &str, &len, &iter); - if (!check_path(str, len)) - return EINVAL; - npath++; - plen += len; + if (!benc_isstr(iter)) + return EINVAL; + benc_str(iter, &str, &len, &iter); + if (!check_path(str, len)) + return EINVAL; + npath++; + plen += len; } if (npath == 0) - return EINVAL; + return EINVAL; if ((tfp->path = malloc(plen + (npath - 1) + 1)) == NULL) - return ENOMEM; + return ENOMEM; iter = benc_first(plst); benc_str(iter, &str, &len, &iter); @@ -109,11 +109,11 @@ fill_fileinfo(const char *fdct, struct fileinfo *tfp) plen = len; npath--; while (npath > 0) { - tfp->path[plen++] = '/'; - benc_str(iter, &str, &len, &iter); - memcpy(tfp->path + plen, str, len); - plen += len; - npath--; + tfp->path[plen++] = '/'; + benc_str(iter, &str, &len, &iter); + memcpy(tfp->path + plen, str, len); + plen += len; + npath--; } tfp->path[plen] = '\0'; return 0; @@ -124,18 +124,18 @@ clear_metainfo(struct metainfo *mip) { int i; if (mip->piece_hash != NULL) - free(mip->piece_hash); + free(mip->piece_hash); if (mip->announce != NULL) - free(mip->announce); + free(mip->announce); if (mip->files != NULL) { - for (i = 0; i < mip->nfiles; i++) { - if (mip->files[i].path != NULL) - free(mip->files[i].path); - } - free(mip->files); + for (i = 0; i < mip->nfiles; i++) { + if (mip->files[i].path != NULL) + free(mip->files[i].path); + } + free(mip->files); } if (mip->name != NULL) - free(mip->name); + free(mip->name); } int @@ -147,91 +147,91 @@ fill_metainfo(const char *bep, struct metainfo *tp, int mem_hashes) const char *hash_addr; if (!benc_isdct(bep)) - return EINVAL; + return EINVAL; if ((err = benc_dget_strz(bep, "announce", &tp->announce, NULL)) != 0) - goto out; + goto out; if ((err = benc_dget_dct(bep, "info", &bep)) != 0) - goto out; + goto out; SHA1(bep, benc_length(bep), tp->info_hash); if ((err = benc_dget_off(bep, "piece length", &tp->piece_length)) != 0) - goto out; + goto out; if ((err = benc_dget_str(bep, "pieces", &hash_addr, &len)) != 0) - goto out; + goto out; if (len % 20 != 0) { - err = EINVAL; - goto out; + err = EINVAL; + goto out; } tp->npieces = len / 20; tp->pieces_off = hash_addr - base_addr; - + if (mem_hashes) { - if ((tp->piece_hash = malloc(len)) == NULL) { - err = ENOMEM; - goto out; - } - bcopy(hash_addr, tp->piece_hash, len); + if ((tp->piece_hash = malloc(len)) == NULL) { + err = ENOMEM; + goto out; + } + bcopy(hash_addr, tp->piece_hash, len); } if ((err = benc_dget_strz(bep, "name", &tp->name, NULL)) != 0) - goto out; + goto out; err = benc_dget_off(bep, "length", &tp->total_length); if (err == 0) { - tp->nfiles = 1; - tp->files = calloc(1, sizeof(struct fileinfo)); - if (tp->files != NULL) { - tp->files[0].length = tp->total_length; - tp->files[0].path = strdup(tp->name); - if (tp->files[0].path == NULL) { - err = ENOMEM; - goto out; - } - } else { - err = ENOMEM; - goto out; - } + tp->nfiles = 1; + tp->files = calloc(1, sizeof(struct fileinfo)); + if (tp->files != NULL) { + tp->files[0].length = tp->total_length; + tp->files[0].path = strdup(tp->name); + if (tp->files[0].path == NULL) { + err = ENOMEM; + goto out; + } + } else { + err = ENOMEM; + goto out; + } } else if (err == ENOENT) { - int i; - const char *flst, *fdct; - - if ((err = benc_dget_lst(bep, "files", &flst)) != 0) - goto out; - - tp->nfiles = benc_nelems(flst); - if (tp->nfiles < 1) { - err = EINVAL; - goto out; - } - tp->files = calloc(tp->nfiles, sizeof(struct fileinfo)); - - tp->total_length = 0; - i = 0; - for (fdct = benc_first(flst); fdct != NULL; fdct = benc_next(fdct)) { - if (!benc_isdct(fdct)) { - err = EINVAL; - goto out; - } - - if ((err = fill_fileinfo(fdct, &tp->files[i])) != 0) - goto out; - - tp->total_length += tp->files[i].length; - i++; - } + int i; + const char *flst, *fdct; + + if ((err = benc_dget_lst(bep, "files", &flst)) != 0) + goto out; + + tp->nfiles = benc_nelems(flst); + if (tp->nfiles < 1) { + err = EINVAL; + goto out; + } + tp->files = calloc(tp->nfiles, sizeof(struct fileinfo)); + + tp->total_length = 0; + i = 0; + for (fdct = benc_first(flst); fdct != NULL; fdct = benc_next(fdct)) { + if (!benc_isdct(fdct)) { + err = EINVAL; + goto out; + } + + if ((err = fill_fileinfo(fdct, &tp->files[i])) != 0) + goto out; + + tp->total_length += tp->files[i].length; + i++; + } } else - goto out; + goto out; out: if (err != 0) - clear_metainfo(tp); + clear_metainfo(tp); return err; } @@ -244,31 +244,31 @@ load_metainfo(const char *path, off_t size, int mem_hashes, int fd, err = 0; if ((fd = open(path, O_RDONLY)) == -1) - return errno; + return errno; if (size <= 0) { - struct stat sb; - if (fstat(fd, &sb) == -1) { - close(fd); - return errno; - } else - size = sb.st_size; + struct stat sb; + if (fstat(fd, &sb) == -1) { + close(fd); + return errno; + } else + size = sb.st_size; } if ((buf = mmap(0, size, PROT_READ, MAP_PRIVATE, fd, 0)) == MAP_FAILED) - err = errno; + err = errno; close(fd); if (err == 0) - err = benc_validate(buf, size); + err = benc_validate(buf, size); if (err == 0) - if ((*res = calloc(1, sizeof(**res))) == NULL) - err = ENOMEM; + if ((*res = calloc(1, sizeof(**res))) == NULL) + err = ENOMEM; if (err == 0) - if ((err = fill_metainfo(buf, *res, mem_hashes)) != 0) - free(*res); + if ((err = fill_metainfo(buf, *res, mem_hashes)) != 0) + free(*res); munmap(buf, size); return err; |