[H2] bto() - Decode Bencoding To Object

Post AHK_H specific scripts & libraries and discuss the usage and development of HotKeyIt's fork/branch
arcticir
Posts: 543
Joined: 17 Nov 2013, 11:32

[H2] bto() - Decode Bencoding To Object

23 Apr 2017, 21:17

Used to parse torrent files.

Code: Select all

buf:=FileRead("*c test.torrent")
size:=VarSetCapacity(buf)

;3 kinds of use:
f:=[]
f.push(bto("test.torrent"))
f.push(bto(buf))
f.push(bto(&buf,size))
MsgBox % ObjTree(f)

Image

Code: Select all

bto(byref file,size:=""){
	type(file)="Integer"
		?ad:=file
		:isbyref(file) and (size:=strlen(file)/2)>259
			?ad:=&file
			:(buf:=FileRead("*c " file),size:=strlen(buf)/2,ad:=&buf)
		,tree:=[],key:=[],len:=0,list:={(f:=first:=[]):1},hex:=bintohex(ad+0,size),hexs:=size*2
		,en:=RegExMatch(StrGet(ad,520,"utf-8"),"iU)8:encoding\d:(.*)\d:",s)?s.1:"utf-8"

	while len<hexs
	{
		if instr("idle1234567890",(str:="",s:=chr("0x" substr(hex,len+1,2))))
			gosub,% s+1?"s":s
		else
			Return 	first.err:=len " " StrGet(ad+len/2-18,36,"utf-8"),first
	}
	Return first.Length()=1?first.1:first
w:
	(list[f]?f.push(str):key[f]?(f[key[f]]:=str,key.delete(f)):key[f]:=str)
	Return
e:
	f:=tree[f],len+=2
	Return
l:
	j:=f,f:=[],len+=2,tree[f]:=j,list[f]:=1,list[j]?j.push(f):key[j]?(j[key[j]]:=f,key.delete(j)):key[j]:=f
	Return
r:
	while (r:=chr("0x" substr(hex,len+1+a_index*2,2))+0)!=""
		str.=r
	len+=strlen(str)*2+4
	Return
i:
	gosub,r
	gosub,w
	Return
d:
	j:=f,f:=[],len+=2,tree[f]:=j,list[j]?j.push(f):key[j]?(j[key[j]]:=f,key.delete(j)):key[j]:=f
	Return
s:
	gosub,r
	b:=substr(hex,len+1,d:=(sz:= s str)*2),str:=sz>5000?b:(hextobin(h,b),m:=StrGet(&h,sz,en),instr(m,"�")?b:m),len+=d
	gosub,w
	Return
}
Last edited by arcticir on 29 Apr 2017, 11:48, edited 2 times in total.
User avatar
SnowFlake
Posts: 368
Joined: 28 Apr 2015, 05:41
Google: floowsnaake
GitHub: floowsnaake
Contact:

Re: [H2] bto() - Decode Bencoding To Object

28 Apr 2017, 11:26

cool
:yawn:

Return to “AutoHotkey_H”

Who is online

Users browsing this forum: No registered users and 0 guests