]> git.sesse.net Git - vlc/blobdiff - modules/access/rtsp/real.c
Do not access b_die directly
[vlc] / modules / access / rtsp / real.c
index c744208b65505eaf9fd5e2f27a34e6c0e6746d2c..86eab8ca045453227745b4e6b3755a14cee5cf95 100644 (file)
@@ -18,9 +18,9 @@
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  * GNU General Public License for more details.
  *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
  *****************************************************************************/
 
 #ifdef HAVE_CONFIG_H
 #endif
 
 #include <vlc_common.h>
+#include <vlc_memory.h>
 
 #include "rtsp.h"
 #include "real.h"
 #include "real_sdpplin.h"
 
-const unsigned char xor_table[] = {
+#define XOR_TABLE_LEN 37
+static const unsigned char xor_table[] = {
     0x05, 0x18, 0x74, 0xd0, 0x0d, 0x09, 0x02, 0x53,
     0xc0, 0x01, 0x05, 0x05, 0x67, 0x03, 0x19, 0x70,
     0x08, 0x27, 0x66, 0x10, 0x10, 0x72, 0x08, 0x09,
@@ -48,7 +50,6 @@ const unsigned char xor_table[] = {
 #define LE_32C(x,y) do {uint32_t in=y; *(uint32_t *)(x)=GetDWLE(&in);} while(0)
 #define MAX(x,y) ((x>y) ? x : y)
 
-
 static void hash(char *field, char *param)
 {
   uint32_t a, b, c, d;
@@ -207,8 +208,7 @@ static void hash(char *field, char *param)
   LE_32C(field+12, d);
 }
 
-static void call_hash (char *key, char *challenge, int len) {
-
+static void call_hash (char *key, char *challenge, unsigned int len) {
   uint8_t *ptr1, *ptr2;
   uint32_t a, b, c, d, tmp;
 
@@ -220,7 +220,7 @@ static void call_hash (char *key, char *challenge, int len) {
   a += len * 8;
   LE_32C(ptr1, a);
 
-  if (a < (uint32_t)(len << 3))
+  if (a < (len << 3))
   {
     lprintf("not verified: (len << 3) > a true\n");
     ptr2 += 4;
@@ -230,14 +230,14 @@ static void call_hash (char *key, char *challenge, int len) {
   LE_32C(ptr2, tmp);
   a = 64 - b;
   c = 0;
-  if (a <= (uint32_t)len)
+  if (a <= len)
   {
     memcpy(key+b+24, challenge, a);
     hash(key, key+24);
     c = a;
     d = c + 0x3f;
 
-    while ( d < (uint32_t)len ) {
+    while ( d < len ) {
       lprintf("not verified:  while ( d < len )\n");
       hash(key, challenge+d-0x3f);
       d += 64;
@@ -275,18 +275,16 @@ static void calc_response (char *result, char *field) {
 }
 
 static void calc_response_string (char *result, char *challenge) {
-  char field[128];
+
+  char field[128] = {
+    0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF,
+    0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
+    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
+  };
   char zres[20];
   int  i;
 
-  /* initialize our field */
-  BE_32C (field,      0x01234567);
-  BE_32C ((field+4),  0x89ABCDEF);
-  BE_32C ((field+8),  0xFEDCBA98);
-  BE_32C ((field+12), 0x76543210);
-  BE_32C ((field+16), 0x00000000);
-  BE_32C ((field+20), 0x00000000);
-
   /* calculate response */
   call_hash(field, challenge, 64);
   calc_response(zres,field);
@@ -303,9 +301,9 @@ static void calc_response_string (char *result, char *challenge) {
   }
 }
 
-void real_calc_response_and_checksum (char *response, char *chksum, char *challenge) {
+static void real_calc_response_and_checksum (char *response, char *chksum, char *challenge) {
 
-  int   ch_len, table_len, resp_len;
+  int   ch_len, resp_len;
   int   i;
   char *ptr;
   char  buf[128];
@@ -338,16 +336,9 @@ void real_calc_response_and_checksum (char *response, char *chksum, char *challe
     memcpy(ptr, challenge, ch_len);
   }
 
-  if (xor_table != NULL)
-  {
-    table_len = strlen((char *)xor_table);
-
-    if (table_len > 56) table_len=56;
-
-    /* xor challenge bytewise with xor_table */
-    for (i=0; i<table_len; i++)
-      ptr[i] = ptr[i] ^ xor_table[i];
-  }
+  /* xor challenge bytewise with xor_table */
+  for (i=0; i<XOR_TABLE_LEN; i++)
+    ptr[i] = ptr[i] ^ xor_table[i];
 
   calc_response_string (response, buf);
 
@@ -360,6 +351,7 @@ void real_calc_response_and_checksum (char *response, char *chksum, char *challe
     chksum[i] = response[i*4];
 }
 
+#define MLTI_BUF_MAX_SIZE 2048
 
 /*
  * takes a MLTI-Chunk and a rule number got from match_asm_rule,
@@ -377,7 +369,7 @@ static int select_mlti_data(const char *mlti_chunk, int mlti_size, int selection
       ||(mlti_chunk[3] != 'I'))
   {
     lprintf("MLTI tag not detected, copying data\n");
-    memcpy(*out, mlti_chunk, mlti_size);
+    memcpy(*out, mlti_chunk, __MIN(mlti_size,MLTI_BUF_MAX_SIZE));
     return mlti_size;
   }
 
@@ -414,7 +406,7 @@ static int select_mlti_data(const char *mlti_chunk, int mlti_size, int selection
   }
   size=BE_32(mlti_chunk);
 
-  memcpy(*out, mlti_chunk+4, size);
+  memcpy(*out, mlti_chunk+4, __MIN(size,MLTI_BUF_MAX_SIZE));
   return size;
 }
 
@@ -422,7 +414,7 @@ static int select_mlti_data(const char *mlti_chunk, int mlti_size, int selection
  * looking at stream description.
  */
 
-rmff_header_t *real_parse_sdp(char *data, char **stream_rules, uint32_t bandwidth) {
+static rmff_header_t *real_parse_sdp(char *data, char **stream_rules, uint32_t bandwidth) {
 
   sdpplin_t *desc = NULL;
   rmff_header_t *header = NULL;
@@ -439,7 +431,7 @@ rmff_header_t *real_parse_sdp(char *data, char **stream_rules, uint32_t bandwidt
   desc=sdpplin_parse(data);
   if( !desc ) return NULL;
 
-  buf= (char *)malloc(2048);
+  buf= (char *)malloc(MLTI_BUF_MAX_SIZE);
   if( !buf ) goto error;
 
   header = calloc( 1, sizeof(rmff_header_t) );
@@ -528,13 +520,13 @@ rmff_header_t *real_parse_sdp(char *data, char **stream_rules, uint32_t bandwidt
 
   rmff_fix_header(header);
 
-  if( desc ) sdpplin_free( desc );
+  sdpplin_free( desc );
   free( buf );
   return header;
 
 error:
-  if( desc ) sdpplin_free( desc );
-  if( header ) rmff_free_header( header );
+  sdpplin_free( desc );
+  rmff_free_header( header );
   free( buf );
   return NULL;
 }
@@ -550,15 +542,18 @@ int real_get_rdt_chunk_header(rtsp_client_t *rtsp_session, rmff_pheader_t *ph) {
 
   n=rtsp_read_data(rtsp_session, header, 8);
   if (n<8) return 0;
-  if (header[0] != 0x24) {
+  if (header[0] != 0x24)
+  {
     lprintf("rdt chunk not recognized: got 0x%02x\n", header[0]);
     return 0;
   }
   size=(header[1]<<16)+(header[2]<<8)+(header[3]);
   flags1=header[4];
-  if ((flags1!=0x40)&&(flags1!=0x42)) {
+  if ((flags1!=0x40)&&(flags1!=0x42))
+  {
     lprintf("got flags1: 0x%02x\n",flags1);
-    if (header[6]==0x06) {
+    if (header[6]==0x06)
+    {
       lprintf("got end of stream packet\n");
       return 0;
     }
@@ -598,6 +593,7 @@ int real_get_rdt_chunk(rtsp_client_t *rtsp_session, rmff_pheader_t *ph,
 
   int n;
   rmff_dump_pheader(ph, (char*)*buffer);
+  if (ph->length<12) return 0;
   n=rtsp_read_data(rtsp_session, (uint8_t*)(*buffer + 12), ph->length - 12);
   return (n <= 0) ? 0 : n+12;
 }
@@ -613,7 +609,9 @@ rmff_header_t  *real_setup_and_get_header(rtsp_client_t *rtsp_session, int bandw
   char challenge2[64];
   char checksum[34];
   char *subscribe=NULL;
-  char *buf=(char*)malloc(256);
+  char *buf = malloc(256);
+  if( !buf )
+    return NULL;
   char *mrl=rtsp_get_mrl(rtsp_session);
   unsigned int size;
   int status;
@@ -666,7 +664,7 @@ rmff_header_t  *real_setup_and_get_header(rtsp_client_t *rtsp_session, int bandw
 
   lprintf("Stream description size: %i\n", size);
 
-  description = (char*)malloc(size+1);
+  description = malloc(size+1);
   if( !description )
     goto error;
   if( rtsp_read_data(rtsp_session, (uint8_t*)description, size) <= 0)
@@ -675,7 +673,7 @@ rmff_header_t  *real_setup_and_get_header(rtsp_client_t *rtsp_session, int bandw
   //fprintf(stderr, "%s", description);
 
   /* parse sdp (sdpplin) and create a header and a subscribe string */
-  subscribe = (char *) malloc(256);
+  subscribe = malloc(256);
   if( !subscribe )
     goto error;
 
@@ -693,23 +691,28 @@ rmff_header_t  *real_setup_and_get_header(rtsp_client_t *rtsp_session, int bandw
 
   /* setup our streams */
   real_calc_response_and_checksum (challenge2, checksum, challenge1);
-  buf = realloc(buf, strlen(challenge2) + strlen(checksum) + 32);
+  buf = realloc_or_free(buf, strlen(challenge2) + strlen(checksum) + 32);
+  if( !buf ) goto error;
   sprintf(buf, "RealChallenge2: %s, sd=%s", challenge2, checksum);
   rtsp_schedule_field(rtsp_session, buf);
-  buf = realloc(buf, strlen(session_id) + 32);
+  buf = realloc_or_free(buf, strlen(session_id) + 32);
+  if( !buf ) goto error;
   sprintf(buf, "If-Match: %s", session_id);
   rtsp_schedule_field(rtsp_session, buf);
   rtsp_schedule_field(rtsp_session, "Transport: x-pn-tng/tcp;mode=play,rtp/avp/tcp;unicast;mode=play");
-  buf = realloc(buf, strlen(mrl) + 32);
+  buf = realloc_or_free(buf, strlen(mrl) + 32);
+  if( !buf ) goto error;
   sprintf(buf, "%s/streamid=0", mrl);
   rtsp_request_setup(rtsp_session,buf);
 
   if (h->prop->num_streams > 1) {
     rtsp_schedule_field(rtsp_session, "Transport: x-pn-tng/tcp;mode=play,rtp/avp/tcp;unicast;mode=play");
-    buf = realloc(buf, strlen(session_id) + 32);
+    buf = realloc_or_free(buf, strlen(session_id) + 32);
+    if( !buf ) goto error;
     sprintf(buf, "If-Match: %s", session_id);
     rtsp_schedule_field(rtsp_session, buf);
-    buf = realloc(buf, strlen(mrl) + 32);
+    buf = realloc_or_free(buf, strlen(mrl) + 32);
+    if( !buf ) goto error;
     sprintf(buf, "%s/streamid=1", mrl);
     rtsp_request_setup(rtsp_session,buf);
   }
@@ -729,7 +732,7 @@ rmff_header_t  *real_setup_and_get_header(rtsp_client_t *rtsp_session, int bandw
   return h;
 
 error:
-  if( h ) rmff_free_header( h );
+  rmff_free_header( h );
   free( challenge1 );
   free( session_id );
   free( description );