1 | /* |
---|
2 | * This file Copyright (C) 2009 Charles Kerr <charles@transmissionbt.com> |
---|
3 | * |
---|
4 | * This file is licensed by the GPL version 2. Works owned by the |
---|
5 | * Transmission project are granted a special exemption to clause 2(b) |
---|
6 | * so that the bulk of its code can remain under the MIT license. |
---|
7 | * This exemption does not extend to derived works not owned by |
---|
8 | * the Transmission project. |
---|
9 | * |
---|
10 | * $Id:$ |
---|
11 | */ |
---|
12 | |
---|
13 | #include <assert.h> |
---|
14 | #include <ctype.h> /* tolower() */ |
---|
15 | #include <string.h> /* strchr() */ |
---|
16 | |
---|
17 | #include "transmission.h" |
---|
18 | #include "magnet.h" |
---|
19 | #include "utils.h" |
---|
20 | #include "web.h" |
---|
21 | |
---|
22 | /*** |
---|
23 | **** |
---|
24 | ***/ |
---|
25 | |
---|
26 | /* this base32 code converted from code by Robert Kaye and Gordon Mohr |
---|
27 | * and is public domain. see http://bitzi.com/publicdomain for more info */ |
---|
28 | |
---|
29 | static const int base32Lookup[] = |
---|
30 | { |
---|
31 | 0xFF,0xFF,0x1A,0x1B,0x1C,0x1D,0x1E,0x1F, // '0', '1', '2', '3', '4', '5', '6', '7' |
---|
32 | 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF, // '8', '9', ':', ';', '<', '=', '>', '?' |
---|
33 | 0xFF,0x00,0x01,0x02,0x03,0x04,0x05,0x06, // '@', 'A', 'B', 'C', 'D', 'E', 'F', 'G' |
---|
34 | 0x07,0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E, // 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O' |
---|
35 | 0x0F,0x10,0x11,0x12,0x13,0x14,0x15,0x16, // 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W' |
---|
36 | 0x17,0x18,0x19,0xFF,0xFF,0xFF,0xFF,0xFF, // 'X', 'Y', 'Z', '[', '\', ']', '^', '_' |
---|
37 | 0xFF,0x00,0x01,0x02,0x03,0x04,0x05,0x06, // '`', 'a', 'b', 'c', 'd', 'e', 'f', 'g' |
---|
38 | 0x07,0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E, // 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o' |
---|
39 | 0x0F,0x10,0x11,0x12,0x13,0x14,0x15,0x16, // 'p', 'q', 'r', 's', 't', 'u', 'v', 'w' |
---|
40 | 0x17,0x18,0x19,0xFF,0xFF,0xFF,0xFF,0xFF // 'x', 'y', 'z', '{', '|', '}', '~', 'DEL' |
---|
41 | }; |
---|
42 | |
---|
43 | static const int base32LookupLen = sizeof( base32Lookup ) / sizeof( base32Lookup[0] ); |
---|
44 | |
---|
45 | static void |
---|
46 | base32_to_sha1( uint8_t * out, const char * in, const int inlen ) |
---|
47 | { |
---|
48 | const int outlen = 20; |
---|
49 | int i, index, offset; |
---|
50 | |
---|
51 | memset( out, 0, 20 ); |
---|
52 | |
---|
53 | assert( inlen == 32 ); |
---|
54 | |
---|
55 | for( i=0, index=0, offset=0; i<inlen; ++i ) |
---|
56 | { |
---|
57 | int digit; |
---|
58 | int lookup = in[i] - '0'; |
---|
59 | |
---|
60 | /* Skip chars outside the lookup table */ |
---|
61 | if( lookup<0 || lookup>=base32LookupLen ) |
---|
62 | continue; |
---|
63 | |
---|
64 | /* If this digit is not in the table, ignore it */ |
---|
65 | digit = base32Lookup[lookup]; |
---|
66 | if( digit == 0xFF ) |
---|
67 | continue; |
---|
68 | |
---|
69 | if( index <= 3 ) { |
---|
70 | index = (index + 5) % 8; |
---|
71 | if( index == 0 ) { |
---|
72 | out[offset] |= digit; |
---|
73 | offset++; |
---|
74 | if( offset >= outlen ) |
---|
75 | break; |
---|
76 | } else { |
---|
77 | out[offset] |= digit << (8 - index); |
---|
78 | } |
---|
79 | } else { |
---|
80 | index = (index + 5) % 8; |
---|
81 | out[offset] |= (digit >> index); |
---|
82 | offset++; |
---|
83 | |
---|
84 | if( offset >= outlen ) |
---|
85 | break; |
---|
86 | out[offset] |= digit << (8 - index); |
---|
87 | } |
---|
88 | } |
---|
89 | } |
---|
90 | |
---|
91 | /*** |
---|
92 | **** |
---|
93 | ***/ |
---|
94 | |
---|
95 | #define MAX_TRACKERS 64 |
---|
96 | |
---|
97 | tr_magnet_info * |
---|
98 | tr_magnetParse( const char * uri ) |
---|
99 | { |
---|
100 | tr_bool got_checksum = FALSE; |
---|
101 | int announceCount = 0; |
---|
102 | char * announceURLs[MAX_TRACKERS]; |
---|
103 | char * displayName = NULL; |
---|
104 | uint8_t sha1[SHA_DIGEST_LENGTH]; |
---|
105 | tr_magnet_info * info = NULL; |
---|
106 | |
---|
107 | if( ( uri != NULL ) && !memcmp( uri, "magnet:?", 8 ) ) |
---|
108 | { |
---|
109 | const char * walk; |
---|
110 | |
---|
111 | for( walk=uri+8; walk && *walk; ) |
---|
112 | { |
---|
113 | const char * key = walk; |
---|
114 | const char * delim = strchr( key, '=' ); |
---|
115 | const char * val = delim == NULL ? NULL : delim + 1; |
---|
116 | const char * next = strchr( delim == NULL ? key : val, '&' ); |
---|
117 | int keylen, vallen; |
---|
118 | |
---|
119 | if( delim != NULL ) |
---|
120 | keylen = delim - key; |
---|
121 | else if( next != NULL ) |
---|
122 | keylen = next - key; |
---|
123 | else |
---|
124 | keylen = strlen( key ); |
---|
125 | |
---|
126 | if( val == NULL ) |
---|
127 | vallen = 0; |
---|
128 | else if( next != NULL ) |
---|
129 | vallen = next - val; |
---|
130 | else |
---|
131 | vallen = strlen( val ); |
---|
132 | |
---|
133 | if( ( keylen==2 ) && !memcmp( key, "xt", 2 ) && !memcmp( val, "urn:btih:", 9 ) ) |
---|
134 | { |
---|
135 | const char * hash = val + 9; |
---|
136 | const int hashlen = vallen - 9; |
---|
137 | |
---|
138 | if( hashlen == 40 ) { |
---|
139 | tr_hex_to_sha1( sha1, hash ); |
---|
140 | got_checksum = TRUE; |
---|
141 | } |
---|
142 | else if( hashlen == 32 ) { |
---|
143 | base32_to_sha1( sha1, hash, hashlen ); |
---|
144 | got_checksum = TRUE; |
---|
145 | } |
---|
146 | } |
---|
147 | |
---|
148 | if( ( keylen==2 ) && !memcmp( key, "dn", 2 ) ) |
---|
149 | displayName = tr_http_unescape( val, vallen ); |
---|
150 | |
---|
151 | if( ( keylen==2 ) && !memcmp( key, "tr", 2 ) ) |
---|
152 | announceURLs[announceCount++] = tr_http_unescape( val, vallen ); |
---|
153 | |
---|
154 | walk = next != NULL ? next + 1 : NULL; |
---|
155 | } |
---|
156 | } |
---|
157 | |
---|
158 | if( got_checksum ) |
---|
159 | { |
---|
160 | info = tr_new0( tr_magnet_info, 1 ); |
---|
161 | info->displayName = displayName; |
---|
162 | info->announceCount = announceCount; |
---|
163 | info->announceURLs = tr_memdup( announceURLs, sizeof(char*) * announceCount ); |
---|
164 | memcpy( info->hash, sha1, sizeof(uint8_t) * SHA_DIGEST_LENGTH ); |
---|
165 | } |
---|
166 | |
---|
167 | return info; |
---|
168 | } |
---|
169 | |
---|
170 | void |
---|
171 | tr_magnetFree( tr_magnet_info * info ) |
---|
172 | { |
---|
173 | if( info != NULL ) |
---|
174 | { |
---|
175 | int i; |
---|
176 | |
---|
177 | for( i=0; i<info->announceCount; ++i ) |
---|
178 | tr_free( info->announceURLs[i] ); |
---|
179 | |
---|
180 | tr_free( info->announceURLs ); |
---|
181 | tr_free( info->displayName ); |
---|
182 | tr_free( info ); |
---|
183 | } |
---|
184 | } |
---|