home *** CD-ROM | disk | FTP | other *** search
/ Computer Shopper 217 / DPCS0306DVD.ISO / Toolkit / Internet / FileZilla / Server / FileZilla_Server-0.9.11.exe / source / SpeedLimit.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2005-04-21  |  4.2 KB  |  223 lines

  1. // FileZilla Server - a Windows ftp server
  2.  
  3. // Copyright (C) 2002-2004 - Tim Kosse <tim.kosse@gmx.de>
  4.  
  5. // This program is free software; you can redistribute it and/or
  6. // modify it under the terms of the GNU General Public License
  7. // as published by the Free Software Foundation; either version 2
  8. // of the License, or (at your option) any later version.
  9.  
  10. // This program is distributed in the hope that it will be useful,
  11. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. // GNU General Public License for more details.
  14.  
  15. // You should have received a copy of the GNU General Public License
  16. // along with this program; if not, write to the Free Software
  17. // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  18.  
  19. // SpeedLimit.cpp: implementation of the CSpeedLimit class.
  20. //
  21. //////////////////////////////////////////////////////////////////////
  22.  
  23. #include "stdafx.h"
  24. #include "SpeedLimit.h"
  25.  
  26. //////////////////////////////////////////////////////////////////////
  27. // Construction/Destruction
  28. //////////////////////////////////////////////////////////////////////
  29.  
  30. CSpeedLimit::CSpeedLimit()
  31. {
  32.     m_Day = 0;
  33.  
  34.     m_Speed = 10;
  35.     m_ToCheck = FALSE;
  36.     m_DateCheck = FALSE;
  37.     m_FromCheck = FALSE;
  38. }
  39.  
  40. CSpeedLimit::~CSpeedLimit()
  41. {
  42.  
  43. }
  44.  
  45. bool CSpeedLimit::IsItActive(const SYSTEMTIME &time) const
  46. {
  47.     if (m_DateCheck)
  48.     {
  49.         if ((m_Date.y != time.wYear) ||
  50.             (m_Date.m != time.wMonth) ||
  51.             (m_Date.d != time.wDay))
  52.             return false;
  53.     }
  54.     else
  55.     {
  56.         int i = (time.wDayOfWeek + 6) % 7;
  57.         
  58.         if (!(m_Day & ( 1 << i)))
  59.             return false;
  60.     }
  61.  
  62.     int curTime = time.wHour * 60 * 60 +
  63.                   time.wMinute * 60 +
  64.                   time.wSecond;
  65.  
  66.     int fromTime = 0;
  67.     int toTime = 0;
  68.     if (m_ToCheck)
  69.         toTime = m_ToTime.h * 60 * 60 +
  70.                  m_ToTime.m * 60 +
  71.                  m_ToTime.s;
  72.     if (m_FromCheck)
  73.         fromTime = m_FromTime.h * 60 * 60 +
  74.                    m_FromTime.m * 60 +
  75.                    m_FromTime.s;
  76.  
  77.     if (m_FromCheck && m_ToCheck)
  78.     {
  79.         int span = toTime - fromTime;
  80.         if (span < 0)
  81.             span += 24 * 60 * 60;
  82.         int ref = curTime - fromTime;
  83.         if (ref < 0)
  84.             ref += 24 * 60 * 60;
  85.         if (span < ref)
  86.             return false;
  87.     }
  88.     else
  89.     {
  90.         if (m_ToCheck)
  91.         {
  92.             if (toTime < curTime)
  93.                 return false;
  94.         }
  95.  
  96.         if (m_FromCheck)
  97.         {
  98.             if (fromTime > curTime)
  99.                 return false;
  100.         }
  101.     }
  102.  
  103.     return true;
  104. }
  105.  
  106. int CSpeedLimit::GetRequiredBufferLen() const
  107. {
  108.     return    2 + //Speed
  109.             4 + //date
  110.             6 +    //2 * time
  111.             1;  //Weekday
  112.     
  113. }
  114.  
  115. char * CSpeedLimit::FillBuffer(char *p) const
  116. {
  117.     *p++ = m_Speed >> 8;
  118.     *p++ = m_Speed % 256;
  119.  
  120.     if (m_DateCheck)
  121.     {
  122.         *p++ = m_Date.y >> 8;
  123.         *p++ = m_Date.y % 256;
  124.         *p++ = m_Date.m;
  125.         *p++ = m_Date.d;
  126.     }
  127.     else
  128.     {
  129.         memset(p, 0, 4);
  130.         p += 4;
  131.     }
  132.     
  133.     if (m_FromCheck)
  134.     {
  135.         *p++ = m_FromTime.h;
  136.         *p++ = m_FromTime.m;
  137.         *p++ = m_FromTime.s;
  138.     }
  139.     else
  140.     {
  141.         memset(p, 0, 3);
  142.         p += 3;
  143.     }
  144.  
  145.     if (m_ToCheck)
  146.     {
  147.         *p++ = m_ToTime.h;
  148.         *p++ = m_ToTime.m;
  149.         *p++ = m_ToTime.s;
  150.     }
  151.     else
  152.     {
  153.         memset(p, 0, 3);
  154.         p += 3;
  155.     }
  156.  
  157.     *p++ = m_Day;
  158.     
  159.     return p;
  160. }
  161.  
  162. unsigned char * CSpeedLimit::ParseBuffer(unsigned char *pBuffer, int length)
  163. {
  164.     if (length < GetRequiredBufferLen())
  165.         return NULL;
  166.  
  167.     unsigned char *p = pBuffer;
  168.  
  169.     m_Speed = *p++ << 8;
  170.     m_Speed |= *p++;
  171.  
  172.     char tmp[4] = {0};
  173.  
  174.     if (memcmp(p, tmp, 4))    
  175.     {
  176.         m_DateCheck = TRUE;
  177.         m_Date.y = *p++ << 8;
  178.         m_Date.y |= *p++;
  179.         m_Date.m = *p++;
  180.         m_Date.d = *p++;
  181.         if (m_Date.y < 1900 || m_Date.y > 3000 || m_Date.m < 1 || m_Date.m > 12 || m_Date.d < 1 || m_Date.d > 31)
  182.             return FALSE;
  183.     }
  184.     else
  185.     {
  186.         p += 4;
  187.         m_DateCheck = FALSE;
  188.     }
  189.     
  190.     if (memcmp(p, tmp, 3))    
  191.     {
  192.         m_FromCheck = TRUE;
  193.         m_FromTime.h = *p++;
  194.         m_FromTime.m = *p++;
  195.         m_FromTime.s = *p++;
  196.         if (m_FromTime.h > 23 || m_FromTime.m > 59 || m_FromTime.s > 59)
  197.             return FALSE;
  198.     }
  199.     else
  200.     {
  201.         p += 3;
  202.         m_FromCheck = FALSE;
  203.     }
  204.  
  205.     if (memcmp(p, tmp, 3))    
  206.     {
  207.         m_ToCheck = TRUE;
  208.         m_ToTime.h = *p++;
  209.         m_ToTime.m = *p++;
  210.         m_ToTime.s = *p++;
  211.         if (m_ToTime.h > 23 || m_ToTime.m > 59 || m_ToTime.s > 59)
  212.             return FALSE;
  213.     }
  214.     else
  215.     {
  216.         p += 3;
  217.         m_ToCheck = FALSE;
  218.     }
  219.  
  220.     m_Day = *p++;
  221.  
  222.     return p;
  223. }