summary history branches tags files
commit:68899a086deacb5aaf0250a30581ef27b397f1fb
author:mrmekon
committer:mrmekon
date:Wed Nov 2 20:13:40 2011 -0400
parents:c718d93a71f3958673fd253187e0da3a7ce19e84
Comments, refactoring, and filled out free_packet() to free memory recursively
diff --git a/simplepgp.xcodeproj/trevor.mode1v3 b/simplepgp.xcodeproj/trevor.mode1v3
line changes: +52/-76
index 027de3b..7423f70
--- a/simplepgp.xcodeproj/trevor.mode1v3
+++ b/simplepgp.xcodeproj/trevor.mode1v3
@@ -197,48 +197,7 @@
 	<key>Notifications</key>
 	<array/>
 	<key>OpenEditors</key>
-	<array>
-		<dict>
-			<key>Content</key>
-			<dict>
-				<key>PBXProjectModuleGUID</key>
-				<string>69868EAE1461A414004C5D9B</string>
-				<key>PBXProjectModuleLabel</key>
-				<string>packet.h</string>
-				<key>PBXSplitModuleInNavigatorKey</key>
-				<dict>
-					<key>Split0</key>
-					<dict>
-						<key>PBXProjectModuleGUID</key>
-						<string>69868EAF1461A414004C5D9B</string>
-						<key>PBXProjectModuleLabel</key>
-						<string>packet.h</string>
-						<key>_historyCapacity</key>
-						<integer>0</integer>
-						<key>bookmark</key>
-						<string>69269F1E14620C8900F69705</string>
-						<key>history</key>
-						<array>
-							<string>69269DF31461F1ED00F69705</string>
-						</array>
-					</dict>
-					<key>SplitCount</key>
-					<string>1</string>
-				</dict>
-				<key>StatusBarVisibility</key>
-				<true/>
-			</dict>
-			<key>Geometry</key>
-			<dict>
-				<key>Frame</key>
-				<string>{{0, 20}, {1136, 649}}</string>
-				<key>PBXModuleWindowStatusBarHidden2</key>
-				<false/>
-				<key>RubberWindowFrame</key>
-				<string>38 162 1136 690 0 0 1440 878 </string>
-			</dict>
-		</dict>
-	</array>
+	<array/>
 	<key>PerspectiveWidths</key>
 	<array>
 		<integer>-1</integer>
@@ -315,8 +274,8 @@
 							<key>PBXSmartGroupTreeModuleOutlineStateSelectionKey</key>
 							<array>
 								<array>
-									<integer>8</integer>
-									<integer>2</integer>
+									<integer>4</integer>
+									<integer>1</integer>
 									<integer>0</integer>
 								</array>
 							</array>
@@ -370,17 +329,16 @@
 										<key>_historyCapacity</key>
 										<integer>0</integer>
 										<key>bookmark</key>
-										<string>69269F1D14620C8900F69705</string>
+										<string>69269FAE1462141200F69705</string>
 										<key>history</key>
 										<array>
 											<string>69868BD11460F42D004C5D9B</string>
 											<string>69868BD91460F468004C5D9B</string>
 											<string>69868C931460F7C3004C5D9B</string>
-											<string>69868D8314618DD7004C5D9B</string>
-											<string>69269DC61461ED9F00F69705</string>
-											<string>69269ED114620AFE00F69705</string>
-											<string>69269EDB14620B2900F69705</string>
-											<string>69269EDC14620B2900F69705</string>
+											<string>69269F5214620FC500F69705</string>
+											<string>69269F5314620FC500F69705</string>
+											<string>69269F8A1462131300F69705</string>
+											<string>69269F8B1462131300F69705</string>
 										</array>
 									</dict>
 									<key>SplitCount</key>
@@ -578,8 +536,9 @@
 	<integer>5</integer>
 	<key>WindowOrderList</key>
 	<array>
+		<string>1CD10A99069EF8BA00B06720</string>
+		<string>69269F5714620FC500F69705</string>
 		<string>69868B2D1460E71D004C5D9B</string>
-		<string>69868EAE1461A414004C5D9B</string>
 		<string>/Users/trevor/Documents/Cocoa Projects/libsimplepgp/simplepgp.xcodeproj</string>
 	</array>
 	<key>WindowString</key>
@@ -599,28 +558,26 @@
 					<key>Dock</key>
 					<array>
 						<dict>
-							<key>BecomeActive</key>
-							<true/>
 							<key>ContentConfiguration</key>
 							<dict>
 								<key>PBXProjectModuleGUID</key>
 								<string>1CD0528F0623707200166675</string>
 								<key>PBXProjectModuleLabel</key>
-								<string>packet_test.c</string>
+								<string>&lt;No Editor&gt;</string>
 								<key>StatusBarVisibility</key>
 								<true/>
 							</dict>
 							<key>GeometryConfiguration</key>
 							<dict>
 								<key>Frame</key>
-								<string>{{0, 0}, {500, 136}}</string>
+								<string>{{0, 0}, {500, 0}}</string>
 								<key>RubberWindowFrame</key>
 								<string>57 313 500 500 0 0 1440 878 </string>
 							</dict>
 							<key>Module</key>
 							<string>PBXNavigatorGroup</string>
 							<key>Proportion</key>
-							<string>136pt</string>
+							<string>0pt</string>
 						</dict>
 						<dict>
 							<key>ContentConfiguration</key>
@@ -637,14 +594,14 @@
 							<key>GeometryConfiguration</key>
 							<dict>
 								<key>Frame</key>
-								<string>{{0, 141}, {500, 318}}</string>
+								<string>{{0, 5}, {500, 454}}</string>
 								<key>RubberWindowFrame</key>
 								<string>57 313 500 500 0 0 1440 878 </string>
 							</dict>
 							<key>Module</key>
 							<string>PBXBuildResultsModule</string>
 							<key>Proportion</key>
-							<string>318pt</string>
+							<string>454pt</string>
 						</dict>
 					</array>
 					<key>Proportion</key>
@@ -706,8 +663,8 @@
 										<string>yes</string>
 										<key>sizes</key>
 										<array>
-											<string>{{0, 0}, {316, 185}}</string>
-											<string>{{316, 0}, {378, 185}}</string>
+											<string>{{0, 0}, {316, 194}}</string>
+											<string>{{316, 0}, {378, 194}}</string>
 										</array>
 									</dict>
 									<key>VerticalSplitView</key>
@@ -722,8 +679,8 @@
 										<string>yes</string>
 										<key>sizes</key>
 										<array>
-											<string>{{0, 0}, {694, 185}}</string>
-											<string>{{0, 185}, {694, 196}}</string>
+											<string>{{0, 0}, {694, 194}}</string>
+											<string>{{0, 194}, {694, 187}}</string>
 										</array>
 									</dict>
 								</dict>
@@ -756,7 +713,7 @@
 										<real>148</real>
 									</array>
 									<key>Frame</key>
-									<string>{{316, 0}, {378, 185}}</string>
+									<string>{{316, 0}, {378, 194}}</string>
 									<key>RubberWindowFrame</key>
 									<string>-1633 963 694 422 -1680 385 1680 1050 </string>
 								</dict>
@@ -784,13 +741,13 @@
 			<key>TableOfContents</key>
 			<array>
 				<string>1CD10A99069EF8BA00B06720</string>
-				<string>69868F581461BBC2004C5D9B</string>
+				<string>69269F8D1462131300F69705</string>
 				<string>1C162984064C10D400B95A72</string>
-				<string>69868F591461BBC2004C5D9B</string>
-				<string>69868F5A1461BBC2004C5D9B</string>
-				<string>69868F5B1461BBC2004C5D9B</string>
-				<string>69868F5C1461BBC2004C5D9B</string>
-				<string>69868F5D1461BBC2004C5D9B</string>
+				<string>69269F8E1462131300F69705</string>
+				<string>69269F8F1462131300F69705</string>
+				<string>69269F901462131300F69705</string>
+				<string>69269F911462131300F69705</string>
+				<string>69269F921462131300F69705</string>
 			</array>
 			<key>ToolbarConfiguration</key>
 			<string>xcode.toolbar.config.debugV3</string>
@@ -1402,33 +1359,40 @@
 			<integer>0</integer>
 		</dict>
 		<dict>
+			<key>FirstTimeWindowDisplayed</key>
+			<false/>
 			<key>Identifier</key>
 			<string>windowTool.refactoring</string>
 			<key>IncludeInToolsMenu</key>
 			<integer>0</integer>
+			<key>IsVertical</key>
+			<true/>
 			<key>Layout</key>
 			<array>
 				<dict>
 					<key>Dock</key>
 					<array>
 						<dict>
-							<key>BecomeActive</key>
-							<integer>1</integer>
+							<key>ContentConfiguration</key>
+							<dict>
+								<key>PBXProjectModuleGUID</key>
+								<string>69269F5614620FC500F69705</string>
+							</dict>
 							<key>GeometryConfiguration</key>
 							<dict>
 								<key>Frame</key>
-								<string>{0, 0}, {500, 335}</string>
+								<string>{{0, 0}, {500, 315}}</string>
 								<key>RubberWindowFrame</key>
-								<string>{0, 0}, {500, 335}</string>
+								<string>-1633 1029 500 356 -1680 385 1680 1050 </string>
 							</dict>
 							<key>Module</key>
 							<string>XCRefactoringModule</string>
 							<key>Proportion</key>
-							<string>100%</string>
+							<string>315pt</string>
 						</dict>
 					</array>
 					<key>Proportion</key>
-					<string>100%</string>
+					<string>315pt</string>
 				</dict>
 			</array>
 			<key>Name</key>
@@ -1437,8 +1401,20 @@
 			<array>
 				<string>XCRefactoringModule</string>
 			</array>
+			<key>StatusbarIsVisible</key>
+			<true/>
+			<key>TableOfContents</key>
+			<array>
+				<string>69269F5714620FC500F69705</string>
+				<string>69269F5814620FC500F69705</string>
+				<string>69269F5614620FC500F69705</string>
+			</array>
 			<key>WindowString</key>
-			<string>200 200 500 356 0 0 1920 1200 </string>
+			<string>-1633 1029 500 356 -1680 385 1680 1050 </string>
+			<key>WindowToolGUID</key>
+			<string>69269F5714620FC500F69705</string>
+			<key>WindowToolIsVisible</key>
+			<false/>
 		</dict>
 	</array>
 </dict>

diff --git a/simplepgp.xcodeproj/trevor.pbxuser b/simplepgp.xcodeproj/trevor.pbxuser
line changes: +208/-3
index 0def0c7..67f66be
--- a/simplepgp.xcodeproj/trevor.pbxuser
+++ b/simplepgp.xcodeproj/trevor.pbxuser
@@ -9,6 +9,7 @@
 		breakpoints = (
 			69868C9D14618450004C5D9B /* packet.c:9 */,
 			69868EE41461AA6F004C5D9B /* packet.c:207 */,
+			69269F4E14620FC200F69705 /* packet.c:167 */,
 		);
 		codeSenseManager = 69868AD51460E35F004C5D9B /* Code sense */;
 		perUserDictionary = {
@@ -231,6 +232,21 @@
 			69269F1814620C6800F69705 /* PBXTextBookmark */ = 69269F1814620C6800F69705 /* PBXTextBookmark */;
 			69269F1D14620C8900F69705 /* PBXTextBookmark */ = 69269F1D14620C8900F69705 /* PBXTextBookmark */;
 			69269F1E14620C8900F69705 /* PBXTextBookmark */ = 69269F1E14620C8900F69705 /* PBXTextBookmark */;
+			69269F5214620FC500F69705 /* PBXTextBookmark */ = 69269F5214620FC500F69705 /* PBXTextBookmark */;
+			69269F5314620FC500F69705 /* PBXTextBookmark */ = 69269F5314620FC500F69705 /* PBXTextBookmark */;
+			69269F5414620FC500F69705 /* PBXTextBookmark */ = 69269F5414620FC500F69705 /* PBXTextBookmark */;
+			69269F5514620FC500F69705 /* PBXTextBookmark */ = 69269F5514620FC500F69705 /* PBXTextBookmark */;
+			69269F5F14620FFB00F69705 /* PBXTextBookmark */ = 69269F5F14620FFB00F69705 /* PBXTextBookmark */;
+			69269F6A146210A800F69705 /* PBXTextBookmark */ = 69269F6A146210A800F69705 /* PBXTextBookmark */;
+			69269F6B146210A800F69705 /* PBXTextBookmark */ = 69269F6B146210A800F69705 /* PBXTextBookmark */;
+			69269F7B1462110A00F69705 /* PBXTextBookmark */ = 69269F7B1462110A00F69705 /* PBXTextBookmark */;
+			69269F8A1462131300F69705 /* PBXTextBookmark */ = 69269F8A1462131300F69705 /* PBXTextBookmark */;
+			69269F8B1462131300F69705 /* PBXTextBookmark */ = 69269F8B1462131300F69705 /* PBXTextBookmark */;
+			69269F8C1462131300F69705 /* PBXTextBookmark */ = 69269F8C1462131300F69705 /* PBXTextBookmark */;
+			69269F991462133E00F69705 /* PBXTextBookmark */ = 69269F991462133E00F69705 /* PBXTextBookmark */;
+			69269FA01462137A00F69705 /* PBXTextBookmark */ = 69269FA01462137A00F69705 /* PBXTextBookmark */;
+			69269FA71462140200F69705 /* PBXTextBookmark */ = 69269FA71462140200F69705 /* PBXTextBookmark */;
+			69269FAE1462141200F69705 /* PBXTextBookmark */ = 69269FAE1462141200F69705 /* PBXTextBookmark */;
 			69868BD11460F42D004C5D9B = 69868BD11460F42D004C5D9B /* PBXTextBookmark */;
 			69868BD91460F468004C5D9B = 69868BD91460F468004C5D9B /* PBXTextBookmark */;
 			69868C931460F7C3004C5D9B = 69868C931460F7C3004C5D9B /* PBXTextBookmark */;
@@ -1955,6 +1971,195 @@
 		vrLen = 991;
 		vrLoc = 720;
 	};
+	69269F2114620D0F00F69705 /* packet_test.c */ = {
+		uiCtxt = {
+			sepNavIntBoundsRect = "{{0, 0}, {1030, 780}}";
+			sepNavSelRange = "{0, 0}";
+			sepNavVisRange = "{0, 1356}";
+		};
+	};
+	69269F2314620D0F00F69705 /* packet.c */ = {
+		uiCtxt = {
+			sepNavIntBoundsRect = "{{0, 0}, {1030, 8294}}";
+			sepNavSelRange = "{4835, 0}";
+			sepNavVisRange = "{4194, 1805}";
+		};
+	};
+	69269F2414620D0F00F69705 /* packet.h */ = {
+		uiCtxt = {
+			sepNavIntBoundsRect = "{{0, 0}, {1030, 1729}}";
+			sepNavSelRange = "{1161, 7}";
+			sepNavVisRange = "{313, 1144}";
+		};
+	};
+	69269F4E14620FC200F69705 /* packet.c:167 */ = {
+		isa = PBXFileBreakpoint;
+		actions = (
+		);
+		breakpointStyle = 0;
+		continueAfterActions = 0;
+		countType = 0;
+		delayBeforeContinue = 0;
+		fileReference = 69269F2314620D0F00F69705 /* packet.c */;
+		functionName = "spgp_decode_message()";
+		hitCount = 0;
+		ignoreCount = 0;
+		lineNumber = 167;
+		location = duckdog;
+		modificationTime = 341971971.281257;
+		originalNumberOfMultipleMatches = 1;
+		state = 2;
+	};
+	69269F5214620FC500F69705 /* PBXTextBookmark */ = {
+		isa = PBXTextBookmark;
+		fRef = AA747D9E0F9514B9006C5449 /* simplepgp_Prefix.pch */;
+		name = "simplepgp_Prefix.pch: 1";
+		rLen = 0;
+		rLoc = 0;
+		rType = 0;
+		vrLen = 188;
+		vrLoc = 0;
+	};
+	69269F5314620FC500F69705 /* PBXTextBookmark */ = {
+		isa = PBXTextBookmark;
+		fRef = 69269F2114620D0F00F69705 /* packet_test.c */;
+		name = "packet_test.c: 1";
+		rLen = 0;
+		rLoc = 0;
+		rType = 0;
+		vrLen = 1356;
+		vrLoc = 0;
+	};
+	69269F5414620FC500F69705 /* PBXTextBookmark */ = {
+		isa = PBXTextBookmark;
+		fRef = 69269F2314620D0F00F69705 /* packet.c */;
+		name = "packet.c: 168";
+		rLen = 0;
+		rLoc = 4835;
+		rType = 0;
+		vrLen = 1715;
+		vrLoc = 3704;
+	};
+	69269F5514620FC500F69705 /* PBXTextBookmark */ = {
+		isa = PBXTextBookmark;
+		fRef = 69269F2314620D0F00F69705 /* packet.c */;
+		name = "packet.c: 168";
+		rLen = 0;
+		rLoc = 4835;
+		rType = 0;
+		vrLen = 1715;
+		vrLoc = 3704;
+	};
+	69269F5F14620FFB00F69705 /* PBXTextBookmark */ = {
+		isa = PBXTextBookmark;
+		fRef = 69269F2314620D0F00F69705 /* packet.c */;
+		name = "packet.c: 168";
+		rLen = 0;
+		rLoc = 4835;
+		rType = 0;
+		vrLen = 1715;
+		vrLoc = 3704;
+	};
+	69269F6A146210A800F69705 /* PBXTextBookmark */ = {
+		isa = PBXTextBookmark;
+		fRef = 69269F2314620D0F00F69705 /* packet.c */;
+		name = "packet.c: 171";
+		rLen = 0;
+		rLoc = 4921;
+		rType = 0;
+		vrLen = 1762;
+		vrLoc = 3553;
+	};
+	69269F6B146210A800F69705 /* PBXTextBookmark */ = {
+		isa = PBXTextBookmark;
+		fRef = 69269F2314620D0F00F69705 /* packet.c */;
+		name = "packet.c: 171";
+		rLen = 0;
+		rLoc = 4921;
+		rType = 0;
+		vrLen = 1762;
+		vrLoc = 3553;
+	};
+	69269F7B1462110A00F69705 /* PBXTextBookmark */ = {
+		isa = PBXTextBookmark;
+		fRef = 69269F2314620D0F00F69705 /* packet.c */;
+		name = "packet.c: 168";
+		rLen = 0;
+		rLoc = 4835;
+		rType = 0;
+		vrLen = 1766;
+		vrLoc = 3553;
+	};
+	69269F8A1462131300F69705 /* PBXTextBookmark */ = {
+		isa = PBXTextBookmark;
+		fRef = 69269F2414620D0F00F69705 /* packet.h */;
+		name = "packet.h: 59";
+		rLen = 7;
+		rLoc = 1161;
+		rType = 0;
+		vrLen = 1144;
+		vrLoc = 313;
+	};
+	69269F8B1462131300F69705 /* PBXTextBookmark */ = {
+		isa = PBXTextBookmark;
+		fRef = 69269F2314620D0F00F69705 /* packet.c */;
+		name = "packet.c: 209";
+		rLen = 0;
+		rLoc = 6071;
+		rType = 0;
+		vrLen = 1536;
+		vrLoc = 4539;
+	};
+	69269F8C1462131300F69705 /* PBXTextBookmark */ = {
+		isa = PBXTextBookmark;
+		fRef = 69269F2314620D0F00F69705 /* packet.c */;
+		name = "packet.c: 197";
+		rLen = 0;
+		rLoc = 5617;
+		rType = 0;
+		vrLen = 1317;
+		vrLoc = 5272;
+	};
+	69269F991462133E00F69705 /* PBXTextBookmark */ = {
+		isa = PBXTextBookmark;
+		fRef = 69269F2314620D0F00F69705 /* packet.c */;
+		name = "packet.c: 200";
+		rLen = 0;
+		rLoc = 5757;
+		rType = 0;
+		vrLen = 1337;
+		vrLoc = 5272;
+	};
+	69269FA01462137A00F69705 /* PBXTextBookmark */ = {
+		isa = PBXTextBookmark;
+		fRef = 69269F2314620D0F00F69705 /* packet.c */;
+		name = "packet.c: 235";
+		rLen = 0;
+		rLoc = 6521;
+		rType = 0;
+		vrLen = 1333;
+		vrLoc = 5302;
+	};
+	69269FA71462140200F69705 /* PBXTextBookmark */ = {
+		isa = PBXTextBookmark;
+		fRef = 69269F2314620D0F00F69705 /* packet.c */;
+		name = "packet.c: 211";
+		rLen = 0;
+		rLoc = 6031;
+		rType = 0;
+		vrLen = 1370;
+		vrLoc = 5279;
+	};
+	69269FAE1462141200F69705 /* PBXTextBookmark */ = {
+		isa = PBXTextBookmark;
+		fRef = 69269F2314620D0F00F69705 /* packet.c */;
+		name = "packet.c: 167";
+		rLen = 0;
+		rLoc = 4835;
+		rType = 0;
+		vrLen = 1805;
+		vrLoc = 4194;
+	};
 	69868AD41460E35F004C5D9B /* Source Control */ = {
 		isa = PBXSourceControlManager;
 		fallbackIsa = XCSourceControlManager;
@@ -2060,7 +2265,7 @@
 		ignoreCount = 0;
 		lineNumber = 9;
 		location = duckdog;
-		modificationTime = 341970025.778636;
+		modificationTime = 341971971.279537;
 		originalNumberOfMultipleMatches = 1;
 		state = 2;
 	};
@@ -2138,7 +2343,7 @@
 		hitCount = 0;
 		ignoreCount = 0;
 		lineNumber = 207;
-		modificationTime = 341970025.779882;
+		modificationTime = 341971971.280463;
 		originalNumberOfMultipleMatches = 0;
 		state = 2;
 	};
@@ -2184,7 +2389,7 @@
 	};
 	AA747D9E0F9514B9006C5449 /* simplepgp_Prefix.pch */ = {
 		uiCtxt = {
-			sepNavIntBoundsRect = "{{0, 0}, {845, 317}}";
+			sepNavIntBoundsRect = "{{0, 0}, {1030, 722}}";
 			sepNavSelRange = "{0, 0}";
 			sepNavVisRange = "{0, 188}";
 		};

diff --git a/src/packet.c b/src/packet.c
line changes: +97/-13
index 29d2316..0c1d5dd
--- a/src/packet.c
+++ b/src/packet.c
@@ -12,7 +12,13 @@
 #include <setjmp.h>
 #include "gcrypt.h"
 
-#define DEBUG_LOG
+
+/**********************************************************************
+**
+** MACROS
+**
+***********************************************************************/
+#pragma mark Macros
 
 #define RAISE(err) do { \
 		_spgp_err = (err); \
@@ -34,6 +40,13 @@
   } } while(0)
 
 
+/**********************************************************************
+**
+** Static variables
+**
+***********************************************************************/
+
+
 static uint32_t _spgp_err;
 static jmp_buf exception;
 
@@ -44,32 +57,55 @@ static uint8_t debug_log_enabled = 0;
 #endif
 
 
-static uint8_t parse_header(uint8_t *msg, uint32_t *idx, 
+/**********************************************************************
+**
+** Static function prototypes
+**
+***********************************************************************/
+
+static uint8_t spgp_parse_header(uint8_t *msg, uint32_t *idx, 
 														uint32_t length, spgp_packet_t *pkt);
+                            
 static uint8_t spgp_parse_secret_key(uint8_t *msg, uint32_t *idx, 
           													 uint32_t length, spgp_packet_t *pkt);
+                                     
 static spgp_mpi_t *spgp_read_mpi(uint8_t *msg, uint32_t *idx,
 														 uint32_t length);
+                             
 static uint8_t spgp_read_all_public_mpis(uint8_t *msg, 
                                          uint32_t *idx,
 														 						 uint32_t length, 
                                          spgp_secret_pkt_t *secret);
+                                         
 static uint8_t spgp_read_all_secret_mpis(uint8_t *msg, 
                                          uint32_t *idx,
 														 						 uint32_t length, 
                                          spgp_secret_pkt_t *secret);
+                                         
 static uint8_t spgp_read_salt(uint8_t *msg, 
                               uint32_t *idx,
                               uint32_t length, 
                               spgp_secret_pkt_t *secret);
+                              
 static uint8_t spgp_read_iv(uint8_t *msg, 
                             uint32_t *idx,
                             uint32_t length, 
                             spgp_secret_pkt_t *secret);
+                            
 static uint8_t spgp_iv_length_for_symmetric_algo(uint8_t algo);
+
 static uint8_t spgp_salt_length_for_hash_algo(uint8_t algo);
 
 
+
+
+/**********************************************************************
+**
+** External function definitions
+**
+***********************************************************************/
+#pragma mark External Function Definitions
+
 uint8_t spgp_debug_log_enabled(void) {
 	return debug_log_enabled;
 }
@@ -80,7 +116,6 @@ void spgp_debug_log_set(uint8_t enable) {
 spgp_packet_t *spgp_decode_message(uint8_t *message, uint32_t length) {
 	spgp_packet_t *head = NULL;
   spgp_packet_t *pkt = NULL;
-  uint32_t startIdx = 0;
   uint32_t idx = 0;
   
 	LOG_PRINT("begin\n");
@@ -99,16 +134,20 @@ spgp_packet_t *spgp_decode_message(uint8_t *message, uint32_t length) {
   	RAISE(INVALID_ARGS);
   }
   
+  // There must be at least one packet, yeah?
   head = malloc(sizeof(*head));
   if (NULL == head) RAISE(OUT_OF_MEMORY);
   memset(head, 0, sizeof(*head));
   pkt = head;
   
+  
+  // Loop to decode every packet in message
   while (idx < length-1) {
-  	startIdx = idx;
-    parse_header(message, &idx, length, pkt);
+  	// Every packet starts with a header
+    spgp_parse_header(message, &idx, length, pkt);
     if (!pkt->header) RAISE(FORMAT_UNSUPPORTED);
     
+    // Decode packet contents based on the type marked in its header
     switch (pkt->header->type) {
       case PKT_TYPE_SECRET_KEY:
       case PKT_TYPE_SECRET_SUBKEY:
@@ -123,15 +162,17 @@ spgp_packet_t *spgp_decode_message(uint8_t *message, uint32_t length) {
         break;
     }
     
-    if (idx >= length-1) break; // we've done the last packet
-    
-    pkt->next = malloc(sizeof(*head));
+    // If we're at the end of the buffer, we're done
+    if (idx >= length-1) break;
+        
+    // Allocate space for another packet
+    pkt->next = malloc(sizeof(*pkt->next));
     if (NULL == pkt->next) RAISE(OUT_OF_MEMORY);
     memset(pkt->next, 0, sizeof(*pkt->next));
     pkt = pkt->next;
     
     // Packet parser increments to it's own last byte.  Need one more to get
-    // to the next packet's beginning 
+    // to the next packet's first byte. 
     SAFE_IDX_INCREMENT(idx, length);
 	}
     
@@ -141,12 +182,45 @@ spgp_packet_t *spgp_decode_message(uint8_t *message, uint32_t length) {
 }
 
 void spgp_free_packet(spgp_packet_t **pkt) {
+	spgp_mpi_t *curMpi, *nextMpi;
+  
 	if (pkt == NULL)
   	return;
   if (*pkt == NULL)
   	return;
   
-  // TODO: make this recursive, free it's next pointer first
+  LOG_PRINT("Freeing packet: %p\n", pkt);
+  
+  // Recursively call on the next packet before freeing parent.
+  if ((*pkt)->next) spgp_free_packet(&((*pkt)->next));
+  
+  // Release memory allocated for secret key fields
+  if (((*pkt)->header->type == PKT_TYPE_SECRET_KEY ||
+  		(*pkt)->header->type == PKT_TYPE_SECRET_SUBKEY) &&
+      (*pkt)->c.secret != NULL) {
+  	if ((*pkt)->c.secret->mpiCount > 0) {
+    	curMpi = (*pkt)->c.secret->mpiHead;
+      while (curMpi->next) {
+      	nextMpi = curMpi->next;
+        free(curMpi);
+        curMpi = nextMpi;
+      }
+      (*pkt)->c.secret->mpiHead = NULL;
+      (*pkt)->c.secret->mpiCount = 0;
+    }
+    if ((*pkt)->c.secret->s2kSalt) {
+    	free((*pkt)->c.secret->s2kSalt);
+      (*pkt)->c.secret->s2kSalt = NULL;
+    }
+    if ((*pkt)->c.secret->key) {
+    	free((*pkt)->c.secret->key);
+      (*pkt)->c.secret->key = NULL;
+    }
+    if ((*pkt)->c.secret->iv) {
+    	free((*pkt)->c.secret->iv);
+      (*pkt)->c.secret->iv = NULL;
+    }
+  }
   
   // release header
   if ((*pkt)->header) {
@@ -154,10 +228,9 @@ void spgp_free_packet(spgp_packet_t **pkt) {
 	  (*pkt)->header = NULL;
   }
   
-  // TODO: should release MPIs here
-  
   // release packet
   free(*pkt);
+
   *pkt = NULL;
 }
 
@@ -196,7 +269,18 @@ void tsb_test(void) {
         
 }
 
-static uint8_t parse_header(uint8_t *msg, uint32_t *idx, 
+
+
+
+/**********************************************************************
+**
+** Static function definitions
+**
+***********************************************************************/
+#pragma mark Static Function Definitions
+
+
+static uint8_t spgp_parse_header(uint8_t *msg, uint32_t *idx, 
 														uint32_t length, spgp_packet_t *pkt) {
 	uint8_t i;