Commits

Andreas Tscharner committed a664194

Complete encryption: Prepare Collector and Parser

* InFileParser: Move get_password into public scope and make it static (used
in Collector)
* New parameters for encryption type and password in Collector

Comments (0)

Files changed (4)

src/compiler/Collector.cxx

 	delete[] m_rcName;
 }
 
-int Collector::collect(std::vector<ResourceData *> *p_resEntries, lrc::CompressionType p_compress)
+int Collector::collect(std::vector<ResourceData *> *p_resEntries,
+                       lrc::CompressionType p_compress,
+                       lrc::EncryptionType p_encrypt,
+                       const unsigned char *p_key)
 {
+	const char WHOLE_FILE[] = "Whole file";
+
 	size_t entriesCount = p_resEntries->size();
 	unsigned int currentOffset = 0, currentEntry = 0;
 	resEntry *resEntries;
 	unsigned char *bufferPos = nullptr;
 	unsigned char *resData = nullptr;
 	unsigned char *compressedData;
+	unsigned char *encryptedData;
 	unsigned char **resourceDataList;
 	int retVal, writtenItems;
 	size_t resDataSize, totalSize, finalSize;
 	inFilePosition warnErr;
 	char *doubleID;
 	lrc::CompressDecompress *compressor;
+	lrc::EncryptDecrypt *encryptor;
 
 
 	retVal = this->are_resIDs_unique(*p_resEntries, warnErr, &doubleID);
 	};
 	DEBUG_PRINT((" Old size: %d; new size: %d.\n", totalSize, finalSize))
 
+	DEBUG_PRINT(("Encrypt the data with encryption type %d.", p_encrypt))
+	try {
+		encryptor = EncryptionFactory::get_encryption_class(p_encrypt, const_cast<char *>(WHOLE_FILE));
+	} catch (lrcEncryptionDisabledException const &encDisEx) {
+		delete[] compressedData;
+		return ERROR_ENCRYPTION_NOT_AVAILABLE;
+	};
+	retVal = encryptor->encrypt(p_key, compressedData, finalSize, &encryptedData, finalSize);
+	delete encryptor;
+	if (! success(retVal)) {
+		DEBUG_PRINT(("Encryption failed with error code: %d\n", retVal))
+
+		return ERROR_ENCRYPTION_ENCRYPT;
+	};
+	DEBUG_PRINT(("\n"))
+
 	rdfFile = fopen(m_rdfName, "wb");
 	if (!rdfFile) {
 		return ERROR_FILE_OPEN;
 	};
 
 	DEBUG_PRINT(("Writing data to file (%s)\n", m_rdfName))
-	writtenItems = fwrite(compressedData, sizeof(unsigned char), finalSize, rdfFile);
+	writtenItems = fwrite(encryptedData, sizeof(unsigned char), finalSize, rdfFile);
 	fclose(rdfFile);
-	delete[] compressedData;
+	delete[] encryptedData;
 
 	if (writtenItems != finalSize) {
 		DEBUG_PRINT(("Error writing to file (written: %d; total: %d)\n", writtenItems, finalSize))

src/compiler/Collector.hxx

  * responsible to collect all resource data
  *
  * \author Andreas Tscharner
- * \date 2012-03-24
+ * \date 2012-05-06
  */
 
 
 		 *
 		 * \param[in] p_resEntries Collection of data resource entries
 		 * \param[in] p_compress Compression type for complete file
+		 * \param[in] p_encrypt Encryption type for complete file
+		 * \param[in] p_key Password if encryption is requested
 		 *
 		 * \retval NO_ERROR Resource Data File successfully compiled
 		 * \retval ERROR_FILE_OPEN An error occurred opening the rdf file
 		 * \retval ERROR_FILE_READ An error occurred while reading the file
 		 * \retval ERROR_FILE_WRITE An error occurred while writing the file
 		 */
-		int collect(std::vector<ResourceData *> *, lrc::CompressionType);
+		int collect(std::vector<ResourceData *> *, lrc::CompressionType, lrc::EncryptionType, const unsigned char *);
 };
 
 

src/compiler/InFileParser.cxx

 #include "InFileParser.hxx"
 
 
-unsigned char *InFileParser::get_password(const char *p_passwdStr) throw(lrcFileNotFoundException)
-{
-	char *pwdFilename;
-	unsigned char *retPasswd;
-	size_t pwdLen;
-	FILE *pwdFile;
-	int pwdFileSize;
-
-
-	if (!p_passwdStr) {
-		return nullptr;
-	};
-
-	pwdLen = strlen(p_passwdStr);
-	if (pwdLen == 0) {
-		return nullptr;
-	};
-
-	if (*p_passwdStr != '@') {
-		retPasswd = new unsigned char[pwdLen+1];
-		memset(retPasswd, 0, (pwdLen+1));
-		memcpy(retPasswd, p_passwdStr, pwdLen);
-		return retPasswd;
-	};
-
-	DEBUG_PRINT(("Password file notation found: %s\n", p_passwdStr))
-	pwdFilename = new char[pwdLen];
-	memset(pwdFilename, 0, pwdLen);
-	memcpy(pwdFilename, &(p_passwdStr[1]), (pwdLen-1));
-
-	if (!file_exists(pwdFilename)) {
-		delete[] pwdFilename;
-		throw lrcFileNotFoundException((char *)&(p_passwdStr[1]));
-	};
-
-	pwdFileSize = file_size(pwdFilename);
-	retPasswd = new unsigned char[pwdFileSize+1];
-	memset(retPasswd, 0, (pwdFileSize+1));
-
-	pwdFile = fopen(pwdFilename, "rb");
-	if (!pwdFile) {
-		delete[] retPasswd;
-		delete[] pwdFilename;
-
-		return nullptr;
-	};
-	if (fread(retPasswd, sizeof(unsigned char), pwdFileSize, pwdFile) != pwdFileSize) {
-		fclose(pwdFile);
-		delete[] retPasswd;
-		delete[] pwdFilename;
-
-		return nullptr;
-	};
-	fclose(pwdFile);
-	delete[] pwdFilename;
-
-	DEBUG_PRINT(("Returned password [%s]\n", retPasswd))
-	return retPasswd;
-}
-
 void InFileParser::clear_internal_error(void)
 {
 	m_errorPosition = std::make_tuple(-1, -1);
 	return lrc::NoneEncryption;
 }
 
+unsigned char *InFileParser::get_password(const char *p_passwdStr) throw(lrcFileNotFoundException)
+{
+	char *pwdFilename;
+	unsigned char *retPasswd;
+	size_t pwdLen;
+	FILE *pwdFile;
+	int pwdFileSize;
+
+
+	if (!p_passwdStr) {
+		return nullptr;
+	};
+
+	pwdLen = strlen(p_passwdStr);
+	if (pwdLen == 0) {
+		return nullptr;
+	};
+
+	if (*p_passwdStr != '@') {
+		retPasswd = new unsigned char[pwdLen+1];
+		memset(retPasswd, 0, (pwdLen+1));
+		memcpy(retPasswd, p_passwdStr, pwdLen);
+		return retPasswd;
+	};
+
+	DEBUG_PRINT(("Password file notation found: %s\n", p_passwdStr))
+	pwdFilename = new char[pwdLen];
+	memset(pwdFilename, 0, pwdLen);
+	memcpy(pwdFilename, &(p_passwdStr[1]), (pwdLen-1));
+
+	if (!file_exists(pwdFilename)) {
+		delete[] pwdFilename;
+		throw lrcFileNotFoundException((char *)&(p_passwdStr[1]));
+	};
+
+	pwdFileSize = file_size(pwdFilename);
+	retPasswd = new unsigned char[pwdFileSize+1];
+	memset(retPasswd, 0, (pwdFileSize+1));
+
+	pwdFile = fopen(pwdFilename, "rb");
+	if (!pwdFile) {
+		delete[] retPasswd;
+		delete[] pwdFilename;
+
+		return nullptr;
+	};
+	if (fread(retPasswd, sizeof(unsigned char), pwdFileSize, pwdFile) != pwdFileSize) {
+		fclose(pwdFile);
+		delete[] retPasswd;
+		delete[] pwdFilename;
+
+		return nullptr;
+	};
+	fclose(pwdFile);
+	delete[] pwdFilename;
+
+	DEBUG_PRINT(("Returned password [%s]\n", retPasswd))
+	return retPasswd;
+}
+
 int InFileParser::get_internal_error(inFilePosition &p_errPos, char **errMsg)
 {
 	const int MAX_ERRMSG_LEN = 512;

src/compiler/InFileParser.hxx

 		int m_lastError;                              //!< Error code of last error
 		internalErrorType m_internalError;            //!< Internal error
 
-		/*! \brief Get password
-		 *
-		 * This method gets the password, either directly from the .rc or
-		 * .rif file or it is getting the password from a different file if
-		 * the "@" notation (re-direct to a file) is used.
-		 *
-		 * \param[in] p_passwdStr Password string
-		 *
-		 * \return Password directly from input file or from re-directed
-		 *         file
-		 *
-		 * \exception lrcFileNotFoundException This exception will be found
-		 *            if the password should be read from a file and the
-		 *            given file could not be found
-		 *
-		 * \remarks The caller is responsible to free the used memory that
-		 *          the returned password needs
-		 */
-		 unsigned char *get_password(const char *) throw(lrcFileNotFoundException);
 
 		/*! \brief Clear all internal errors
 		 *
 		 *          defines no other encryption type
 		 */
 		static lrc::EncryptionType eval_encryption_type(const char *);
+		/*! \brief Get password
+		 *
+		 * This method gets the password, either directly from the .rc or
+		 * .rif file or it is getting the password from a different file if
+		 * the "@" notation (re-direct to a file) is used.
+		 *
+		 * \param[in] p_passwdStr Password string
+		 *
+		 * \return Password directly from input file or from re-directed
+		 *         file
+		 *
+		 * \exception lrcFileNotFoundException This exception will be found
+		 *            if the password should be read from a file and the
+		 *            given file could not be found
+		 *
+		 * \remarks The caller is responsible to free the used memory that
+		 *          the returned password needs
+		 */
+		 static unsigned char *get_password(const char *) throw(lrcFileNotFoundException);
 
 		/*! \brief Parses the file
 		 *
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.