解析FTP LIST命令返回的目录列表

简介:
有个程序要分析FTP服务器返回的目录列表,本来以为比较简单,也在网上查了几个帖子,可都是一知半解的。于是下载了Filezilla的源代码,她的源文件
directorylistingparser.h
directorylistingparser.cpp
就是解析目录列表的,有同样需求的不妨看一看,还是挺费事的,不同平台要都要特殊处理。我把头文件贴出来:


#ifndef __DIRECTORYLISTINGPARSER_H__
#define __DIRECTORYLISTINGPARSER_H__

/* This class is responsible for parsing the directory listings returned by
 * the server.
 * Unfortunatly, RFC959 did not specify the format of directory listings, so
 * each server uses its own format. In addition to that, in most cases the 
 * listings were not designed to be machine-parsable, they were meant to be
 * human readable by users of that particular server.
 * By far the most common format is the one returned by the Unix "ls -l"
 * command. However, legacy systems are still in place, especially in big
 * companies. These often use very exotic listing styles.
 * Another problem are localized listings containing date strings. In some
 * cases these listings are ambiguous and cannot be distinguished.
 * Example for an ambiguous date: 04-05-06. All of the 6 permutations for
 * the location of year, month and day are valid dates.
 * Some servers send multiline listings where a single entry can span two
 * lines, this has to be detected as well, as far as possible.
 *
 * Some servers send MVS style listings which can consist of just the 
 * filename without any additional data. In order to prevent problems, this 
 * format is only parsed if the server is in fact recognizes as MVS server.
 *
 * Please see tests/dirparsertest.cpp for a list of supported formats and the
 * expected parser result.
 *
 * If adding data to the parser, it first decomposes the raw data into lines,
 * which then are processed further. Each line gets consecutively tested for
 * different formats, starting with the most common Unix style format.
 * Lines not containing a recognized format (e.g. a part of a multiline
 * entry) are rememberd and if the next line cannot be parsed either, they
 * get concatenated to be parsed again (and discarded if not recognized).
 */


class CLine;
class CToken;
class CControlSocket;
class CDirectoryListingParser
{
public:
  CDirectoryListingParser(CControlSocket* pControlSocket,  const CServer& server);
  ~CDirectoryListingParser();

  CDirectoryListing Parse( const CServerPath &path);

   void AddData( char *pData,  int len);
   void AddLine( const wxChar* pLine);

   void Reset();

   void SetTimezoneOffset( const wxTimeSpan& span) { m_timezoneOffset = span; }

   void SetServer( const CServer& server) { m_server = server; };

protected:
  CLine *GetLine( bool breakAtEnd =  false);

   void ParseData( bool partial);

   bool ParseLine(CLine *pLine,  const  enum ServerType serverType,  bool concatenated);

   bool ParseAsUnix(CLine *pLine, CDirentry &entry,  bool expect_date);
   bool ParseAsDos(CLine *pLine, CDirentry &entry);
   bool ParseAsEplf(CLine *pLine, CDirentry &entry);
   bool ParseAsVms(CLine *pLine, CDirentry &entry);
   bool ParseAsIbm(CLine *pLine, CDirentry &entry);
   bool ParseOther(CLine *pLine, CDirentry &entry);
   bool ParseAsWfFtp(CLine *pLine, CDirentry &entry);
   bool ParseAsIBM_MVS(CLine *pLine, CDirentry &entry);
   bool ParseAsIBM_MVS_PDS(CLine *pLine, CDirentry &entry);
   bool ParseAsIBM_MVS_PDS2(CLine *pLine, CDirentry &entry);
   bool ParseAsIBM_MVS_Migrated(CLine *pLine, CDirentry &entry);
   bool ParseAsMlsd(CLine *pLine, CDirentry &entry);
   bool ParseAsOS9(CLine *pLine, CDirentry &entry);
  
   // Only call this if servertype set to ZVM since it conflicts
   // with other formats.
   bool ParseAsZVM(CLine *pLine, CDirentry &entry);

   // Only call this if servertype set to HPNONSTOP since it conflicts
   // with other formats.
   bool ParseAsHPNonstop(CLine *pLine, CDirentry &entry);

   // Date / time parsers
   bool ParseUnixDateTime(CLine *pLine,  int &index, CDirentry &entry);
   bool ParseShortDate(CToken &token, CDirentry &entry,  bool saneFieldOrder =  false);
   bool ParseTime(CToken &token, CDirentry &entry);

   // Parse file sizes given like this: 123.4M
   bool ParseComplexFileSize(CToken& token, wxLongLong& size,  int blocksize = -1);

   bool GetMonthFromName( const wxString& name,  int &month);

  CControlSocket* m_pControlSocket;

   static std::map<wxString,  int> m_MonthNamesMap;
  
   struct t_list
  {
     char *p;
     int len;
  };
   int m_currentOffset;

  std::list<t_list> m_DataList;
  std::list<CDirentry> m_entryList;

  CLine *m_prevLine;

  CServer m_server;

   bool m_fileListOnly;
  std::list<wxString> m_fileList;
  
   bool m_maybeMultilineVms;

  wxTimeSpan m_timezoneOffset;
};

#endif









本文转自 h2appy  51CTO博客,原文链接:http://blog.51cto.com/h2appy/122279,如需转载请自行联系原作者
目录
相关文章
|
3月前
|
存储 消息中间件 NoSQL
【Redis】常用数据结构之List篇:从常用命令到典型使用场景
本文将系统探讨 Redis List 的核心特性、完整命令体系、底层存储实现以及典型实践场景,为读者构建从理论到应用的完整认知框架,助力开发者在实际业务中高效运用这一数据结构解决问题。
|
12月前
|
安全 编译器 Linux
深入解析与防范:基于缓冲区溢出的FTP服务器攻击及调用计算器示例
本文深入解析了利用缓冲区溢出漏洞对FTP服务器进行远程攻击的技术,通过分析FreeFlow FTP 1.75版本的漏洞,展示了如何通过构造过长的用户名触发缓冲区溢出并调用计算器(`calc.exe`)。文章详细介绍了攻击原理、关键代码组件及其实现步骤,并提出了有效的防范措施,如输入验证、编译器保护和安全编程语言的选择,以保障系统的安全性。环境搭建基于Windows XP SP3和Kali Linux,使用Metasploit Framework进行攻击演示。请注意,此内容仅用于教育和研究目的。
406 4
|
存储 消息中间件 NoSQL
Redis数据结构:List类型全面解析
Redis数据结构——List类型全面解析:存储多个有序的字符串,列表中每个字符串成为元素 Eelement,最多可以存储 2^32-1 个元素。可对列表两端插入(push)和弹出(pop)、获取指定范围的元素列表等,常见命令。 底层数据结构:3.2版本之前,底层采用**压缩链表ZipList**和**双向链表LinkedList**;3.2版本之后,底层数据结构为**快速链表QuickList** 列表是一种比较灵活的数据结构,可以充当栈、队列、阻塞队列,在实际开发中有很多应用场景。
|
存储 编译器 C++
【C++篇】揭开 C++ STL list 容器的神秘面纱:从底层设计到高效应用的全景解析(附源码)
【C++篇】揭开 C++ STL list 容器的神秘面纱:从底层设计到高效应用的全景解析(附源码)
240 2
|
前端开发 Java 项目管理
List.of 问题之使用List.of方法为什么会引发前端解析失败的问题,如何解决
List.of 问题之使用List.of方法为什么会引发前端解析失败的问题,如何解决
|
消息中间件 JSON NoSQL
Redis深度解析:核心数据类型之hash、list、set
Redis深度解析:核心数据类型之hash、list、set
|
安全 网络安全 PHP
Pikachu 目录遍历通关解析
Pikachu 目录遍历通关解析
|
Java Redis
redis-学习笔记(Jedis list简单命令)
redis-学习笔记(Jedis list简单命令)
162 1
|
开发者 Kotlin
Kotlin中List的Lambda表达式应用与解析
Kotlin中List的Lambda表达式应用与解析
139 0

热门文章

最新文章

推荐镜像

更多
  • DNS