Thanks Wizard and David!
Emule seems to encode constraints this way:
- MinSize: (input * 1024 * 1024) - 1;
- MaxSize: (input * 1024 * 1024) + 1;
- Availability: input - 1;
- Completed Sources: input - 1;
- Bitrate: input - 1;
- Duration: toSeconds(input) - 1;
This is my function to decode search exp
ressions:
var searchExpr = function(token, type, value) {
switch (type) {
case 0xff: return 'str='+value;
case 0x00: return ' AND ';
case 0x01: return ' OR ';
case 0x02: return ' NOT ';
case 0x030001: return 'type='+value;
case 0x040001: return 'ext='+value;
case 0xd50001: return 'codec='+value;
case 0x02000101: return 'size>'+value;
case 0x02000102: return 'size<'+value;
case 0x15000101: return 'sources>'+value;
case 0xd4000101: return 'bitrate>'+value;
case 0xd3000101: return 'duration>'+value;
case 0x30000101: return 'sourcescompleted='+value;
default: log.warn('searchExpr: unknown type: 0x'+type.toString(16)+'; token: 0x'+token.toString(16)+'; value: '+value);
}
}
Buffer.prototype.getSearchString = function() {
var token = this.getUInt8();
switch (token) {
case 0x01: // text match
return searchExpr(token, 0xff, this.getString());
case 0x02: // string
var value = this.getString();
var type = this.getUInt8();
type+= this.getUInt16LE() * 0x100;
return searchExpr(token, type, value);
case 0x03: // 32bit value
var value = this.getUInt32LE();
var type = this.getUInt32LE();
return searchExpr(token, type, value);
case 0x08: // 64bit value
var value = this.getUInt32LE();
value+= this.getUInt32LE() * 0x10000 * 0x10000;
var type = this.getUInt32LE();
return searchExpr(token, type, value);
case 0x00: // tree node
var type = this.getUInt8();
var s = '(' + this.getSearchString();
s+= searchExpr(token, type, null);
return s + this.getSearchString() + ')';
default:
log.warn('Buffer.getSearchString: unknown token: 0x'+token.toString(16));
}
return '';
};
Sample input search of text "aaa AND bbb OR ccc NOT ddd" and all possible constraints:
00000000: 0000 0000 0103 0061 6161 0001 0103 0062 .......aaa.....b
00000010: 6262 0002 0103 0063 6363 0103 0064 6464 bb.....ccc...ddd
00000020: 0000 0205 004d 5045 4734 0100 d500 0003 .....MPEG4..�...
00000030: 0f0e 0000 0101 00d3 0000 0399 0200 0001 .......�...�....
00000040: 0100 d400 0003 e603 0000 0101 0030 0000 ..�...�......0..
00000050: 03ff ff0f 1401 0100 0200 0008 0100 1043 .��............C
00000060: 0d00 0000 0201 0002 0000 0377 0300 0001 ...........w....
00000070: 0100 1502 0300 7171 7101 0004 ......qqq...
Sample output:
((str=aaa AND (str=bbb OR (str=ccc NOT str=ddd))) AND (codec=MPEG4 AND (duration>3599 AND (bitrate>665 AND (sourcescompleted=998 AND (size>336592895 AND (size<56959696897 AND (sources>887 AND ext=qqq))))))))
This post has been edited by petermrg: 31 January 2013 - 01:05 PM