Official eMule-Board: Importing Ip Filters From Peerblock Cache - Official eMule-Board

Jump to content


Page 1 of 1

Importing Ip Filters From Peerblock Cache

#1 User is offline   ElKarro 

  • Newbie
  • Pip
  • Group: Members
  • Posts: 10
  • Joined: 07-February 10

Posted 15 January 2011 - 05:41 AM

Hello,
I've just added to my eMule the import of IP filters directly from PeerBlock cache.p2b file, and I can say it's very handy.
Original v0.50a doesn't digest revision 3 of .p2b, which is used by PeerBlock.

I report the mod, hope it can be useful (I've got trouble with tabs...).

ref. file IPFilter.cpp, function CIPFilter::AddFromFile()

replace the block from row 120 to row 156 with this code:

// Version 1: strings are ISO-8859-1 encoded
// Version 2: strings are UTF-8	encoded
// Version 3: PeerBlock	cache.p2b format
uint8 nVersion;
if (fread(&nVersion, sizeof	nVersion, 1, readFile)==1)
{
	switch (nVersion)
	{
		case 1:
		case 2:	while (!feof(readFile))
				{
					CHAR szName[256];
					int	iLen = 0;
					for	(;;) //	read until NUL or EOF
					{
						int	iChar =	getc(readFile);
						if (iChar == EOF)
							break;
						if (iLen < sizeof szName - 1)
							szName[iLen++] = (CHAR)iChar;
						if (iChar == '\0')
							break;
					}
					szName[iLen] = '\0';

					uint32 uStart;
					if (fread(&uStart, sizeof uStart, 1, readFile) != 1)
						break;
					uStart = ntohl(uStart);

					uint32 uEnd;
					if (fread(&uEnd, sizeof	uEnd, 1, readFile) != 1)
						break;
					uEnd = ntohl(uEnd);

					iLine++;
					// (nVersion ==	2) ? OptUtf8ToStr(szName, iLen)	:
					AddIPRange(uStart, uEnd, DFLT_FILTER_LEVEL,	CStringA(szName, iLen));
					iFoundRanges++;
				}
			break;
		case 3:	uint32 namecount;

				if (fread(&namecount, sizeof namecount,	1, readFile) ==	1)
				{
					namecount =	ntohl(namecount);
					long *nameIdx =	new	long [namecount	+ 1];

					if (nameIdx)
					{
						uint32 maxStrLen = 0;
						uint32 strLen = 0;

						nameIdx[0] = 0x0C;

						for	(uint32	i =	0; i < namecount; i++)
						{
							for	(strLen	= 0; ; strLen++)
							{
								CHAR ch;

								if (fread(&ch, sizeof ch, 1, readFile) == 1)
								{
									if (ch == '\0')
										break;
								}
								else
								{
								//	unexpected EOF
									strLen = 0;
									break;
								}
							}

							if (strLen)
							{
								if (maxStrLen <	strLen)
									maxStrLen =	strLen;

								nameIdx[i+1] = nameIdx[i] +	strLen + 1;
							}
							else
							{
								break;
							}
						}

						if (strLen)
						{
							uint32 rangecount;

							if (fread(&rangecount, sizeof rangecount, 1, readFile) == 1)
							{
								long rangeOfs =	ftell(readFile);

								if (rangeOfs >=	0x0C)
								{
									char *szName = new char	[maxStrLen];

									if (szName)
									{
										rangecount = ntohl(rangecount);

										for	(uint32	i =	0; i < rangecount; i++)
										{
											struct {
												uint32 name;
												uint32 start;
												uint32 end;
											} theRange;

											if (fread(&theRange, sizeof	theRange, 1, readFile) == 1)
											{
												uint32 name	= ntohl(theRange.name);

												if (name < namecount)
												{
													if (fseek(readFile,	nameIdx[name], SEEK_SET) ==	0)
													{
														uint32 iLen	= nameIdx[name+1]-nameIdx[name]-1;

														if (fread(szName, sizeof(char),	iLen, readFile)	== iLen)
														{
															iLine++;
															AddIPRange(ntohl(theRange.start), ntohl(theRange.end), DFLT_FILTER_LEVEL, CStringA(szName, iLen));
															iFoundRanges++;

															rangeOfs +=	sizeof theRange;

															if (fseek(readFile,	rangeOfs, SEEK_SET))
																break;
														}
													}
												}
											}
										}

										delete [] szName;
									}
								}
							}
						}
					}

					delete [] nameIdx;
				}
			break;
		default:
			break;
	}
}


elKarro

This post has been edited by ElKarro: 15 January 2011 - 12:14 PM

0

  • Member Options

Page 1 of 1

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users