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);
76 bool IP2CParserV3::readSectionIpv4(
IP2CParser &
self, QByteArray &§ion)
78 QBuffer buffer(§ion);
79 buffer.open(QIODevice::ReadOnly);
80 QDataStream dstream(&buffer);
81 dstream.setByteOrder(QDataStream::LittleEndian);
86 QMap<unsigned, IP2CRange> hashTable;
88 while (stream.hasRemaining())
90 QString countryCode = QString::fromUtf8(stream.readRawUntilByte(
'\0'));
94 baseEntry.country = countryCode;
95 if (!stream.hasRemaining())
98 quint32 numOfIpBlocks = stream.readQUInt32();
100 for (quint32 x = 0; x < numOfIpBlocks; ++x)
105 entry.ipStart = stream.readQUInt32();
106 if (!stream.hasRemaining())
108 entry.ipEnd = stream.readQUInt32();
110 hashTable[entry.ipStart] = entry;
114 self.ranges_ = hashTable.values();
118 bool IP2CParserV3::readSectionLegalNotice(
IP2CParser &
self, QByteArray &§ion)
120 QBuffer buffer(§ion);
121 buffer.open(QIODevice::ReadOnly);
122 QDataStream dstream(&buffer);
123 dstream.setByteOrder(QDataStream::LittleEndian);
126 while (stream.hasRemaining())
128 auto locale = QString::fromUtf8(stream.readRawUntilByte(
'\0'));
129 if (stream.hasRemaining())
131 auto text = QString::fromUtf8(stream.readRawUntilByte(
'\0'));
132 self.licences_.insert(std::move(locale), std::move(text));
140 bool IP2CParserV3::readSectionUrl(
IP2CParser &
self, QByteArray &§ion)
142 QBuffer buffer(§ion);
143 buffer.open(QIODevice::ReadOnly);
144 QDataStream dstream(&buffer);
145 dstream.setByteOrder(QDataStream::LittleEndian);