Welcome to mirror list, hosted at ThFree Co, Russian Federation.

DebIn.cpp « Deb « Archive « 7zip - github.com/kornelski/7z.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 55f1e0346a13a9a6e996031dfd472c70692d1ba2 (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
// Archive/DebIn.cpp

#include "StdAfx.h"

#include "DebIn.h"
#include "DebHeader.h"

#include "Windows/Defs.h"

namespace NArchive {
namespace NDeb {

using namespace NHeader;

HRESULT CInArchive::ReadBytes(void *data, UInt32 size, UInt32 &processedSize)
{
  RINOK(m_Stream->Read(data, size, &processedSize));
  m_Position += processedSize;
  return S_OK;
}

HRESULT CInArchive::Open(IInStream *inStream)
{
  RINOK(inStream->Seek(0, STREAM_SEEK_CUR, &m_Position));
  char signature[kSignatureLen];
  UInt32 processedSize;
  RINOK(inStream->Read(signature, kSignatureLen, &processedSize));
  m_Position += processedSize;
  if (processedSize != kSignatureLen)
    return S_FALSE;
  if (memcmp(signature, kSignature, kSignatureLen) != 0)
    return S_FALSE;
  m_Stream = inStream;
  return S_OK;
}

static bool CheckString(const char *srcString, int numChars, int radix)
{
  for(int i = 0; i < numChars; i++)
  {
    char c = srcString[i];
    if (c == 0)
      return true;
    if (c >= '0' && c <= '0' + radix - 1)
      continue;
    if (c != ' ')
      return false;
  }
  return true;
}
static bool CheckOctalString(const char *srcString, int numChars)
  { return CheckString(srcString, numChars, 8); }
static bool CheckDecimalString(const char *srcString, int numChars)
  { return CheckString(srcString, numChars, 10); }

#define ReturnIfBadOctal(x, y) { if (!CheckOctalString((x), (y))) return S_FALSE; }
#define ReturnIfBadDecimal(x, y) { if (!CheckDecimalString((x), (y))) return S_FALSE; }

static UInt32 StringToNumber(const char *srcString, int numChars, int radix)
{
  AString modString;
  for (int i = 0; i < numChars; i++)
    modString += srcString[i];
  char *endPtr;
  return strtoul(modString, &endPtr, radix);
}
static UInt32 OctalToNumber(const char *srcString, int numChars)
  { return StringToNumber(srcString, numChars, 8); }
static UInt32 DecimalToNumber(const char *srcString, int numChars)
  { return StringToNumber(srcString, numChars, 10); }

HRESULT CInArchive::GetNextItemReal(bool &filled, CItemEx &item)
{
  filled = false;

  char header[NHeader::kHeaderSize];
  const char *cur = header;

  UInt32 processedSize;
  item.HeaderPosition = m_Position;
  RINOK(ReadBytes(header, sizeof(header), processedSize));
  if (processedSize < sizeof(header))
    return S_OK;
  
  char tempString[kNameSize + 1];
  strncpy(tempString, cur, kNameSize);
  cur += kNameSize;
  tempString[kNameSize] = '\0';
  item.Name = tempString;
  item.Name.Trim();

  for (int i = 0; i < item.Name.Length(); i++)
    if (((Byte)item.Name[i]) < 0x20)
      return S_FALSE;

  ReturnIfBadDecimal(cur, kTimeSize);
  item.ModificationTime = DecimalToNumber(cur, kTimeSize);
  cur += kTimeSize;

  cur += 6 + 6;
  
  ReturnIfBadOctal(cur, kModeSize);
  item.Mode = OctalToNumber(cur, kModeSize);
  cur += kModeSize;

  ReturnIfBadDecimal(cur, kSizeSize);
  item.Size = DecimalToNumber(cur, kSizeSize);
  cur += kSizeSize;

  filled = true;
  return S_OK;
}

HRESULT CInArchive::GetNextItem(bool &filled, CItemEx &item)
{
  while(true)
  {
    RINOK(GetNextItemReal(filled, item));
    if (!filled)
      return S_OK;
    if (item.Name.CompareNoCase("debian-binary") != 0)
      return S_OK;
    if (item.Size != 4)
      return S_OK;
    SkeepData(item.Size);
  }
  return S_OK;
}

HRESULT CInArchive::Skeep(UInt64 numBytes)
{
  UInt64 newPostion;
  RINOK(m_Stream->Seek(numBytes, STREAM_SEEK_CUR, &newPostion));
  m_Position += numBytes;
  if (m_Position != newPostion)
    return E_FAIL;
  return S_OK;
}

HRESULT CInArchive::SkeepData(UInt64 dataSize)
{
  return Skeep((dataSize + 1) & (~((UInt64)0x1)));
}

}}