-
Notifications
You must be signed in to change notification settings - Fork 43
/
encname.cpp
77 lines (72 loc) · 1.83 KB
/
encname.cpp
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
#include "rar.hpp"
EncodeFileName::EncodeFileName()
{
Flags=0;
FlagBits=0;
FlagsPos=0;
DestSize=0;
}
void EncodeFileName::Decode(const char *Name,size_t NameSize,
const byte *EncName,size_t EncSize,
std::wstring &NameW)
{
size_t EncPos=0,DecPos=0;
byte HighByte=EncPos<EncSize ? EncName[EncPos++] : 0;
while (EncPos<EncSize)
{
if (FlagBits==0)
{
Flags=EncName[EncPos++];
FlagBits=8;
}
switch(Flags>>6)
{
case 0:
if (EncPos>=EncSize)
break;
// We need DecPos also for ASCII "Name", so resize() instead of push_back().
NameW.resize(DecPos+1);
NameW[DecPos++]=EncName[EncPos++];
break;
case 1:
if (EncPos>=EncSize)
break;
NameW.resize(DecPos+1);
NameW[DecPos++]=EncName[EncPos++]+(HighByte<<8);
break;
case 2:
if (EncPos+1>=EncSize)
break;
NameW.resize(DecPos+1);
NameW[DecPos++]=EncName[EncPos]+(EncName[EncPos+1]<<8);
EncPos+=2;
break;
case 3:
{
if (EncPos>=EncSize)
break;
int Length=EncName[EncPos++];
if ((Length & 0x80)!=0)
{
if (EncPos>=EncSize)
break;
byte Correction=EncName[EncPos++];
for (Length=(Length&0x7f)+2;Length>0 && DecPos<NameSize;Length--,DecPos++)
{
NameW.resize(DecPos+1);
NameW[DecPos]=((Name[DecPos]+Correction)&0xff)+(HighByte<<8);
}
}
else
for (Length+=2;Length>0 && DecPos<NameSize;Length--,DecPos++)
{
NameW.resize(DecPos+1);
NameW[DecPos]=Name[DecPos];
}
}
break;
}
Flags<<=2;
FlagBits-=2;
}
}