src: Improving consistency and correcting code style

As of this commit, all data structures can be directly constructed from
wire format.

This commit excludes full correction of code style in security/ and
tools/ndnsec*, which will be part of a different commit.

Change-Id: I121ac1f81948bc7468990df52cdefeb2988d91a1
Refs: #1403
diff --git a/src/security/sec-tpm-file.cpp b/src/security/sec-tpm-file.cpp
index 98e117f..3a05282 100644
--- a/src/security/sec-tpm-file.cpp
+++ b/src/security/sec-tpm-file.cpp
@@ -29,11 +29,11 @@
 public:
   Impl(const string& dir)
   {
-    if(dir.empty())
+    if (dir.empty())
       m_keystorePath = boost::filesystem::path(getenv("HOME")) / ".ndn" / "ndnsec-tpm-file";
     else
       m_keystorePath = dir;
-    
+
     boost::filesystem::create_directories (m_keystorePath);
   }
 
@@ -47,22 +47,22 @@
 
     boost::algorithm::trim(digest);
     std::replace(digest.begin(), digest.end(), '/', '%');
-    
+
     return m_keystorePath / (digest + extension);
   }
 
-  string 
+  string
   maintainMapping(const string& keyName)
   {
     string keyFileName = nameTransform(keyName, "").string();
-    
+
     ofstream outfile;
     string dirFile = (m_keystorePath / "mapping.txt").string();
-    
+
     outfile.open(dirFile.c_str(), std::ios_base::app);
     outfile << keyName << ' ' << keyFileName << '\n';
     outfile.close();
-    
+
     return keyFileName;
   }
 
@@ -77,19 +77,19 @@
 {}
 
 void
-SecTpmFile::generateKeyPairInTpm(const Name & keyName, KeyType keyType, int keySize)
+SecTpmFile::generateKeyPairInTpm(const Name& keyName, KeyType keyType, int keySize)
 {
   string keyURI = keyName.toUri();
 
-  if(doesKeyExistInTpm(keyName, KEY_CLASS_PUBLIC))
+  if (doesKeyExistInTpm(keyName, KEY_CLASS_PUBLIC))
     throw Error("public key exists");
-  if(doesKeyExistInTpm(keyName, KEY_CLASS_PRIVATE))
+  if (doesKeyExistInTpm(keyName, KEY_CLASS_PRIVATE))
     throw Error("private key exists");
 
   string keyFileName = m_impl->maintainMapping(keyURI);
 
   try{
-    switch(keyType){
+    switch (keyType){
     case KEY_TYPE_RSA:
       {
         using namespace CryptoPP;
@@ -97,18 +97,18 @@
 
 	InvertibleRSAFunction privateKey;
 	privateKey.Initialize(rng, keySize);
-	
+
 	string privateKeyFileName = keyFileName + ".pri";
 	Base64Encoder privateKeySink(new FileSink(privateKeyFileName.c_str()));
 	privateKey.DEREncode(privateKeySink);
 	privateKeySink.MessageEnd();
-	
+
 	RSAFunction publicKey(privateKey);
 	string publicKeyFileName = keyFileName + ".pub";
 	Base64Encoder publicKeySink(new FileSink(publicKeyFileName.c_str()));
 	publicKey.DEREncode(publicKeySink);
 	publicKeySink.MessageEnd();
-	
+
 	/*set file permission*/
 	chmod(privateKeyFileName.c_str(), 0000400);
 	chmod(publicKeyFileName.c_str(), 0000444);
@@ -117,37 +117,37 @@
     default:
       throw Error("Unsupported key type!");
     }
-  }catch(const CryptoPP::Exception& e){
+  }catch (const CryptoPP::Exception& e){
     throw Error(e.what());
   }
 }
 
 void
-SecTpmFile::deleteKeyPairInTpm(const Name &keyName)
+SecTpmFile::deleteKeyPairInTpm(const Name& keyName)
 {
   boost::filesystem::path publicKeyPath(m_impl->nameTransform(keyName.toUri(), ".pub"));
   boost::filesystem::path privateKeyPath(m_impl->nameTransform(keyName.toUri(), ".pri"));
 
-  if(boost::filesystem::exists(publicKeyPath))
+  if (boost::filesystem::exists(publicKeyPath))
     boost::filesystem::remove(publicKeyPath);
 
-  if(boost::filesystem::exists(privateKeyPath))
+  if (boost::filesystem::exists(privateKeyPath))
     boost::filesystem::remove(privateKeyPath);
 }
 
 shared_ptr<PublicKey>
-SecTpmFile::getPublicKeyFromTpm(const Name & keyName)
+SecTpmFile::getPublicKeyFromTpm(const Name&  keyName)
 {
   string keyURI = keyName.toUri();
 
-  if(!doesKeyExistInTpm(keyName, KEY_CLASS_PUBLIC))
+  if (!doesKeyExistInTpm(keyName, KEY_CLASS_PUBLIC))
     throw Error("Public Key already exist");
 
   ostringstream os;
   try{
     using namespace CryptoPP;
     FileSource(m_impl->nameTransform(keyURI, ".pub").string().c_str(), true, new Base64Decoder(new FileSink(os)));
-  }catch(const CryptoPP::Exception& e){
+  }catch (const CryptoPP::Exception& e){
     throw Error(e.what());
   }
 
@@ -158,9 +158,9 @@
 SecTpmFile::exportPrivateKeyPkcs1FromTpm(const Name& keyName)
 {
   OBufferStream privateKeyOs;
-  CryptoPP::FileSource(m_impl->nameTransform(keyName.toUri(), ".pri").string().c_str(), true, 
+  CryptoPP::FileSource(m_impl->nameTransform(keyName.toUri(), ".pri").string().c_str(), true,
                        new CryptoPP::Base64Decoder(new CryptoPP::FileSink(privateKeyOs)));
-  
+
   return privateKeyOs.buf();
 }
 
@@ -173,7 +173,7 @@
     CryptoPP::StringSource(buf, size, true,
                            new CryptoPP::Base64Encoder(new CryptoPP::FileSink(keyFileName.c_str())));
     return true;
-  }catch(...){
+  }catch (...){
     return false;
   }
 }
@@ -187,19 +187,19 @@
     CryptoPP::StringSource(buf, size, true,
                            new CryptoPP::Base64Encoder(new CryptoPP::FileSink(keyFileName.c_str())));
     return true;
-  }catch(...){
+  }catch (...){
     return false;
   }
 }
 
 Block
-SecTpmFile::signInTpm(const uint8_t *data, size_t dataLength, const Name& keyName, DigestAlgorithm digestAlgorithm)
+SecTpmFile::signInTpm(const uint8_t* data, size_t dataLength, const Name& keyName, DigestAlgorithm digestAlgorithm)
 {
   string keyURI = keyName.toUri();
 
-  if(!doesKeyExistInTpm(keyName, KEY_CLASS_PRIVATE))
+  if (!doesKeyExistInTpm(keyName, KEY_CLASS_PRIVATE))
     throw Error("private key doesn't exists");
- 
+
   try{
     using namespace CryptoPP;
     AutoSeededRandomPool rng;
@@ -211,22 +211,22 @@
     bytes.MessageEnd();
     RSA::PrivateKey privateKey;
     privateKey.Load(bytes);
-  
+
     //Sign message
-    switch(digestAlgorithm){
+    switch (digestAlgorithm){
     case DIGEST_ALGORITHM_SHA256:
       {
 	RSASS<PKCS1v15, SHA256>::Signer signer(privateKey);
-	
+
 	OBufferStream os;
 	StringSource(data, dataLength, true, new SignerFilter(rng, signer, new FileSink(os)));
-	
+
 	return Block(Tlv::SignatureValue, os.buf());
       }
     default:
       throw Error("Unsupported digest algorithm!");
     }
-  }catch(const CryptoPP::Exception& e){
+  }catch (const CryptoPP::Exception& e){
     throw Error(e.what());
   }
 }
@@ -239,7 +239,7 @@
   // string keyURI = keyName.toUri();
   // if (!isSymmetric)
   //   {
-  //     if(!doesKeyExistInTpm(keyName, KEY_CLASS_PRIVATE))
+  //     if (!doesKeyExistInTpm(keyName, KEY_CLASS_PRIVATE))
   //       throw Error("private key doesn't exist");
 
   //     try{
@@ -254,27 +254,27 @@
   //       RSA::PrivateKey privateKey;
   //       privateKey.Load(bytes);
   //       RSAES_PKCS1v15_Decryptor decryptor(privateKey);
-	
+
   //       OBufferStream os;
   //       StringSource(data, dataLength, true, new PK_DecryptorFilter(rng, decryptor, new FileSink(os)));
-	
+
   //       return os.buf();
   //     }
-  //     catch(const CryptoPP::Exception& e){
+  //     catch (const CryptoPP::Exception& e){
   //       throw Error(e.what());
   //     }
   //   }
   // else
   //   {
   //     throw Error("Symmetric encryption is not implemented!");
-  //     // if(!doesKeyExistInTpm(keyName, KEY_CLASS_SYMMETRIC))
+  //     // if (!doesKeyExistInTpm(keyName, KEY_CLASS_SYMMETRIC))
   //     // 	throw Error("symmetric key doesn't exist");
 
   //     // try{
   //     // 	string keyBits;
   //     // 	string symKeyFileName = m_impl->nameTransform(keyURI, ".key");
   //     // 	FileSource(symKeyFileName, true, new HexDecoder(new StringSink(keyBits)));
-	
+
   //     // 	using CryptoPP::AES;
   //     // 	AutoSeededRandomPool rnd;
   //     // 	byte iv[AES::BLOCKSIZE];
@@ -282,12 +282,12 @@
 
   //     // 	CFB_Mode<AES>::Decryption decryptor;
   //     // 	decryptor.SetKeyWithIV(reinterpret_cast<const uint8_t*>(keyBits.c_str()), keyBits.size(), iv);
-	
+
   //     // 	OBufferStream os;
   //     // 	StringSource(data, dataLength, true, new StreamTransformationFilter(decryptor,new FileSink(os)));
   //     // 	return os.buf();
 
-  //     // }catch(const CryptoPP::Exception& e){
+  //     // }catch (const CryptoPP::Exception& e){
   //     // 	throw Error(e.what());
   //     // }
   //   }
@@ -301,7 +301,7 @@
 
   // if (!isSymmetric)
   //   {
-  //     if(!doesKeyExistInTpm(keyName, KEY_CLASS_PUBLIC))
+  //     if (!doesKeyExistInTpm(keyName, KEY_CLASS_PUBLIC))
   //       throw Error("public key doesn't exist");
   //     try
   //       {
@@ -322,14 +322,14 @@
   //         StringSource(data, dataLength, true, new PK_EncryptorFilter(rng, encryptor, new FileSink(os)));
   //         return os.buf();
   //       }
-  //     catch(const CryptoPP::Exception& e){
+  //     catch (const CryptoPP::Exception& e){
   //       throw Error(e.what());
   //     }
   //   }
   // else
   //   {
   //     throw Error("Symmetric encryption is not implemented!");
-  //     // if(!doesKeyExistInTpm(keyName, KEY_CLASS_SYMMETRIC))
+  //     // if (!doesKeyExistInTpm(keyName, KEY_CLASS_SYMMETRIC))
   //     // 	throw Error("symmetric key doesn't exist");
 
   //     // try{
@@ -348,7 +348,7 @@
   //     // 	OBufferStream os;
   //     // 	StringSource(data, dataLength, true, new StreamTransformationFilter(encryptor, new FileSink(os)));
   //     // 	return os.buf();
-  //     // }catch(const CryptoPP::Exception& e){
+  //     // }catch (const CryptoPP::Exception& e){
   //     // 	throw Error(e.what());
   //     // }
   //   }
@@ -356,19 +356,19 @@
 
 
 void
-SecTpmFile::generateSymmetricKeyInTpm(const Name & keyName, KeyType keyType, int keySize)
+SecTpmFile::generateSymmetricKeyInTpm(const Name& keyName, KeyType keyType, int keySize)
 {
   throw Error("SecTpmFile::generateSymmetricKeyInTpm is not supported!");
   // string keyURI = keyName.toUri();
 
-  // if(doesKeyExistInTpm(keyName, KEY_CLASS_SYMMETRIC))
+  // if (doesKeyExistInTpm(keyName, KEY_CLASS_SYMMETRIC))
   //   throw Error("symmetric key exists");
 
   // string keyFileName = m_impl->maintainMapping(keyURI);
   // string symKeyFileName = keyFileName + ".key";
 
   // try{
-  //   switch(keyType){
+  //   switch (keyType){
   //   case KEY_TYPE_AES:
   //     {
   //       using namespace CryptoPP;
@@ -376,41 +376,41 @@
 
   //       SecByteBlock key(0x00, keySize);
   //       rng.GenerateBlock(key, keySize);
-	
+
   //       StringSource(key, key.size(), true, new HexEncoder(new FileSink(symKeyFileName.c_str())));
-	
+
   //       chmod(symKeyFileName.c_str(), 0000400);
   //       return;
   //     }
   //   default:
   //     throw Error("Unsupported symmetric key type!");
   //   }
-  // }catch(const CryptoPP::Exception& e){
+  // }catch (const CryptoPP::Exception& e){
   //   throw Error(e.what());
   // }
 }
 
 bool
-SecTpmFile::doesKeyExistInTpm(const Name & keyName, KeyClass keyClass)
+SecTpmFile::doesKeyExistInTpm(const Name& keyName, KeyClass keyClass)
 {
   string keyURI = keyName.toUri();
   if (keyClass == KEY_CLASS_PUBLIC)
     {
-      if(boost::filesystem::exists(m_impl->nameTransform(keyURI, ".pub")))
+      if (boost::filesystem::exists(m_impl->nameTransform(keyURI, ".pub")))
         return true;
       else
         return false;
     }
   if (keyClass == KEY_CLASS_PRIVATE)
     {
-      if(boost::filesystem::exists(m_impl->nameTransform(keyURI, ".pri")))
+      if (boost::filesystem::exists(m_impl->nameTransform(keyURI, ".pri")))
         return true;
       else
         return false;
     }
   if (keyClass == KEY_CLASS_SYMMETRIC)
     {
-      if(boost::filesystem::exists(m_impl->nameTransform(keyURI, ".key")))
+      if (boost::filesystem::exists(m_impl->nameTransform(keyURI, ".key")))
         return true;
       else
         return false;
@@ -421,11 +421,12 @@
 bool
 SecTpmFile::generateRandomBlock(uint8_t* res, size_t size)
 {
-  try{
+  try {
     CryptoPP::AutoSeededRandomPool rng;
     rng.GenerateBlock(res, size);
     return true;
-  }catch(const CryptoPP::Exception& e){
+  }
+  catch (const CryptoPP::Exception& e) {
     return false;
   }
 }