POLARSSL_PEM_C split into POLARSSL_PEM_PARSE_C and POLARSSL_PEM_WRITE_C
diff --git a/library/dhm.c b/library/dhm.c
index 98c8e9f..625837e 100644
--- a/library/dhm.c
+++ b/library/dhm.c
@@ -34,7 +34,7 @@
 
 #include "polarssl/dhm.h"
 
-#if defined(POLARSSL_PEM_C)
+#if defined(POLARSSL_PEM_PARSE_C)
 #include "polarssl/pem.h"
 #endif
 
@@ -397,7 +397,7 @@
     int ret;
     size_t len;
     unsigned char *p, *end;
-#if defined(POLARSSL_PEM_C)
+#if defined(POLARSSL_PEM_PARSE_C)
     pem_context pem;
 
     pem_init( &pem );
@@ -456,7 +456,7 @@
     ret = 0;
 
 exit:
-#if defined(POLARSSL_PEM_C)
+#if defined(POLARSSL_PEM_PARSE_C)
     pem_free( &pem );
 #endif
     if( ret != 0 )
diff --git a/library/error.c b/library/error.c
index 78c5faa..7aeb84f 100644
--- a/library/error.c
+++ b/library/error.c
@@ -109,7 +109,7 @@
 #include "polarssl/pbkdf2.h"
 #endif
 
-#if defined(POLARSSL_PEM_C)
+#if defined(POLARSSL_PEM_PARSE_C) || defined(POLARSSL_PEM_WRITE_C)
 #include "polarssl/pem.h"
 #endif
 
@@ -238,7 +238,7 @@
             snprintf( buf, buflen, "MD - Opening or reading of file failed" );
 #endif /* POLARSSL_MD_C */
 
-#if defined(POLARSSL_PEM_C)
+#if defined(POLARSSL_PEM_PARSE_C) || defined(POLARSSL_PEM_WRITE_C)
         if( use_ret == -(POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT) )
             snprintf( buf, buflen, "PEM - No PEM header or footer found" );
         if( use_ret == -(POLARSSL_ERR_PEM_INVALID_DATA) )
@@ -257,7 +257,7 @@
             snprintf( buf, buflen, "PEM - Unavailable feature, e.g. hashing/encryption combination" );
         if( use_ret == -(POLARSSL_ERR_PEM_BAD_INPUT_DATA) )
             snprintf( buf, buflen, "PEM - Bad input parameters to function" );
-#endif /* POLARSSL_PEM_C */
+#endif /* POLARSSL_PEM_PARSE_C || POLARSSL_PEM_WRITE_C */
 
 #if defined(POLARSSL_PK_C)
         if( use_ret == -(POLARSSL_ERR_PK_MALLOC_FAILED) )
diff --git a/library/pem.c b/library/pem.c
index ff7f32c..d602d8a 100644
--- a/library/pem.c
+++ b/library/pem.c
@@ -25,8 +25,7 @@
 
 #include "polarssl/config.h"
 
-#if defined(POLARSSL_PEM_C)
-
+#if defined(POLARSSL_PEM_PARSE_C) || defined(POLARSSL_PEM_WRITE_C)
 #include "polarssl/pem.h"
 #include "polarssl/base64.h"
 #include "polarssl/des.h"
@@ -43,6 +42,7 @@
 
 #include <stdlib.h>
 
+#if defined(POLARSSL_PEM_PARSE_C)
 void pem_init( pem_context *ctx )
 {
     memset( ctx, 0, sizeof( pem_context ) );
@@ -285,7 +285,7 @@
             s1 += 32;
         }
 #endif /* POLARSSL_AES_C */
-        
+
         if( enc_alg == POLARSSL_CIPHER_NONE )
             return( POLARSSL_ERR_PEM_UNKNOWN_ENC_ALG );
 
@@ -312,7 +312,7 @@
         polarssl_free( buf );
         return( POLARSSL_ERR_PEM_INVALID_DATA + ret );
     }
-    
+
     if( enc != 0 )
     {
 #if defined(POLARSSL_MD5_C) && defined(POLARSSL_CIPHER_MODE_CBC) &&         \
@@ -363,6 +363,19 @@
     return( 0 );
 }
 
+void pem_free( pem_context *ctx )
+{
+    if( ctx->buf )
+        polarssl_free( ctx->buf );
+
+    if( ctx->info )
+        polarssl_free( ctx->info );
+
+    memset( ctx, 0, sizeof( pem_context ) );
+}
+#endif /* POLARSSL_PEM_PARSE_C */
+
+#if defined(POLARSSL_PEM_WRITE_C)
 int pem_write_buffer( const char *header, const char *footer,
                       const unsigned char *der_data, size_t der_len,
                       unsigned char *buf, size_t buf_len, size_t *olen )
@@ -412,16 +425,5 @@
     polarssl_free( encode_buf );
     return( 0 );
 }
-
-void pem_free( pem_context *ctx )
-{
-    if( ctx->buf )
-        polarssl_free( ctx->buf );
-
-    if( ctx->info )
-        polarssl_free( ctx->info );
-
-    memset( ctx, 0, sizeof( pem_context ) );
-}
-
-#endif
+#endif /* POLARSSL_PEM_WRITE_C */
+#endif /* POLARSSL_PEM_PARSE_C || POLARSSL_PEM_WRITE_C */
diff --git a/library/pkparse.c b/library/pkparse.c
index 2d244f1..5a58abe 100644
--- a/library/pkparse.c
+++ b/library/pkparse.c
@@ -40,7 +40,7 @@
 #if defined(POLARSSL_ECDSA_C)
 #include "polarssl/ecdsa.h"
 #endif
-#if defined(POLARSSL_PEM_C)
+#if defined(POLARSSL_PEM_PARSE_C)
 #include "polarssl/pem.h"
 #endif
 #if defined(POLARSSL_PKCS5_C)
@@ -754,7 +754,7 @@
     int ret;
     const pk_info_t *pk_info;
 
-#if defined(POLARSSL_PEM_C)
+#if defined(POLARSSL_PEM_PARSE_C)
     size_t len;
     pem_context pem;
 
@@ -855,7 +855,7 @@
 #else
     ((void) pwd);
     ((void) pwdlen);
-#endif /* POLARSSL_PEM_C */
+#endif /* POLARSSL_PEM_PARSE_C */
 
     /*
     * At this point we only know it's not a PEM formatted key. Could be any
@@ -919,7 +919,7 @@
 {
     int ret;
     unsigned char *p;
-#if defined(POLARSSL_PEM_C)
+#if defined(POLARSSL_PEM_PARSE_C)
     size_t len;
     pem_context pem;
 
@@ -947,7 +947,7 @@
 
     ret = pk_parse_get_pubkey( &p, p + keylen, ctx );
 
-#if defined(POLARSSL_PEM_C)
+#if defined(POLARSSL_PEM_PARSE_C)
     pem_free( &pem );
 #endif
 
diff --git a/library/pkwrite.c b/library/pkwrite.c
index 44823d7..a3e9c57 100644
--- a/library/pkwrite.c
+++ b/library/pkwrite.c
@@ -40,7 +40,7 @@
 #if defined(POLARSSL_ECDSA_C)
 #include "polarssl/ecdsa.h"
 #endif
-#if defined(POLARSSL_PEM_C)
+#if defined(POLARSSL_PEM_WRITE_C)
 #include "polarssl/pem.h"
 #endif
 
@@ -276,7 +276,7 @@
     return( len );
 }
 
-#if defined(POLARSSL_PEM_C)
+#if defined(POLARSSL_PEM_WRITE_C)
 
 #define PEM_BEGIN_PUBLIC_KEY    "-----BEGIN PUBLIC KEY-----\n"
 #define PEM_END_PUBLIC_KEY      "-----END PUBLIC KEY-----\n"
@@ -345,6 +345,6 @@
 
     return( 0 );
 }
-#endif /* POLARSSL_PEM_C */
+#endif /* POLARSSL_PEM_WRITE_C */
 
 #endif /* POLARSSL_PK_WRITE_C */
diff --git a/library/x509parse.c b/library/x509parse.c
index 5d32167..331c8a0 100644
--- a/library/x509parse.c
+++ b/library/x509parse.c
@@ -41,8 +41,9 @@
 #include "polarssl/x509.h"
 #include "polarssl/asn1.h"
 #include "polarssl/oid.h"
+#if defined(POLARSSL_PEM_PARSE_C)
 #include "polarssl/pem.h"
-#include "polarssl/dhm.h"
+#endif
 #if defined(POLARSSL_PKCS5_C)
 #include "polarssl/pkcs5.h"
 #endif
@@ -1344,7 +1345,7 @@
      * Determine buffer content. Buffer contains either one DER certificate or
      * one or more PEM certificates.
      */
-#if defined(POLARSSL_PEM_C)
+#if defined(POLARSSL_PEM_PARSE_C)
     if( strstr( (const char *) buf, "-----BEGIN CERTIFICATE-----" ) != NULL )
         buf_format = X509_FORMAT_PEM;
 #endif
@@ -1352,7 +1353,7 @@
     if( buf_format == X509_FORMAT_DER )
         return x509parse_crt_der( chain, buf, buflen );
 
-#if defined(POLARSSL_PEM_C)
+#if defined(POLARSSL_PEM_PARSE_C)
     if( buf_format == X509_FORMAT_PEM )
     {
         int ret;
@@ -1438,7 +1439,7 @@
     int ret;
     size_t len;
     unsigned char *p, *end;
-#if defined(POLARSSL_PEM_C)
+#if defined(POLARSSL_PEM_PARSE_C)
     size_t use_len;
     pem_context pem;
 #endif
@@ -1451,7 +1452,7 @@
 
     memset( csr, 0, sizeof( x509_csr ) );
 
-#if defined(POLARSSL_PEM_C)
+#if defined(POLARSSL_PEM_PARSE_C)
     pem_init( &pem );
     ret = pem_read_buffer( &pem,
                            "-----BEGIN CERTIFICATE REQUEST-----",
@@ -1638,7 +1639,7 @@
     size_t len;
     unsigned char *p, *end;
     x509_crl *crl;
-#if defined(POLARSSL_PEM_C)
+#if defined(POLARSSL_PEM_PARSE_C)
     size_t use_len;
     pem_context pem;
 #endif
@@ -1671,7 +1672,7 @@
         memset( crl, 0, sizeof( x509_crl ) );
     }
 
-#if defined(POLARSSL_PEM_C)
+#if defined(POLARSSL_PEM_PARSE_C)
     pem_init( &pem );
     ret = pem_read_buffer( &pem,
                            "-----BEGIN X509 CRL-----",
diff --git a/library/x509write.c b/library/x509write.c
index 3a8c89d..f020b9e 100644
--- a/library/x509write.c
+++ b/library/x509write.c
@@ -42,7 +42,7 @@
 
 #include "polarssl/sha1.h"
 
-#if defined(POLARSSL_PEM_C)
+#if defined(POLARSSL_PEM_WRITE_C)
 #include "polarssl/pem.h"
 #endif
 
@@ -816,7 +816,7 @@
 #define PEM_BEGIN_CSR           "-----BEGIN CERTIFICATE REQUEST-----\n"
 #define PEM_END_CSR             "-----END CERTIFICATE REQUEST-----\n"
 
-#if defined(POLARSSL_PEM_C)
+#if defined(POLARSSL_PEM_WRITE_C)
 int x509write_crt_pem( x509write_cert *crt, unsigned char *buf, size_t size,
                        int (*f_rng)(void *, unsigned char *, size_t),
                        void *p_rng )