base_file_utilities.cpp 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. // This file is part of Desktop App Toolkit,
  2. // a set of libraries for developing nice desktop applications.
  3. //
  4. // For license and copyright information please follow this link:
  5. // https://github.com/desktop-app/legal/blob/master/LEGAL
  6. //
  7. #include "base/base_file_utilities.h"
  8. #include "base/platform/base_platform_file_utilities.h"
  9. #include <QtCore/QResource>
  10. namespace base {
  11. QString FileNameFromUserString(QString name) {
  12. // We don't want LTR/RTL mark/embedding/override/isolate chars
  13. // in filenames, because they introduce a security issue, when
  14. // an executable "Fil[x]gepj.exe" may look like "Filexe.jpeg".
  15. const ushort kBad[] = {
  16. 0x200E, // LTR Mark
  17. 0x200F, // RTL Mark
  18. 0x202A, // LTR Embedding
  19. 0x202B, // RTL Embedding
  20. 0x202D, // LTR Override
  21. 0x202E, // RTL Override
  22. 0x2066, // LTR Isolate
  23. 0x2067, // RTL Isolate
  24. '/', '\\', '<', '>', ':', '"', '|', '?', '*' };
  25. for (auto &ch : name) {
  26. if (ch.unicode() < 32 || ranges::find(kBad, ch.unicode()) != end(kBad)) {
  27. ch = '_';
  28. }
  29. }
  30. if (name.isEmpty() || name.endsWith(' ') || name.endsWith('.')) {
  31. name.append('_');
  32. }
  33. return Platform::FileNameFromUserString(std::move(name));
  34. }
  35. void RegisterBundledResources(const QString &name) {
  36. const auto location = Platform::BundledResourcesPath();
  37. if (!QResource::registerResource(location + '/' + name)) {
  38. Unexpected("Packed resources not found.");
  39. }
  40. }
  41. } // namespace base