23 #include "ip2cparser_v3.h"
25 #include "ip2c/ip2cparser.h"
26 #include "ip2c/ip2crange.h"
28 #include "datastreamoperatorwrapper.h"
31 #include <QDataStream>
37 enum class Section : uint8_t
44 bool IP2CParserV3::parse(
IP2CParser &
self, QIODevice &dataBase)
46 QDataStream dstream(&dataBase);
47 dstream.setByteOrder(QDataStream::LittleEndian);
50 while (stream.hasRemaining())
52 auto id = stream.readQUInt8();
53 auto size = stream.readQUInt64();
56 case decltype(
id)(Section::LICENCE_TEXTS):
57 if (!readSectionLegalNotice(
self, stream.readRaw(size)))
60 case decltype(
id)(Section::IPV4):
61 if (!readSectionIpv4(
self, stream.readRaw(size)))
64 case decltype(
id)(Section::URL_DB):
65 if (!readSectionUrl(
self, stream.readRaw(size)))
69 stream.skipRawData(size);
77 bool IP2CParserV3::readSectionIpv4(
IP2CParser &
self, QByteArray &§ion)
79 QBuffer buffer(§ion);
80 buffer.open(QIODevice::ReadOnly);
81 QDataStream dstream(&buffer);
82 dstream.setByteOrder(QDataStream::LittleEndian);
87 QMap<unsigned, IP2CRange> hashTable;
89 while (stream.hasRemaining())
91 auto countryCode = stream.readUtf8CString();
95 baseEntry.country = countryCode;
96 if (!stream.hasRemaining())
99 quint32 numOfIpBlocks = stream.readQUInt32();
101 for (quint32 x = 0; x < numOfIpBlocks; ++x)
106 entry.ipStart = stream.readQUInt32();
107 if (!stream.hasRemaining())
109 entry.ipEnd = stream.readQUInt32();
111 hashTable[entry.ipStart] = entry;
115 self.ranges_ = hashTable.values();
119 bool IP2CParserV3::readSectionLegalNotice(
IP2CParser &
self, QByteArray &§ion)
121 QBuffer buffer(§ion);
122 buffer.open(QIODevice::ReadOnly);
123 QDataStream dstream(&buffer);
124 dstream.setByteOrder(QDataStream::LittleEndian);
127 while (stream.hasRemaining())
129 auto locale = stream.readUtf8CString();
130 if (stream.hasRemaining())
132 auto text = stream.readUtf8CString();
133 self.licences_.insert(std::move(locale), std::move(text));
141 bool IP2CParserV3::readSectionUrl(
IP2CParser &
self, QByteArray &§ion)
143 QBuffer buffer(§ion);
144 buffer.open(QIODevice::ReadOnly);
145 QDataStream dstream(&buffer);
146 dstream.setByteOrder(QDataStream::LittleEndian);