2 # $MirOS: contrib/code/Snippets/gened2k,v 1.4 2008/05/03 01:09:23 tg Exp $
5 # Thorsten "mirabilos" Glaser <tg@mirbsd.de>
7 # Provided that these terms and disclaimer and all copyright notices
8 # are retained or reproduced in an accompanying document, permission
9 # is granted to deal in this work without restriction, including un-
10 # limited rights to use, publicly perform, distribute, sell, modify,
11 # merge, give away, or sublicence.
13 # This work is provided "AS IS" and WITHOUT WARRANTY of any kind, to
14 # the utmost extent permitted by applicable law, neither express nor
15 # implied; without malicious intent or gross negligence. In no event
16 # may a licensor, author or contributor be held liable for indirect,
17 # direct, other damage, loss, or other issues arising in any way out
18 # of dealing in the work, even if advised of the possibility of such
19 # damage or existence of a defect, except proven that it results out
20 # of said person's immediate fault when using the work as intended.
22 # This script produces an "ed2k" type URI for a given existing file.
23 # It is by no means normative, but developed by reverse engineering.
25 # Structure of an "ed2k" type URI:
26 # ed2k://|file|<filename>|<size>|<hash>|[/]
27 # where the trailing slash is optional, "filename" is posix, except
28 # that no pipe sign can occur, size in octets (decimal) and hash...
29 # well, that's the hard part below. It's hexadecimal lowercase.
37 set -A o -- $(/bin/ls -l "$1")
42 typeset name="${1##*/}"
44 # I can't use integer here because it's limited to 32 bit signed :(
47 # This equals a s/\|//g regexp...
48 while [[ $name = *\|* ]]; do
49 name="${name##*\|}${name%\|*}"
53 print -r "ed2k://|file|${name}|${size}|${hash}|/"
60 if [[ -z "$1" ]]; then
64 set -A o -- $(cksum -a md4 "$i")
73 typeset -i sz=$(getsize "$fn")
80 elif ((sz < 9728000)); then
84 while (((nr * 9728000) < sz)); do
85 hash=$(dd if="$fn" bs=9728000 count=1 skip=$nr \
88 fullhash="$fullhash$hash"
90 hash=$(printf "$(print $fullhash \
91 | sed -e 's/\(..\)/\\x\1/g')" | getmd4)
98 if [[ $# -lt 1 || $1 == -@(h|H|?) ]]; then
100 print " $0 [-h | file ...]"
105 test -r "$fn" && continue
106 print -r "Error: '$fn' not readable."
111 size=$(getsize "$fn")
112 hash=$(gethash "$fn")
113 constructuri "$fn" $size $hash