Advertisement
Guest User

Diff for Crypto++ gzip for filename and comment processing

a guest
Jan 2nd, 2015
337
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Diff 5.27 KB | None | 0 0
  1. Index: gzip.h
  2. ===================================================================
  3. --- gzip.h  (revision 541)
  4. +++ gzip.h  (working copy)
  5. @@ -12,13 +12,20 @@
  6.  {
  7.  public:
  8.     Gzip(BufferedTransformation *attachment=NULL, unsigned int deflateLevel=DEFAULT_DEFLATE_LEVEL, unsigned int log2WindowSize=DEFAULT_LOG2_WINDOW_SIZE, bool detectUncompressible=true)
  9. -       : Deflator(attachment, deflateLevel, log2WindowSize, detectUncompressible) {}
  10. +       : Deflator(attachment, deflateLevel, log2WindowSize, detectUncompressible), m_filetime(0) {}
  11.     Gzip(const NameValuePairs &parameters, BufferedTransformation *attachment=NULL)
  12. -       : Deflator(parameters, attachment) {}
  13. +       : Deflator(parameters, attachment), m_filetime(0) {}
  14. +    
  15. +    void SetFiletime(word32 filetime) { m_filetime = filetime; }
  16. +    void SetFilename(const std::string& filename) { m_filename = filename; }
  17. +    void SetComment(const std::string& comment) { m_comment = comment; }
  18.  
  19.  protected:
  20.     enum {MAGIC1=0x1f, MAGIC2=0x8b,   // flags for the header
  21.           DEFLATED=8, FAST=4, SLOW=2};
  22. +    
  23. +    enum FLAG_MASKS {
  24. +       FILENAME=8, COMMENTS=16};
  25.  
  26.     void WritePrestreamHeader();
  27.     void ProcessUncompressedData(const byte *string, size_t length);
  28. @@ -26,6 +33,10 @@
  29.  
  30.     word32 m_totalLen;
  31.     CRC32 m_crc;
  32. +    
  33. +    word32 m_filetime;
  34. +    std::string m_filename;
  35. +    std::string m_comment;
  36.  };
  37.  
  38.  /// GZIP Decompression (RFC 1952)
  39. @@ -42,6 +53,10 @@
  40.         \param autoSignalPropagation 0 to turn off MessageEnd signal
  41.     */
  42.     Gunzip(BufferedTransformation *attachment = NULL, bool repeat = false, int autoSignalPropagation = -1);
  43. +    
  44. +    word32 GetFiletime() const { return m_filetime; }
  45. +    const std::string& GetFilename() const { return m_filename; }
  46. +    const std::string& GetComment() const { return m_comment; }
  47.  
  48.  protected:
  49.     enum {MAGIC1=0x1f, MAGIC2=0x8b,   // flags for the header
  50. @@ -58,6 +73,10 @@
  51.  
  52.     word32 m_length;
  53.     CRC32 m_crc;
  54. +
  55. +    word32 m_filetime;
  56. +    std::string m_filename;
  57. +    std::string m_comment;
  58.  };
  59.  
  60.  NAMESPACE_END
  61. Index: gzip.cpp
  62. ===================================================================
  63. --- gzip.cpp    (revision 541)
  64. +++ gzip.cpp    (working copy)
  65. @@ -9,15 +9,27 @@
  66.  {
  67.     m_totalLen = 0;
  68.     m_crc.Restart();
  69. +    
  70. +    word32 flags = 0;
  71. +    if(!m_filename.empty())
  72. +        flags |= FILENAME;
  73. +    if(!m_comment.empty())
  74. +        flags |= COMMENTS;
  75.  
  76.     AttachedTransformation()->Put(MAGIC1);
  77.     AttachedTransformation()->Put(MAGIC2);
  78.     AttachedTransformation()->Put(DEFLATED);
  79. -   AttachedTransformation()->Put(0);       // general flag
  80. -   AttachedTransformation()->PutWord32(0); // time stamp
  81. -   byte extra = (GetDeflateLevel() == 1) ? FAST : ((GetDeflateLevel() == 9) ? SLOW : 0);
  82. +   AttachedTransformation()->Put(flags);               // general flag
  83. +   AttachedTransformation()->PutWord32(m_filetime);    // time stamp
  84. +    byte extra = (GetDeflateLevel() == 1) ? FAST : ((GetDeflateLevel() == 9) ? SLOW : 0);
  85.     AttachedTransformation()->Put(extra);
  86. -   AttachedTransformation()->Put(GZIP_OS_CODE);
  87. +    AttachedTransformation()->Put(GZIP_OS_CODE);
  88. +    
  89. +    if(!m_filename.empty())
  90. +        AttachedTransformation()->Put((const unsigned char*)m_filename.data(), m_filename.size() +1);
  91. +    
  92. +    if(!m_comment.empty())
  93. +        AttachedTransformation()->Put((const unsigned char*)m_comment.data(), m_comment.size() +1);
  94.  }
  95.  
  96.  void Gzip::ProcessUncompressedData(const byte *inString, size_t length)
  97. @@ -37,7 +49,7 @@
  98.  // *************************************************************
  99.  
  100.  Gunzip::Gunzip(BufferedTransformation *attachment, bool repeat, int propagation)
  101. -   : Inflator(attachment, repeat, propagation)
  102. +   : Inflator(attachment, repeat, propagation), m_filetime(0)
  103.  {
  104.  }
  105.  
  106. @@ -45,16 +57,25 @@
  107.  {
  108.     m_length = 0;
  109.     m_crc.Restart();
  110. +    
  111. +    m_filetime = 0;
  112. +    
  113. +    m_filename.erase(0);
  114. +    m_filename.reserve(16);
  115. +    
  116. +    m_comment.erase(0);
  117. +    m_comment.reserve(32);
  118.  
  119.     byte buf[6];
  120.     byte b, flags;
  121.  
  122.     if (m_inQueue.Get(buf, 2)!=2) throw HeaderErr();
  123.     if (buf[0] != MAGIC1 || buf[1] != MAGIC2) throw HeaderErr();
  124. -   if (!m_inQueue.Skip(1)) throw HeaderErr();   // skip extra flags
  125. +   if (!m_inQueue.Get(b) || (b != DEFLATED)) throw HeaderErr();     // skip CM flag
  126.     if (!m_inQueue.Get(flags)) throw HeaderErr();
  127.     if (flags & (ENCRYPTED | CONTINUED)) throw HeaderErr();
  128. -   if (m_inQueue.Skip(6)!=6) throw HeaderErr();    // Skip file time, extra flags and OS type
  129. +    if (m_inQueue.GetWord32(m_filetime, LITTLE_ENDIAN_ORDER) != 4) throw HeaderErr();
  130. +   if (m_inQueue.Skip(2)!=2) throw HeaderErr();    // Skip extra flags and OS type
  131.  
  132.     if (flags & EXTRA_FIELDS)   // skip extra fields
  133.     {
  134. @@ -63,15 +84,25 @@
  135.         if (m_inQueue.Skip(length)!=length) throw HeaderErr();
  136.     }
  137.  
  138. -   if (flags & FILENAME)   // skip filename
  139. +   if (flags & FILENAME)   // extract filename
  140. +    {
  141.         do
  142. +        {
  143.             if(!m_inQueue.Get(b)) throw HeaderErr();
  144. -       while (b);
  145. +            if(b) m_filename.append( 1, (char)b );
  146. +       }
  147. +        while (b);
  148. +    }
  149.  
  150. -   if (flags & COMMENTS)   // skip comments
  151. +   if (flags & COMMENTS)   // extract comments
  152. +    {
  153.         do
  154. +        {
  155.             if(!m_inQueue.Get(b)) throw HeaderErr();
  156. +            if(b) m_comment.append( 1, (char)b );
  157. +        }
  158.         while (b);
  159. +    }
  160.  }
  161.  
  162.  void Gunzip::ProcessDecompressedData(const byte *inString, size_t length)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement