mark the DES magic number arrays const
authorDan Winship <danw@src.gnome.org>
Sat, 9 Feb 2008 05:08:29 +0000 (05:08 +0000)
committerDan Winship <danw@src.gnome.org>
Sat, 9 Feb 2008 05:08:29 +0000 (05:08 +0000)
* libsoup/soup-auth-manager-ntlm.c: mark the DES magic number
arrays const

* libsoup/soup-date.c (months, days): add an extra "const" to each
of these declarations, as one "const" is apparently not enough.
(soup_date_to_time_t): remove redundant copy of days_before array.

* libsoup/soup-dns.c (soup_dns_init): use g_once_init_enter/leave

* libsoup/soup-gnutls.c (soup_ssl_supported)
(soup_gnutls_channel_funcs): Mark these const
(soup_gnutls_init, init_dh_params): Use g_once_init_enter/leave

* libsoup/soup-status.c (reason_phrases): mark this const

* tests/ssl-test.c: Remove the workaround for soup_gnutls_init()
not being thread-safe, since it is now.

svn path=/trunk/; revision=1080

ChangeLog
libsoup/soup-auth-manager-ntlm.c
libsoup/soup-date.c
libsoup/soup-dns.c
libsoup/soup-gnutls.c
libsoup/soup-misc.h
libsoup/soup-nossl.c
libsoup/soup-status.c
tests/ssl-test.c

index 54610cc..b8a39ce 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,23 @@
+2008-02-09  Dan Winship  <danw@gnome.org>
+
+       * libsoup/soup-auth-manager-ntlm.c: mark the DES magic number
+       arrays const
+
+       * libsoup/soup-date.c (months, days): add an extra "const" to each
+       of these declarations, as one "const" is apparently not enough.
+       (soup_date_to_time_t): remove redundant copy of days_before array.
+
+       * libsoup/soup-dns.c (soup_dns_init): use g_once_init_enter/leave
+
+       * libsoup/soup-gnutls.c (soup_ssl_supported)
+       (soup_gnutls_channel_funcs): Mark these const
+       (soup_gnutls_init, init_dh_params): Use g_once_init_enter/leave
+
+       * libsoup/soup-status.c (reason_phrases): mark this const
+
+       * tests/ssl-test.c: Remove the workaround for soup_gnutls_init()
+       not being thread-safe, since it is now.
+
 2008-02-08  Dan Winship  <danw@gnome.org>
 
        * libsoup/soup-message-headers.c (SoupMessageHeadersIter)
index bb9c8b1..5f8f88b 100644 (file)
@@ -685,7 +685,7 @@ md4sum (const unsigned char *in, int nbytes, unsigned char digest[16])
 
 
 /* Public domain DES implementation from Phil Karn */
-static guint32 Spbox[8][64] = {
+static const guint32 Spbox[8][64] = {
        { 0x01010400,0x00000000,0x00010000,0x01010404,
          0x01010004,0x00010404,0x00000004,0x00010000,
          0x00000400,0x01010400,0x01010404,0x00000400,
@@ -924,7 +924,7 @@ des (guint32 ks[16][2], unsigned char block[8])
 /* Key schedule-related tables from FIPS-46 */
 
 /* permuted choice table (key) */
-static unsigned char pc1[] = {
+static const unsigned char pc1[] = {
        57, 49, 41, 33, 25, 17,  9,
         1, 58, 50, 42, 34, 26, 18,
        10,  2, 59, 51, 43, 35, 27,
@@ -937,12 +937,12 @@ static unsigned char pc1[] = {
 };
 
 /* number left rotations of pc1 */
-static unsigned char totrot[] = {
+static const unsigned char totrot[] = {
        1,2,4,6,8,10,12,14,15,17,19,21,23,25,27,28
 };
 
 /* permuted choice key (table) */
-static unsigned char pc2[] = {
+static const unsigned char pc2[] = {
        14, 17, 11, 24,  1,  5,
         3, 28, 15,  6, 21, 10,
        23, 19, 12,  4, 26,  8,
@@ -957,7 +957,7 @@ static unsigned char pc2[] = {
 
 
 /* bit 0 is left-most in byte */
-static int bytebit[] = {
+static const int bytebit[] = {
        0200,0100,040,020,010,04,02,01
 };
 
index beffc96..ba616d7 100644 (file)
  **/
 
 /* Do not internationalize */
-static const char *months[] = {
+static const char *const months[] = {
        "Jan", "Feb", "Mar", "Apr", "May", "Jun",
        "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
 };
 
 /* Do not internationalize */
-static const char *days[] = {
+static const char *const days[] = {
        "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
 };
 
@@ -513,9 +513,6 @@ time_t
 soup_date_to_time_t (SoupDate *date)
 {
        time_t tt;
-       static const int days_before[] = {
-               0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334
-       };
 
        /* FIXME: offset, etc */
 
@@ -533,7 +530,7 @@ soup_date_to_time_t (SoupDate *date)
 
        tt = (date->year - 1970) * 365;
        tt += (date->year - 1968) / 4;
-       tt += days_before[date->month - 1] + date->day - 1;
+       tt += days_before[date->month] + date->day - 1;
        if (date->year % 4 == 0 && date->month <= 2)
                tt--;
        tt = ((((tt * 24) + date->hour) * 60) + date->minute) * 60 + date->second;
index 709f43e..de0d469 100644 (file)
@@ -155,13 +155,17 @@ static GMutex *soup_gethost_lock;
 void
 soup_dns_init (void)
 {
-       if (soup_dns_cache == NULL) {
+       static volatile gsize inited_dns = 0;
+
+       if (g_once_init_enter (&inited_dns)) {
                soup_dns_cache = g_hash_table_new (soup_str_case_hash, soup_str_case_equal);
                soup_dns_lock = g_mutex_new ();
                soup_dns_cond = g_cond_new ();
 #if !defined (HAVE_GETADDRINFO) || !defined (HAVE_GETNAMEINFO)
                soup_gethost_lock = g_mutex_new ();
 #endif
+
+               g_once_init_leave (&inited_dns, TRUE);
        }
 }
 
index b402850..014cff6 100644 (file)
@@ -31,7 +31,7 @@
  *
  * Can be used to test if libsoup was compiled with ssl support.
  **/
-gboolean soup_ssl_supported = TRUE;
+const gboolean soup_ssl_supported = TRUE;
 
 #define DH_BITS 1024
 
@@ -335,7 +335,7 @@ soup_gnutls_get_flags (GIOChannel *channel)
        return chan->real_sock->funcs->io_get_flags (channel);
 }
 
-GIOFuncs soup_gnutls_channel_funcs = {
+const GIOFuncs soup_gnutls_channel_funcs = {
        soup_gnutls_read,
        soup_gnutls_write,
        soup_gnutls_seek,
@@ -351,21 +351,20 @@ static gnutls_dh_params dh_params = NULL;
 static gboolean
 init_dh_params (void)
 {
-       if (gnutls_dh_params_init (&dh_params) != 0)
-               goto THROW_CREATE_ERROR;
-
-       if (gnutls_dh_params_generate2 (dh_params, DH_BITS) != 0)
-               goto THROW_CREATE_ERROR;
-
-       return TRUE;
-
-THROW_CREATE_ERROR:
-       if (dh_params) {
-               gnutls_dh_params_deinit (dh_params);
-               dh_params = NULL;
+       static volatile gsize inited_dh_params = 0;
+
+       if (g_once_init_enter (&inited_dh_params)) {
+               if (gnutls_dh_params_init (&dh_params) != 0 ||
+                   gnutls_dh_params_generate2 (dh_params, DH_BITS) != 0) {
+                       if (dh_params) {
+                               gnutls_dh_params_deinit (dh_params);
+                               dh_params = NULL;
+                       }
+               }
+               g_once_init_leave (&inited_dh_params, TRUE);
        }
 
-       return FALSE;
+       return dh_params != NULL;
 }
 
 /**
@@ -427,7 +426,7 @@ soup_ssl_wrap_iochannel (GIOChannel *sock, SoupSSLType type,
        g_io_channel_ref (sock);
 
        gchan = (GIOChannel *) chan;
-       gchan->funcs = &soup_gnutls_channel_funcs;
+       gchan->funcs = (GIOFuncs *)&soup_gnutls_channel_funcs;
        g_io_channel_init (gchan);
        gchan->is_readable = gchan->is_writeable = TRUE;
        gchan->use_buffer = FALSE;
@@ -440,8 +439,6 @@ soup_ssl_wrap_iochannel (GIOChannel *sock, SoupSSLType type,
        return NULL;
 }
 
-static gboolean soup_gnutls_inited = FALSE;
-
 #ifdef GCRY_THREAD_OPTION_PTHREAD_IMPL
 GCRY_THREAD_OPTION_PTHREAD_IMPL;
 #endif
@@ -449,11 +446,15 @@ GCRY_THREAD_OPTION_PTHREAD_IMPL;
 static void
 soup_gnutls_init (void)
 {
+       static volatile gsize inited_gnutls = 0;
+
+       if (g_once_init_enter (&inited_gnutls)) {
 #ifdef GCRY_THREAD_OPTION_PTHREAD_IMPL
-       gcry_control (GCRYCTL_SET_THREAD_CBS, &gcry_threads_pthread);
+               gcry_control (GCRYCTL_SET_THREAD_CBS, &gcry_threads_pthread);
 #endif
-       gnutls_global_init ();
-       soup_gnutls_inited = TRUE;
+               gnutls_global_init ();
+               g_once_init_leave (&inited_gnutls, TRUE);
+       }
 }
 
 /**
@@ -477,8 +478,7 @@ soup_ssl_get_client_credentials (const char *ca_file)
        SoupSSLCredentials *creds;
        int status;
 
-       if (!soup_gnutls_inited)
-               soup_gnutls_init ();
+       soup_gnutls_init ();
 
        creds = g_slice_new0 (SoupSSLCredentials);
        gnutls_certificate_allocate_credentials (&creds->creds);
@@ -533,12 +533,9 @@ soup_ssl_get_server_credentials (const char *cert_file, const char *key_file)
 {
        SoupSSLCredentials *creds;
 
-       if (!soup_gnutls_inited)
-               soup_gnutls_init ();
-       if (!dh_params) {
-               if (!init_dh_params ())
-                       return NULL;
-       }
+       soup_gnutls_init ();
+       if (!init_dh_params ())
+               return NULL;
 
        creds = g_slice_new0 (SoupSSLCredentials);
        gnutls_certificate_allocate_credentials (&creds->creds);
index 3699606..20583fc 100644 (file)
@@ -35,7 +35,7 @@ guint              soup_str_case_hash        (gconstpointer key);
 gboolean           soup_str_case_equal       (gconstpointer v1,
                                              gconstpointer v2);
 
-extern gboolean soup_ssl_supported;
+extern const gboolean soup_ssl_supported;
 
 #define SOUP_SSL_ERROR soup_ssl_error_quark()
 
index b7ca76c..f8e3888 100644 (file)
@@ -14,7 +14,7 @@
 
 #ifndef HAVE_SSL
 
-gboolean soup_ssl_supported = FALSE;
+const gboolean soup_ssl_supported = FALSE;
 
 GIOChannel *
 soup_ssl_wrap_iochannel (GIOChannel *sock, SoupSSLType type,
index 923b72a..6f2b5e4 100644 (file)
  * network and internal errors.
  **/
 
-static struct {
+static const struct {
        guint code;
        const char *phrase;
 } reason_phrases [] = {
index 13eed66..16dc566 100644 (file)
@@ -277,7 +277,11 @@ main (int argc, char **argv)
        getsockname (listener, (struct sockaddr *)&sin, (void *)&sin_len);
        port = ntohs (sin.sin_port);
 
-       /* Create the client */
+       /* Now spawn server thread */
+       server = g_thread_create (server_thread, GINT_TO_POINTER (listener),
+                                 TRUE, NULL);
+
+       /* And create the client */
        addr = soup_address_new ("127.0.0.1", port);
        creds = soup_ssl_get_client_credentials (NULL);
        sock = soup_socket_new (SOUP_SOCKET_REMOTE_ADDRESS, addr,
@@ -293,10 +297,6 @@ main (int argc, char **argv)
 
        soup_socket_start_ssl (sock, NULL);
 
-       /* Now spawn server thread */
-       server = g_thread_create (server_thread, GINT_TO_POINTER (listener),
-                                 TRUE, NULL);
-
        /* Synchronous client test */
        for (i = 0; i < BUFSIZE; i++)
                writebuf[i] = i & 0xFF;