1 /**********************************************************
2  *
3  * libmp3splt -- library based on mp3splt,
4  *               for mp3/ogg splitting without decoding
5  *
6  * Copyright (c) 2002-2005 M. Trotta - <mtrotta@users.sourceforge.net>
7  * Copyright (c) 2005-2014 Alexandru Munteanu - m@ioalex.net
8  *
9  * http://mp3splt.sourceforge.net
10  *
11  *********************************************************/
12 
13 /**********************************************************
14  *
15  * This program is free software; you can redistribute it and/or
16  * modify it under the terms of the GNU General Public License
17  * as published by the Free Software Foundation; either version 2
18  * of the License, or (at your option) any later version.
19  *
20  * This program is distributed in the hope that it will be useful,
21  * but WITHOUT ANY WARRANTY; without even the implied warranty of
22  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23  * GNU General Public License for more details.
24  *
25  * You should have received a copy of the GNU General Public License
26  * along with this program; if not, write to the Free Software
27  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
28  * USA.
29  *
30  *********************************************************/
31 
32 #include "splt.h"
33 
34 #include <string.h>
35 
36 static void splt_pr_free_proxy_address(splt_state *state);
37 static void splt_pr_free_proxy_authentification(splt_state *state);
38 static char *splt_pr_encode3to4(const unsigned char *source, int srcoffset, int num,
39     char *destination, int destoffset);
40 
splt_pr_use_proxy(splt_state * state,const char * proxy_address,int proxy_port)41 splt_code splt_pr_use_proxy(splt_state *state, const char *proxy_address, int proxy_port)
42 {
43   if (proxy_address == NULL || proxy_address[0] == '\0')
44   {
45     return SPLT_OK;
46   }
47 
48   splt_pr_free_proxy_address(state);
49   splt_su_copy(proxy_address, &state->proxy.proxy_address);
50 
51   state->proxy.proxy_port = proxy_port;
52 
53   return SPLT_OK;
54 }
55 
splt_pr_has_proxy(splt_state * state)56 int splt_pr_has_proxy(splt_state *state)
57 {
58   return state->proxy.proxy_address != NULL && state->proxy.proxy_port != -1;
59 }
60 
splt_pr_has_proxy_authentification(splt_state * state)61 int splt_pr_has_proxy_authentification(splt_state *state)
62 {
63   return state->proxy.authentification != NULL;
64 }
65 
splt_pr_get_proxy_authentification(splt_state * state)66 const char *splt_pr_get_proxy_authentification(splt_state *state)
67 {
68   return state->proxy.authentification;
69 }
70 
splt_pr_get_proxy_address(splt_state * state)71 const char *splt_pr_get_proxy_address(splt_state *state)
72 {
73   return state->proxy.proxy_address;
74 }
75 
splt_pr_get_proxy_port(splt_state * state)76 int splt_pr_get_proxy_port(splt_state *state)
77 {
78   return state->proxy.proxy_port;
79 }
80 
splt_pr_use_base64_authentification(splt_state * state,const char * base64_authentification)81 splt_code splt_pr_use_base64_authentification(splt_state *state,
82     const char *base64_authentification)
83 {
84   if (base64_authentification == NULL)
85   {
86     return SPLT_OK;
87   }
88 
89   splt_pr_free_proxy_authentification(state);
90   splt_su_copy(base64_authentification, &state->proxy.authentification);
91 
92   return SPLT_OK;
93 }
94 
splt_pr_set_default_values(splt_state * state)95 void splt_pr_set_default_values(splt_state *state)
96 {
97   state->proxy.proxy_address = NULL;
98   state->proxy.proxy_port = -1;
99   state->proxy.authentification = NULL;
100 }
101 
splt_pr_free(splt_state * state)102 void splt_pr_free(splt_state *state)
103 {
104   splt_pr_free_proxy_address(state);
105   splt_pr_free_proxy_authentification(state);
106   splt_pr_set_default_values(state);
107 }
108 
splt_pr_base64(const unsigned char * source)109 char *splt_pr_base64(const unsigned char *source)
110 {
111   int len = strlen((char *)source);
112 
113   int d = ((len*4/3)+((len%3)>0?4:0));
114 
115   char *out = malloc(d + 1);
116   if (out == NULL) { return NULL; }
117   memset(out, 0x00, d+1);
118 
119   int e = 0;
120   for (d = 0;d < (len-2); d+=3,e+=4)
121   {
122     out = splt_pr_encode3to4(source, d, 3, out, e);
123   }
124 
125   if (d < len)
126   {
127     out = splt_pr_encode3to4(source, d, len-d, out, e);
128   }
129 
130   return out;
131 }
132 
splt_pr_free_proxy_address(splt_state * state)133 static void splt_pr_free_proxy_address(splt_state *state)
134 {
135   if (state->proxy.proxy_address)
136   {
137     free(state->proxy.proxy_address);
138     state->proxy.proxy_address = NULL;
139   }
140 }
141 
splt_pr_free_proxy_authentification(splt_state * state)142 static void splt_pr_free_proxy_authentification(splt_state *state)
143 {
144   if (state->proxy.authentification)
145   {
146     size_t authentification_length = strlen(state->proxy.authentification);
147     memset(state->proxy.authentification, '\0', authentification_length);
148     free(state->proxy.authentification);
149     state->proxy.authentification = NULL;
150   }
151 }
152 
153 /*
154  * Base64 Algorithm: Base64.java v. 1.3.6 by Robert Harder
155  * Ported and optimized for C by Matteo Trotta
156  */
157 static const char alphabet [] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
158 
splt_pr_encode3to4(const unsigned char * source,int srcoffset,int num,char * destination,int destoffset)159 static char *splt_pr_encode3to4(const unsigned char *source, int srcoffset, int num, char *destination, int destoffset)
160 {
161   int inbuff=
162     (num>0 ? (source[srcoffset] << 16) : 0)|
163     (num>1 ? (source[srcoffset+1] << 8) : 0) |
164     (num > 2 ? (source[srcoffset+2]) : 0);
165 
166   switch (num)
167   {
168     case 3:
169       destination[destoffset] = alphabet[(inbuff>>18)];
170       destination[destoffset+1] = alphabet[(inbuff>>12) & 0x3f];
171       destination[destoffset+2] = alphabet[(inbuff>>6) & 0x3f];
172       destination[destoffset+3] = alphabet[(inbuff) & 0x3f];
173       return destination;
174     case 2:
175       destination[destoffset] = alphabet[(inbuff>>18)];
176       destination[destoffset+1] = alphabet[(inbuff>>12) & 0x3f];
177       destination[destoffset+2] = alphabet[(inbuff>>6) & 0x3f];
178       destination[destoffset+3] = '=';
179       return destination;
180     case 1:
181       destination[destoffset] = alphabet[(inbuff>>18)];
182       destination[destoffset+1] = alphabet[(inbuff>>12) & 0x3f];
183       destination[destoffset+2] = '=';
184       destination[destoffset+3] = '=';
185       return destination;
186     default:
187       return destination;
188   }
189 }
190 
191