uint JsonEngine::findEnd(QFile &file) { int len = file.size(); QByteArray data; uchar *fmap = file.map(0, file.size()); if(!fmap) { data = file.readAll(); fmap = (uchar *)data.constData(); } uint end = file.size(); const uchar *s = K8JSON::skipBlanks(fmap, &len); uchar qch = *s; if(!s || (qch != '[' && qch != '{')) { if(data.isEmpty()) file.unmap(fmap); return end; } qch = (qch == '{' ? '}' : ']'); s++; len--; bool first = true; while(s) { s = K8JSON::skipBlanks(s, &len); if(len < 2 || (s && *s == qch)) { if(*(s-1) == '\n') s--; end = (uint)(s - fmap); break; } if(!s) break; if((!first && *s != ',') || (first && *s == ',')) break; first = false; if(*s == ',') { s++; len--; } if(!(s = K8JSON::skipRec(s, &len))) break; } if(data.isEmpty()) file.unmap(fmap); return end; }
QByteArray Scanner::hashFile(QFile &f) { uchar *map; QCryptographicHash hasher(QCryptographicHash::Sha1); hasher.reset(); /* Try map first */ map = f.map( 0, f.size() ); if (NULL==map) { /* no mmap, read in chunks */ uchar buffer[512]; qint64 r; do { r = f.read((char*)buffer,sizeof(buffer)); if (r<0){ throw 1; } if (r<=0) break; hasher.addData( (const char*)buffer, r); } while (r>0); } else { hasher.addData((const char*)map,f.size()); f.unmap(map); } return hasher.result(); }
FixFile::~FixFile() { // if the file was read into _data, it will be deleted automatically if (_used_mmap && _file) { if (0) qDebug("Unmapping '%s'", qPrintable( _filename )); #if QT_VERSION >= 0x040400 QFile* mappableDevice = dynamic_cast<QFile*>(_file); Q_ASSERT(mappableDevice); if (!mappableDevice->unmap( (uchar*) _base )) qWarning( "munmap: %s", strerror( errno ) ); #endif } }
QByteArray QCOMM::md5(QFile &file) { unsigned char *buf; bool isopen; if(!file.exists()) return QByteArray(); if(file.size()==0) return QByteArray(); if(file.isOpen()) isopen = TRUE; if(isopen&&(!file.open(QIODevice::ReadOnly))) return QByteArray(); buf = file.map(0,file.size()); if(isopen) file.close(); if(0==buf) return QByteArray(); QByteArray data = QByteArray::fromRawData((char*)buf,file.size()); QByteArray md5 = QCryptographicHash::hash (data, QCryptographicHash::Md5); file.unmap(buf); return md5; }