loudmouth/lm-utils.c
changeset 66 577d5059b718
parent 65 e6871fda6fc9
child 84 7ae7b690aa89
equal deleted inserted replaced
65:e6871fda6fc9 66:577d5059b718
    60 	static gint  number = 0;
    60 	static gint  number = 0;
    61 
    61 
    62 	return g_strdup_printf ("msg_%d", ++number);
    62 	return g_strdup_printf ("msg_%d", ++number);
    63 }
    63 }
    64 
    64 
       
    65 gchar * 
       
    66 _lm_utils_base64_encode (const gchar *s)
       
    67 {
       
    68 	static const gchar *base64chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
       
    69 
       
    70 	guint    i, j;
       
    71 	guint32  bits = 0;
       
    72 	guint    maxlen = (strlen(s) * 2) + 3;
       
    73 	gchar   *str;
       
    74 
       
    75 	str = g_malloc(maxlen);
       
    76 
       
    77 	j = 0;
       
    78 	for (i = 0; i < strlen(s); i++) {
       
    79 		bits <<= 8;
       
    80 		bits |= s[i] & 0xff;
       
    81 
       
    82 		if (!((i+1) % 3)) {
       
    83 			guint indices[4];
       
    84 
       
    85 			indices[0] = (bits >> 18) & 0x3f;
       
    86 			indices[1] = (bits >> 12) & 0x3f;
       
    87 			indices[2] = (bits >> 6) & 0x3f;
       
    88 			indices[3] = bits & 0x3f;
       
    89 			bits = 0;
       
    90 
       
    91 			str[j++] = base64chars[(indices[0])];
       
    92 			str[j++] = base64chars[(indices[1])];
       
    93 			str[j++] = base64chars[(indices[2])];
       
    94 			str[j++] = base64chars[(indices[3])];
       
    95 		}
       
    96 	}
       
    97 
       
    98 	if (j + 4 < maxlen) {
       
    99 		if ((i % 3) == 1) {
       
   100 			guint indices[2];
       
   101 
       
   102 			indices[0] = (bits >> 2) & 0x3f;
       
   103 			indices[1] = (bits << 4) & 0x3f;
       
   104 
       
   105 			str[j++] = base64chars[(indices[0])];
       
   106 			str[j++] = base64chars[(indices[1])];
       
   107 			str[j++] = '=';
       
   108 			str[j++] = '=';
       
   109 		} else if ((i % 3) == 2) {
       
   110 			guint indices[3];
       
   111 
       
   112 			indices[0] = (bits >> 10) & 0x3f;
       
   113 			indices[1] = (bits >> 4) & 0x3f;
       
   114 			indices[2] = (bits << 2) & 0x3f;
       
   115 
       
   116 			str[j++] = base64chars[(indices[0])];
       
   117 			str[j++] = base64chars[(indices[1])];
       
   118 			str[j++] = base64chars[(indices[2])];
       
   119 			str[j++] = '=';
       
   120 		}
       
   121 	}
       
   122 
       
   123 	str[j] = '\0';
       
   124 
       
   125 	return str;
       
   126 }
       
   127 
    65 struct tm *
   128 struct tm *
    66 lm_utils_get_localtime (const gchar *stamp)
   129 lm_utils_get_localtime (const gchar *stamp)
    67 {
   130 {
    68 	struct tm tm;
   131 	struct tm tm;
    69 	time_t    t;
   132 	time_t    t;