~ubuntu-branches/ubuntu/trusty/rhash/trusty

« back to all changes in this revision

Viewing changes to librhash/torrent.c

  • Committer: Package Import Robot
  • Author(s): Aleksey Kravchenko
  • Date: 2013-09-16 19:48:55 UTC
  • mfrom: (1.2.1) (16.1.1 experimental)
  • Revision ID: package-import@ubuntu.com-20130916194855-sengvtinypyl41ld
Tags: 1.3.0-1
* New upstream release version 1.3.0
 - switched the package back to unstable

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/* torrent.c - create BitTorrent files and calculate BitTorrent  InfoHash (BTIH).
2
2
 *
3
 
 * Copyright: 2010 Aleksey Kravchenko <rhash.admin@gmail.com>
 
3
 * Copyright: 2010-2012 Aleksey Kravchenko <rhash.admin@gmail.com>
4
4
 *
5
5
 * Permission is hereby granted,  free of charge,  to any person  obtaining a
6
6
 * copy of this software and associated documentation files (the "Software"),
8
8
 * the rights to  use, copy, modify,  merge, publish, distribute, sublicense,
9
9
 * and/or sell copies  of  the Software,  and to permit  persons  to whom the
10
10
 * Software is furnished to do so.
 
11
 *
 
12
 * This program  is  distributed  in  the  hope  that it will be useful,  but
 
13
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 
14
 * or FITNESS FOR A PARTICULAR PURPOSE.  Use this program  at  your own risk!
11
15
 */
12
16
 
13
17
#include <string.h>
62
66
}
63
67
 
64
68
/**
 
69
 * Free memory allocated by properties of torrent_vect structure.
 
70
 *
 
71
 * @param vect vector to clean
 
72
 */
 
73
static void bt_vector_clean(torrent_vect *vect)
 
74
{
 
75
        size_t i;
 
76
        for(i = 0; i < vect->size; i++) {
 
77
                free(vect->array[i]);
 
78
        }
 
79
        free(vect->array);
 
80
}
 
81
 
 
82
/**
65
83
 * Clean up torrent context by freeing all dynamically
66
84
 * allocated memory.
67
85
 *
69
87
 */
70
88
void bt_cleanup(torrent_ctx *ctx)
71
89
{
72
 
        size_t i;
73
90
        assert(ctx != NULL);
74
91
 
75
 
        /* destroy array of hash blocks */
76
 
        for(i = 0; i < ctx->hash_blocks.size; i++) {
77
 
                free(ctx->hash_blocks.array[i]);
78
 
        }
79
 
 
80
 
        /* destroy array of file paths */
81
 
        for(i = 0; i < ctx->files.size; i++) {
82
 
                free(ctx->files.array[i]);
83
 
        }
 
92
        /* destroy arrays of hash blocks and file paths */
 
93
        bt_vector_clean(&ctx->hash_blocks);
 
94
        bt_vector_clean(&ctx->files);
84
95
 
85
96
        free(ctx->program_name);
86
97
        free(ctx->announce);
324
335
 */
325
336
static void bt_bencode_pieces(torrent_ctx* ctx)
326
337
{
327
 
        int pieces_length = ctx->piece_count * BT_HASH_SIZE;
 
338
        size_t pieces_length = ctx->piece_count * BT_HASH_SIZE;
328
339
        int num_len;
329
340
        int size, i;
330
341
        char* p;
339
350
        *(p++) = ':';
340
351
        p[pieces_length] = '\0'; /* terminate with \0 just in case */
341
352
 
342
 
        for(size = ctx->piece_count, i = 0; size > 0;
 
353
        for(size = (int)ctx->piece_count, i = 0; size > 0;
343
354
                size -= BT_BLOCK_SIZE, i++)
344
355
        {
345
356
                memcpy(p, ctx->hash_blocks.array[i],