@@ -690,8 +690,8 @@ static int parse_plumbing_options (const char** key_name, const char** key_file,
690690 return parse_options (options, argc, argv);
691691}
692692
693- // Encrypt contents of stdin and write to stdout
694- int clean (int argc, const char ** argv)
693+ // Encrypt contents of &in and write to &out
694+ int clean (int argc, const char ** argv, std::istream& in, std::ostream& out )
695695{
696696 const char * key_name = 0 ;
697697 const char * key_path = 0 ;
@@ -724,10 +724,10 @@ int clean (int argc, const char** argv)
724724
725725 char buffer[1024 ];
726726
727- while (std::cin && file_size < Aes_ctr_encryptor::MAX_CRYPT_BYTES) {
728- std::cin .read (buffer, sizeof (buffer));
727+ while (in && file_size < Aes_ctr_encryptor::MAX_CRYPT_BYTES) {
728+ in .read (buffer, sizeof (buffer));
729729
730- const size_t bytes_read = std::cin .gcount ();
730+ const size_t bytes_read = in .gcount ();
731731
732732 hmac.add (reinterpret_cast <unsigned char *>(buffer), bytes_read);
733733 file_size += bytes_read;
@@ -775,8 +775,8 @@ int clean (int argc, const char** argv)
775775 hmac.get (digest);
776776
777777 // Write a header that...
778- std::cout .write (" \0 GITCRYPT\0 " , 10 ); // ...identifies this as an encrypted file
779- std::cout .write (reinterpret_cast <char *>(digest), Aes_ctr_encryptor::NONCE_LEN); // ...includes the nonce
778+ out .write (" \0 GITCRYPT\0 " , 10 ); // ...identifies this as an encrypted file
779+ out .write (reinterpret_cast <char *>(digest), Aes_ctr_encryptor::NONCE_LEN); // ...includes the nonce
780780
781781 // Now encrypt the file and write to stdout
782782 Aes_ctr_encryptor aes (key->aes_key , digest);
@@ -787,7 +787,7 @@ int clean (int argc, const char** argv)
787787 while (file_data_len > 0 ) {
788788 const size_t buffer_len = std::min (sizeof (buffer), file_data_len);
789789 aes.process (file_data, reinterpret_cast <unsigned char *>(buffer), buffer_len);
790- std::cout .write (buffer, buffer_len);
790+ out .write (buffer, buffer_len);
791791 file_data += buffer_len;
792792 file_data_len -= buffer_len;
793793 }
@@ -803,14 +803,14 @@ int clean (int argc, const char** argv)
803803 aes.process (reinterpret_cast <unsigned char *>(buffer),
804804 reinterpret_cast <unsigned char *>(buffer),
805805 buffer_len);
806- std::cout .write (buffer, buffer_len);
806+ out .write (buffer, buffer_len);
807807 }
808808 }
809809
810810 return 0 ;
811811}
812812
813- static int decrypt_file_to_stdout (const Key_file& key_file, const unsigned char * header, std::istream& in)
813+ static int decrypt_file_to_stream (const Key_file& key_file, const unsigned char * header, std::istream& in, std::ostream& out = std::cout )
814814{
815815 const unsigned char * nonce = header + 10 ;
816816 uint32_t key_version = 0 ; // TODO: get the version from the file header
@@ -828,7 +828,7 @@ static int decrypt_file_to_stdout (const Key_file& key_file, const unsigned char
828828 in.read (reinterpret_cast <char *>(buffer), sizeof (buffer));
829829 aes.process (buffer, buffer, in.gcount ());
830830 hmac.add (buffer, in.gcount ());
831- std::cout .write (reinterpret_cast <char *>(buffer), in.gcount ());
831+ out .write (reinterpret_cast <char *>(buffer), in.gcount ());
832832 }
833833
834834 unsigned char digest[Hmac_sha1_state::LEN];
@@ -844,8 +844,8 @@ static int decrypt_file_to_stdout (const Key_file& key_file, const unsigned char
844844 return 0 ;
845845}
846846
847- // Decrypt contents of stdin and write to stdout
848- int smudge (int argc, const char ** argv)
847+ // Decrypt contents of &in and write to &out
848+ int smudge (int argc, const char ** argv, std::istream& in, std::ostream& out )
849849{
850850 const char * key_name = 0 ;
851851 const char * key_path = 0 ;
@@ -864,21 +864,21 @@ int smudge (int argc, const char** argv)
864864
865865 // Read the header to get the nonce and make sure it's actually encrypted
866866 unsigned char header[10 + Aes_ctr_decryptor::NONCE_LEN];
867- std::cin .read (reinterpret_cast <char *>(header), sizeof (header));
868- if (std::cin .gcount () != sizeof (header) || std::memcmp (header, " \0 GITCRYPT\0 " , 10 ) != 0 ) {
867+ in .read (reinterpret_cast <char *>(header), sizeof (header));
868+ if (in .gcount () != sizeof (header) || std::memcmp (header, " \0 GITCRYPT\0 " , 10 ) != 0 ) {
869869 // File not encrypted - just copy it out to stdout
870870 std::clog << " git-crypt: Warning: file not encrypted" << std::endl;
871871 std::clog << " git-crypt: Run 'git-crypt status' to make sure all files are properly encrypted." << std::endl;
872872 std::clog << " git-crypt: If 'git-crypt status' reports no problems, then an older version of" << std::endl;
873873 std::clog << " git-crypt: this file may be unencrypted in the repository's history. If this" << std::endl;
874874 std::clog << " git-crypt: file contains sensitive information, you can use 'git filter-branch'" << std::endl;
875875 std::clog << " git-crypt: to remove its old versions from the history." << std::endl;
876- std::cout .write (reinterpret_cast <char *>(header), std::cin .gcount ()); // include the bytes which we already read
877- std::cout << std::cin .rdbuf ();
876+ out .write (reinterpret_cast <char *>(header), in .gcount ()); // include the bytes which we already read
877+ out << in .rdbuf ();
878878 return 0 ;
879879 }
880880
881- return decrypt_file_to_stdout (key_file, header, std::cin );
881+ return decrypt_file_to_stream (key_file, header, in, out );
882882}
883883
884884int diff (int argc, const char ** argv)
@@ -920,7 +920,7 @@ int diff (int argc, const char** argv)
920920 }
921921
922922 // Go ahead and decrypt it
923- return decrypt_file_to_stdout (key_file, header, in);
923+ return decrypt_file_to_stream (key_file, header, in);
924924}
925925
926926void help_init (std::ostream& out)
0 commit comments