summaryrefslogtreecommitdiffstats
path: root/src/libs/3rdparty/7zip/unix/CPP/7zip/UI/Common/ExtractingFilePath.cpp
blob: 852fd5adb5e70262f234a2e9b9f05aec7056b782 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
// ExtractingFilePath.cpp

#include "StdAfx.h"

#include "../../../Common/Wildcard.h"

#include "../../../Windows/FileName.h"

#include "ExtractingFilePath.h"

static UString ReplaceIncorrectChars(const UString &s, bool repaceColon)
{
  #ifdef _WIN32
  UString res;
  bool beforeColon = true;
  {
    for (unsigned i = 0; i < s.Len(); i++)
    {
      wchar_t c = s[i];
      if (beforeColon)
        if (c == '*' || c == '?' || c < 0x20 || c == '<' || c == '>' || c == '|' || c == '"')
          c = '_';
      if (c == ':')
      {
        if (repaceColon)
          c = '_';
        else
          beforeColon = false;
      }
      res += c;
    }
  }
  if (beforeColon)
  {
    for (int i = res.Len() - 1; i >= 0; i--)
    {
      wchar_t c = res[i];
      if (c != '.' && c != ' ')
        break;
      res.ReplaceOneCharAtPos(i, '_');
    }
  }
  return res;
  #else
  return s;
  #endif
}

#ifdef _WIN32

static const wchar_t *g_ReservedNames[] =
{
  L"CON", L"PRN", L"AUX", L"NUL"
};

static bool CheckTail(const UString &name, unsigned len)
{
  int dotPos = name.Find(L'.');
  if (dotPos < 0)
    dotPos = name.Len();
  UString s = name.Left(dotPos);
  s.TrimRight();
  return s.Len() != len;
}

static bool CheckNameNum(const UString &name, const wchar_t *reservedName)
{
  unsigned len = MyStringLen(reservedName);
  if (name.Len() <= len)
    return true;
  if (MyStringCompareNoCase_N(name, reservedName, len) != 0)
    return true;
  wchar_t c = name[len];
  if (c < L'0' || c > L'9')
    return true;
  return CheckTail(name, len + 1);
}

static bool IsSupportedName(const UString &name)
{
  for (unsigned i = 0; i < ARRAY_SIZE(g_ReservedNames); i++)
  {
    const wchar_t *reservedName = g_ReservedNames[i];
    unsigned len = MyStringLen(reservedName);
    if (name.Len() < len)
      continue;
    if (MyStringCompareNoCase_N(name, reservedName, len) != 0)
      continue;
    if (!CheckTail(name, len))
      return false;
  }
  if (!CheckNameNum(name, L"COM"))
    return false;
  return CheckNameNum(name, L"LPT");
}

#endif

static UString GetCorrectFileName(const UString &path, bool repaceColon)
{
  if (path == L".." || path == L".")
    return UString();
  return ReplaceIncorrectChars(path, repaceColon);
}

void MakeCorrectPath(bool isPathFromRoot, UStringVector &pathParts, bool replaceAltStreamColon)
{
  for (unsigned i = 0; i < pathParts.Size();)
  {
    UString &s = pathParts[i];
    #ifdef _WIN32
    bool needReplaceColon = (replaceAltStreamColon || i != pathParts.Size() - 1);
    if (i == 0 && isPathFromRoot && NWindows::NFile::NName::IsDrivePath(s))
    {
      UString s2 = s[0];
      s2 += L'_';
      s2 += GetCorrectFileName(s.Ptr(2), needReplaceColon);
      s = s2;
    }
    else
      s = GetCorrectFileName(s, needReplaceColon);
    #endif

    if (s.IsEmpty())
      pathParts.Delete(i);
    else
    {
      #ifdef _WIN32
      if (!IsSupportedName(s))
        s = (UString)L"_" + s;
      #endif
      i++;
    }
  }
}

UString MakePathNameFromParts(const UStringVector &parts)
{
  UString result;
  FOR_VECTOR (i, parts)
  {
    if (i != 0)
      result += WCHAR_PATH_SEPARATOR;
    result += parts[i];
  }
  return result;
}

static const wchar_t *k_EmptyReplaceName = L"[]";

void Correct_IfEmptyLastPart(UStringVector &parts)
{
  if (parts.IsEmpty())
    parts.Add(k_EmptyReplaceName);
  else
  {
    UString &s = parts.Back();
    if (s.IsEmpty())
      s = k_EmptyReplaceName;
  }
}

UString GetCorrectFsPath(const UString &path)
{
  UString res = GetCorrectFileName(path, true);
  #ifdef _WIN32
  if (!IsSupportedName(res))
    res = (UString)L"_" + res;
  #endif
  if (res.IsEmpty())
    res = k_EmptyReplaceName;
  return res;
}

UString GetCorrectFullFsPath(const UString &path)
{
  UStringVector parts;
  SplitPathToParts(path, parts);
  FOR_VECTOR (i, parts)
  {
    UString &s = parts[i];
    #ifdef _WIN32
    while (!s.IsEmpty() && (s.Back() == '.' || s.Back() == ' '))
      s.DeleteBack();
    if (!IsSupportedName(s))
      s.InsertAtFront(L'_');
    #endif
  }
  return MakePathNameFromParts(parts);
}