Branch data Line data Source code
1 : : // SPDX-FileCopyrightText: 2018 Anne Jan Brouwer
2 : : // SPDX-License-Identifier: GPL-3.0-or-later
3 : : /**
4 : : * @file
5 : : * @brief Implements FileContent::parse() as a single streaming pass over the
6 : : * entry, with the field cap, the display-line cap and the OTP candidate
7 : : * search, plus the cached secret-field-name matcher.
8 : : */
9 : : #include "filecontent.h"
10 : : #include "totp.h"
11 : : #include <QRegularExpression>
12 : :
13 : : #include <QCoreApplication>
14 : : #include <QStringTokenizer>
15 : :
16 : : #include <algorithm>
17 : : #include <limits>
18 : :
19 : : #include <utility>
20 : :
21 : : /**
22 : : * @brief Shorthand for the public FileContent::isOtpUriValue.
23 : : * @param value A line or field value.
24 : : * @return true when it is an `otpauth://` URI.
25 : : */
26 : : static auto isOtpUri(const QString &value) -> bool {
27 : 31192 : return FileContent::isOtpUriValue(value);
28 : : }
29 : :
30 : : /**
31 : : * @brief The otpauth URI a line is written as: a bare `otpauth://` line, or
32 : : * the split `otpauth: //...` that 1.8 to 2.0-rc2 saved (a template field
33 : : * "otpauth" with the rest of the URI as its value), its blanks dropped.
34 : : * @param line One line of the entry.
35 : : * @return The URI, trimmed, or an empty string when the line is not one.
36 : : */
37 : 31068 : static auto bareOtpUriOf(const QString &line) -> QString {
38 : : QString trimmed = line.trimmed();
39 [ + + ]: 31068 : if (isOtpUri(trimmed)) {
40 : 28 : return trimmed;
41 : : }
42 : 31054 : static const QString scheme = QStringLiteral("otpauth:");
43 [ + + + ]: 31040 : if (!trimmed.startsWith(scheme, Qt::CaseInsensitive)) {
44 : 31025 : return {};
45 : : }
46 : : qsizetype i = scheme.size();
47 [ + - + + ]: 30 : while (i < trimmed.size() && trimmed.at(i).isSpace()) {
48 : 15 : ++i;
49 : : }
50 [ + + + ]: 30 : if (trimmed.mid(i, 2) != QLatin1String("//")) {
51 : 5 : return {};
52 : : }
53 : 20 : return trimmed.left(scheme.size()) + trimmed.mid(i);
54 : : }
55 : :
56 : : /**
57 : : * @brief Whether a body line must stay out of the parsed display because it
58 : : * carries a TOTP shared secret (FileContent::isOtpLine()). "Display the
59 : : * file's content as-is" bypasses this filter and shows the entry raw.
60 : : * @param line One line of the entry body.
61 : : * @return true when the line is kept out of getRemainingDataForDisplay().
62 : : */
63 : : static auto isLineHidden(const QString &line) -> bool {
64 : 30258 : return FileContent::isOtpLine(line);
65 : : }
66 : :
67 : : /**
68 : : * @brief Whether @p value is written as a TOTP seed: base32 letters and the
69 : : * digits 2 to 7 only (either case; white space, dashes, dots or underscores
70 : : * between groups and "=" padding allowed), at least 16 of them (80 bits, the
71 : : * shortest seed in common use), that Totp::isValid() takes.
72 : : *
73 : : * Strict where Totp::parse() is lenient (it drops punctuation and reads 0, 1
74 : : * and 8 as letters, which almost any password survives): this decides
75 : : * whether a first line is a seed or the password.
76 : : * @param value The value of an `OTP:`/`TOTP:` field.
77 : : * @return true when it can only be read as a seed.
78 : : */
79 : 49 : static auto isBase32Seed(const QString &value) -> bool {
80 : : qsizetype letters = 0;
81 [ + + + + ]: 699 : for (const QChar c : value) {
82 : : const char16_t u = c.unicode();
83 : : // Grouping and padding, as seeds are written and pasted: spaces, tabs, a
84 : : // no-break space, dashes, dots, underscores, "=". Base32::sanitizeInput()
85 : : // drops them too, and Totp::parse() accepts them.
86 [ + + + + ]: 664 : if (c.isSpace() || u == u'-' || u == u'.' || u == u'_' || u == u'=') {
87 : 80 : continue;
88 : : }
89 [ + + ]: 584 : if (!((u >= u'A' && u <= u'Z') || (u >= u'a' && u <= u'z') ||
90 [ + + ]: 80 : (u >= u'2' && u <= u'7'))) {
91 : : return false;
92 : : }
93 : 570 : ++letters;
94 : : }
95 : : constexpr qsizetype kMinSeedLetters = 16;
96 [ + + - + ]: 35 : return letters >= kMinSeedLetters && Totp::isValid(value);
97 : : }
98 : :
99 : : /**
100 : : * @brief What follows a line's first colon; empty without one.
101 : : * @param line One line of the entry.
102 : : * @return The text after the first ':', or an empty string.
103 : : */
104 : 55 : static auto afterFirstColon(const QString &line) -> QString {
105 : 55 : const qsizetype colon = line.indexOf(u':');
106 [ - + ]: 55 : return colon < 0 ? QString() : line.mid(colon + 1);
107 : : }
108 : :
109 : : /**
110 : : * @brief Whether a first line is written as an OTP configuration that
111 : : * otpConfigOf() can read: a bare otpauth URI (split or not, see
112 : : * bareOtpUriOf()), a field whose value is one, or
113 : : * an `OTP:`/`TOTP:` field whose value is written as a base32 seed.
114 : : * @param line The entry's first line.
115 : : * @return true when the line is read as the OTP configuration.
116 : : */
117 : 793 : static auto holdsOtpConfig(const QString &line) -> bool {
118 [ + + ]: 1586 : if (!bareOtpUriOf(line).isEmpty()) {
119 : : return true;
120 : : }
121 : 786 : const qsizetype colon = line.indexOf(u':');
122 [ + + ]: 786 : if (colon < 0) {
123 : : return false;
124 : : }
125 : 69 : const QString value = line.mid(colon + 1);
126 : : // Any field holding an otpauth URI is a shared secret; an OTP-named field
127 : : // only when what it holds would give a code: the line is the password
128 : : // otherwise, as the pass format has it.
129 [ + + + + ]: 136 : return isOtpUri(value) ||
130 [ + + + + : 185 : (FileContent::isOtpFieldName(line.left(colon)) && isBase32Seed(value));
+ ]
131 : : }
132 : :
133 : : /**
134 : : * @brief The OTP configuration an OTP line holds: the URI for a bare
135 : : * `otpauth://` line (bareOtpUriOf()), the value for a field.
136 : : * @param line A line for which holdsOtpConfig() is true.
137 : : * @return The configuration, trimmed.
138 : : */
139 : 17 : static auto otpConfigOf(const QString &line) -> QString {
140 [ + + ]: 17 : if (const QString uri = bareOtpUriOf(line); !uri.isEmpty()) {
141 : : return uri;
142 : : }
143 : 20 : return line.mid(line.indexOf(u':') + 1).trimmed();
144 : : }
145 : :
146 : : namespace {
147 : :
148 : : /**
149 : : * @brief The OTP configurations found of one kind: the first, and the first
150 : : * that gives a code.
151 : : */
152 : : struct OtpCandidate {
153 : : QString first; ///< The first configuration offered.
154 : : QString usable; ///< The first one Totp::isValid() accepts.
155 : :
156 : : /**
157 : : * @brief Note @p config, until one that gives a code was noted.
158 : : * @param config A configuration of this kind, trimmed.
159 : : */
160 : 79 : void offer(const QString &config) {
161 [ + + ]: 79 : if (!usable.isEmpty()) {
162 : : return;
163 : : }
164 [ + + ]: 76 : if (first.isEmpty()) {
165 : 71 : first = config;
166 : : }
167 [ + + ]: 76 : if (Totp::isValid(config)) {
168 : 61 : usable = config;
169 : : }
170 : : }
171 : : };
172 : :
173 : : /**
174 : : * @brief Where an OTP configuration was found, in the order getOtpUri()
175 : : * prefers: a named field first, then a bare `otpauth://` line, then an
176 : : * `OTP:` line the template did not promote to a field.
177 : : */
178 : : struct OtpCandidates {
179 : : OtpCandidate field; ///< OTP-named or URI-valued fields.
180 : : OtpCandidate bareLine; ///< Bare `otpauth://` lines (password line too).
181 : : OtpCandidate remainingField; ///< `OTP:` lines left as text.
182 : :
183 : : /**
184 : : * @brief The first configuration that gives a code, in the order above,
185 : : * so an unusable one (an `otpauth-migration://` export, HOTP) does not
186 : : * hide a later one that works. With none usable, the first found, for
187 : : * the "Invalid OTP secret" note.
188 : : * @return The configuration string, or empty when none was found.
189 : : */
190 : 247 : auto pick() const -> QString {
191 [ + + + + ]: 849 : for (const OtpCandidate *kind : {&field, &bareLine, &remainingField}) {
192 [ + + ]: 662 : if (!kind->usable.isEmpty()) {
193 : 60 : return kind->usable;
194 : : }
195 : : }
196 [ + + + + ]: 726 : for (const OtpCandidate *kind : {&field, &bareLine, &remainingField}) {
197 [ + + ]: 548 : if (!kind->first.isEmpty()) {
198 : 9 : return kind->first;
199 : : }
200 : : }
201 : 178 : return {};
202 : : }
203 : : };
204 : :
205 : : /**
206 : : * @brief Takes @p line as a field when the template lists it, "Template all
207 : : * fields" is on and the value is not a URL, or @p alsoField asks for its
208 : : * name; records any OTP configuration in @p otp either way.
209 : : * @param line One line of the entry body.
210 : : * @param templateFields Field names the template promotes.
211 : : * @param allFields The "Template all fields" setting.
212 : : * @param alsoField Extra predicate on the trimmed name; may be empty.
213 : : * @param namedValues Receives the field when it is taken.
214 : : * @param otp Is offered the OTP candidates of each kind.
215 : : * @return true when the line became a field and is not remaining text.
216 : : */
217 : 33380 : auto takeNamedValue(const QString &line, const QStringList &templateFields,
218 : : bool allFields,
219 : : const std::function<bool(const QString &)> &alsoField,
220 : : NamedValues &namedValues, OtpCandidates &otp) -> bool {
221 : 33380 : const qsizetype colon = line.indexOf(u':');
222 [ + + ]: 33380 : if (colon < 0) {
223 : : return false;
224 : : }
225 : 3213 : const QString name = line.left(colon);
226 : 3213 : const QString value = line.right(line.length() - colon - 1);
227 : : // A value starting with // means the colon is probably from a URL.
228 [ + + ]: 3125 : if (!templateFields.contains(name) &&
229 [ + + + + : 9551 : !(allFields && !value.startsWith(QStringLiteral("//"))) &&
+ + + + +
+ + - - ]
230 [ + + + + : 3234 : !(alsoField && alsoField(name.trimmed()))) {
+ - - ]
231 : : // An `OTP:` line the template did not promote to a field.
232 [ + + + ]: 49 : if (FileContent::isOtpFieldName(name)) {
233 : 10 : otp.remainingField.offer(value.trimmed());
234 : : }
235 : 49 : return false;
236 : : }
237 : 6328 : namedValues.append({name.trimmed(), value.trimmed(), line});
238 : : // Keyed on the value as well as the name: a field called anything at all
239 : : // whose value is an otpauth URI is still a shared secret.
240 [ + + + + : 3164 : if (FileContent::isOtpFieldName(name) || FileContent::isOtpUriValue(value)) {
+ + ]
241 : 66 : otp.field.offer(value.trimmed());
242 : : }
243 : : return true;
244 : : }
245 : :
246 : : /**
247 : : * @brief The line ending a text cut at FileContent::kMaxDisplayLines.
248 : : * @param count How many lines were left out.
249 : : * @return The translated "… %n more line(s) not shown" line.
250 : : */
251 : 2 : auto moreLinesNotShown(qsizetype count) -> QString {
252 : : // The count in a variable of its own: lupdate up to 6.8 loses a
253 : : // translate() call with template arguments among its arguments and would
254 : : // mark the string vanished.
255 : : const int n = static_cast<int>(
256 : 2 : std::min<qsizetype>(count, std::numeric_limits<int>::max()));
257 : : return QCoreApplication::translate("FileContent",
258 : 2 : "… %n more line(s) not shown", nullptr, n);
259 : : }
260 : :
261 : : /**
262 : : * @brief The lines of an entry that are no field, as kept and as shown.
263 : : */
264 : 247 : struct RemainingText {
265 : : QString data; ///< Every line, joined with "\n".
266 : : QString display; ///< The lines shown, cut at the display cap.
267 : : qsizetype lines = 0; ///< How many lines data holds.
268 : : qsizetype displayed = 0; ///< How many lines display holds.
269 : : qsizetype notShown = 0; ///< Shown lines left out past the cap.
270 : :
271 : : /**
272 : : * @brief Adds @p line to the text, and to the screen when @p shown.
273 : : * @param line The line, without its line break.
274 : : * @param shown Whether the line may be on the screen.
275 : : */
276 : 30258 : void add(const QString &line, bool shown) {
277 : : // Joined with "\n" as they go, as split() and join() did.
278 [ + + ]: 30258 : if (lines > 0) {
279 : 30087 : data += QLatin1Char('\n');
280 : : }
281 : 30258 : data += line;
282 : 30258 : ++lines;
283 [ + + ]: 30258 : if (!shown) {
284 : : return;
285 : : }
286 [ + + ]: 30217 : if (displayed == FileContent::kMaxDisplayLines) {
287 : 7 : ++notShown;
288 : 7 : return;
289 : : }
290 [ + + ]: 30210 : if (displayed > 0) {
291 : 30056 : display += QLatin1Char('\n');
292 : : }
293 : 30210 : display += line;
294 : 30210 : ++displayed;
295 : : }
296 : :
297 : : /**
298 : : * @brief The text for the screen.
299 : : * @return display, with a "more lines" line when the cap left some
300 : : * out.
301 : : */
302 : 247 : [[nodiscard]] auto forDisplay() const -> QString {
303 [ + + ]: 247 : if (notShown > 0) {
304 : 2 : return display + QLatin1Char('\n') + moreLinesNotShown(notShown);
305 : : }
306 : : return display;
307 : : }
308 : : };
309 : :
310 : : } // namespace
311 : :
312 : 247 : auto FileContent::parse(const QString &fileContent,
313 : : const QStringList &templateFields, bool allFields,
314 : : const std::function<bool(const QString &)> &alsoField)
315 : : -> FileContent {
316 : : // Line by line over the text, never a list of every line: a compressed
317 : : // entry can expand into tens of millions of lines within the plaintext
318 : : // limit, and a QString each (empty ones too) would take gigabytes.
319 : : QStringTokenizer tokens = QStringView(fileContent).tokenize(u'\n');
320 : : auto it = tokens.begin();
321 : 247 : QString password;
322 [ + - ]: 247 : if (it != tokens.end()) {
323 : 494 : password = passwordLine(*it);
324 : : ++it;
325 : : }
326 : 247 : RemainingText remaining;
327 : : // Past kMaxNamedFields a line is still offered to takeNamedValue(), into
328 : : // this throwaway list, for what it notes about OTP: the line stays text.
329 : 247 : NamedValues pastTheCap;
330 : 247 : NamedValues namedValues;
331 : 247 : OtpCandidates otp;
332 : : // An entry from `pass otp insert` has the URI as its only (password) line,
333 : : // which the loop below never sees; an `OTP:`/`TOTP:` field written there by
334 : : // hand holding a usable secret is the same, and yields a code as it does
335 : : // further down. Read only from what is written as a configuration
336 : : // (holdsOtpConfig()); a first line with a URI elsewhere in it is kept off
337 : : // the screen all the same (isOtpPasswordLine()).
338 [ + + + ]: 247 : if (holdsOtpConfig(password)) {
339 : 34 : otp.bareLine.offer(otpConfigOf(password));
340 : : }
341 [ + + ]: 33660 : for (; it != tokens.end(); ++it) {
342 : : const QString line = it->toString();
343 : : bool secretPastTheCap = false;
344 : : // A line with an otpauth URI in it is never a field unless the URI is
345 : : // its value ("2fa: otpauth://..."), also not when the template lists
346 : : // "otpauth": split at a colon, the value would lose the scheme that
347 : : // marks it as a shared secret and be shown seed and all ("otpauth://...",
348 : : // "AWS: backup phone: otpauth://..."). Past kMaxNamedFields, lines stay
349 : : // text.
350 [ + + + + : 33468 : if (!carriesOtpUri(line) || isOtpUri(afterFirstColon(line))) {
+ + - - ]
351 [ + + ]: 33380 : if (namedValues.size() < kMaxNamedFields) {
352 [ + + + ]: 33370 : if (takeNamedValue(line, templateFields, allFields, alsoField,
353 : : namedValues, otp)) {
354 : : continue;
355 : : }
356 : : } else {
357 : 10 : takeNamedValue(line, templateFields, allFields, alsoField, pastTheCap,
358 : : otp);
359 : : // A secret past the cap stays text, but not on screen: the panel
360 : : // would have hidden it.
361 [ + + + + : 13 : secretPastTheCap = !pastTheCap.isEmpty() && alsoField &&
+ + ]
362 : 3 : alsoField(pastTheCap.first().name);
363 : 10 : pastTheCap.clear();
364 : : }
365 : : }
366 [ + + + + : 30299 : remaining.add(line, !isLineHidden(line) && !secretPastTheCap);
+ ]
367 : : // A bare otpauth:// line, the convention the pass-otp extension uses,
368 : : // or the split form older versions saved.
369 [ + + + ]: 30258 : if (const QString uri = bareOtpUriOf(line); !uri.isEmpty()) {
370 : 24 : otp.bareLine.offer(uri);
371 : : }
372 : : }
373 : : // The OTP value stays in namedValues/remainingData: the edit dialog reads it
374 : : // from there and PasswordDialog::getPassword() drops empty fields, so
375 : : // stripping it here would delete the secret on the next save. Suppression
376 : : // happens in getRemainingDataForDisplay() and PasswordDisplayPanel.
377 : : return {std::move(password), std::move(namedValues),
378 : 494 : remaining.data, remaining.forDisplay(),
379 : 988 : otp.pick(), remaining.lines};
380 : 247 : }
381 : :
382 : 3601 : auto FileContent::isOtpFieldName(const QString &name) -> bool {
383 : : const QString trimmed = name.trimmed();
384 [ + + + + ]: 10701 : return trimmed.compare(QStringLiteral("OTP"), Qt::CaseInsensitive) == 0 ||
385 [ + + ]: 7100 : trimmed.compare(QStringLiteral("TOTP"), Qt::CaseInsensitive) == 0;
386 : : }
387 : :
388 : 112 : auto FileContent::isSecretFieldName(const QString &name,
389 : : const QStringList &secrets) -> bool {
390 : : // One pattern for the whole list, compiled once per list: the panel asks
391 : : // for every field of every entry shown, and the list rarely changes.
392 : 112 : thread_local QStringList cachedSecrets;
393 : 112 : thread_local QStringList cachedWords;
394 : 112 : thread_local QRegularExpression cachedPattern;
395 : : thread_local bool cachedYet = false;
396 : : constexpr auto options = QRegularExpression::CaseInsensitiveOption |
397 : : QRegularExpression::UseUnicodePropertiesOption;
398 [ + + + + ]: 112 : if (!cachedYet || secrets != cachedSecrets) {
399 : 10 : cachedWords.clear();
400 [ + + ]: 40057 : for (const QString &secret : secrets) {
401 : : const QString word = secret.trimmed();
402 [ + + ]: 40047 : if (word.isEmpty()) {
403 : : continue;
404 : : }
405 : : // A word counts when no letter touches it: digits and `_` end a word
406 : : // as a blank does ("PIN2", "api_key"), and a plural "s" may follow
407 : : // ("tokens", "Security answers"); "key" is still not in "keyboard".
408 : : // A word that starts or ends with a digit is not part of a longer
409 : : // number either ("tan1" not in "tan12").
410 : 40046 : const QString letters = QStringLiteral("\\p{L}\\p{M}");
411 : : const QString before =
412 [ - + - - : 80092 : word.front().isDigit() ? letters + QStringLiteral("\\d") : letters;
+ + + ]
413 : : const QString after =
414 [ + + + + : 80092 : word.back().isDigit() ? letters + QStringLiteral("\\d") : letters;
+ + ]
415 : 40046 : cachedWords << QStringLiteral("(?<![%1])%2s?(?![%3])")
416 : 120138 : .arg(before, QRegularExpression::escape(word), after);
417 : : }
418 : : cachedSecrets = secrets;
419 : 10 : cachedYet = true;
420 : 10 : cachedPattern = QRegularExpression(
421 : 20 : QStringLiteral("(?:%1)").arg(cachedWords.join(QLatin1Char('|'))),
422 [ + + ]: 20 : options);
423 : : }
424 [ + + ]: 112 : if (cachedWords.isEmpty()) {
425 : : return false;
426 : : }
427 : : // camelCase names its words with capitals, which a case-insensitive match
428 : : // does not see, so the name is also tried with a blank at each word start:
429 : : // "apiKey" as "api Key", "APIKey" as "API Key". The acronym rule cuts a
430 : : // capitals word that a lower-case letter follows ("cardPINs" as "card PI
431 : : // Ns"), so a split at lower-to-upper starts alone is tried too ("card
432 : : // PINs", "sim PINcode"), and the name as written ("PINcode").
433 : : static const QRegularExpression lowerThenUpper(
434 : 3 : QStringLiteral("(?<=[\\p{Ll}\\d])(?=\\p{Lu})"),
435 [ + + + - : 116 : QRegularExpression::UseUnicodePropertiesOption);
+ ]
436 : : static const QRegularExpression acronymThenWord(
437 : 3 : QStringLiteral("(?<=\\p{Lu})(?=\\p{Lu}\\p{Ll})"),
438 [ + + + - : 116 : QRegularExpression::UseUnicodePropertiesOption);
+ ]
439 : : QString camel = name;
440 : 220 : camel.replace(lowerThenUpper, QStringLiteral(" "));
441 : : QString words = camel;
442 : 220 : words.replace(acronymThenWord, QStringLiteral(" "));
443 : 120230 : const auto matches = [&](const QRegularExpression &pattern) {
444 [ + + + + ]: 240427 : return pattern.match(name).hasMatch() ||
445 [ + + + + : 240460 : (camel != name && pattern.match(camel).hasMatch()) ||
+ + + + +
+ - - ]
446 [ + + + + : 240460 : (words != camel && pattern.match(words).hasMatch());
+ + - - ]
447 : 110 : };
448 [ + + + ]: 110 : if (cachedPattern.isValid()) {
449 : 106 : return matches(cachedPattern);
450 : : }
451 : : // Too many words for one pattern (PCRE2's size limit): word by word,
452 : : // rather than every secret field shown in plain view.
453 : 4 : return std::any_of(cachedWords.cbegin(), cachedWords.cend(),
454 : 120124 : [&matches, options](const QString &word) {
455 : 120124 : return matches(QRegularExpression(word, options));
456 : 4 : });
457 : : }
458 : :
459 : 34331 : auto FileContent::isOtpUriValue(const QString &value) -> bool {
460 : 68662 : return value.trimmed().startsWith(QStringLiteral("otpauth://"),
461 : 34331 : Qt::CaseInsensitive);
462 : : }
463 : :
464 : 94105 : auto FileContent::carriesOtpUri(const QString &text) -> bool {
465 : : // Not only at the start: "- otpauth://", "\"otpauth://...\"", "old token
466 : : // otpauth://", "AWS: backup phone: otpauth://" and the split "otpauth:
467 : : // //totp/" all showed the seed, being neither a bare line nor the value
468 : : // after the first colon. By hand, not a regular expression: parse() asks
469 : : // this for every line of an entry, up to 64 MiB of them.
470 : 94113 : static const QString scheme = QStringLiteral("otpauth:");
471 [ + + ]: 94117 : for (qsizetype at = text.indexOf(scheme, 0, Qt::CaseInsensitive); at >= 0;
472 : 12 : at = text.indexOf(scheme, at + 1, Qt::CaseInsensitive)) {
473 : 139 : qsizetype i = at + scheme.size();
474 [ + - + + ]: 169 : while (i < text.size() && text.at(i).isSpace()) {
475 : 30 : ++i;
476 : : }
477 [ + + ]: 278 : if (text.mid(i, 2) == QLatin1String("//")) {
478 : : return true;
479 : : }
480 : : }
481 : : return false;
482 : : }
483 : :
484 : 260 : auto FileContent::passwordLine(QStringView text) -> QString {
485 : : // left(-1) is the whole text when there is no line feed.
486 : : QStringView line = text.left(text.indexOf(u'\n'));
487 : : if (line.endsWith(u'\r')) {
488 : : line.chop(1);
489 : : }
490 : 260 : return line.toString();
491 : : }
492 : :
493 : 60051 : auto FileContent::isOtpLine(const QString &line) -> bool {
494 [ + + ]: 60051 : if (carriesOtpUri(line)) {
495 : : return true;
496 : : }
497 : 60009 : const qsizetype colon = line.indexOf(u':');
498 [ + + + ]: 60078 : return colon >= 0 && isOtpFieldName(line.left(colon));
499 : : }
500 : :
501 : 567 : auto FileContent::isOtpPasswordLine(const QString &line) -> bool {
502 [ + + + + ]: 567 : return carriesOtpUri(line) || holdsOtpConfig(line);
503 : : }
504 : :
505 : 134 : auto FileContent::getPassword() const -> QString { return this->password; }
506 : :
507 : 61 : auto FileContent::getPasswordForDisplay() const -> QString {
508 [ + + ]: 61 : return isOtpPasswordLine(this->password) ? QString() : this->password;
509 : : }
510 : :
511 : 165 : auto FileContent::getNamedValues() const -> NamedValues {
512 : 165 : return this->namedValues;
513 : : }
514 : :
515 : 222 : auto FileContent::getRemainingData() const -> QString {
516 : 222 : return this->remainingData;
517 : : }
518 : :
519 : 77 : auto FileContent::getRemainingDataForDisplay() const -> QString {
520 : 77 : return this->remainingDataDisplay;
521 : : }
522 : :
523 : 88 : auto FileContent::getOtpUri() const -> QString { return this->otpUri; }
524 : :
525 : 247 : FileContent::FileContent(QString password, NamedValues namedValues,
526 : : QString remainingData, QString remainingDataDisplay,
527 : 247 : QString otpUri, qsizetype remainingLines)
528 : : : password(std::move(password)), namedValues(std::move(namedValues)),
529 : : remainingData(std::move(remainingData)),
530 : : remainingDataDisplay(std::move(remainingDataDisplay)),
531 : 247 : otpUri(std::move(otpUri)), remainingLines(remainingLines) {}
532 : :
533 : 5 : auto FileContent::boundedForDisplay(const QString &text) -> QString {
534 : : // Where the line after the last one shown starts, if there is one.
535 : : qsizetype end = -1;
536 [ + + ]: 20011 : for (int line = 0; line < kMaxDisplayLines; ++line) {
537 : 20010 : end = text.indexOf(QLatin1Char('\n'), end + 1);
538 [ + + ]: 20010 : if (end < 0) {
539 : : return text;
540 : : }
541 : : }
542 : : const qsizetype more =
543 : 1 : text.count(QLatin1Char('\n'), Qt::CaseSensitive) - (kMaxDisplayLines - 1);
544 : 2 : return text.left(end) + QLatin1Char('\n') + moreLinesNotShown(more);
545 : : }
546 : :
547 : 1 : auto FileContent::getRemainingLineCount() const -> qsizetype {
548 : 1 : return remainingLines;
549 : : }
550 : :
551 : 668 : NamedValues::NamedValues() = default;
552 : :
553 : 55 : NamedValues::NamedValues(std::initializer_list<NamedValue> values)
554 : 55 : : QList(values) {}
555 : :
556 : 11 : auto NamedValues::takeValue(const QString &name) -> QString {
557 : 11 : return takeNamedValue(name).value;
558 : : }
559 : :
560 : 103 : auto NamedValues::takeNamedValue(const QString &name) -> NamedValue {
561 [ + + ]: 117 : for (int i = 0; i < length(); ++i) {
562 [ + + ]: 45 : if (at(i).name == name) {
563 : 31 : return takeAt(i);
564 : : }
565 : : }
566 : 72 : return {};
567 : : }
|