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(QIODevice* file, const QString& filename) { _file = file; if (!file) { _len = 0; _currentLeft = 0; _openError = true; return; } _filename = filename; if (!file->isOpen() && !file->open( QIODevice::ReadOnly ) ) { qWarning( "%s: %s", (const char*)QFile::encodeName(_filename), strerror( errno ) ); _len = 0; _currentLeft = 0; _openError = true; return; } _openError = false; _used_mmap = false; uchar* addr = 0; #if QT_VERSION >= 0x040400 // QFile::map was introduced with Qt 4.4 if (file->size() >0) { QFile* mappableDevice = dynamic_cast<QFile*>(file); if (mappableDevice) { addr = mappableDevice->map( 0, file->size() ); } } #endif if (addr) { // map succeeded _base = (char*) addr; _len = file->size(); _used_mmap = true; if (0) qDebug("Mapped '%s'", qPrintable( _filename )); } else { // try reading the data into memory instead file->seek(0); _data = file->readAll(); _base = _data.data(); _len = _data.size(); } _current = _base; _currentLeft = _len; }
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; }
static QFile* map(QString path, raw_buffer &fData) { const char *message; QFile *f = new QFile(path); if (!f->open(QIODevice::ReadOnly)) { message = "is not readable"; goto map_failed; } if (f->size() > std::numeric_limits<typeof(fData.m_len)>::max()) { message = "is too big"; goto map_failed; } if (f->size() != 0) { fData.m_len = f->size(); fData.m_str = (const char *)f->map(0, f->size()); if (fData.m_str == NULL) { message = "failed to memory map"; goto map_failed; } } else { qDebug() << "File" << path << "is empty"; fData.m_len = 0; fData.m_str = ""; } return f; map_failed: f->close(); delete f; f = NULL; const char *fileNameStr = qPrintable(path); fprintf(stderr, "%s %s\n", fileNameStr, message); return f; }
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; }