summaryrefslogtreecommitdiffstats
path: root/xbmc/filesystem/FTPParse.cpp
blob: fb5e0350a4d86a715b76ea5143dec93337fbb876 (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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
/*
 *  Copyright (C) 2010-2018 Team Kodi
 *  This file is part of Kodi - https://kodi.tv
 *
 *  SPDX-License-Identifier: GPL-2.0-or-later
 *  See LICENSES/README.md for more information.
 */

#include "FTPParse.h"

#include <cmath>

#include <pcrecpp.h>

CFTPParse::CFTPParse()
{
  m_flagtrycwd = 0;
  m_flagtryretr = 0;
  m_size = 0;
}

std::string CFTPParse::getName()
{
  return m_name;
}

int CFTPParse::getFlagtrycwd()
{
  return m_flagtrycwd;
}

int CFTPParse::getFlagtryretr()
{
  return m_flagtryretr;
}

uint64_t CFTPParse::getSize()
{
  return m_size;
}

time_t CFTPParse::getTime()
{
  return m_time;
}

void CFTPParse::setTime(const std::string& str)
{
  /* Variables used to capture patterns via the regexes */
  std::string month;
  std::string day;
  std::string year;
  std::string hour;
  std::string minute;
  std::string second;
  std::string am_or_pm;

  /* time struct used to set the time_t variable */
  struct tm time_struct = {};

  /* Regex to read Unix, NetWare and NetPresenz time format */
  pcrecpp::RE unix_re("^([A-Za-z]{3})" // month
    "\\s+(\\d{1,2})" // day of month
    "\\s+([:\\d]{4,5})$" // time of day or year
  );

  /* Regex to read MultiNet time format */
  pcrecpp::RE multinet_re("^(\\d{1,2})" // day of month
    "-([A-Za-z]{3})" // month
    "-(\\d{4})" // year
    "\\s+(\\d{2})" // hour
    ":(\\d{2})" // minute
    "(:(\\d{2}))?$" // second
  );

  /* Regex to read MSDOS time format */
  pcrecpp::RE msdos_re("^(\\d{2})" // month
    "-(\\d{2})" // day of month
    "-(\\d{2})" // year
    "\\s+(\\d{2})" // hour
    ":(\\d{2})" // minute
    "([AP]M)$" // AM or PM
  );

  if (unix_re.FullMatch(str, &month, &day, &year))
  {
    /* set the month */
    if (pcrecpp::RE("jan",
        pcrecpp::RE_Options().set_caseless(true)).FullMatch(month))
      time_struct.tm_mon = 0;
    else if (pcrecpp::RE("feb",
        pcrecpp::RE_Options().set_caseless(true)).FullMatch(month))
      time_struct.tm_mon = 1;
    else if (pcrecpp::RE("mar",
        pcrecpp::RE_Options().set_caseless(true)).FullMatch(month))
      time_struct.tm_mon = 2;
    else if (pcrecpp::RE("apr",
        pcrecpp::RE_Options().set_caseless(true)).FullMatch(month))
      time_struct.tm_mon = 3;
    else if (pcrecpp::RE("may",
        pcrecpp::RE_Options().set_caseless(true)).FullMatch(month))
      time_struct.tm_mon = 4;
    else if (pcrecpp::RE("jun",
        pcrecpp::RE_Options().set_caseless(true)).FullMatch(month))
      time_struct.tm_mon = 5;
    else if (pcrecpp::RE("jul",
        pcrecpp::RE_Options().set_caseless(true)).FullMatch(month))
      time_struct.tm_mon = 6;
    else if (pcrecpp::RE("aug",
        pcrecpp::RE_Options().set_caseless(true)).FullMatch(month))
      time_struct.tm_mon = 7;
    else if (pcrecpp::RE("sep",
        pcrecpp::RE_Options().set_caseless(true)).FullMatch(month))
      time_struct.tm_mon = 8;
    else if (pcrecpp::RE("oct",
        pcrecpp::RE_Options().set_caseless(true)).FullMatch(month))
      time_struct.tm_mon = 9;
    else if (pcrecpp::RE("nov",
        pcrecpp::RE_Options().set_caseless(true)).FullMatch(month))
      time_struct.tm_mon = 10;
    else if (pcrecpp::RE("dec",
        pcrecpp::RE_Options().set_caseless(true)).FullMatch(month))
      time_struct.tm_mon = 11;

    /* set the day of the month */
    time_struct.tm_mday = atoi(day.c_str());

    time_t t = time(NULL);
    struct tm *current_time;
#ifdef LOCALTIME_R
    struct tm result = {};
    current_time = localtime_r(&t, &result);
#else
    current_time = localtime(&t);
#endif
    if (pcrecpp::RE("(\\d{2}):(\\d{2})").FullMatch(year, &hour, &minute))
    {
      /* set the hour and minute */
      time_struct.tm_hour = atoi(hour.c_str());
      time_struct.tm_min = atoi(minute.c_str());

      /* set the year */
      if ((current_time->tm_mon - time_struct.tm_mon < 0) ||
         ((current_time->tm_mon - time_struct.tm_mon == 0) &&
          (current_time->tm_mday - time_struct.tm_mday < 0)))
        time_struct.tm_year = current_time->tm_year - 1;
      else
        time_struct.tm_year = current_time->tm_year;
    }
    else
    {
      /* set the year */
      time_struct.tm_year = atoi(year.c_str()) - 1900;
    }

    /* set the day of the week */
    time_struct.tm_wday = getDayOfWeek(time_struct.tm_mon + 1,
                                   time_struct.tm_mday,
                                   time_struct.tm_year + 1900);
  }
  else if (multinet_re.FullMatch(str, &day, &month, &year,
                            &hour, &minute, (void*)NULL, &second))
  {
    /* set the month */
    if (pcrecpp::RE("jan",
        pcrecpp::RE_Options().set_caseless(true)).FullMatch(month))
      time_struct.tm_mon = 0;
    else if (pcrecpp::RE("feb",
        pcrecpp::RE_Options().set_caseless(true)).FullMatch(month))
      time_struct.tm_mon = 1;
    else if (pcrecpp::RE("mar",
        pcrecpp::RE_Options().set_caseless(true)).FullMatch(month))
      time_struct.tm_mon = 2;
    else if (pcrecpp::RE("apr",
        pcrecpp::RE_Options().set_caseless(true)).FullMatch(month))
      time_struct.tm_mon = 3;
    else if (pcrecpp::RE("may",
        pcrecpp::RE_Options().set_caseless(true)).FullMatch(month))
      time_struct.tm_mon = 4;
    else if (pcrecpp::RE("jun",
        pcrecpp::RE_Options().set_caseless(true)).FullMatch(month))
      time_struct.tm_mon = 5;
    else if (pcrecpp::RE("jul",
        pcrecpp::RE_Options().set_caseless(true)).FullMatch(month))
      time_struct.tm_mon = 6;
    else if (pcrecpp::RE("aug",
        pcrecpp::RE_Options().set_caseless(true)).FullMatch(month))
      time_struct.tm_mon = 7;
    else if (pcrecpp::RE("sep",
        pcrecpp::RE_Options().set_caseless(true)).FullMatch(month))
      time_struct.tm_mon = 8;
    else if (pcrecpp::RE("oct",
        pcrecpp::RE_Options().set_caseless(true)).FullMatch(month))
      time_struct.tm_mon = 9;
    else if (pcrecpp::RE("nov",
        pcrecpp::RE_Options().set_caseless(true)).FullMatch(month))
      time_struct.tm_mon = 10;
    else if (pcrecpp::RE("dec",
        pcrecpp::RE_Options().set_caseless(true)).FullMatch(month))
      time_struct.tm_mon = 11;

    /* set the day of the month and year */
    time_struct.tm_mday = atoi(day.c_str());
    time_struct.tm_year = atoi(year.c_str()) - 1900;

    /* set the hour and minute */
    time_struct.tm_hour = atoi(hour.c_str());
    time_struct.tm_min = atoi(minute.c_str());

    /* set the second if given*/
    if (second.length() > 0)
      time_struct.tm_sec = atoi(second.c_str());

    /* set the day of the week */
    time_struct.tm_wday = getDayOfWeek(time_struct.tm_mon + 1,
                                   time_struct.tm_mday,
                                   time_struct.tm_year + 1900);
  }
  else if (msdos_re.FullMatch(str, &month, &day,
                              &year, &hour, &minute, &am_or_pm))
  {
    /* set the month and the day of the month */
    time_struct.tm_mon = atoi(month.c_str()) - 1;
    time_struct.tm_mday = atoi(day.c_str());

    /* set the year */
    time_struct.tm_year = atoi(year.c_str());
    if (time_struct.tm_year < 70)
      time_struct.tm_year += 100;

    /* set the hour */
    time_struct.tm_hour = atoi(hour.c_str());
    if (time_struct.tm_hour == 12)
      time_struct.tm_hour -= 12;
    if (pcrecpp::RE("PM").FullMatch(am_or_pm))
      time_struct.tm_hour += 12;

    /* set the minute */
    time_struct.tm_min = atoi(minute.c_str());

    /* set the day of the week */
    time_struct.tm_wday = getDayOfWeek(time_struct.tm_mon + 1,
                                   time_struct.tm_mday,
                                   time_struct.tm_year + 1900);
  }

  /* now set m_time */
  m_time = mktime(&time_struct);
}

int CFTPParse::getDayOfWeek(int month, int date, int year)
{
  /* Here we use the Doomsday rule to calculate the day of the week */

  /* First determine the anchor day */
  int anchor;
  if (year >= 1900 && year < 2000)
    anchor = 3;
  else if (year >= 2000 && year < 2100)
    anchor = 2;
  else if (year >= 2100 && year < 2200)
    anchor = 0;
  else if (year >= 2200 && year < 2300)
    anchor = 5;
  else // must have been given an invalid year :-/
    return -1;

  /* Now determine the doomsday */
  int y = year % 100;
  int dday =
    ((y/12 + (y % 12) + ((y % 12)/4)) % 7) + anchor;

  /* Determine if the given year is a leap year */
  int leap_year = 0;
  if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0))
    leap_year = 1;

  /* Now select a doomsday for the given month */
  int mdday = 1;
  if (month == 1)
  {
    if (leap_year)
      mdday = 4;
    else
      mdday = 3;
  }
  if (month == 2)
  {
    if (leap_year)
      mdday = 1;
    else
      mdday = 7;
  }
  if (month == 3)
    mdday = 7;
  if (month == 4)
    mdday = 4;
  if (month == 5)
    mdday = 9;
  if (month == 6)
    mdday = 6;
  if (month == 7)
    mdday = 11;
  if (month == 8)
    mdday = 8;
  if (month == 9)
    mdday = 5;
  if (month == 10)
    mdday = 10;
  if (month == 11)
    mdday = 9;
  if (month == 12)
    mdday = 12;

  /* Now calculate the day of the week
   * Sunday = 0, Monday = 1, Tuesday = 2, Wednesday = 3, Thursday = 4,
   * Friday = 5, Saturday = 6.
   */
  int day_of_week = ((date - 1) % 7) - ((mdday - 1) % 7) + dday;
  if (day_of_week >= 7)
    day_of_week -= 7;

  return day_of_week;
}

int CFTPParse::FTPParse(const std::string& str)
{
  /* Various variable to capture patterns via the regexes */
  std::string permissions;
  std::string link_count;
  std::string owner;
  std::string group;
  std::string size;
  std::string date;
  std::string name;
  std::string type;
  std::string stuff;
  std::string facts;
  std::string version;
  std::string file_id;

  /* Regex for standard Unix listing formats */
  pcrecpp::RE unix_re("^([-bcdlps])" // type
    "([-rwxXsStT]{9})" // permissions
    "\\s+(\\d+)" // hard link count
    "\\s+(\\w+)" // owner
    "\\s+(\\w+)" // group
    "\\s+(\\d+)" // size
    "\\s+([A-Za-z]{3}\\s+\\d{1,2}\\s+[:\\d]{4,5})" // modification date
    "\\s+(.+)$" // name
  );

  /* Regex for NetWare listing formats */
  /* See http://www.novell.com/documentation/oes/ftp_enu/data/a3ep22p.html#fbhbaijf */
  pcrecpp::RE netware_re("^([-d])" // type
    "\\s+(\\[[-SRWCIEMFA]{8}\\])" // rights
    "\\s+(\\w+)" // owner
    "\\s+(\\d+)" // size
    "\\s+([A-Za-z]{3}\\s+\\d{1,2}\\s+[:\\d]{4,5})" // time
    "\\s+(.+)$" // name
  );

  /* Regex for NetPresenz */
  /* See http://files.stairways.com/other/ftp-list-specs-info.txt */
  /* Here we will capture permissions and size if given */
  pcrecpp::RE netpresenz_re("^([-dl])" // type
    "([-rwx]{9}|)" // permissions
    "\\s+(.*)" // stuff
    "\\s+(\\d+|)" // size
    "\\s+([A-Za-z]{3}\\s+\\d{1,2}\\s+[:\\d]{4,5})" // modification date
    "\\s+(.+)$" // name
  );

  /* Regex for EPLF */
  /* See http://cr.yp.to/ftp/list/eplf.html */
  /* SAVE: "(/,|r,|s\\d+,|m\\d+,|i[\\d!#@$%^&*()]+(\\.[\\d!#@$%^&*()]+|),)+" */
  pcrecpp::RE eplf_re("^\\+" // initial "plus" sign
    "([^\\s]+)" // facts
    "\\s(.+)$" // name
  );

  /* Regex for MultiNet */
  /* Best documentation found was
   * http://www-sld.slac.stanford.edu/SLDWWW/workbook/vms_files.html */
  pcrecpp::RE multinet_re("^([^;]+)" // name
    ";(\\d+)" // version
    "\\s+([\\d/]+)" // file id
    "\\s+(\\d{1,2}-[A-Za-z]{3}-\\d{4}\\s+\\d{2}:\\d{2}(:\\d{2})?)" // date
    "\\s+\\[([^\\]]+)\\]" // owner,group
    "\\s+\\(([^\\)]+)\\)$" // permissions
  );

  /* Regex for MSDOS */
  pcrecpp::RE msdos_re("^(\\d{2}-\\d{2}-\\d{2}\\s+\\d{2}:\\d{2}[AP]M)" // date
    "\\s+(<DIR>|[\\d]+)" // dir or size
    "\\s+(.+)$" // name
  );

  if (unix_re.FullMatch(str, &type, &permissions, &link_count, &owner, &group, &size, &date, &name))
  {
    m_name = name;
    m_size = (uint64_t)strtod(size.c_str(), NULL);
    if (pcrecpp::RE("d").FullMatch(type))
      m_flagtrycwd = 1;
    if (pcrecpp::RE("-").FullMatch(type))
      m_flagtryretr = 1;
    if (pcrecpp::RE("l").FullMatch(type))
    {
      m_flagtrycwd = m_flagtryretr = 1;
      // handle symlink
      size_t found = m_name.find(" -> ");
      if (found != std::string::npos)
        m_name = m_name.substr(0, found);
    }
    setTime(date);

    return 1;
  }
  if (netware_re.FullMatch(str, &type, &permissions, &owner, &size, &date, &name))
  {
    m_name = name;
    m_size = (uint64_t)strtod(size.c_str(), NULL);
    if (pcrecpp::RE("d").FullMatch(type))
      m_flagtrycwd = 1;
    if (pcrecpp::RE("-").FullMatch(type))
      m_flagtryretr = 1;
    setTime(date);

    return 1;
  }
  if (netpresenz_re.FullMatch(str, &type, &permissions, &stuff, &size, &date, &name))
  {
    m_name = name;
    m_size = (uint64_t)strtod(size.c_str(), NULL);
    if (pcrecpp::RE("d").FullMatch(type))
      m_flagtrycwd = 1;
    if (pcrecpp::RE("-").FullMatch(type))
      m_flagtryretr = 1;
    if (pcrecpp::RE("l").FullMatch(type))
    {
      m_flagtrycwd = m_flagtryretr = 1;
      // handle symlink
      size_t found = m_name.find(" -> ");
      if (found != std::string::npos)
        m_name = m_name.substr(0, found);
    }
    setTime(date);

    return 1;
  }
  if (eplf_re.FullMatch(str, &facts, &name))
  {
    /* Get the type, size, and date from the facts */
    pcrecpp::RE("(\\+|,)(r|/),").PartialMatch(facts, (void*)NULL, &type);
    pcrecpp::RE("(\\+|,)s(\\d+),").PartialMatch(facts, (void*)NULL, &size);
    pcrecpp::RE("(\\+|,)m(\\d+),").PartialMatch(facts, (void*)NULL, &date);

    m_name = name;
    m_size = (uint64_t)strtod(size.c_str(), NULL);
    if (pcrecpp::RE("/").FullMatch(type))
      m_flagtrycwd = 1;
    if (pcrecpp::RE("r").FullMatch(type))
      m_flagtryretr = 1;
    /* eplf stores the date in time_t format already */
    m_time = atoi(date.c_str());

    return 1;
  }
  if (multinet_re.FullMatch(str, &name, &version, &file_id, &date, (void*)NULL, &owner, &permissions))
  {
    if (pcrecpp::RE("\\.DIR$").PartialMatch(name))
    {
      name.resize(name.size() - 4);
      m_flagtrycwd = 1;
    }
    else
      m_flagtryretr = 1;
    m_name = name;
    setTime(date);
    /* Multinet doesn't provide a size */
    m_size = 0;

    return 1;
  }
  if (msdos_re.FullMatch(str, &date, &size, &name))
  {
    m_name = name;
    if (pcrecpp::RE("<DIR>").FullMatch(size))
    {
      m_flagtrycwd = 1;
      m_size = 0;
    }
    else
    {
      m_flagtryretr = 1;
      m_size = (uint64_t)strtod(size.c_str(), NULL);
    }
    setTime(date);

    return 1;
  }

  return 0;
}