From 3773e265c76002de5a3231a3a8dbfb448fd9dcad Mon Sep 17 00:00:00 2001
From: Martin Nilsson <nilsson@opera.com>
Date: Wed, 19 Mar 2014 13:39:41 +0100
Subject: [PATCH] Less magic numbers. Made PAD_* into proper constants.

---
 lib/modules/Crypto.pmod/module.pmod | 10 ++++----
 src/post_modules/Nettle/cipher.cmod | 37 +++++++++++++++++++++--------
 2 files changed, 32 insertions(+), 15 deletions(-)

diff --git a/lib/modules/Crypto.pmod/module.pmod b/lib/modules/Crypto.pmod/module.pmod
index 2102c49719..0245739262 100644
--- a/lib/modules/Crypto.pmod/module.pmod
+++ b/lib/modules/Crypto.pmod/module.pmod
@@ -121,8 +121,8 @@ class Buffer {
 function(string(8bit):string(8bit)) rot13 =
   Crypto.Substitution()->set_rot_key()->crypt;
 
-constant PAD_SSL = 0;
-constant PAD_ISO_10126 = 1;
-constant PAD_ANSI_X923 = 2;
-constant PAD_PKCS7 = 3;
-constant PAD_ZERO = 4;
+constant PAD_SSL       = Nettle.PAD_SSL;
+constant PAD_ISO_10126 = Nettle.PAD_ISO_10126;
+constant PAD_ANSI_X923 = Nettle.PAD_ANSI_X923;
+constant PAD_PKCS7     = Nettle.PAD_PKCS7;
+constant PAD_ZERO      = Nettle.PAD_ZERO;
diff --git a/src/post_modules/Nettle/cipher.cmod b/src/post_modules/Nettle/cipher.cmod
index 0cd71d036e..eee5288765 100644
--- a/src/post_modules/Nettle/cipher.cmod
+++ b/src/post_modules/Nettle/cipher.cmod
@@ -2473,6 +2473,13 @@ PIKECLASS GCM
 
 #endif /* HAVE_NETTLE_GCM_H */
 
+#define PAD_SSL        0
+#define PAD_ISO_10126  1
+#define PAD_ANSI_X923  2
+#define PAD_PKCS7      3
+#define PAD_ZERO       4
+
+
 /*! @class Proxy
  *! Acts as a buffer so that data can be fed to a cipher in blocks
  *! that don't correspond to cipher block sizes.
@@ -2743,10 +2750,10 @@ PIKECLASS Proxy {
 
     switch(m)
     {
-    case 0:
+    case PAD_SSL:
       size--;
       break;
-    case 4:
+    case PAD_ZERO:
       if( THIS->backlog_len>0 &&
           THIS->backlog[THIS->backlog_len-1] == 0 )
         Pike_error("Using zero padding on a zero terminated string.\n");
@@ -2759,20 +2766,20 @@ PIKECLASS Proxy {
       {
       default:
         Pike_error("Unknown method.\n");
-      case 0:
-      case 1:
+      case PAD_SSL:
+      case PAD_ISO_10126:
         /* ISO 10126 */
         THIS->backlog[i] = DO_NOT_WARN((unsigned char)(my_rand() & 0xff));
         break;
-      case 2:
+      case PAD_ANSI_X923:
         /* ANSI X.923 */
         THIS->backlog[i] = 0;
         break;
-      case 3:
+      case PAD_PKCS7:
         /* PKCS7 / RFC 3852 */
         THIS->backlog[i] = DO_NOT_WARN((unsigned char)size);
         break;
-      case 4:
+      case PAD_ZERO:
         /* Null only */
         THIS->backlog[i] = 0;
         break;
@@ -2832,7 +2839,7 @@ PIKECLASS Proxy {
 		 DO_NOT_WARN((long)Pike_sp[-1].u.string->len));
     str = Pike_sp[-1].u.string;
 
-    if( m==0 )
+    if( m==PAD_SSL )
     {
       if (str->str[len - 1]+1 > THIS->block_size)
         Pike_error("Invalid padding (%d > %d)\n",
@@ -2847,10 +2854,10 @@ PIKECLASS Proxy {
     len -= str->str[len - 1];
     switch( m )
     {
-    case 0:
+    case PAD_SSL:
       len--;
       break;
-    case 4:
+    case PAD_ZERO:
       {
         int c=THIS->block_size;
         while( str->str[len-1]==0 && c>0 )
@@ -2889,10 +2896,20 @@ PIKECLASS Proxy {
 
 /*! @endmodule Nettle */
 
+#define PAD_ISO_10126  1
+#define PAD_ANSI_X923  2
+#define PAD_PKCS7      3
+#define PAD_ZERO       4
+
 void
 cipher_init(void)
 {
   INIT;
+  ADD_INT_CONSTANT("PAD_SSL",       PAD_SSL,       0);
+  ADD_INT_CONSTANT("PAD_ISO_10126", PAD_ISO_10126, 0);
+  ADD_INT_CONSTANT("PAD_ANSI_X923", PAD_ANSI_X923, 0);
+  ADD_INT_CONSTANT("PAD_PKCS7",     PAD_PKCS7,     0);
+  ADD_INT_CONSTANT("PAD_ZERO",      PAD_ZERO,      0);
 }
 
 void
-- 
GitLab