8#include <qcryptographichash.h>
9#include <qstandardpaths.h>
10#include <qstringconverter.h>
14#if defined(Q_OS_DARWIN)
15# include "private/qcore_mac_p.h"
16# if !defined(SHM_NAME_MAX)
21# define SHM_NAME_MAX 30
23#elif defined(Q_OS_WINDOWS)
24# include "qt_windows.h"
27#if QT_CONFIG(sharedmemory) || QT_CONFIG(systemsemaphore)
31using namespace Qt::StringLiterals;
33static QStringView staticTypeToString(QNativeIpcKey::Type type)
36 case QNativeIpcKey::Type::SystemV:
38 case QNativeIpcKey::Type::PosixRealtime:
40 case QNativeIpcKey::Type::Windows:
46static QString typeToString(QNativeIpcKey::Type type)
48 QStringView typeString = staticTypeToString(type);
50 case QNativeIpcKey::Type::SystemV:
51 case QNativeIpcKey::Type::PosixRealtime:
52 case QNativeIpcKey::Type::Windows:
53 return QString::fromRawData(typeString.constData(), typeString.size());
56 int value =
int(type);
57 if (value >= 1 && value <= 0xff) {
59 typeString = staticTypeToString(QNativeIpcKey::Type::SystemV);
60 return typeString + QString::number(-value);
66static QNativeIpcKey::Type stringToType(QStringView typeString)
68 if (typeString == staticTypeToString(QNativeIpcKey::Type::PosixRealtime))
69 return QNativeIpcKey::Type::PosixRealtime;
70 if (typeString == staticTypeToString(QNativeIpcKey::Type::Windows))
71 return QNativeIpcKey::Type::Windows;
73 auto fromNumber = [](QStringView number,
int low,
int high) {
75 int n = -number.toInt(&ok, 10);
76 if (!ok || n < low || n > high)
77 return QNativeIpcKey::Type{};
78 return QNativeIpcKey::Type(n);
81 QStringView sysv = staticTypeToString(QNativeIpcKey::Type::SystemV);
82 if (typeString.startsWith(sysv)) {
83 if (typeString.size() == sysv.size())
84 return QNativeIpcKey::Type::SystemV;
85 return fromNumber(typeString.sliced(sysv.size()), 1, 0xff);
89 return QNativeIpcKey::Type{};
93
94
95
96
97
98
99
100
101
102
103
104QNativeIpcKey QtIpcCommon::legacyPlatformSafeKey(
const QString &key, QtIpcCommon::IpcType ipcType,
105 QNativeIpcKey::Type type)
107 QNativeIpcKey k(type);
111 QByteArray hex = QCryptographicHash::hash(key.toUtf8(), QCryptographicHash::Sha1).toHex();
113 if (type == QNativeIpcKey::Type::PosixRealtime) {
114#if defined(Q_OS_DARWIN)
115 if (qt_apple_isSandboxed()) {
120 QNativeIpcKeyPrivate::setNativeAndLegacyKeys(k, key, key);
126 QString native = u'/' + QLatin1StringView(hex).left(SHM_NAME_MAX - 1);
127 QNativeIpcKeyPrivate::setNativeAndLegacyKeys(k, native, key);
134 result.reserve(1 + 18 + key.size() + 40);
136 case IpcType::SharedMemory:
137 result +=
"qipc_sharedmemory_"_L1;
139 case IpcType::SystemSemaphore:
140 result +=
"qipc_systemsem_"_L1;
144 for (QChar ch : key) {
145 if ((ch >= u'a' && ch <= u'z') ||
146 (ch >= u'A' && ch <= u'Z'))
149 result.append(QLatin1StringView(hex));
152 case QNativeIpcKey::Type::Windows:
153 if (isIpcSupported(ipcType, QNativeIpcKey::Type::Windows))
154 QNativeIpcKeyPrivate::setNativeAndLegacyKeys(k, result, key);
156 case QNativeIpcKey::Type::PosixRealtime:
157 result.prepend(u'/');
158 if (isIpcSupported(ipcType, QNativeIpcKey::Type::PosixRealtime))
159 QNativeIpcKeyPrivate::setNativeAndLegacyKeys(k, result, key);
161 case QNativeIpcKey::Type::SystemV:
164 if (isIpcSupported(ipcType, QNativeIpcKey::Type::SystemV)) {
165 result = QStandardPaths::writableLocation(QStandardPaths::TempLocation) + u'/' + result;
166 QNativeIpcKeyPrivate::setNativeAndLegacyKeys(k, result, key);
172
173
174
175
176
177
178QNativeIpcKey QtIpcCommon::platformSafeKey(
const QString &key, QtIpcCommon::IpcType ipcType,
179 QNativeIpcKey::Type type)
181 QNativeIpcKey k(type);
186 case QNativeIpcKey::Type::PosixRealtime:
187 if (isIpcSupported(ipcType, QNativeIpcKey::Type::PosixRealtime)) {
192 k.setNativeKey(u'/' + QStringView(key).left(SHM_NAME_MAX - 1));
194 k.setNativeKey(u'/' + key);
199 case QNativeIpcKey::Type::Windows:
200 if (isIpcSupported(ipcType, QNativeIpcKey::Type::Windows)) {
202 QStringView payload = key;
204 for (QStringView candidate : { u"Local\\"_sv, u"Global\\"_sv }) {
205 if (!key.startsWith(candidate))
208 payload = payload.sliced(prefix.size());
214 case IpcType::SharedMemory: mid = u"shm_";
break;
215 case IpcType::SystemSemaphore: mid = u"sem_";
break;
218 QString result = prefix + mid + payload;
220 result.truncate(MAX_PATH);
222 k.setNativeKey(result);
226 case QNativeIpcKey::Type::SystemV:
231 if (isIpcSupported(ipcType, QNativeIpcKey::Type::SystemV)) {
232 if (key.startsWith(u'/')) {
235 QString baseDir = QStandardPaths::writableLocation(QStandardPaths::RuntimeLocation);
236 k.setNativeKey(baseDir + u'/' + key);
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
328
329
330
331
332
333
334
335
336
337
338
341
342
343
344
345
346
347
348
349
352
353
354
355
356
357
358
359
360
361
362
363#if defined(Q_OS_DARWIN)
364QNativeIpcKey::Type QNativeIpcKey::defaultTypeForOs_internal()
noexcept
366 if (qt_apple_isSandboxed())
367 return Type::PosixRealtime;
368 return Type::SystemV;
373
374
375
376
379
380
381
382
383
384
387
388
389
390
391
392
393
394void QNativeIpcKey::copy_internal(
const QNativeIpcKey &other)
396 d =
new QNativeIpcKeyPrivate(*other.d);
399void QNativeIpcKey::move_internal(QNativeIpcKey &&)
noexcept
404QNativeIpcKey &QNativeIpcKey::assign_internal(
const QNativeIpcKey &other)
406 Q_ASSERT(d || other.d);
412 d =
new QNativeIpcKeyPrivate(*other.d);
417
418
419
420
421void QNativeIpcKey::destroy_internal()
noexcept
427
428
429
432
433
434
435
436
437
440
441
442
443
444
445
448
449
450
451
452
453
454
455
456
457
458
461
462
463
464
465
466
469
470
471
472
473
474
475void QNativeIpcKey::setType_internal(Type type)
481
482
483
484
485
486
489
490
491
492
493
494
495void QNativeIpcKey::setNativeKey_internal(
const QString &)
497 d->legacyKey_.clear();
501
502
503
504size_t qHash(
const QNativeIpcKey &ipcKey, size_t seed)
noexcept
508 return qHashMulti(seed, ipcKey.key, ipcKey.type());
512
513
514
515
516
517int QNativeIpcKey::compare_internal(
const QNativeIpcKey &lhs,
const QNativeIpcKey &rhs)
noexcept
519 return (QNativeIpcKeyPrivate::legacyKey(lhs) == QNativeIpcKeyPrivate::legacyKey(rhs)) ? 0 : 1;
523
524
525
526
527
528
529
530
531
532QString QNativeIpcKey::toString()
const
534 QString prefix = typeToString(type());
535 if (prefix.isEmpty()) {
536 Q_ASSERT(prefix.isNull());
540 QString copy = nativeKey();
541 copy.replace(u'%',
"%25"_L1);
542 if (copy.startsWith(
"//"_L1))
543 copy.replace(0, 2, u"/%2F"_s);
547 u.setPath(copy, QUrl::TolerantMode);
550 if (!d->legacyKey_.isEmpty())
551 q.addQueryItem(u"legacyKey"_s, QString(d->legacyKey_).replace(u'%',
"%25"_L1));
554 return u.toString(QUrl::DecodeReserved);
558
559
560
561
562
563
564
565
566
567QNativeIpcKey QNativeIpcKey::fromString(
const QString &text)
569 QUrl u(text, QUrl::TolerantMode);
570 Type invalidType = {};
571 Type type = stringToType(u.scheme());
572 if (type == invalidType || !u.isValid() || !u.userInfo().isEmpty() || !u.host().isEmpty()
574 return QNativeIpcKey(invalidType);
576 QNativeIpcKey result(QString(), type);
577 if (result.type() != type)
578 return QNativeIpcKey(invalidType);
581 result.setNativeKey(u.path());
584 const QList items = QUrlQuery(u).queryItems();
585 for (
const auto &item : items) {
586 if (item.first == u"legacyKey"_s) {
587 QString legacyKey = QUrl::fromPercentEncoding(item.second.toUtf8());
588 QNativeIpcKeyPrivate::setLegacyKey(result, std::move(legacyKey));
591 return QNativeIpcKey(invalidType);
600#include "moc_qtipccommon.cpp"