Rhash.xs

filter: Statementstmt 67.2% 37 / 55 Branchbran 50.0% 12 / 24 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 62.0% 49 / 79 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« _edonr.c ↑ Summary _rhash.c »
1 #include "EXTERN.h"
2 #include "perl.h"
3 #include "XSUB.h"
4  
5 #include <rhash.h>
6 #include <rhash_torrent.h>
7  
8 typedef unsigned long long ulonglong;
9  
10 /* helper macros and functions */
11 #define BASE32_LENGTH(size) (((size) * 8 + 4) / 5)
12 #define BASE64_LENGTH(size) ((((size) + 2) / 3) * 4)
13  
14 38 void verify_single_bit_hash_id(unsigned hash_id, CV* cv)
15 {
16 const char* error;
17 const GV *gv;
18 const char *func_name;
19  
20 38 ▶ if(0 == (hash_id & RHASH_ALL_HASHES)) {
Branch: 1 branch
BranchTrueFalseTotal
if(0 == (hash_id & RHASH_ALL_HASHES)) { 0 38 38
21 0 error = "%s: unsupported hash_id = 0x%x";
22 38 ▶ } else if(0 != (hash_id & (hash_id - 1))) {
Branch: 1 branch
BranchTrueFalseTotal
} else if(0 != (hash_id & (hash_id - 1))) { 0 38 38
23 0 error = "%s: hash_id is not a single bit: 0x%x";
24 } else {
25 38 return; /* success */
26 }
27  
28 0 gv = CvGV(cv);
29 0 ▶ func_name = (gv ? GvNAME(gv) : "Rhash");
Branch: 1 branch
BranchTrueFalseTotal
func_name = (gv ? GvNAME(gv) : "Rhash"); 0 0 0
30 0 croak(error, func_name, hash_id);
31 }
32  
33 /* allocate a perl string scalar variable, containing str_len + 1 bytes */
34 7 SV * allocate_string_buffer(STRLEN str_len)
35 {
36 7 SV * sv = newSV(str_len); /* allocates (str_len + 1) bytes */
37 7 SvPOK_only(sv);
38 7 SvCUR_set(sv, str_len);
39 7 return sv;
40 }
41  
42 MODULE = Crypt::Rhash PACKAGE = Crypt::Rhash
43  
44 ##############################################################################
45 # Initialize LibRHash in the module bootstrap function
46  
47 BOOT:
48 3 rhash_library_init();
49  
50 ##############################################################################
51 # perl bindings for Hi-level functions
52  
53 SV *
54 rhash_msg_raw(hash_id, message)
55 unsigned hash_id
56 PROTOTYPE: $$
57 PREINIT:
58 STRLEN length;
59 unsigned char out[264];
60 int res;
61 INPUT:
62 char* message = SvPV(ST(1), length);
63 CODE:
64 1 verify_single_bit_hash_id(hash_id, cv);
65 1 res = rhash_msg(hash_id, message, length, out);
66 1 ▶ if(res < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if(res < 0) { 0 1 1
67 0 croak("%s: %s", "rhash_msg_raw", strerror(errno));
68 }
69 1 RETVAL = newSVpv((char*)out, rhash_get_digest_size(hash_id));
70 OUTPUT:
71 RETVAL
72  
73 SV *
74 rhash_file_raw(hash_id, filepath)
75 unsigned hash_id
76 char * filepath
77 PROTOTYPE: $$
78 PREINIT:
79 int res;
80 unsigned char out[264];
81 CODE:
82 0 verify_single_bit_hash_id(hash_id, cv);
83 0 res = rhash_file(hash_id, filepath, out);
84 0 ▶ if(res < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if(res < 0) { 0 0 0
85 0 croak("%s: %s: %s", "rhash_file", filepath, strerror(errno));
86 }
87 0 RETVAL = newSVpv((char*)out, rhash_get_digest_size(hash_id));
88 OUTPUT:
89 RETVAL
90  
91 ##############################################################################
92 # perl bindings for Low-level functions
93  
94 struct rhash_context *
95 rhash_init(hash_id)
96 unsigned hash_id
97 PROTOTYPE: $
98  
99 int
100 rhash_update(ctx, message)
101 struct rhash_context * ctx
102 PROTOTYPE: $$
103 PREINIT:
104 STRLEN length;
105 INPUT:
106 char* message = SvPV(ST(1), length);
107 CODE:
108 7 RETVAL = rhash_update(ctx, message, length);
109 OUTPUT:
110 RETVAL
111  
112 int
113 rhash_final(ctx)
114 struct rhash_context * ctx
115 PROTOTYPE: $
116 CODE:
117 3 RETVAL = rhash_final(ctx, 0);
118 OUTPUT:
119 RETVAL
120  
121 void
122 rhash_reset(ctx)
123 struct rhash_context * ctx
124 PROTOTYPE: $
125  
126 void
127 rhash_free(ctx)
128 struct rhash_context * ctx
129 PROTOTYPE: $
130  
131 SV *
132 rhash_print(ctx, hash_id, flags = 0)
133 struct rhash_context * ctx
134 unsigned hash_id
135 int flags
136 PROTOTYPE: $$;$
137 PREINIT:
138 int len;
139 char out[264];
140 CODE:
141 42 ▶ if(hash_id != 0) verify_single_bit_hash_id(hash_id, cv);
Branch: 1 branch
BranchTrueFalseTotal
if(hash_id != 0) verify_single_bit_hash_id(hash_id, cv); 37 5 42
142  
143 42 len = rhash_print(out, ctx, hash_id, flags);
144  
145 /* set exact length to support raw output (RHPR_RAW) */
146 42 RETVAL = newSVpv(out, len);
147 OUTPUT:
148 RETVAL
149  
150 SV *
151 rhash_print_magnet(ctx, filename, hash_mask)
152 struct rhash_context * ctx
153 SV * filename
154 SV * hash_mask
155 PROTOTYPE: $;$$
156 PREINIT:
157 /* process undefined values */
158 4 ▶ char * name = (SvOK(filename) ? SvPV_nolen(filename) : 0);
Branch: 1 branch
BranchTrueFalseTotal
char * name = (SvOK(filename) ? SvPV_nolen(filename) : 0); 1 2 3
159 3 ▶ unsigned mask = (SvOK(hash_mask) ? (unsigned)SvUV(hash_mask) : RHASH_ALL_HASHES);
Branch: 2 branches
BranchTrueFalseTotal
unsigned mask = (SvOK(hash_mask) ? (unsigned)SvUV(hash_mask) : RHASH_ALL_HASHES); 2 1 3
unsigned mask = (SvOK(hash_mask) ? (unsigned)SvUV(hash_mask) : RHASH_ALL_HASHES); 3 0 3
160 size_t buf_size;
161 CODE:
162 /* allocate a string buffer and print magnet link into it */
163 3 buf_size = rhash_print_magnet(0, name, ctx, mask, RHPR_FILESIZE);
164 3 RETVAL = allocate_string_buffer(buf_size - 1);
165 3 rhash_print_magnet(SvPVX(RETVAL), name, ctx, mask, RHPR_FILESIZE);
166  
167 /* note: length(RETVAL) = (buf_size - 1),
168 * so the following call is not required:
169 * SvCUR_set(RETVAL, strlen(SvPVX(RETVAL))); */
170 OUTPUT:
171 RETVAL
172  
173 unsigned
174 rhash_get_hash_id(ctx)
175 struct rhash_context * ctx
176 PROTOTYPE: $
177 CODE:
178 1 ▶ RETVAL = ctx->hash_id;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = ctx->hash_id; 0 1 1
179 OUTPUT:
180 RETVAL
181  
182 ulonglong
183 rhash_get_hashed_length(ctx)
184 struct rhash_context * ctx
185 PROTOTYPE: $
186 CODE:
187 1 ▶ RETVAL = ctx->msg_size;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = ctx->msg_size; 0 1 1
188 OUTPUT:
189 RETVAL
190  
191 ##############################################################################
192 # Hash information functions
193  
194 int
195 count()
196 CODE:
197 1 RETVAL = rhash_count();
198 OUTPUT:
199 RETVAL
200  
201 int
202 is_base32(hash_id)
203 unsigned hash_id
204 PROTOTYPE: $
205 CODE:
206 3 RETVAL = rhash_is_base32(hash_id);
207 OUTPUT:
208 RETVAL
209  
210 int
211 get_digest_size(hash_id)
212 unsigned hash_id
213 PROTOTYPE: $
214 CODE:
215 1 RETVAL = rhash_get_digest_size(hash_id);
216 OUTPUT:
217 RETVAL
218  
219 int
220 get_hash_length(hash_id)
221 unsigned hash_id
222 PROTOTYPE: $
223 CODE:
224 1 RETVAL = rhash_get_hash_length(hash_id);
225 OUTPUT:
226 RETVAL
227  
228 const char *
229 get_name(hash_id)
230 unsigned hash_id
231 PROTOTYPE: $
232 CODE:
233 1 RETVAL = rhash_get_name(hash_id);
234 OUTPUT:
235 RETVAL
236  
237 ##############################################################################
238 # Hash printing functions
239  
240 ##############################################################################
241 # Hash conversion functions
242  
243 SV *
244 raw2hex(bytes)
245 PROTOTYPE: $
246 PREINIT:
247 STRLEN size;
248 INPUT:
249 unsigned char * bytes = (unsigned char*)SvPV(ST(0), size);
250 CODE:
251 2 RETVAL = allocate_string_buffer(size * 2);
252 2 rhash_print_bytes(SvPVX(RETVAL), bytes, size, RHPR_HEX);
253 OUTPUT:
254 RETVAL
255  
256 SV *
257 raw2base32(bytes)
258 PROTOTYPE: $
259 PREINIT:
260 STRLEN size;
261 INPUT:
262 unsigned char * bytes = (unsigned char*)SvPV(ST(0), size);
263 CODE:
264 1 RETVAL = allocate_string_buffer(BASE32_LENGTH(size));
265 1 rhash_print_bytes(SvPVX(RETVAL), bytes, size, RHPR_BASE32);
266 OUTPUT:
267 RETVAL
268  
269 SV *
270 raw2base64(bytes)
271 PROTOTYPE: $
272 PREINIT:
273 STRLEN size;
274 INPUT:
275 unsigned char * bytes = (unsigned char*)SvPV(ST(0), size);
276 CODE:
277 1 RETVAL = allocate_string_buffer(BASE64_LENGTH(size));
278 1 rhash_print_bytes(SvPVX(RETVAL), bytes, size, RHPR_BASE64);
279 OUTPUT:
280 RETVAL
281  
282 ##############################################################################
283 # BTIH / BitTorrent support functions
284  
285 void
286 rhash_bt_add_filename(ctx, filename, filesize)
287 struct rhash_context * ctx
288 char * filename
289 ulonglong filesize
290 PROTOTYPE: $$$
291 CODE:
292 0 rhash_torrent_add_file(ctx, filename, filesize);
293  
294 void
295 rhash_bt_set_piece_length(ctx, piece_length)
296 struct rhash_context * ctx
297 unsigned piece_length
298 PROTOTYPE: $$
299 CODE:
300 0 rhash_torrent_set_piece_length(ctx, piece_length);
301  
302 void
303 rhash_bt_set_private(ctx)
304 struct rhash_context * ctx
305 PROTOTYPE: $
306 CODE:
307 0 rhash_torrent_set_options(ctx, RHASH_TORRENT_OPT_PRIVATE);
308  
309 SV *
310 rhash_bt_get_torrent_text(ctx)
311 struct rhash_context * ctx
312 PROTOTYPE: $
313 PREINIT:
314 const rhash_str* text;
315 CODE:
316 0 text = rhash_torrent_generate_content(ctx);
317 0 ▶ if(!text) {
Branch: 1 branch
BranchTrueFalseTotal
if(!text) { 0 0 0
318 0 XSRETURN_UNDEF;
319 }
320 0 RETVAL = newSVpv(text->str, text->length);
321 OUTPUT:
322 RETVAL
« _edonr.c ↑ Summary _rhash.c »