belle_sip_utils.c 19.2 KB
Newer Older
jehan's avatar
jehan committed
1 2 3
/*
	belle-sip - SIP (RFC3261) library.
    Copyright (C) 2010  Belledonne Communications SARL
jehan's avatar
jehan committed
4

jehan's avatar
jehan committed
5 6 7 8 9 10 11 12 13 14 15 16 17
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    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, see <http://www.gnu.org/licenses/>.
*/
18 19

#include "belle_sip_internal.h"
jehan's avatar
jehan committed
20

21
#include <time.h>
jehan's avatar
jehan committed
22
#include "clock_gettime.h" /*for apple*/
jehan's avatar
jehan committed
23 24 25 26 27 28 29 30 31 32 33 34 35 36

static FILE *__log_file=0;

/**
 *@param file a FILE pointer where to output the belle logs.
 *
**/
void belle_sip_set_log_file(FILE *file)
{
        __log_file=file;
}

static void __belle_sip_logv_out(belle_sip_log_level lev, const char *fmt, va_list args);

37
belle_sip_log_function_t belle_sip_logv_out=__belle_sip_logv_out;
jehan's avatar
jehan committed
38 39 40 41 42

/**
 *@param func: your logging function, compatible with the OrtpLogFunc prototype.
 *
**/
43
void belle_sip_set_log_handler(belle_sip_log_function_t func){
jehan's avatar
jehan committed
44 45 46 47
        belle_sip_logv_out=func;
}


Simon Morlat's avatar
Simon Morlat committed
48
unsigned int __belle_sip_log_mask=BELLE_SIP_LOG_WARNING|BELLE_SIP_LOG_ERROR|BELLE_SIP_LOG_FATAL;
jehan's avatar
jehan committed
49 50

/**
Simon Morlat's avatar
Simon Morlat committed
51 52
 * @ param level: either BELLE_SIP_LOG_DEBUG, BELLE_SIP_LOG_MESSAGE, BELLE_SIP_LOG_WARNING, BELLE_SIP_LOG_ERROR
 * BELLE_SIP_LOG_FATAL .
jehan's avatar
jehan committed
53
**/
Simon Morlat's avatar
Simon Morlat committed
54 55
void belle_sip_set_log_level(int level){
        __belle_sip_log_mask=(level<<1)-1;
jehan's avatar
jehan committed
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
}

char * belle_sip_strdup_vprintf(const char *fmt, va_list ap)
{
        /* Guess we need no more than 100 bytes. */
        int n, size = 200;
        char *p,*np;
#ifndef WIN32
        va_list cap;/*copy of our argument list: a va_list cannot be re-used (SIGSEGV on linux 64 bits)*/
#endif
        if ((p = (char *) malloc (size)) == NULL)
                return NULL;
        while (1)
        {
                /* Try to print in the allocated space. */
#ifndef WIN32
                va_copy(cap,ap);
                n = vsnprintf (p, size, fmt, cap);
                va_end(cap);
#else
                /*this works on 32 bits, luckily*/
                n = vsnprintf (p, size, fmt, ap);
#endif
                /* If that worked, return the string. */
                if (n > -1 && n < size)
                        return p;
                //printf("Reallocing space.\n");
                /* Else try again with more space. */
                if (n > -1)     /* glibc 2.1 */
                        size = n + 1;   /* precisely what is needed */
                else            /* glibc 2.0 */
                        size *= 2;      /* twice the old size */
                if ((np = (char *) realloc (p, size)) == NULL)
                  {
                    free(p);
                    return NULL;
                  }
                else
                  {
                    p = np;
                  }
        }
}

char *belle_sip_strdup_printf(const char *fmt,...){
        char *ret;
        va_list args;
        va_start (args, fmt);
        ret=belle_sip_strdup_vprintf(fmt, args);
        va_end (args);
        return ret;
}

#if     defined(WIN32) || defined(_WIN32_WCE)
#define ENDLINE "\r\n"
#else
#define ENDLINE "\n"
#endif

#if     defined(WIN32) || defined(_WIN32_WCE)
void belle_sip_logv(int level, const char *fmt, va_list args)
{
        if (belle_sip_logv_out!=NULL && belle_sip_log_level_enabled(level))
                belle_sip_logv_out(level,fmt,args);
Simon Morlat's avatar
Simon Morlat committed
120
        if ((level)==BELLE_SIP_LOG_FATAL) abort();
jehan's avatar
jehan committed
121 122 123 124 125 126 127 128
}
#endif

static void __belle_sip_logv_out(belle_sip_log_level lev, const char *fmt, va_list args){
        const char *lname="undef";
        char *msg;
        if (__log_file==NULL) __log_file=stderr;
        switch(lev){
Simon Morlat's avatar
Simon Morlat committed
129
                case BELLE_SIP_LOG_DEBUG:
jehan's avatar
jehan committed
130 131
                        lname="debug";
                        break;
Simon Morlat's avatar
Simon Morlat committed
132
                case BELLE_SIP_LOG_MESSAGE:
jehan's avatar
jehan committed
133 134
                        lname="message";
                        break;
Simon Morlat's avatar
Simon Morlat committed
135
                case BELLE_SIP_LOG_WARNING:
jehan's avatar
jehan committed
136 137
                        lname="warning";
                        break;
Simon Morlat's avatar
Simon Morlat committed
138
                case BELLE_SIP_LOG_ERROR:
jehan's avatar
jehan committed
139 140
                        lname="error";
                        break;
Simon Morlat's avatar
Simon Morlat committed
141
                case BELLE_SIP_LOG_FATAL:
jehan's avatar
jehan committed
142 143 144 145 146 147 148
                        lname="fatal";
                        break;
                default:
                        belle_sip_fatal("Bad level !");
        }
        msg=belle_sip_strdup_vprintf(fmt,args);
#if defined(_MSC_VER) && !defined(_WIN32_WCE)
149
	#ifndef _UNICODE
jehan's avatar
jehan committed
150 151
        OutputDebugString(msg);
        OutputDebugString("\r\n");
152 153 154 155 156 157 158 159 160 161
	#else
		{
			int len=strlen(msg);
			wchar_t *tmp=(wchar_t*)belle_sip_malloc((len+1)*sizeof(wchar_t));
			mbstowcs(tmp,msg,len);
			OutputDebugString(tmp);
			OutputDebugString(L"\r\n");
			belle_sip_free(tmp);
		}
	#endif
jehan's avatar
jehan committed
162 163 164 165 166 167
#endif
        fprintf(__log_file,"belle-sip-%s-%s" ENDLINE,lname,msg);
        fflush(__log_file);
        free(msg);
}

168
belle_sip_list_t* belle_sip_list_new(void *data){
169
	belle_sip_list_t* new_elem=belle_sip_new0(belle_sip_list_t);
jehan's avatar
jehan committed
170 171 172 173
	new_elem->data=data;
	return new_elem;
}

174
belle_sip_list_t*  belle_sip_list_append_link(belle_sip_list_t* elem,belle_sip_list_t *new_elem){
175
	belle_sip_list_t* it=elem;
176 177
	if (elem==NULL)  return new_elem;
	if (new_elem==NULL)  return elem;
jehan's avatar
jehan committed
178 179 180 181 182 183
	while (it->next!=NULL) it=belle_sip_list_next(it);
	it->next=new_elem;
	new_elem->prev=it;
	return elem;
}

184 185 186 187 188
belle_sip_list_t*  belle_sip_list_append(belle_sip_list_t* elem, void * data){
	belle_sip_list_t* new_elem=belle_sip_list_new(data);
	return belle_sip_list_append_link(elem,new_elem);
}

189 190
belle_sip_list_t*  belle_sip_list_prepend(belle_sip_list_t* elem, void *data){
	belle_sip_list_t* new_elem=belle_sip_list_new(data);
jehan's avatar
jehan committed
191 192 193 194 195 196 197
	if (elem!=NULL) {
		new_elem->next=elem;
		elem->prev=new_elem;
	}
	return new_elem;
}

198 199 200 201 202 203 204
belle_sip_list_t * belle_sip_list_last_elem(const belle_sip_list_t *l){
	if (l==NULL) return NULL;
	while(l->next){
		l=l->next;
	}
	return (belle_sip_list_t*)l;
}
jehan's avatar
jehan committed
205

206 207
belle_sip_list_t*  belle_sip_list_concat(belle_sip_list_t* first, belle_sip_list_t* second){
	belle_sip_list_t* it=first;
jehan's avatar
jehan committed
208
	if (it==NULL) return second;
209
	if (second==NULL) return first;
jehan's avatar
jehan committed
210 211 212 213 214 215
	while(it->next!=NULL) it=belle_sip_list_next(it);
	it->next=second;
	second->prev=it;
	return first;
}

216 217 218
belle_sip_list_t*  belle_sip_list_free(belle_sip_list_t* list){
	belle_sip_list_t* elem = list;
	belle_sip_list_t* tmp;
jehan's avatar
jehan committed
219 220 221 222
	if (list==NULL) return NULL;
	while(elem->next!=NULL) {
		tmp = elem;
		elem = elem->next;
223
		belle_sip_free(tmp);
jehan's avatar
jehan committed
224
	}
225
	belle_sip_free(elem);
jehan's avatar
jehan committed
226 227 228
	return NULL;
}

229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244
belle_sip_list_t * belle_sip_list_free_with_data(belle_sip_list_t *list, void (*freefunc)(void*)){
	belle_sip_list_t* elem = list;
	belle_sip_list_t* tmp;
	if (list==NULL) return NULL;
	while(elem->next!=NULL) {
		tmp = elem;
		elem = elem->next;
		freefunc(tmp->data);
		belle_sip_free(tmp);
	}
	freefunc(elem->data);
	belle_sip_free(elem);
	return NULL;
}


245 246
belle_sip_list_t*  belle_sip_list_remove(belle_sip_list_t* first, void *data){
	belle_sip_list_t* it;
jehan's avatar
jehan committed
247
	it=belle_sip_list_find(first,data);
Simon Morlat's avatar
Simon Morlat committed
248
	if (it) return belle_sip_list_delete_link(first,it);
jehan's avatar
jehan committed
249 250 251 252 253 254
	else {
		belle_sip_warning("belle_sip_list_remove: no element with %p data was in the list", data);
		return first;
	}
}

255
int belle_sip_list_size(const belle_sip_list_t* first){
jehan's avatar
jehan committed
256 257 258 259 260 261 262 263
	int n=0;
	while(first!=NULL){
		++n;
		first=first->next;
	}
	return n;
}

264
void belle_sip_list_for_each(const belle_sip_list_t* list, void (*func)(void *)){
jehan's avatar
jehan committed
265 266 267 268 269
	for(;list!=NULL;list=list->next){
		func(list->data);
	}
}

270
void belle_sip_list_for_each2(const belle_sip_list_t* list, void (*func)(void *, void *), void *user_data){
jehan's avatar
jehan committed
271 272 273 274 275
	for(;list!=NULL;list=list->next){
		func(list->data,user_data);
	}
}

276 277
belle_sip_list_t* belle_sip_list_remove_link(belle_sip_list_t* list, belle_sip_list_t* elem){
	belle_sip_list_t* ret;
jehan's avatar
jehan committed
278 279 280 281 282 283 284 285 286 287 288 289 290 291
	if (elem==list){
		ret=elem->next;
		elem->prev=NULL;
		elem->next=NULL;
		if (ret!=NULL) ret->prev=NULL;
		return ret;
	}
	elem->prev->next=elem->next;
	if (elem->next!=NULL) elem->next->prev=elem->prev;
	elem->next=NULL;
	elem->prev=NULL;
	return list;
}

Simon Morlat's avatar
Simon Morlat committed
292 293
belle_sip_list_t * belle_sip_list_delete_link(belle_sip_list_t* list, belle_sip_list_t* elem){
	belle_sip_list_t *ret=belle_sip_list_remove_link(list,elem);
294
	belle_sip_free(elem);
Simon Morlat's avatar
Simon Morlat committed
295 296 297
	return ret;
}

298
belle_sip_list_t* belle_sip_list_find(belle_sip_list_t* list, void *data){
jehan's avatar
jehan committed
299 300 301 302 303 304
	for(;list!=NULL;list=list->next){
		if (list->data==data) return list;
	}
	return NULL;
}

305
belle_sip_list_t* belle_sip_list_find_custom(belle_sip_list_t* list, belle_sip_compare_func compare_func, const void *user_data){
jehan's avatar
jehan committed
306 307 308 309 310 311
	for(;list!=NULL;list=list->next){
		if (compare_func(list->data,user_data)==0) return list;
	}
	return NULL;
}

Simon Morlat's avatar
Simon Morlat committed
312 313 314 315 316 317 318 319
belle_sip_list_t *belle_sip_list_delete_custom(belle_sip_list_t *list, belle_sip_compare_func compare_func, const void *user_data){
	belle_sip_list_t *elem=belle_sip_list_find_custom(list,compare_func,user_data);
	if (elem!=NULL){
		list=belle_sip_list_delete_link(list,elem);
	}
	return list;
}

320
void * belle_sip_list_nth_data(const belle_sip_list_t* list, int index){
jehan's avatar
jehan committed
321 322 323 324 325 326 327 328
	int i;
	for(i=0;list!=NULL;list=list->next,++i){
		if (i==index) return list->data;
	}
	belle_sip_error("belle_sip_list_nth_data: no such index in list.");
	return NULL;
}

329
int belle_sip_list_position(const belle_sip_list_t* list, belle_sip_list_t* elem){
jehan's avatar
jehan committed
330 331 332 333 334 335 336 337
	int i;
	for(i=0;list!=NULL;list=list->next,++i){
		if (elem==list) return i;
	}
	belle_sip_error("belle_sip_list_position: no such element in list.");
	return -1;
}

338
int belle_sip_list_index(const belle_sip_list_t* list, void *data){
jehan's avatar
jehan committed
339 340 341 342 343 344 345 346
	int i;
	for(i=0;list!=NULL;list=list->next,++i){
		if (data==list->data) return i;
	}
	belle_sip_error("belle_sip_list_index: no such element in list.");
	return -1;
}

347 348 349 350
belle_sip_list_t* belle_sip_list_insert_sorted(belle_sip_list_t* list, void *data, int (*compare_func)(const void *, const void*)){
	belle_sip_list_t* it,*previt=NULL;
	belle_sip_list_t* nelem;
	belle_sip_list_t* ret=list;
jehan's avatar
jehan committed
351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373
	if (list==NULL) return belle_sip_list_append(list,data);
	else{
		nelem=belle_sip_list_new(data);
		for(it=list;it!=NULL;it=it->next){
			previt=it;
			if (compare_func(data,it->data)<=0){
				nelem->prev=it->prev;
				nelem->next=it;
				if (it->prev!=NULL)
					it->prev->next=nelem;
				else{
					ret=nelem;
				}
				it->prev=nelem;
				return ret;
			}
		}
		previt->next=nelem;
		nelem->prev=previt;
	}
	return ret;
}

374 375
belle_sip_list_t* belle_sip_list_insert(belle_sip_list_t* list, belle_sip_list_t* before, void *data){
	belle_sip_list_t* elem;
jehan's avatar
jehan committed
376 377 378 379 380 381
	if (list==NULL || before==NULL) return belle_sip_list_append(list,data);
	for(elem=list;elem!=NULL;elem=belle_sip_list_next(elem)){
		if (elem==before){
			if (elem->prev==NULL)
				return belle_sip_list_prepend(list,data);
			else{
382
				belle_sip_list_t* nelem=belle_sip_list_new(data);
jehan's avatar
jehan committed
383 384 385 386 387 388 389 390 391 392
				nelem->prev=elem->prev;
				nelem->next=elem;
				elem->prev->next=nelem;
				elem->prev=nelem;
			}
		}
	}
	return list;
}

393 394 395
belle_sip_list_t* belle_sip_list_copy(const belle_sip_list_t* list){
	belle_sip_list_t* copy=NULL;
	const belle_sip_list_t* iter;
jehan's avatar
jehan committed
396 397 398 399 400 401
	for(iter=list;iter!=NULL;iter=belle_sip_list_next(iter)){
		copy=belle_sip_list_append(copy,iter->data);
	}
	return copy;
}

jehan's avatar
jehan committed
402 403 404 405 406 407 408 409 410 411
belle_sip_list_t* belle_sip_list_copy_with_data(const belle_sip_list_t* list, void* (*copyfunc)(void*)){
	belle_sip_list_t* copy=NULL;
	const belle_sip_list_t* iter;
	for(iter=list;iter!=NULL;iter=belle_sip_list_next(iter)){
		copy=belle_sip_list_append(copy,copyfunc(iter->data));
	}
	return copy;
}


jehan's avatar
jehan committed
412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456
char * belle_sip_concat (const char *str, ...) {
  va_list ap;
  size_t allocated = 100;
  char *result = (char *) malloc (allocated);

  if (result != NULL)
    {
      char *newp;
      char *wp;
      const char* s;

      va_start (ap, str);

      wp = result;
      for (s = str; s != NULL; s = va_arg (ap, const char *)) {
          size_t len = strlen (s);

          /* Resize the allocated memory if necessary.  */
          if (wp + len + 1 > result + allocated)
            {
              allocated = (allocated + len) * 2;
              newp = (char *) realloc (result, allocated);
              if (newp == NULL)
                {
                  free (result);
                  return NULL;
                }
              wp = newp + (wp - result);
              result = newp;
            }
          memcpy (wp, s, len);
          wp +=len;
        }

      /* Terminate the result string.  */
      *wp++ = '\0';

      /* Resize memory to the optimal size.  */
      newp = realloc (result, wp - result);
      if (newp != NULL)
        result = newp;

      va_end (ap);
    }

457
	return result;
jehan's avatar
jehan committed
458
}
459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477

void *belle_sip_malloc(size_t size){
	return malloc(size);
}

void *belle_sip_malloc0(size_t size){
	void *p=malloc(size);
	memset(p,0,size);
	return p;
}

void *belle_sip_realloc(void *ptr, size_t size){
	return realloc(ptr,size);
}

void belle_sip_free(void *ptr){
	free(ptr);
}

Simon Morlat's avatar
Simon Morlat committed
478 479 480 481
char * belle_sip_strdup(const char *s){
	return strdup(s);
}

Simon Morlat's avatar
Simon Morlat committed
482
#ifndef WIN32
483 484 485 486 487 488 489 490 491 492 493 494 495
uint64_t belle_sip_time_ms(void){
	struct timespec ts;
	static int clock_id=CLOCK_MONOTONIC;
	if (clock_gettime(clock_id,&ts)==-1){
		belle_sip_error("clock_gettime() error for clock_id=%i: %s",clock_id,strerror(errno));
		if (clock_id==CLOCK_MONOTONIC){
			clock_id=CLOCK_REALTIME;
			return belle_sip_time_ms();
		}
		return 0;
	}
	return (ts.tv_sec*1000LL) + (ts.tv_nsec/1000000LL);
}
Simon Morlat's avatar
Simon Morlat committed
496 497
#else
uint64_t belle_sip_time_ms(void){
498
#ifdef WINAPI_FAMILY_PHONE_APP
Ghislain MARY's avatar
Ghislain MARY committed
499 500
	return GetTickCount64();
#else
Simon Morlat's avatar
Simon Morlat committed
501
	return GetTickCount();
Ghislain MARY's avatar
Ghislain MARY committed
502
#endif
Simon Morlat's avatar
Simon Morlat committed
503 504
}
#endif
505

jehan's avatar
jehan committed
506 507 508 509 510 511 512 513
/**
 * parser parameter pair
 */



belle_sip_param_pair_t* belle_sip_param_pair_new(const char* name,const char* value) {
	belle_sip_param_pair_t* lPair = (belle_sip_param_pair_t*)belle_sip_new0(belle_sip_param_pair_t);
jehan's avatar
jehan committed
514 515
	lPair->name=name?belle_sip_strdup(name):NULL;
	lPair->value=value?belle_sip_strdup(value):NULL;
jehan's avatar
jehan committed
516 517 518
	return lPair;
}

jehan's avatar
jehan committed
519 520 521 522
void belle_sip_param_pair_destroy(belle_sip_param_pair_t*  pair) {
	if (pair->name) belle_sip_free(pair->name);
	if (pair->value) belle_sip_free(pair->value);
	belle_sip_free (pair);
jehan's avatar
jehan committed
523 524 525 526 527
}

int belle_sip_param_pair_comp_func(const belle_sip_param_pair_t *a, const char*b) {
	return strcmp(a->name,b);
}
jehan's avatar
jehan committed
528 529 530
int belle_sip_param_pair_case_comp_func(const belle_sip_param_pair_t *a, const char*b) {
	return strcasecmp(a->name,b);
}
jehan's avatar
jehan committed
531

532
char* _belle_sip_str_dup_and_unquote_string(const char* quoted_string) {
533 534 535 536 537
	size_t value_size = strlen(quoted_string);
	char* unquoted_string = belle_sip_malloc0(value_size-2+1);
	strncpy(unquoted_string,quoted_string+1,value_size-2);
	return unquoted_string;
}
jehan's avatar
jehan committed
538

Simon Morlat's avatar
Simon Morlat committed
539
unsigned int belle_sip_random(void){
Simon Morlat's avatar
Simon Morlat committed
540
#if  defined(__linux) || defined(__APPLE__)
Simon Morlat's avatar
Simon Morlat committed
541 542 543 544 545 546 547 548
	static int fd=-1;
	if (fd==-1) fd=open("/dev/urandom",O_RDONLY);
	if (fd!=-1){
		unsigned int tmp;
		if (read(fd,&tmp,4)!=4){
			belle_sip_error("Reading /dev/urandom failed.");
		}else return tmp;
	}else belle_sip_error("Could not open /dev/urandom");
Simon Morlat's avatar
Simon Morlat committed
549 550 551
#elif defined(WIN32)
	static int initd=0;
	if (!initd) {
552
		srand((unsigned int)belle_sip_time_ms());
Simon Morlat's avatar
Simon Morlat committed
553 554 555
		initd=1;
	}
	return rand()<<16 | rand();
Simon Morlat's avatar
Simon Morlat committed
556 557 558
#endif
	/*fallback to random()*/
#ifndef WIN32
Simon Morlat's avatar
Simon Morlat committed
559
	return (unsigned int) random();
Simon Morlat's avatar
Simon Morlat committed
560
#endif
Simon Morlat's avatar
Simon Morlat committed
561 562
}

Simon Morlat's avatar
Simon Morlat committed
563 564
static const char *symbols="aAbBcCdDeEfFgGhHiIjJkKlLmMnNoOpPqQrRsStTuUvVwWxXyYzZ0123456789-~";

Simon Morlat's avatar
Simon Morlat committed
565 566 567 568 569
/**
 * Write a random text token of supplied size.
**/
char * belle_sip_random_token(char *ret, size_t size){
	unsigned int val;
570
	unsigned int i,j;
Simon Morlat's avatar
Simon Morlat committed
571
	for(i=0,j=0;i<size-1;++i,++j){
Simon Morlat's avatar
Simon Morlat committed
572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593
		if (j%5==0) val=belle_sip_random();
		ret[i]=symbols[val & 63];
		val=val>>6;
	}
	ret[i]=0;
	return ret;
}

typedef struct bits_reader{
	const uint8_t *buffer;
	size_t buf_size;
	int bit_index;
}bits_reader_t;

static void bits_reader_init(bits_reader_t *reader, const uint8_t *buffer, size_t bufsize){
	reader->buffer=buffer;
	reader->buf_size=bufsize;
	reader->bit_index=0;
}

static int bits_reader_read(bits_reader_t *reader, int count, unsigned int *ret){
	unsigned int tmp;
594 595
	size_t byte_index=reader->bit_index/8;
	size_t bit_index=reader->bit_index % 8;
Simon Morlat's avatar
Simon Morlat committed
596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628
	int shift=32-bit_index-count;
	
	if (count>=24){
		belle_sip_error("This bit reader cannot read more than 24 bits at once.");
		return -1;
	}
	
	if (byte_index<reader->buf_size)
		tmp=((unsigned int)reader->buffer[byte_index++])<<24;
	else{
		belle_sip_error("Bit reader goes end of stream.");
		return -1;
	}
	if (byte_index<reader->buf_size)
		tmp|=((unsigned int)reader->buffer[byte_index++])<<16;
	if (byte_index<reader->buf_size)
		tmp|=((unsigned int)reader->buffer[byte_index++])<<8;
	if (byte_index<reader->buf_size)
		tmp|=((unsigned int)reader->buffer[byte_index++]);
	
	tmp=tmp>>shift;
	tmp=tmp & ((1<<count)-1);
	reader->bit_index+=count;
	*ret=tmp;
	return 0;
}

char * belle_sip_octets_to_text(const uint8_t *hash, size_t hash_len, char *ret, size_t size){
	int i;
	bits_reader_t bitctx;
	
	bits_reader_init(&bitctx,hash,hash_len);
	
629
	for(i=0;i<(int)size-1;++i){
Simon Morlat's avatar
Simon Morlat committed
630 631 632 633
		unsigned int val=0;
		if (bits_reader_read(&bitctx,6,&val)==0){
			ret[i]=symbols[val];
		}else break;
Simon Morlat's avatar
Simon Morlat committed
634 635 636 637 638
	}
	ret[i]=0;
	return ret;
}

639 640 641 642 643 644 645 646 647 648 649 650 651 652 653
void belle_sip_util_copy_headers(belle_sip_message_t *orig, belle_sip_message_t *dest, const char*header, int multiple){
	const belle_sip_list_t *elem;
	elem=belle_sip_message_get_headers(orig,header);
	for (;elem!=NULL;elem=elem->next){
		belle_sip_header_t *ref_header=(belle_sip_header_t*)elem->data;
		if (ref_header){
			ref_header=(belle_sip_header_t*)belle_sip_object_clone((belle_sip_object_t*)ref_header);
			if (!multiple){
				belle_sip_message_set_header(dest,ref_header);
				break;
			}else
				belle_sip_message_add_header(dest,ref_header);
		}
	}
}
jehan's avatar
jehan committed
654

jehan's avatar
jehan committed
655 656
int belle_sip_get_char (const char*a,int n,char*out) {
	char result;
657
	unsigned int tmp;
jehan's avatar
jehan committed
658
	if (*a=='%' && n>2) {
659 660
		sscanf(a+1,"%02x",&tmp);
		*out=(char)tmp;
jehan's avatar
jehan committed
661 662 663 664 665 666 667
		return 3;
	} else {
		*out=*a;
		return 1;
	}
return result;
}
668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726
char* belle_sip_to_unescaped_string(const char* buff) {
	char output_buff[BELLE_SIP_MAX_TO_STRING_SIZE];
	unsigned int i;
	unsigned int out_buff_index=0;
	output_buff[BELLE_SIP_MAX_TO_STRING_SIZE-1]='\0';
	for(i=0;buff[i]!='\0' &&i<BELLE_SIP_MAX_TO_STRING_SIZE /*to make sure last param can be stored in escaped form*/;) {
		i+=belle_sip_get_char(buff+i,3,output_buff+out_buff_index++);
	}
	output_buff[out_buff_index]='\0';
	return belle_sip_strdup(output_buff);
}
char* belle_sip_to_escaped_string(const char* buff) {
	char output_buff[BELLE_SIP_MAX_TO_STRING_SIZE];
	unsigned int i;
	unsigned int out_buff_index=0;
	output_buff[BELLE_SIP_MAX_TO_STRING_SIZE-1]='\0';
	for(i=0;buff[i]!='\0' &&i<BELLE_SIP_MAX_TO_STRING_SIZE-4 /*to make sure last param can be stored in escaped form*/;i++) {

		/*hvalue          =  *( hnv-unreserved / unreserved / escaped )
		hnv-unreserved  =  "[" / "]" / "/" / "?" / ":" / "+" / "$"
		unreserved  =  alphanum / mark
      	mark        =  "-" / "_" / "." / "!" / "~" / "*" / "'"
                     / "(" / ")"*/

		switch(buff[i]) {
		case '[' :
		case ']' :
		case '/' :
		case '?' :
		case ':' :
		case '+' :
		case '$' :
		case '-' :
		case '_' :
		case '.' :
		case '!' :
		case '~' :
		case '*' :
		case '\'' :
		case '(' :
		case ')' :
			output_buff[out_buff_index++]=buff[i];
			break;
		default:
			/*serach for alfanum*/
			if ((buff[i]>='0' && buff[i]<='9')
				|| (buff[i]>='A' && buff[i]<='Z')
				|| (buff[i]>='a' && buff[i]<='z')
				|| (buff[i]=='\0')) {
				output_buff[out_buff_index++]=buff[i];
			} else {
				out_buff_index+=sprintf(output_buff+out_buff_index,"%%%02x",buff[i]);
			}
			break;
		}
	}
	output_buff[out_buff_index]='\0';
	return belle_sip_strdup(output_buff);
}