summaryrefslogtreecommitdiffstats
path: root/src/libs/7zip/win/CPP/7zip/UI/Common/PropIDUtils.cpp
blob: dacaca5f9889d047050f862e31429cd1566c6b81 (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
// PropIDUtils.cpp

#include "StdAfx.h"

#include "Common/IntToString.h"

#include "Windows/FileFind.h"
#include "Windows/PropVariantConversions.h"

#include "../../PropID.h"

#include "PropIDUtils.h"

using namespace NWindows;

void ConvertUInt32ToHex(UInt32 value, wchar_t *s)
{
  for (int i = 0; i < 8; i++)
  {
    int t = value & 0xF;
    value >>= 4;
    s[7 - i] = (wchar_t)((t < 10) ? (L'0' + t) : (L'A' + (t - 10)));
  }
  s[8] = L'\0';
}

static const char g_WinAttrib[17] = "RHS8DAdNTsrCOnE_";
/*
0 READONLY
1 HIDDEN
3 SYSTEM

4 DIRECTORY
5 ARCHIVE
6 DEVICE
7 NORMAL
8 TEMPORARY
9 SPARSE_FILE
10 REPARSE_POINT
11 COMPRESSED
12 OFFLINE
13 NOT_CONTENT_INDEXED
14 ENCRYPTED

16 VIRTUAL
*/

static const char kPosixTypes[16] = { '0', 'p', 'c', '3', 'd', '5', 'b', '7', '-', '9', 'l', 'B', 's', 'D', 'E', 'F' };
#define MY_ATTR_CHAR(a, n, c) ((a )& (1 << (n))) ? c : L'-';

UString ConvertPropertyToString(const PROPVARIANT &prop, PROPID propID, bool full)
{
  switch(propID)
  {
    case kpidCTime:
    case kpidATime:
    case kpidMTime:
    {
      if (prop.vt != VT_FILETIME)
        break;
      FILETIME localFileTime;
      if ((prop.filetime.dwHighDateTime == 0 &&
          prop.filetime.dwLowDateTime == 0) ||
          !::FileTimeToLocalFileTime(&prop.filetime, &localFileTime))
        return UString();
      return ConvertFileTimeToString(localFileTime, true, full);
    }
    case kpidCRC:
    {
      if (prop.vt != VT_UI4)
        break;
      wchar_t temp[12];
      ConvertUInt32ToHex(prop.ulVal, temp);
      return temp;
    }
    case kpidAttrib:
    {
      if (prop.vt != VT_UI4)
        break;
      UInt32 a = prop.ulVal;
      wchar_t sz[32];
      int pos = 0;
      for (int i = 0; i < 16; i++)
        if (a & (1 << i) && i != 7)
          sz[pos++] = g_WinAttrib[i];
      sz[pos] = '\0';
      return sz;
    }
    case kpidPosixAttrib:
    {
      if (prop.vt != VT_UI4)
        break;
      UString res;
      UInt32 a = prop.ulVal;
      wchar_t temp[16];

      temp[0] = kPosixTypes[(a >> 12) & 0xF];
      for (int i = 6; i >= 0; i -= 3)
      {
        temp[7 - i] = MY_ATTR_CHAR(a, i + 2, L'r');
        temp[8 - i] = MY_ATTR_CHAR(a, i + 1, L'w');
        temp[9 - i] = MY_ATTR_CHAR(a, i + 0, L'x');
      }
      if ((a & 0x800) != 0) temp[3] = ((a & (1 << 6)) ? 's' : 'S');
      if ((a & 0x400) != 0) temp[6] = ((a & (1 << 3)) ? 's' : 'S');
      if ((a & 0x200) != 0) temp[9] = ((a & (1 << 0)) ? 't' : 'T');
      temp[10] = 0;
      res = temp;

      a &= ~(UInt32)0xFFFF;
      if (a != 0)
      {
        ConvertUInt32ToHex(a, temp);
        res = UString(temp) + L' ' + res;
      }
      return res;
    }
  }
  return ConvertPropVariantToString(prop);
}