summaryrefslogtreecommitdiffstats
path: root/src/versit/qversitreader_p.cpp
diff options
context:
space:
mode:
authorChris Adams <chris.adams@jollamobile.com>2013-12-20 12:00:45 +1000
committerThe Qt Project <gerrit-noreply@qt-project.org>2013-12-23 15:58:42 +0100
commit5275f553721380f6c165bd3f53c79db12fcaad9a (patch)
treea3b09b5b82ab52b2810f0f8ebd990ab21bfe0439 /src/versit/qversitreader_p.cpp
parent87b76ba77b48f3de9cbd51cc8d3f2c5289c17e2c (diff)
[versit] Handle malformed concatentations of vCard files
If vCard documents which do not end in \r\n are concatenated together, the result is a malformed vCard document with a line like END:VCARDBEGIN:VCARD\r\n in it. This commit adds logic to the parser to detect those types of lines and replace them with END:VCARD\r\nBEGIN:VCARD\r\n lines. Signed-off-by: Chris Adams <chris.adams@jollamobile.com> Change-Id: I9de0bfebaa640fd2a166342e4e310edb76f1cc15 Reviewed-by: Matthew Vogt <matthew.vogt@qinetic.com.au> Reviewed-by: Konstantin Ritt <ritt.ks@gmail.com> Reviewed-by: Robin Burchell <robin+qt@viroteck.net> Reviewed-by: Renato Araujo Oliveira Filho <renato.filho@canonical.com> Reviewed-by: Alex Blasche <alexander.blasche@digia.com>
Diffstat (limited to 'src/versit/qversitreader_p.cpp')
-rw-r--r--src/versit/qversitreader_p.cpp38
1 files changed, 36 insertions, 2 deletions
diff --git a/src/versit/qversitreader_p.cpp b/src/versit/qversitreader_p.cpp
index 32f060d45..17db4d24a 100644
--- a/src/versit/qversitreader_p.cpp
+++ b/src/versit/qversitreader_p.cpp
@@ -334,8 +334,42 @@ bool LineReader::tryReadLine(LByteArray *cursor, bool atEnd)
return false;
} else {
// Found the CRLF.
- cursor->mEnd = crlfPos;
- return true;
+ // Hack: if malformed vCard files (having no \r\n or \r\n\r\n ending) are
+ // concatenated, we can get a malformed line in the document which looks like:
+ // END:VCARDBEGIN:VCARD
+ // In that situation, we should actually insert the \r\n sequence manually,
+ // and return mEnd after the END:VCARD\r\n position.
+ QByteArray cr(VersitUtils::encode('\r', mCodec));
+ QByteArray lf(VersitUtils::encode('\n', mCodec));
+ QByteArray ev(VersitUtils::encode(QByteArray("END:VCARD"), mCodec));
+ QByteArray evbv(VersitUtils::encode(QByteArray("END:VCARDBEGIN:VCARD"), mCodec));
+ QByteArray evcrlf(VersitUtils::encode(QByteArray("END:VCARD\r\n"), mCodec));
+ int crSz = cr.size();
+ int lfSz = lf.size();
+ int evSz = ev.size();
+ int evcrlfSz = evcrlf.size();
+
+ QByteArray possiblyMalformedLine = cursor->mData.mid(cursor->mStart, crlfPos-cursor->mStart);
+ int pmlEnd = possiblyMalformedLine.size() - 1;
+ while (true) {
+ if (QVersitReaderPrivate::containsAt(possiblyMalformedLine, cr, pmlEnd - crSz)) {
+ possiblyMalformedLine.chop(crSz);
+ } else if (QVersitReaderPrivate::containsAt(possiblyMalformedLine, lf, pmlEnd - lfSz)) {
+ possiblyMalformedLine.chop(lfSz);
+ } else {
+ break;
+ }
+ }
+ if (possiblyMalformedLine == evbv) {
+ // fix up the malformed line, return the end cursor after it.
+ cursor->mData.replace(cursor->mStart, evSz, evcrlf);
+ cursor->mEnd = cursor->mStart+evcrlfSz;
+ return true;
+ } else {
+ // A well-formed line.
+ cursor->mEnd = crlfPos;
+ return true;
+ }
}
}
}