]> git.sesse.net Git - vlc/blob - modules/access/http.c
* Massive spelling corrections.
[vlc] / modules / access / http.c
1 /*****************************************************************************
2  * http.c: HTTP input module
3  *****************************************************************************
4  * Copyright (C) 2001-2004 VideoLAN
5  * $Id$
6  *
7  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8  *          Christophe Massiot <massiot@via.ecp.fr>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
23  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28 #include <stdlib.h>
29
30 #include <vlc/vlc.h>
31 #include <vlc/input.h>
32
33 #include "vlc_playlist.h"
34 #include "network.h"
35
36 /*****************************************************************************
37  * Module descriptor
38  *****************************************************************************/
39 static int  Open ( vlc_object_t * );
40 static void Close( vlc_object_t * );
41
42 #define PROXY_TEXT N_("HTTP proxy")
43 #define PROXY_LONGTEXT N_( \
44     "You can specify an HTTP proxy to use. It must be of the form " \
45     "http://myproxy.mydomain:myport/. If none is specified, the HTTP_PROXY " \
46     "environment variable will be tried." )
47
48 #define CACHING_TEXT N_("Caching value in ms")
49 #define CACHING_LONGTEXT N_( \
50     "Allows you to modify the default caching value for http streams. This " \
51     "value should be set in millisecond units." )
52
53 #define USER_TEXT N_("HTTP user name")
54 #define USER_LONGTEXT N_("Allows you to modify the user name that will " \
55     "be used for the connection (Basic authentication only).")
56
57 #define PASS_TEXT N_("HTTP password")
58 #define PASS_LONGTEXT N_("Allows you to modify the password that will be " \
59     "used for the connection.")
60
61 #define AGENT_TEXT N_("HTTP user agent")
62 #define AGENT_LONGTEXT N_("Allows you to modify the user agent that will be " \
63     "used for the connection.")
64
65 vlc_module_begin();
66     set_description( _("HTTP input") );
67     set_capability( "access", 0 );
68
69     add_string( "http-proxy", NULL, NULL, PROXY_TEXT, PROXY_LONGTEXT,
70                 VLC_FALSE );
71     add_integer( "http-caching", 4 * DEFAULT_PTS_DELAY / 1000, NULL,
72                  CACHING_TEXT, CACHING_LONGTEXT, VLC_TRUE );
73     add_string( "http-user", NULL, NULL, USER_TEXT, USER_LONGTEXT, VLC_FALSE );
74     add_string( "http-pwd", NULL , NULL, PASS_TEXT, PASS_LONGTEXT, VLC_FALSE );
75     add_string( "http-user-agent", COPYRIGHT_MESSAGE , NULL, AGENT_TEXT,
76                 AGENT_LONGTEXT, VLC_FALSE );
77
78     add_shortcut( "http" );
79     add_shortcut( "http4" );
80     add_shortcut( "http6" );
81     set_callbacks( Open, Close );
82 vlc_module_end();
83
84 /*****************************************************************************
85  * Local prototypes
86  *****************************************************************************/
87 struct access_sys_t
88 {
89     int fd;
90
91     /* From uri */
92     vlc_url_t url;
93     char    *psz_user;
94     char    *psz_passwd;
95     char    *psz_user_agent;
96
97     /* Proxy */
98     vlc_bool_t b_proxy;
99     vlc_url_t  proxy;
100
101     /* */
102     int        i_code;
103     char      *psz_protocol;
104     int        i_version;
105
106     char       *psz_mime;
107     char       *psz_location;
108
109     vlc_bool_t b_chunked;
110     int64_t    i_chunk;
111     int64_t    i_tell;
112     int64_t    i_size;
113 };
114
115 static void    Seek( input_thread_t *, off_t );
116 static ssize_t Read( input_thread_t *, byte_t *, size_t );
117
118 static void    ParseURL( access_sys_t *, char *psz_url );
119 static int     Connect( input_thread_t *, vlc_bool_t *, off_t *, off_t );
120
121 static char *b64_encode( unsigned char *src );
122
123 /*****************************************************************************
124  * Open:
125  *****************************************************************************/
126 static int  Open ( vlc_object_t *p_this )
127 {
128     input_thread_t *p_input = (input_thread_t*)p_this;
129     access_sys_t   *p_sys;
130     vlc_value_t    val;
131
132     /* Create private struct */
133     p_sys = p_input->p_access_data = malloc( sizeof( access_sys_t ) );
134     memset( p_sys, 0, sizeof( access_sys_t ) );
135     p_sys->fd = -1;
136     p_sys->b_proxy = VLC_FALSE;
137     p_sys->i_version = 1;
138     p_sys->psz_mime = NULL;
139     p_sys->psz_location = NULL;
140     p_sys->psz_user_agent = NULL;
141
142     /* First set ipv4/ipv6 */
143     var_Create( p_input, "ipv4", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
144     var_Create( p_input, "ipv6", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
145
146     if( *p_input->psz_access )
147     {
148         /* Find out which shortcut was used */
149         if( !strncmp( p_input->psz_access, "http4", 6 ) )
150         {
151             val.b_bool = VLC_TRUE;
152             var_Set( p_input, "ipv4", val );
153
154             val.b_bool = VLC_FALSE;
155             var_Set( p_input, "ipv6", val );
156         }
157         else if( !strncmp( p_input->psz_access, "http6", 6 ) )
158         {
159             val.b_bool = VLC_TRUE;
160             var_Set( p_input, "ipv6", val );
161
162             val.b_bool = VLC_FALSE;
163             var_Set( p_input, "ipv4", val );
164         }
165     }
166
167     /* Parse URI */
168     ParseURL( p_sys, p_input->psz_name );
169     if( p_sys->url.psz_host == NULL || *p_sys->url.psz_host == '\0' )
170     {
171         msg_Warn( p_input, "invalid host" );
172         goto error;
173     }
174     if( p_sys->url.i_port <= 0 )
175     {
176         p_sys->url.i_port = 80;
177     }
178     if( !p_sys->psz_user || *p_sys->psz_user == '\0' )
179     {
180         var_Create( p_input, "http-user", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
181         var_Get( p_input, "http-user", &val );
182         p_sys->psz_user = val.psz_string;
183
184         var_Create( p_input, "http-pwd", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
185         var_Get( p_input, "http-pwd", &val );
186         p_sys->psz_passwd = val.psz_string;
187     }
188
189     /* Do user agent */
190     var_Create( p_input, "http-user-agent", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
191     var_Get( p_input, "http-user-agent", &val );
192     p_sys->psz_user_agent = val.psz_string;
193
194     /* Check proxy */
195     var_Create( p_input, "http-proxy", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
196     var_Get( p_input, "http-proxy", &val );
197     if( val.psz_string && *val.psz_string )
198     {
199         p_sys->b_proxy = VLC_TRUE;
200         vlc_UrlParse( &p_sys->proxy, val.psz_string, 0 );
201     }
202     else
203     {
204         char *psz_proxy = getenv( "http_proxy" );
205         if( psz_proxy && *psz_proxy )
206         {
207             p_sys->b_proxy = VLC_TRUE;
208             vlc_UrlParse( &p_sys->proxy, val.psz_string, 0 );
209         }
210         if( psz_proxy )
211         {
212             free( psz_proxy );
213         }
214     }
215     if( val.psz_string )
216     {
217         free( val.psz_string );
218     }
219
220     if( p_sys->b_proxy )
221     {
222         if( p_sys->proxy.psz_host == NULL || *p_sys->proxy.psz_host == '\0' )
223         {
224             msg_Warn( p_input, "invalid proxy host" );
225             goto error;
226         }
227         if( p_sys->proxy.i_port <= 0 )
228         {
229             p_sys->proxy.i_port = 80;
230         }
231     }
232
233     msg_Dbg( p_input, "http: server='%s' port=%d file='%s",
234              p_sys->url.psz_host, p_sys->url.i_port, p_sys->url.psz_path );
235     if( p_sys->b_proxy )
236     {
237         msg_Dbg( p_input, "      proxy %s:%d", p_sys->proxy.psz_host,
238                  p_sys->proxy.i_port );
239     }
240     if( p_sys->psz_user && *p_sys->psz_user )
241     {
242         msg_Dbg( p_input, "      user='%s', pwd='%s'",
243                  p_sys->psz_user, p_sys->psz_passwd );
244     }
245
246     /* Connect */
247     if( Connect( p_input, &p_input->stream.b_seekable,
248                  &p_input->stream.p_selected_area->i_size, 0 ) )
249     {
250         /* Retry with http 1.0 */
251         p_sys->i_version = 0;
252
253         if( p_input->b_die ||
254             Connect( p_input, &p_input->stream.b_seekable,
255                      &p_input->stream.p_selected_area->i_size, 0 ) )
256         {
257             goto error;
258         }
259     }
260
261     if( ( p_sys->i_code == 301 || p_sys->i_code == 302 ||
262           p_sys->i_code == 303 || p_sys->i_code == 307 ) &&
263         p_sys->psz_location && *p_sys->psz_location )
264     {
265         playlist_t * p_playlist;
266
267         msg_Dbg( p_input, "redirection to %s", p_sys->psz_location );
268
269         p_playlist = vlc_object_find( p_input, VLC_OBJECT_PLAYLIST, FIND_PARENT );
270         if( !p_playlist )
271         {
272             msg_Err( p_input, "redirection failed: can't find playlist" );
273             goto error;
274         }
275         p_playlist->pp_items[p_playlist->i_index]->b_autodeletion = VLC_TRUE;
276         playlist_Add( p_playlist, p_sys->psz_location, p_sys->psz_location,
277                       PLAYLIST_INSERT | PLAYLIST_GO,
278                       p_playlist->i_index + 1 );
279         vlc_object_release( p_playlist );
280
281         p_sys->i_size = 0;  /* Force to stop reading */
282     }
283
284     /* Finish to set up p_input */
285     p_input->pf_read = Read;
286     p_input->pf_set_program = input_SetProgram;
287     p_input->pf_set_area = NULL;
288     p_input->pf_seek = Seek;
289
290     vlc_mutex_lock( &p_input->stream.stream_lock );
291     p_input->stream.b_pace_control = VLC_TRUE;
292     p_input->stream.p_selected_area->i_tell = 0;
293     p_input->stream.i_method = INPUT_METHOD_NETWORK;
294     vlc_mutex_unlock( &p_input->stream.stream_lock );
295     p_input->i_mtu = 0;
296     if( !strcmp( p_sys->psz_protocol, "ICY" ) &&
297         ( !p_input->psz_demux || !*p_input->psz_demux ) )
298     {
299         if( p_sys->psz_mime && !strcasecmp( p_sys->psz_mime, "video/nsv" ) )
300         {
301             p_input->psz_demux = strdup( "nsv" );
302         }
303         else
304         {
305             p_input->psz_demux = strdup( "mp3" );
306         }
307         msg_Info( p_input, "ICY server found, %s demuxer selected",
308                   p_input->psz_demux );
309     }
310
311     /* Update default_pts to a suitable value for http access */
312     var_Create( p_input, "http-caching", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
313     var_Get( p_input, "http-caching", &val );
314     p_input->i_pts_delay = val.i_int * 1000;
315
316     return VLC_SUCCESS;
317
318 error:
319     vlc_UrlClean( &p_sys->url );
320     vlc_UrlClean( &p_sys->proxy );
321     if( p_sys->psz_mime ) free( p_sys->psz_mime );
322     if( p_sys->psz_location ) free( p_sys->psz_location );
323     if( p_sys->psz_user_agent ) free( p_sys->psz_user_agent );
324     if( p_sys->psz_user ) free( p_sys->psz_user );
325     if( p_sys->psz_passwd ) free( p_sys->psz_passwd );
326
327     if( p_sys->fd > 0 )
328     {
329         net_Close( p_sys->fd );
330     }
331     free( p_sys );
332     return VLC_EGENERIC;
333 }
334
335 /*****************************************************************************
336  * Close:
337  *****************************************************************************/
338 static void Close( vlc_object_t *p_this )
339 {
340     input_thread_t *p_input = (input_thread_t*)p_this;
341     access_sys_t   *p_sys   = p_input->p_access_data;
342
343     vlc_UrlClean( &p_sys->url );
344     vlc_UrlClean( &p_sys->proxy );
345
346     if( p_sys->psz_user ) free( p_sys->psz_user );
347     if( p_sys->psz_passwd ) free( p_sys->psz_passwd );
348
349     if( p_sys->psz_mime ) free( p_sys->psz_mime );
350     if( p_sys->psz_location ) free( p_sys->psz_location );
351
352     if( p_sys->psz_user_agent ) free( p_sys->psz_user_agent );
353
354     if( p_sys->fd > 0 )
355     {
356         net_Close( p_sys->fd );
357     }
358     free( p_sys );
359 }
360
361 /*****************************************************************************
362  * Seek: close and re-open a connection at the right place
363  *****************************************************************************/
364 static void Seek( input_thread_t * p_input, off_t i_pos )
365 {
366     access_sys_t   *p_sys   = p_input->p_access_data;
367
368     msg_Dbg( p_input, "trying to seek to "I64Fd, i_pos );
369
370     net_Close( p_sys->fd ); p_sys->fd = -1;
371
372     if( Connect( p_input, &p_input->stream.b_seekable,
373                  &p_input->stream.p_selected_area->i_size, i_pos ) )
374     {
375         msg_Err( p_input, "seek failed" );
376     }
377
378     vlc_mutex_lock( &p_input->stream.stream_lock );
379     p_input->stream.p_selected_area->i_tell = i_pos;
380     vlc_mutex_unlock( &p_input->stream.stream_lock );
381 }
382
383 /*****************************************************************************
384  * Read: Read up to i_len bytes from the http connection and place in
385  * p_buffer. Return the actual number of bytes read
386  *****************************************************************************/
387 static ssize_t Read( input_thread_t * p_input, byte_t * p_buffer, size_t i_len )
388 {
389     access_sys_t   *p_sys   = p_input->p_access_data;
390     int            i_read;
391
392     if( p_sys->fd < 0 )
393     {
394         return -1;
395     }
396     if( p_sys->i_size > 0 && i_len + p_sys->i_tell > p_sys->i_size )
397     {
398         if( ( i_len = p_sys->i_size - p_sys->i_tell ) == 0 )
399         {
400             return 0;
401         }
402     }
403     if( p_sys->b_chunked )
404     {
405         if( p_sys->i_chunk < 0 )
406         {
407             return 0;
408         }
409
410         if( p_sys->i_chunk <= 0 )
411         {
412             char *psz = net_Gets( VLC_OBJECT(p_input), p_sys->fd );
413             /* read the chunk header */
414             if( psz == NULL )
415             {
416                 msg_Dbg( p_input, "failed reading chunk-header line" );
417                 return -1;
418             }
419             p_sys->i_chunk = strtoll( psz, NULL, 16 );
420             free( psz );
421
422             if( p_sys->i_chunk <= 0 )   /* eof */
423             {
424                 p_sys->i_chunk = -1;
425                 return 0;
426             }
427         }
428
429         if( i_len > p_sys->i_chunk )
430         {
431             i_len = p_sys->i_chunk;
432         }
433     }
434
435
436     i_read = net_Read( p_input, p_sys->fd, p_buffer, i_len, VLC_FALSE );
437     if( i_read > 0 )
438     {
439         p_sys->i_tell += i_read;
440
441         if( p_sys->b_chunked )
442         {
443             p_sys->i_chunk -= i_read;
444             if( p_sys->i_chunk <= 0 )
445             {
446                 /* read the empty line */
447                 char *psz = net_Gets( VLC_OBJECT(p_input), p_sys->fd );
448                 if( psz )
449                 {
450                     free( psz );
451                 }
452             }
453         }
454     }
455     return i_read;
456 }
457
458 /*****************************************************************************
459  * ParseURL: extract user:password
460  *****************************************************************************/
461 static void ParseURL( access_sys_t *p_sys, char *psz_url )
462 {
463     char *psz_dup = strdup( psz_url );
464     char *p = psz_dup;
465     char *psz;
466
467     /* Syntax //[user:password]@<hostname>[:<port>][/<path>] */
468     while( *p == '/' )
469     {
470         p++;
471     }
472     psz = p;
473
474     /* Parse auth */
475     if( ( p = strchr( psz, '@' ) ) )
476     {
477         char *comma;
478
479         *p++ = '\0';
480         comma = strchr( psz, ':' );
481
482         /* Retreive user:password */
483         if( comma )
484         {
485             *comma++ = '\0';
486
487             p_sys->psz_user = strdup( psz );
488             p_sys->psz_passwd = strdup( comma );
489         }
490         else
491         {
492             p_sys->psz_user = strdup( psz );
493         }
494     }
495     else
496     {
497         p = psz;
498     }
499
500     /* Parse uri */
501     vlc_UrlParse( &p_sys->url, p, 0 );
502
503     free( psz_dup );
504 }
505
506 /*****************************************************************************
507  * Connect:
508  *****************************************************************************/
509 static int Connect( input_thread_t *p_input, vlc_bool_t *pb_seekable,
510                     off_t *pi_size, off_t i_tell )
511 {
512     access_sys_t   *p_sys   = p_input->p_access_data;
513     vlc_url_t      srv = p_sys->b_proxy ? p_sys->proxy : p_sys->url;
514     char           *psz;
515
516     /* Clean info */
517     if( p_sys->psz_location ) free( p_sys->psz_location );
518     if( p_sys->psz_mime ) free( p_sys->psz_mime );
519
520     p_sys->psz_location = NULL;
521     p_sys->psz_mime = NULL;
522     p_sys->b_chunked = VLC_FALSE;
523     p_sys->i_chunk = 0;
524     p_sys->i_size = -1;
525     p_sys->i_tell = i_tell;
526
527
528     /* Open connection */
529     p_sys->fd = net_OpenTCP( p_input, srv.psz_host, srv.i_port );
530     if( p_sys->fd < 0 )
531     {
532         msg_Err( p_input, "cannot connect to %s:%d", srv.psz_host, srv.i_port );
533         return VLC_EGENERIC;
534     }
535
536     if( p_sys->b_proxy )
537     {
538         net_Printf( VLC_OBJECT(p_input), p_sys->fd,
539                     "GET http://%s:%d/%s HTTP/1.%d\r\n",
540                     p_sys->url.psz_host, p_sys->url.i_port,
541                     p_sys->url.psz_path, p_sys->i_version );
542     }
543     else
544     {
545         char *psz_path = p_sys->url.psz_path;
546         if( !psz_path || !*psz_path )
547         {
548             psz_path = "/";
549         }
550         net_Printf( VLC_OBJECT(p_input), p_sys->fd,
551                     "GET %s HTTP/1.%d\r\nHost: %s\r\n",
552                     psz_path, p_sys->i_version, p_sys->url.psz_host );
553     }
554     /* User Agent */
555     net_Printf( VLC_OBJECT(p_input), p_sys->fd, "User-Agent: %s\r\n",
556                 p_sys->psz_user_agent );
557     /* Offset */
558     if( p_sys->i_version == 1 )
559     {
560         net_Printf( VLC_OBJECT(p_input), p_sys->fd,
561                     "Range: bytes="I64Fd"-\r\n", i_tell );
562     }
563     /* Authentification */
564     if( p_sys->psz_user && *p_sys->psz_user )
565     {
566         char *buf;
567         char *b64;
568
569         asprintf( &buf, "%s:%s", p_sys->psz_user,
570                    p_sys->psz_passwd ? p_sys->psz_passwd : "" );
571
572         b64 = b64_encode( buf );
573
574         net_Printf( VLC_OBJECT(p_input), p_sys->fd,
575                     "Authorization: Basic %s\r\n", b64 );
576         free( b64 );
577     }
578     net_Printf( VLC_OBJECT(p_input), p_sys->fd, "Connection: Close\r\n" );
579
580     if( net_Printf( VLC_OBJECT(p_input), p_sys->fd, "\r\n" ) < 0 )
581     {
582         msg_Err( p_input, "failed to send request" );
583         net_Close( p_sys->fd ); p_sys->fd = -1;
584         return VLC_EGENERIC;
585     }
586
587     /* Set values */
588     *pb_seekable = p_sys->i_version == 1 ? VLC_TRUE : VLC_FALSE;
589     *pi_size = 0;
590
591     /* Read Answer */
592     if( ( psz = net_Gets( VLC_OBJECT(p_input), p_sys->fd ) ) == NULL )
593     {
594         msg_Err( p_input, "failed to read answer" );
595         goto error;
596     }
597     if( !strncmp( psz, "HTTP/1.", 7 ) )
598     {
599         p_sys->psz_protocol = "HTTP";
600         p_sys->i_code = atoi( &psz[9] );
601     }
602     else if( !strncmp( psz, "ICY", 3 ) )
603     {
604         p_sys->psz_protocol = "ICY";
605         p_sys->i_code = atoi( &psz[4] );
606     }
607     else
608     {
609         msg_Err( p_input, "invalid HTTP reply '%s'", psz );
610         free( psz );
611         goto error;
612     }
613     msg_Dbg( p_input, "protocol '%s' answer code %d",
614              p_sys->psz_protocol, p_sys->i_code );
615     if( !strcmp( p_sys->psz_protocol, "ICY" ) )
616     {
617         *pb_seekable = VLC_FALSE;
618     }
619     if( p_sys->i_code != 206 )
620     {
621         *pb_seekable = VLC_FALSE;
622     }
623     if( p_sys->i_code >= 400 )
624     {
625         msg_Err( p_input, "error: %s", psz );
626         free( psz );
627         goto error;
628     }
629     free( psz );
630
631     for( ;; )
632     {
633         char *psz = net_Gets( VLC_OBJECT(p_input), p_sys->fd );
634         char *p;
635
636         if( psz == NULL )
637         {
638             msg_Err( p_input, "failed to read answer" );
639             goto error;
640         }
641
642         /* msg_Dbg( p_input, "Line=%s", psz ); */
643         if( *psz == '\0' )
644         {
645             free( psz );
646             break;
647         }
648
649
650         if( ( p = strchr( psz, ':' ) ) == NULL )
651         {
652             msg_Err( p_input, "malformed header line: %s", psz );
653             free( psz );
654             goto error;
655         }
656         *p++ = '\0';
657         while( *p == ' ' ) p++;
658
659         if( !strcasecmp( psz, "Content-Length" ) )
660         {
661             *pi_size = p_sys->i_size = i_tell + atoll( p );
662             msg_Dbg( p_input, "stream size="I64Fd, p_sys->i_size );
663         }
664         else if( !strcasecmp( psz, "Location" ) )
665         {
666             if( p_sys->psz_location ) free( p_sys->psz_location );
667             p_sys->psz_location = strdup( p );
668         }
669         else if( !strcasecmp( psz, "Content-Type" ) )
670         {
671             if( p_sys->psz_mime ) free( p_sys->psz_mime );
672             p_sys->psz_mime = strdup( p );
673             msg_Dbg( p_input, "Content-Type: %s", p_sys->psz_mime );
674         }
675         else if( !strcasecmp( psz, "Transfer-Encoding" ) )
676         {
677             msg_Dbg( p_input, "Transfer-Encoding: %s", p );
678             if( !strncasecmp( p, "chunked", 7 ) )
679             {
680                 p_sys->b_chunked = VLC_TRUE;
681             }
682         }
683
684         free( psz );
685     }
686     return VLC_SUCCESS;
687
688 error:
689     net_Close( p_sys->fd ); p_sys->fd = -1;
690     return VLC_EGENERIC;
691 }
692
693 /*****************************************************************************
694  * b64_encode:
695  *****************************************************************************/
696 static char *b64_encode( unsigned char *src )
697 {
698     static const char b64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
699
700     char *dst = malloc( strlen( src ) * 4 / 3 + 12 );
701     char *ret = dst;
702     unsigned i_bits = 0;
703     unsigned i_shift = 0;
704
705     for( ;; )
706     {
707         if( *src )
708         {
709             i_bits = ( i_bits << 8 )|( *src++ );
710             i_shift += 8;
711         }
712         else if( i_shift > 0 )
713         {
714            i_bits <<= 6 - i_shift;
715            i_shift = 6;
716         }
717         else
718         {
719             *dst++ = '=';
720             break;
721         }
722
723         while( i_shift >= 6 )
724         {
725             i_shift -= 6;
726             *dst++ = b64[(i_bits >> i_shift)&0x3f];
727         }
728     }
729
730     *dst++ = '\0';
731
732     return ret;
733 }