FTPDirectoryParser.cpp [plain text]
#include "config.h"
#if ENABLE(FTPDIR)
#include "FTPDirectoryParser.h"
#if OS(WINDOWS) && (USE(PTHREADS) || HAVE(PTHREAD_H))
#include <pthread.h>
#endif
#include <wtf/ASCIICType.h>
#include <stdio.h>
namespace WebCore {
using namespace WTF;
#if OS(WINDOWS) && !defined(gmtime_r)
#define gmtime_r(x, y) gmtime_s((y), (x))
#endif
static inline FTPEntryType ParsingFailed(ListState& state)
{
if (state.parsedOne || state.listStyle)
return FTPJunkEntry;
return FTPMiscEntry;
}
FTPEntryType parseOneFTPLine(const char* line, ListState& state, ListResult& result)
{
result.clear();
if (!line)
return FTPJunkEntry;
state.numLines++;
unsigned int carry_buf_len = state.carryBufferLength;
state.carryBufferLength = 0;
unsigned linelen = 0;
while (*line == ' ' || *line == '\t')
line++;
const char* p = line;
while (*p && *p != '\n')
p++;
linelen = p - line;
if (linelen > 0 && *p == '\n' && *(p-1) == '\r')
linelen--;
if (linelen > 0)
{
static const char *month_names = "JanFebMarAprMayJunJulAugSepOctNovDec";
const char *tokens[16];
unsigned int toklen[WTF_ARRAY_LENGTH(tokens)];
unsigned int linelen_sans_wsp; unsigned int numtoks = 0;
unsigned int tokmarker = 0;
unsigned int month_num = 0;
char tbuf[4];
int lstyle = 0;
if (carry_buf_len)
{
tokens[0] = state.carryBuffer;
toklen[0] = carry_buf_len;
numtoks++;
}
unsigned int pos = 0;
while (pos < linelen && numtoks < WTF_ARRAY_LENGTH(tokens))
{
while (pos < linelen &&
(line[pos] == ' ' || line[pos] == '\t' || line[pos] == '\r'))
pos++;
if (pos < linelen)
{
tokens[numtoks] = &line[pos];
while (pos < linelen &&
(line[pos] != ' ' && line[pos] != '\t' && line[pos] != '\r'))
pos++;
if (tokens[numtoks] != &line[pos])
{
toklen[numtoks] = (&line[pos] - tokens[numtoks]);
numtoks++;
}
}
}
if (!numtoks)
return ParsingFailed(state);
linelen_sans_wsp = &(tokens[numtoks-1][toklen[numtoks-1]]) - tokens[0];
if (numtoks == WTF_ARRAY_LENGTH(tokens))
{
pos = linelen;
while (pos > 0 && (line[pos-1] == ' ' || line[pos-1] == '\t'))
pos--;
linelen_sans_wsp = pos;
}
#if defined(SUPPORT_EPLF)
if (!lstyle && (!state.listStyle || state.listStyle == 'E'))
{
if (*line == '+' && linelen > 4 && numtoks >= 2)
{
pos = 1;
while (pos < (linelen-1))
{
p = &line[pos++];
if (*p == '/')
result.type = FTPDirectoryEntry;
else if (*p == 'r')
result.type = FTPFileEntry;
else if (*p == 'm')
{
if (isASCIIDigit(line[pos]))
{
while (pos < linelen && isASCIIDigit(line[pos]))
pos++;
if (pos < linelen && line[pos] == ',')
{
unsigned long long seconds = 0;
sscanf(p + 1, "%llu", &seconds);
time_t t = static_cast<time_t>(seconds);
gmtime_r(&t, &result.modifiedTime);
result.modifiedTime.tm_year += 1900;
}
}
}
else if (*p == 's')
{
if (isASCIIDigit(line[pos]))
{
while (pos < linelen && isASCIIDigit(line[pos]))
pos++;
if (pos < linelen && line[pos] == ',')
result.fileSize = String(p + 1, &line[pos] - p + 1);
}
}
else if (isASCIIAlpha(*p))
{
while (pos < linelen && *++p != ',')
pos++;
}
else if (*p != '\t' || (p+1) != tokens[1])
{
break;
}
else
{
state.parsedOne = true;
state.listStyle = lstyle = 'E';
p = &(line[linelen_sans_wsp]);
result.filename = tokens[1];
result.filenameLength = p - tokens[1];
if (!result.type)
{
result.type = FTPFileEntry;
return FTPJunkEntry;
}
return result.type;
}
if (pos >= (linelen-1) || line[pos] != ',')
break;
pos++;
}
result.clear();
}
}
#endif
#if defined(SUPPORT_VMS)
if (!lstyle && (!state.listStyle || state.listStyle == 'V'))
{
if (!state.parsedOne &&
(numtoks == 1 || (numtoks == 2 && toklen[0] == 9 &&
memcmp(tokens[0], "Directory", 9)==0 )))
{
p = tokens[0];
pos = toklen[0];
if (numtoks == 2)
{
p = tokens[1];
pos = toklen[1];
}
pos--;
if (pos >= 3)
{
while (pos > 0 && p[pos] != '[')
{
pos--;
if (p[pos] == '-' || p[pos] == '$')
{
if (pos == 0 || p[pos-1] == '[' || p[pos-1] == '.' ||
(p[pos] == '-' && (p[pos+1] == ']' || p[pos+1] == '.')))
break;
}
else if (p[pos] != '.' && p[pos] != '~' &&
!isASCIIDigit(p[pos]) && !isASCIIAlpha(p[pos]))
break;
else if (isASCIIAlpha(p[pos]) && p[pos] != toASCIIUpper(p[pos]))
break;
}
if (pos > 0)
{
pos--;
if (p[pos] != ':' || p[pos+1] != '[')
pos = 0;
}
}
if (pos > 0 && p[pos] == ':')
{
while (pos > 0)
{
pos--;
if (p[pos] != '$' && p[pos] != '_' && p[pos] != '-' &&
p[pos] != '~' && !isASCIIDigit(p[pos]) && !isASCIIAlpha(p[pos]))
break;
else if (isASCIIAlpha(p[pos]) && p[pos] != toASCIIUpper(p[pos]))
break;
}
if (pos == 0)
{
state.listStyle = 'V';
return FTPJunkEntry;
}
}
}
else if ((tokens[0][toklen[0]-1]) != ';')
{
if (numtoks == 1 && (state.listStyle == 'V' && !carry_buf_len))
lstyle = 'V';
else if (numtoks < 4)
;
else if (toklen[1] >= 10 && memcmp(tokens[1], "%RMS-E-PRV", 10) == 0)
lstyle = 'V';
else if ((&line[linelen] - tokens[1]) >= 22 &&
memcmp(tokens[1], "insufficient privilege", 22) == 0)
lstyle = 'V';
else if (numtoks != 4 && numtoks != 6)
;
else if (numtoks == 6 && (
toklen[5] < 4 || *tokens[5] != '(' ||
(tokens[5][toklen[5]-1]) != ')' ))
;
else if ( (toklen[2] == 10 || toklen[2] == 11) &&
(tokens[2][toklen[2]-5]) == '-' &&
(tokens[2][toklen[2]-9]) == '-' &&
(((toklen[3]==4 || toklen[3]==5 || toklen[3]==7 || toklen[3]==8) &&
(tokens[3][toklen[3]-3]) == ':' ) ||
((toklen[3]==10 || toklen[3]==11 ) &&
(tokens[3][toklen[3]-3]) == '.' )
) &&
isASCIIDigit(*tokens[1]) &&
isASCIIDigit(*tokens[2]) &&
isASCIIDigit(*tokens[3])
)
{
lstyle = 'V';
}
if (lstyle == 'V')
{
tokmarker = 0;
p = tokens[0];
pos = 0;
if (*p == '[' && toklen[0] >= 4)
{
if (p[1] != ']')
{
p++;
pos++;
}
while (lstyle && pos < toklen[0] && *p != ']')
{
if (*p != '$' && *p != '.' && *p != '_' && *p != '-' &&
*p != '~' && !isASCIIDigit(*p) && !isASCIIAlpha(*p))
lstyle = 0;
pos++;
p++;
}
if (lstyle && pos < (toklen[0]-1))
{
ASSERT(*p == ']');
pos++;
p++;
tokmarker = pos;
} else {
lstyle = 0;
}
}
while (lstyle && pos < toklen[0] && *p != ';')
{
if (*p != '$' && *p != '.' && *p != '_' && *p != '-' &&
*p != '~' && !isASCIIDigit(*p) && !isASCIIAlpha(*p))
lstyle = 0;
else if (isASCIIAlpha(*p) && *p != toASCIIUpper(*p))
lstyle = 0;
p++;
pos++;
}
if (lstyle && *p == ';')
{
if (pos == 0 || pos == (toklen[0]-1))
lstyle = 0;
for (pos++;lstyle && pos < toklen[0];pos++)
{
if (!isASCIIDigit(tokens[0][pos]))
lstyle = 0;
}
}
pos = (p - tokens[0]);
pos -= tokmarker;
p = &(tokens[0][tokmarker]);
if (!lstyle || pos == 0 || pos > 80)
{
lstyle = 0;
}
else if (numtoks == 1)
{
if (pos >= (sizeof(state.carryBuffer)-1))
pos = (sizeof(state.carryBuffer)-1);
memcpy( state.carryBuffer, p, pos );
state.carryBufferLength = pos;
return FTPJunkEntry;
}
else if (isASCIIDigit(*tokens[1]))
{
for (pos = 0; lstyle && pos < (toklen[1]); pos++)
{
if (!isASCIIDigit((tokens[1][pos])) && (tokens[1][pos]) != '/')
lstyle = 0;
}
if (lstyle && numtoks > 4)
{
for (pos = 1; lstyle && pos < (toklen[5]-1); pos++)
{
p = &(tokens[5][pos]);
if (*p!='R' && *p!='W' && *p!='E' && *p!='D' && *p!=',')
lstyle = 0;
}
}
}
}
}
if (lstyle == 'V')
{
state.parsedOne = true;
state.listStyle = lstyle;
if (isASCIIDigit(*tokens[1]))
{
if (*tokens[0] == '[')
{
pos = toklen[0]-1;
p = tokens[0]+1;
while (*p != ']')
{
p++;
pos--;
}
toklen[0] = --pos;
tokens[0] = ++p;
}
pos = 0;
while (pos < toklen[0] && (tokens[0][pos]) != ';')
pos++;
result.caseSensitive = true;
result.type = FTPFileEntry;
result.filename = tokens[0];
result.filenameLength = pos;
if (pos > 4)
{
p = &(tokens[0][pos-4]);
if (p[0] == '.' && p[1] == 'D' && p[2] == 'I' && p[3] == 'R')
{
result.filenameLength -= 4;
result.type = FTPDirectoryEntry;
}
}
if (result.type != FTPDirectoryEntry)
{
pos = 0;
while (pos < toklen[1] && (tokens[1][pos]) != '/')
pos++;
#if 0
if (pos < toklen[1] && ( (pos<<1) > (toklen[1]-1) ||
(strtoul(tokens[1], (char **)0, 10) >
strtoul(tokens[1]+pos+1, (char **)0, 10)) ))
{
if (pos > (sizeof(result.fe_size)-1))
pos = sizeof(result.fe_size)-1;
memcpy( result.fe_size, tokens[1], pos );
result.fe_size[pos] = '\0';
}
else
#endif
{
uint64_t size = strtoull(tokens[1], 0, 10) * 512;
result.fileSize = String::number(size);
}
}
p = tokens[2] + 2;
if (*p == '-')
p++;
tbuf[0] = p[0];
tbuf[1] = toASCIILower(p[1]);
tbuf[2] = toASCIILower(p[2]);
month_num = 0;
for (pos = 0; pos < (12*3); pos+=3)
{
if (tbuf[0] == month_names[pos+0] &&
tbuf[1] == month_names[pos+1] &&
tbuf[2] == month_names[pos+2])
break;
month_num++;
}
if (month_num >= 12)
month_num = 0;
result.modifiedTime.tm_mon = month_num;
result.modifiedTime.tm_mday = atoi(tokens[2]);
result.modifiedTime.tm_year = atoi(p+4);
p = tokens[3] + 2;
if (*p == ':')
p++;
if (p[2] == ':')
result.modifiedTime.tm_sec = atoi(p+3);
result.modifiedTime.tm_hour = atoi(tokens[3]);
result.modifiedTime.tm_min = atoi(p);
return result.type;
}
return FTPJunkEntry;
}
}
#endif
#if defined(SUPPORT_CMS)
if (!lstyle && (!state.listStyle || state.listStyle == 'C'))
{
if (numtoks >= 7 && (toklen[0]+toklen[1]) <= 16)
{
for (pos = 1; !lstyle && (pos+5) < numtoks; pos++)
{
p = tokens[pos];
if ((toklen[pos] == 1 && (*p == 'F' || *p == 'V')) ||
(toklen[pos] == 3 && *p == 'D' && p[1] == 'I' && p[2] == 'R'))
{
if (toklen[pos+5] == 8 && (tokens[pos+5][2]) == ':' &&
(tokens[pos+5][5]) == ':' )
{
p = tokens[pos+4];
if ((toklen[pos+4] == 10 && p[4] == '-' && p[7] == '-') ||
(toklen[pos+4] >= 7 && toklen[pos+4] <= 9 &&
p[((p[1]!='/')?(2):(1))] == '/' &&
p[((p[1]!='/')?(5):(4))] == '/'))
{
if ( (*tokens[pos+1] == '-' &&
*tokens[pos+2] == '-' &&
*tokens[pos+3] == '-') ||
(isASCIIDigit(*tokens[pos+1]) &&
isASCIIDigit(*tokens[pos+2]) &&
isASCIIDigit(*tokens[pos+3])) )
{
lstyle = 'C';
tokmarker = pos;
}
}
}
}
}
}
if (lstyle && !state.listStyle)
{
for (pos = 0, p = tokens[0]; lstyle && pos < toklen[0]; pos++, p++)
{
if (isASCIIAlpha(*p) && toASCIIUpper(*p) != *p)
lstyle = 0;
}
for (pos = tokmarker+1; pos <= tokmarker+3; pos++)
{
if (!(toklen[pos] == 1 && *tokens[pos] == '-'))
{
for (p = tokens[pos]; lstyle && p<(tokens[pos]+toklen[pos]); p++)
{
if (!isASCIIDigit(*p))
lstyle = 0;
}
}
}
for (pos = 0, p = tokens[tokmarker+4];
lstyle && pos < toklen[tokmarker+4]; pos++, p++)
{
if (*p == '/')
{
if ((tokens[tokmarker+4][1]) == '/')
{
if (pos != 1 && pos != 4)
lstyle = 0;
}
else if (pos != 2 && pos != 5)
lstyle = 0;
}
else if (*p != '-' && !isASCIIDigit(*p))
lstyle = 0;
else if (*p == '-' && pos != 4 && pos != 7)
lstyle = 0;
}
for (pos = 0, p = tokens[tokmarker+5];
lstyle && pos < toklen[tokmarker+5]; pos++, p++)
{
if (*p != ':' && !isASCIIDigit(*p))
lstyle = 0;
else if (*p == ':' && pos != (toklen[tokmarker+5]-3)
&& pos != (toklen[tokmarker+5]-6))
lstyle = 0;
}
}
if (lstyle == 'C')
{
state.parsedOne = true;
state.listStyle = lstyle;
p = tokens[tokmarker+4];
if (toklen[tokmarker+4] == 10)
{
result.modifiedTime.tm_year = atoi(p+0) - 1900;
result.modifiedTime.tm_mon = atoi(p+5) - 1;
result.modifiedTime.tm_mday = atoi(p+8);
}
else
{
pos = toklen[tokmarker+4];
result.modifiedTime.tm_mon = atoi(p) - 1;
result.modifiedTime.tm_mday = atoi((p+pos)-5);
result.modifiedTime.tm_year = atoi((p+pos)-2);
if (result.modifiedTime.tm_year < 70)
result.modifiedTime.tm_year += 100;
}
p = tokens[tokmarker+5];
pos = toklen[tokmarker+5];
result.modifiedTime.tm_hour = atoi(p);
result.modifiedTime.tm_min = atoi((p+pos)-5);
result.modifiedTime.tm_sec = atoi((p+pos)-2);
result.caseSensitive = true;
result.filename = tokens[0];
result.filenameLength = toklen[0];
result.type = FTPFileEntry;
p = tokens[tokmarker];
if (toklen[tokmarker] == 3 && *p=='D' && p[1]=='I' && p[2]=='R')
result.type = FTPDirectoryEntry;
if (( toklen[tokmarker+4] == 10 && tokmarker > 1) ||
( toklen[tokmarker+4] != 10 && tokmarker > 2))
{
char *dot;
p = &(tokens[0][toklen[0]]);
memcpy( &dot, &p, sizeof(dot) );
*dot++ = '.';
p = tokens[1];
for (pos = 0; pos < toklen[1]; pos++)
*dot++ = *p++;
result.filenameLength += 1 + toklen[1];
}
return result.type;
}
}
#endif
#if defined(SUPPORT_DOS)
if (!lstyle && (!state.listStyle || state.listStyle == 'W'))
{
if ((numtoks >= 4) && toklen[0] == 8 && toklen[1] == 7 &&
(*tokens[2] == '<' || isASCIIDigit(*tokens[2])) )
{
p = tokens[0];
if ( isASCIIDigit(p[0]) && isASCIIDigit(p[1]) && p[2]=='-' &&
isASCIIDigit(p[3]) && isASCIIDigit(p[4]) && p[5]=='-' &&
isASCIIDigit(p[6]) && isASCIIDigit(p[7]) )
{
p = tokens[1];
if ( isASCIIDigit(p[0]) && isASCIIDigit(p[1]) && p[2]==':' &&
isASCIIDigit(p[3]) && isASCIIDigit(p[4]) &&
(p[5]=='A' || p[5]=='P') && p[6]=='M')
{
lstyle = 'W';
if (!state.listStyle)
{
p = tokens[2];
if (*p != '<' || p[toklen[2]-1] != '>')
{
for (pos = 1; (lstyle && pos < toklen[2]); pos++)
{
if (!isASCIIDigit(*++p))
lstyle = 0;
}
}
}
}
}
}
if (lstyle == 'W')
{
state.parsedOne = true;
state.listStyle = lstyle;
p = &(line[linelen]);
result.caseSensitive = true;
result.filename = tokens[3];
result.filenameLength = p - tokens[3];
result.type = FTPDirectoryEntry;
if (*tokens[2] != '<')
{
if (tokens[2] + toklen[2] - line == 38) {
result.filename = &(line[39]);
result.filenameLength = p - result.filename;
}
result.type = FTPFileEntry;
pos = toklen[2];
result.fileSize = String(tokens[2], pos);
}
else {
if (tokens[2] - line == 24 && (toklen[2] == 5 || toklen[2] == 10) &&
tokens[3] - line >= 39) {
result.filename = &(line[39]);
result.filenameLength = p - result.filename;
}
if ((tokens[2][1]) != 'D')
{
result.type = FTPJunkEntry;
if (result.filenameLength > 4)
{
p = result.filename;
for (pos = result.filenameLength - 4; pos > 0; pos--)
{
if (p[0] == ' ' && p[3] == ' ' && p[2] == '>' &&
(p[1] == '=' || p[1] == '-'))
{
result.type = FTPLinkEntry;
result.filenameLength = p - result.filename;
result.linkname = p + 4;
result.linknameLength = &(line[linelen])
- result.linkname;
break;
}
p++;
}
}
}
}
result.modifiedTime.tm_mon = atoi(tokens[0]+0);
if (result.modifiedTime.tm_mon != 0)
{
result.modifiedTime.tm_mon--;
result.modifiedTime.tm_mday = atoi(tokens[0]+3);
result.modifiedTime.tm_year = atoi(tokens[0]+6);
if (result.modifiedTime.tm_year < 80)
result.modifiedTime.tm_year += 2000;
else if (result.modifiedTime.tm_year < 100)
result.modifiedTime.tm_year += 1900;
}
result.modifiedTime.tm_hour = atoi(tokens[1]+0);
result.modifiedTime.tm_min = atoi(tokens[1]+3);
if ((tokens[1][5]) == 'P' && result.modifiedTime.tm_hour < 12)
result.modifiedTime.tm_hour += 12;
return result.type;
}
}
#endif
#if defined(SUPPORT_OS2)
if (!lstyle && (!state.listStyle || state.listStyle == 'O'))
{
p = &(line[toklen[0]]);
if (numtoks >= 4 && toklen[0] <= 18 && isASCIIDigit(*tokens[0]) &&
(linelen - toklen[0]) >= (53-18) &&
p[18-18] == ' ' && p[34-18] == ' ' &&
p[37-18] == '-' && p[40-18] == '-' && p[43-18] == ' ' &&
p[45-18] == ' ' && p[48-18] == ':' && p[51-18] == ' ' &&
isASCIIDigit(p[35-18]) && isASCIIDigit(p[36-18]) &&
isASCIIDigit(p[38-18]) && isASCIIDigit(p[39-18]) &&
isASCIIDigit(p[41-18]) && isASCIIDigit(p[42-18]) &&
isASCIIDigit(p[46-18]) && isASCIIDigit(p[47-18]) &&
isASCIIDigit(p[49-18]) && isASCIIDigit(p[50-18])
)
{
lstyle = 'O';
if (!state.listStyle)
{
for (pos = 1; lstyle && pos < toklen[0]; pos++)
{
if (!isASCIIDigit(tokens[0][pos]))
lstyle = 0;
}
}
}
if (lstyle == 'O')
{
state.parsedOne = true;
state.listStyle = lstyle;
p = &(line[toklen[0]]);
result.caseSensitive = true;
result.filename = &p[53-18];
result.filenameLength = (&(line[linelen_sans_wsp]))
- (result.filename);
result.type = FTPFileEntry;
for (pos = (18-18); pos < ((35-18)-4); pos++)
{
if (p[pos+0] == ' ' && p[pos+1] == 'D' &&
p[pos+2] == 'I' && p[pos+3] == 'R')
{
result.type = FTPDirectoryEntry;
break;
}
}
if (result.type != FTPDirectoryEntry)
{
pos = toklen[0];
result.fileSize = String(tokens[0], pos);
}
result.modifiedTime.tm_mon = atoi(&p[35-18]) - 1;
result.modifiedTime.tm_mday = atoi(&p[38-18]);
result.modifiedTime.tm_year = atoi(&p[41-18]);
if (result.modifiedTime.tm_year < 80)
result.modifiedTime.tm_year += 100;
result.modifiedTime.tm_hour = atoi(&p[46-18]);
result.modifiedTime.tm_min = atoi(&p[49-18]);
return result.type;
}
}
#endif
#if defined(SUPPORT_LSL)
if (!lstyle && (!state.listStyle || state.listStyle == 'U'))
{
bool isOldHellsoft = false;
if (numtoks >= 6)
{
if (toklen[0] == 1 || (tokens[0][1]) == '[')
{
if (*tokens[0] == 'd' || *tokens[0] == '-')
{
pos = toklen[0]-1;
p = tokens[0] + 1;
if (pos == 0)
{
p = tokens[1];
pos = toklen[1];
}
if ((pos == 9 || pos == 10) &&
(*p == '[' && p[pos-1] == ']') &&
(p[1] == 'R' || p[1] == '-') &&
(p[2] == 'W' || p[2] == '-') &&
(p[3] == 'C' || p[3] == '-') &&
(p[4] == 'E' || p[4] == '-'))
{
lstyle = 'U';
if (toklen[0] == 10)
isOldHellsoft = true;
}
}
}
else if ((toklen[0] == 10 || toklen[0] == 11)
&& strchr("-bcdlpsw?DFam", *tokens[0]))
{
p = &(tokens[0][1]);
if ((p[0] == 'r' || p[0] == '-') &&
(p[1] == 'w' || p[1] == '-') &&
(p[3] == 'r' || p[3] == '-') &&
(p[4] == 'w' || p[4] == '-') &&
(p[6] == 'r' || p[6] == '-') &&
(p[7] == 'w' || p[7] == '-'))
{
lstyle = 'U';
}
}
}
if (lstyle == 'U')
{
lstyle = 0;
for (pos = (numtoks-5); !lstyle && pos > 1; pos--)
{
if (isASCIIDigit(*tokens[pos])
&& toklen[pos+1] == 3 && isASCIIAlpha(*tokens[pos+1]) &&
isASCIIAlpha(tokens[pos+1][1]) && isASCIIAlpha(tokens[pos+1][2])
&& isASCIIDigit(*tokens[pos+2]) &&
(toklen[pos+2] == 1 ||
(toklen[pos+2] == 2 && isASCIIDigit(tokens[pos+2][1])))
&& toklen[pos+3] >= 4 && isASCIIDigit(*tokens[pos+3])
&& (toklen[pos+3] <= 5 || (
(toklen[pos+3] == 7 || toklen[pos+3] == 8) &&
(tokens[pos+3][toklen[pos+3]-3]) == ':'))
&& isASCIIDigit(tokens[pos+3][toklen[pos+3]-2])
&& isASCIIDigit(tokens[pos+3][toklen[pos+3]-1])
&& (
((toklen[pos+3] == 4 || toklen[pos+3] == 5) &&
isASCIIDigit(tokens[pos+3][1]) &&
isASCIIDigit(tokens[pos+3][2]) )
|| ((toklen[pos+3] == 4 || toklen[pos+3] == 7) &&
(tokens[pos+3][1]) == ':' &&
isASCIIDigit(tokens[pos+3][2]) && isASCIIDigit(tokens[pos+3][3]))
|| ((toklen[pos+3] == 5 || toklen[pos+3] == 8) &&
isASCIIDigit(tokens[pos+3][1]) && (tokens[pos+3][2]) == ':' &&
isASCIIDigit(tokens[pos+3][3]) && isASCIIDigit(tokens[pos+3][4]))
)
)
{
lstyle = 'U';
tokmarker = pos;
p = tokens[tokmarker];
for (unsigned int i = 0; lstyle && i < toklen[tokmarker]; ++i)
{
if (!isASCIIDigit(*p++))
lstyle = 0;
}
if (lstyle)
{
month_num = 0;
p = tokens[tokmarker+1];
for (unsigned int i = 0; i < (12*3); i+=3)
{
if (p[0] == month_names[i+0] &&
p[1] == month_names[i+1] &&
p[2] == month_names[i+2])
break;
month_num++;
}
if (month_num >= 12)
lstyle = 0;
}
}
}
}
if (lstyle == 'U')
{
state.parsedOne = true;
state.listStyle = lstyle;
result.caseSensitive = false;
result.type = FTPJunkEntry;
if (*tokens[0] == 'd' || *tokens[0] == 'D')
result.type = FTPDirectoryEntry;
else if (*tokens[0] == 'l')
result.type = FTPLinkEntry;
else if (*tokens[0] == '-' || *tokens[0] == 'F')
result.type = FTPFileEntry;
if (result.type != FTPDirectoryEntry)
{
pos = toklen[tokmarker];
result.fileSize = String(tokens[tokmarker], pos);
}
result.modifiedTime.tm_mon = month_num;
result.modifiedTime.tm_mday = atoi(tokens[tokmarker+2]);
if (result.modifiedTime.tm_mday == 0)
result.modifiedTime.tm_mday++;
p = tokens[tokmarker+3];
pos = (unsigned int)atoi(p);
if (p[1] == ':')
p--;
if (p[2] != ':')
{
result.modifiedTime.tm_year = pos;
}
else
{
result.modifiedTime.tm_hour = pos;
result.modifiedTime.tm_min = atoi(p+3);
if (p[5] == ':')
result.modifiedTime.tm_sec = atoi(p+6);
if (!state.now)
{
time_t now = time(nullptr);
state.now = now * 1000000.0;
gmtime_r(&now, &state.nowFTPTime);
state.nowFTPTime.tm_year += 1900;
}
result.modifiedTime.tm_year = state.nowFTPTime.tm_year;
if ( (( state.nowFTPTime.tm_mon << 5) + state.nowFTPTime.tm_mday) <
((result.modifiedTime.tm_mon << 5) + result.modifiedTime.tm_mday) )
result.modifiedTime.tm_year--;
}
if (!isOldHellsoft)
result.filename = tokens[tokmarker+3] + toklen[tokmarker+3] + 1;
else
result.filename = tokens[tokmarker+4];
result.filenameLength = (&(line[linelen]))
- (result.filename);
if (result.type == FTPLinkEntry && result.filenameLength > 4)
{
unsigned int fileSize = result.fileSize.toUInt();
if (result.filenameLength > (fileSize + 4) &&
strncmp(result.filename + result.filenameLength - fileSize - 4, " -> ", 4) == 0)
{
result.linkname = result.filename + (result.filenameLength - fileSize);
result.linknameLength = (&(line[linelen])) - (result.linkname);
result.filenameLength -= fileSize + 4;
}
else
{
p = result.filename + (result.filenameLength - 5);
for (pos = (result.filenameLength - 5); pos > 0; pos--)
{
if (strncmp(p, " -> ", 4) == 0)
{
result.linkname = p + 4;
result.linknameLength = (&(line[linelen]))
- (result.linkname);
result.filenameLength = pos;
break;
}
p--;
}
}
}
#if defined(SUPPORT_LSLF)
if (result.filenameLength > 1)
{
p = result.filename[result.filenameLength-1];
pos = result.type;
if (pos == 'd') {
if (*p == '/') result.filenameLength--;
} else if (pos == 'l') {
if (*p == '@') result.filenameLength--;
} else if (pos == 'f') {
if (*p == '*') result.filenameLength--;
} else if (*p == '=' || *p == '%' || *p == '|') {
result.filenameLength--;
}
}
#endif
return result.type;
}
}
#endif
#if defined(SUPPORT_W16)
if (!lstyle && (!state.listStyle || state.listStyle == 'w'))
{
if (numtoks >= 4 && toklen[0] < 13 &&
((toklen[1] == 5 && *tokens[1] == '<') || isASCIIDigit(*tokens[1])) )
{
if (numtoks == 4
&& (toklen[2] == 8 || toklen[2] == 9)
&& (((tokens[2][2]) == '/' && (tokens[2][5]) == '/') ||
((tokens[2][2]) == '-' && (tokens[2][5]) == '-'))
&& (toklen[3] == 4 || toklen[3] == 5)
&& (tokens[3][toklen[3]-3]) == ':'
&& isASCIIDigit(tokens[2][0]) && isASCIIDigit(tokens[2][1])
&& isASCIIDigit(tokens[2][3]) && isASCIIDigit(tokens[2][4])
&& isASCIIDigit(tokens[2][6]) && isASCIIDigit(tokens[2][7])
&& (toklen[2] < 9 || isASCIIDigit(tokens[2][8]))
&& isASCIIDigit(tokens[3][toklen[3]-1]) && isASCIIDigit(tokens[3][toklen[3]-2])
&& isASCIIDigit(tokens[3][toklen[3]-4]) && isASCIIDigit(*tokens[3])
)
{
lstyle = 'w';
}
else if ((numtoks == 6 || numtoks == 7)
&& toklen[2] == 3 && toklen[3] == 2
&& toklen[4] == 4 && toklen[5] == 5
&& (tokens[5][2]) == ':'
&& isASCIIAlpha(tokens[2][0]) && isASCIIAlpha(tokens[2][1])
&& isASCIIAlpha(tokens[2][2])
&& isASCIIDigit(tokens[3][0]) && isASCIIDigit(tokens[3][1])
&& isASCIIDigit(tokens[4][0]) && isASCIIDigit(tokens[4][1])
&& isASCIIDigit(tokens[4][2]) && isASCIIDigit(tokens[4][3])
&& isASCIIDigit(tokens[5][0]) && isASCIIDigit(tokens[5][1])
&& isASCIIDigit(tokens[5][3]) && isASCIIDigit(tokens[5][4])
)
{
lstyle = 'w';
}
if (lstyle && state.listStyle != lstyle)
{
p = tokens[1];
if (toklen[1] != 5 || p[0] != '<' || p[1] != 'D' ||
p[2] != 'I' || p[3] != 'R' || p[4] != '>')
{
for (pos = 0; lstyle && pos < toklen[1]; pos++)
{
if (!isASCIIDigit(*p++))
lstyle = 0;
}
}
}
}
if (lstyle == 'w')
{
state.parsedOne = true;
state.listStyle = lstyle;
result.caseSensitive = true;
result.filename = tokens[0];
result.filenameLength = toklen[0];
result.type = FTPDirectoryEntry;
p = tokens[1];
if (isASCIIDigit(*p))
{
result.type = FTPFileEntry;
pos = toklen[1];
result.fileSize = String(p, pos);
}
p = tokens[2];
if (toklen[2] == 3)
{
tbuf[0] = toASCIIUpper(p[0]);
tbuf[1] = toASCIILower(p[1]);
tbuf[2] = toASCIILower(p[2]);
for (pos = 0; pos < (12*3); pos+=3)
{
if (tbuf[0] == month_names[pos+0] &&
tbuf[1] == month_names[pos+1] &&
tbuf[2] == month_names[pos+2])
{
result.modifiedTime.tm_mon = pos/3;
result.modifiedTime.tm_mday = atoi(tokens[3]);
result.modifiedTime.tm_year = atoi(tokens[4]) - 1900;
break;
}
}
pos = 5;
}
else
{
result.modifiedTime.tm_mon = atoi(p+0)-1;
result.modifiedTime.tm_mday = atoi(p+3);
result.modifiedTime.tm_year = atoi(p+6);
if (result.modifiedTime.tm_year < 80)
result.modifiedTime.tm_year += 100;
pos = 3;
}
result.modifiedTime.tm_hour = atoi(tokens[pos]);
result.modifiedTime.tm_min = atoi(&(tokens[pos][toklen[pos]-2]));
return result.type;
}
}
#endif
#if defined(SUPPORT_DLS)
if (!lstyle &&
(state.listStyle == 'D' || (!state.listStyle && state.numLines == 1)))
{
if (!state.listStyle && line[linelen-1] == ':' &&
linelen >= 2 && toklen[numtoks-1] != 1)
{
pos = 0;
p = line;
while (pos < (linelen-1))
{
if (*p == '<' || *p == '|' || *p == '>' ||
*p == '?' || *p == '*' || *p == '\\')
break;
if (*p == '/' && pos < (linelen-2) && p[1] == '/')
break;
pos++;
p++;
}
if (pos == (linelen-1))
{
state.listStyle = 'D';
return FTPJunkEntry;
}
}
if (!lstyle && numtoks >= 2)
{
pos = 22;
if (state.listStyle && carry_buf_len)
pos = toklen[1]-1;
if (linelen > pos)
{
p = &line[pos];
if ((*p == '-' || *p == '=' || isASCIIDigit(*p)) &&
((linelen == (pos+1)) ||
(linelen >= (pos+3) && p[1] == ' ' && p[2] == ' ')) )
{
tokmarker = 1;
if (!carry_buf_len)
{
pos = 1;
while (pos < numtoks && (tokens[pos]+toklen[pos]) < (&line[23]))
pos++;
tokmarker = 0;
if ((tokens[pos]+toklen[pos]) == (&line[23]))
tokmarker = pos;
}
if (tokmarker)
{
lstyle = 'D';
if (*tokens[tokmarker] == '-' || *tokens[tokmarker] == '=')
{
if (toklen[tokmarker] != 1 ||
(tokens[tokmarker-1][toklen[tokmarker-1]-1]) != '/')
lstyle = 0;
}
else
{
for (pos = 0; lstyle && pos < toklen[tokmarker]; pos++)
{
if (!isASCIIDigit(tokens[tokmarker][pos]))
lstyle = 0;
}
}
if (lstyle && !state.listStyle)
{
for (p = tokens[0]; lstyle &&
p < &(tokens[tokmarker-1][toklen[tokmarker-1]]); p++)
{
if (*p == '<' || *p == '|' || *p == '>' ||
*p == '?' || *p == '*' || *p == '/' || *p == '\\')
lstyle = 0;
}
}
}
}
}
}
if (!lstyle && state.listStyle == 'D' && !carry_buf_len)
{
pos = linelen;
if (pos > (sizeof(state.carryBuffer)-1))
pos = sizeof(state.carryBuffer)-1;
memcpy( state.carryBuffer, line, pos );
state.carryBufferLength = pos;
return FTPJunkEntry;
}
if (lstyle == 'D')
{
state.parsedOne = true;
state.listStyle = lstyle;
p = &(tokens[tokmarker-1][toklen[tokmarker-1]]);
result.filename = tokens[0];
result.filenameLength = p - tokens[0];
result.type = FTPFileEntry;
if (result.filename[result.filenameLength-1] == '/')
{
if (result.linknameLength == 1)
result.type = FTPJunkEntry;
else
{
result.filenameLength--;
result.type = FTPDirectoryEntry;
}
}
else if (isASCIIDigit(*tokens[tokmarker]))
{
pos = toklen[tokmarker];
result.fileSize = String(tokens[tokmarker], pos);
}
if ((tokmarker+3) < numtoks &&
(&(tokens[numtoks-1][toklen[numtoks-1]]) -
tokens[tokmarker+1]) >= (1+1+3+1+4) )
{
pos = (tokmarker+3);
p = tokens[pos];
pos = toklen[pos];
if ((pos == 4 || pos == 5)
&& isASCIIDigit(*p) && isASCIIDigit(p[pos-1]) && isASCIIDigit(p[pos-2])
&& ((pos == 5 && p[2] == ':') ||
(pos == 4 && (isASCIIDigit(p[1]) || p[1] == ':')))
)
{
month_num = tokmarker+1;
pos = tokmarker+2;
if (isASCIIDigit(*tokens[month_num]))
{
month_num++;
pos--;
}
p = tokens[month_num];
if (isASCIIDigit(*tokens[pos])
&& (toklen[pos] == 1 ||
(toklen[pos] == 2 && isASCIIDigit(tokens[pos][1])))
&& toklen[month_num] == 3
&& isASCIIAlpha(*p) && isASCIIAlpha(p[1]) && isASCIIAlpha(p[2]) )
{
pos = atoi(tokens[pos]);
if (pos > 0 && pos <= 31)
{
result.modifiedTime.tm_mday = pos;
month_num = 1;
for (pos = 0; pos < (12*3); pos+=3)
{
if (p[0] == month_names[pos+0] &&
p[1] == month_names[pos+1] &&
p[2] == month_names[pos+2])
break;
month_num++;
}
if (month_num > 12)
result.modifiedTime.tm_mday = 0;
else
result.modifiedTime.tm_mon = month_num - 1;
}
}
if (result.modifiedTime.tm_mday)
{
tokmarker += 3;
p = tokens[tokmarker];
pos = atoi(p);
if (pos > 24)
result.modifiedTime.tm_year = pos-1900;
else
{
if (p[1] == ':')
p--;
result.modifiedTime.tm_hour = pos;
result.modifiedTime.tm_min = atoi(p+3);
if (!state.now)
{
time_t now = time(nullptr);
state.now = now * 1000000.0;
gmtime_r(&now, &state.nowFTPTime);
state.nowFTPTime.tm_year += 1900;
}
result.modifiedTime.tm_year = state.nowFTPTime.tm_year;
if ( (( state.nowFTPTime.tm_mon << 4) + state.nowFTPTime.tm_mday) <
((result.modifiedTime.tm_mon << 4) + result.modifiedTime.tm_mday) )
result.modifiedTime.tm_year--;
}
}
}
}
if (numtoks > (tokmarker+2))
{
pos = tokmarker+1;
p = tokens[pos];
if (toklen[pos] == 2 && *p == '-' && p[1] == '>')
{
p = &(tokens[numtoks-1][toklen[numtoks-1]]);
result.type = FTPLinkEntry;
result.linkname = tokens[pos+1];
result.linknameLength = p - result.linkname;
if (result.linknameLength > 1 &&
result.linkname[result.linknameLength-1] == '/')
result.linknameLength--;
}
}
return result.type;
}
}
#endif
}
return ParsingFailed(state);
}
}
#endif // ENABLE(FTPDIR)