Prechádzať zdrojové kódy

* Fix overloaded problems with writeFile() not passing the proper enc_flags around

Bryan Drewery 16 rokov pred
rodič
commit
fbbfdf4e18
3 zmenil súbory, kde vykonal 20 pridanie a 18 odobranie
  1. 7 8
      src/EncryptedStream.c
  2. 11 9
      src/EncryptedStream.h
  3. 2 1
      src/mod/share.mod/share.c

+ 7 - 8
src/EncryptedStream.c

@@ -14,7 +14,6 @@ int EncryptedStream::loadFile (const int fd) {
     return 1;
 
   bd::String in_buf;
-  char enc_flags = 0;
 
   /* Peak at the first few bytes to determine the algorithm used */
   if (str[0] == 0x7F && str[2] == 0x7F) {
@@ -40,20 +39,20 @@ int EncryptedStream::loadFile (const int fd) {
     bd::String buf(in_buf), line, out_buf;
     while (buf.length()) {
       line = newsplit(buf, '\n');
-      unapply_filters(line, enc_flags);
+      unapply_filters(line);
       line += '\n';
       out_buf += line;
     }
     in_buf = out_buf;
   } else {
-    unapply_filters(in_buf, enc_flags);
+    unapply_filters(in_buf);
   }
 
   str = in_buf;
   return 0;
 }
 
-void EncryptedStream::apply_filters(bd::String& buf, const char enc_flags) const {
+void EncryptedStream::apply_filters(bd::String& buf) const {
 #ifdef unimplemented
   if (enc_flags & ENC_AES_256_CBC)
     buf = encrypt_string_cbc(key, buf);
@@ -66,7 +65,7 @@ void EncryptedStream::apply_filters(bd::String& buf, const char enc_flags) const
     buf = bd::base64Encode(buf);
 }
 
-void EncryptedStream::unapply_filters(bd::String& buf, const char enc_flags) const {
+void EncryptedStream::unapply_filters(bd::String& buf) const {
   if (enc_flags & ENC_BASE64_BROKEN)
     buf = broken_base64Decode(buf);
   if (enc_flags & ENC_BASE64)
@@ -79,7 +78,7 @@ void EncryptedStream::unapply_filters(bd::String& buf, const char enc_flags) con
     buf = decrypt_string(key, buf);
 }
 
-int EncryptedStream::writeFile (const int fd, const char enc_flags) const {
+int EncryptedStream::writeFile (const int fd) const {
   if (!key.length()) return bd::Stream::writeFile(fd);
 
   /* Encrypt the stream before writing it out */
@@ -89,14 +88,14 @@ int EncryptedStream::writeFile (const int fd, const char enc_flags) const {
     bd::String buf(str), line;
     while (buf.length()) {
       line = newsplit(buf, '\n');
-      apply_filters(line, enc_flags);
+      apply_filters(line);
       line += '\n';
       out_buf += line;
     }
 
   } else {
     out_buf = str;
-    apply_filters(out_buf, enc_flags);
+    apply_filters(out_buf);
   }
 
   if (enc_flags & ENC_NO_HEADER)

+ 11 - 9
src/EncryptedStream.h

@@ -23,21 +23,23 @@ namespace bd {
 class EncryptedStream : public bd::Stream {
   private:
         bd::String key;
-        void apply_filters(bd::String& buf, const char enc_flags) const;
-        void unapply_filters(bd::String& buf, const char enc_flags) const;
+        mutable char enc_flags;
+        void apply_filters(bd::String& buf) const;
+        void unapply_filters(bd::String& buf) const;
 
   protected:
 
   public:
-        EncryptedStream(const char* keyStr) : Stream(), key(bd::String(keyStr)) {};
-        EncryptedStream(bd::String& keyStr) : Stream(), key(keyStr) {};
-        EncryptedStream(EncryptedStream& stream) : Stream(stream), key(stream.key) {};
+        EncryptedStream(const char* keyStr) : Stream(), key(bd::String(keyStr)), enc_flags(0) {};
+        EncryptedStream(bd::String& keyStr) : Stream(), key(keyStr), enc_flags(0) {};
+        EncryptedStream(EncryptedStream& stream) : Stream(stream), key(stream.key), enc_flags(0) {};
 
+        void setFlags(const char _enc_flags) const { enc_flags = _enc_flags; }
         virtual int loadFile(const int fd);
-        virtual int loadFile(const char* fname) { return bd::Stream::loadFile(fname); }
+        virtual int writeFile(const int fd) const;
 
-        virtual int writeFile(const int fd, const char enc_flags) const;
-        virtual int writeFile(const int fd) const { return writeFile(fd, ENC_DEFAULT); }
-        int writeFile(const char* fname, mode_t mode = (S_IRUSR|S_IWUSR)) const { return bd::Stream::writeFile(fname, mode); }
+        // Overloaded virtuals need to be called to prevent 'hiding' ... good job compiler.
+        virtual int loadFile(const char* fname) { return bd::Stream::loadFile(fname); }
+        virtual int writeFile(const char* fname, mode_t mode = (S_IRUSR|S_IWUSR)) const { return bd::Stream::writeFile(fname, mode); }
 };
 #endif

+ 2 - 1
src/mod/share.mod/share.c

@@ -1483,7 +1483,8 @@ start_sending_users(int idx)
   const char salt1[] = SALT1;
   EncryptedStream stream(salt1);
   stream_writeuserfile(stream, userlist, idx, old);
-  if (stream.writeFile(share_file, (ENC_KEEP_NEWLINES|ENC_AES_256_ECB|ENC_BASE64_BROKEN))) {
+  stream.setFlags(ENC_KEEP_NEWLINES|ENC_AES_256_ECB|ENC_BASE64_BROKEN);
+  if (stream.writeFile(share_file)) {
     putlog(LOG_MISC, "*", "ERROR writing user file to transfer.");
     unlink(share_file);
   }