ut.h 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905
  1. /*
  2. *$Id$
  3. *
  4. * - various general purpose functions
  5. *
  6. * Copyright (C) 2001-2003 FhG Fokus
  7. *
  8. * Permission to use, copy, modify, and distribute this software for any
  9. * purpose with or without fee is hereby granted, provided that the above
  10. * copyright notice and this permission notice appear in all copies.
  11. *
  12. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  13. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  14. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  15. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  16. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  17. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  18. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  19. *
  20. * History
  21. * ------
  22. * 2003-01-18 un_escape function introduced for convenience of code needing
  23. * the complex&slow feature of unescaping
  24. * 2003-01-28 scratchpad removed (jiri)
  25. * 2003-01-29 pathmax added (jiri)
  26. * 2003-02-13 strlower added (janakj)
  27. * 2003-02-28 scratchpad compatibility abandoned (jiri)
  28. * 2003-03-30 str2int and str2float added (janakj)
  29. * 2003-04-26 ZSW (jiri)
  30. * 2004-03-08 updated int2str (64 bits, INT2STR_MAX_LEN used) (andrei)
  31. * 2005-11-29 reverse_hex2int/int2reverse_hex switched to unsigned int (andrei)
  32. * 2005-12-09 added msgid_var (andrei)
  33. * 2007-05-14 added get_sys_ver() (andrei)
  34. * 2007-06-05 added MAX_UVAR_VALUE(), MAX_int(a,b) MIN_int(a,b) (andrei)
  35. * 2008-05-21 added ushort2sbuf(), ushort2str() (andrei)
  36. * 2009-03-16 added sint2strbuf() and incremented INST2STR_MAX_LEN to account
  37. * for sign (andrei)
  38. */
  39. /** various general purpose/helper functions.
  40. * @file
  41. */
  42. #ifndef ut_h
  43. #define ut_h
  44. #include "comp_defs.h"
  45. #include <sys/types.h>
  46. #include <sys/select.h>
  47. #include <sys/time.h>
  48. #include <limits.h>
  49. #include <time.h>
  50. #include <unistd.h>
  51. #include <ctype.h>
  52. #include <string.h>
  53. #include <strings.h>
  54. #include "compiler_opt.h"
  55. #include "config.h"
  56. #include "dprint.h"
  57. #include "str.h"
  58. #include "mem/mem.h"
  59. #include "mem/shm_mem.h"
  60. /* zero-string wrapper */
  61. #define ZSW(_c) ((_c)?(_c):"")
  62. /* returns string beginning and length without insignificant chars */
  63. #define trim_len( _len, _begin, _mystr ) \
  64. do{ static char _c; \
  65. (_len)=(_mystr).len; \
  66. while ((_len) && ((_c=(_mystr).s[(_len)-1])==0 || _c=='\r' || \
  67. _c=='\n' || _c==' ' || _c=='\t' )) \
  68. (_len)--; \
  69. (_begin)=(_mystr).s; \
  70. while ((_len) && ((_c=*(_begin))==' ' || _c=='\t')) { \
  71. (_len)--;\
  72. (_begin)++; \
  73. } \
  74. }while(0)
  75. #define trim_r( _mystr ) \
  76. do{ static char _c; \
  77. while( ((_mystr).len) && ( ((_c=(_mystr).s[(_mystr).len-1]))==0 ||\
  78. _c=='\r' || _c=='\n' ) \
  79. ) \
  80. (_mystr).len--; \
  81. }while(0)
  82. #define translate_pointer( _new_buf , _org_buf , _p) \
  83. ( (_p)?(_new_buf + (_p-_org_buf)):(0) )
  84. #define via_len(_via) \
  85. ((_via)->bsize-((_via)->name.s-\
  86. ((_via)->hdr.s+(_via)->hdr.len)))
  87. /* rounds to sizeof(type), but type must have a 2^k size (e.g. short, int,
  88. * long, void*) */
  89. #define ROUND2TYPE(s, type) \
  90. (((s)+(sizeof(type)-1))&(~(sizeof(type)-1)))
  91. /* rounds to sizeof(char*) - the first 4 byte multiple on 32 bit archs
  92. * and the first 8 byte multiple on 64 bit archs */
  93. #define ROUND_POINTER(s) ROUND2TYPE(s, char*)
  94. /* rounds to sizeof(long) - the first 4 byte multiple on 32 bit archs
  95. * and the first 8 byte multiple on 64 bit archs (equiv. to ROUND_POINTER)*/
  96. #define ROUND_LONG(s) ROUND2TYPE(s, long)
  97. /* rounds to sizeof(int) - the first t byte multiple on 32 and 64 bit archs */
  98. #define ROUND_INT(s) ROUND2TYPE(s, int)
  99. /* rounds to sizeof(short) - the first 2 byte multiple */
  100. #define ROUND_SHORT(s) ROUND2TYPE(s, short)
  101. /* params: v - either a variable name, structure member or a type
  102. * returns an unsigned long containing the maximum possible value that will
  103. * fit in v, if v is unsigned or converted to an unsigned version
  104. * example: MAX_UVAR_VALUE(unsigned short); MAX_UVAR_VALUE(i);
  105. * MAX_UVAR_VALUE(((struct foo*)0)->bar) */
  106. #define MAX_UVAR_VALUE(v) \
  107. (((unsigned long)(-1))>>((sizeof(unsigned long)-sizeof(v))*8UL))
  108. #define MIN_int(a, b) (((a)<(b))?(a):(b))
  109. #define MAX_int(a, b) (((a)>(b))?(a):(b))
  110. #define MIN_unsigned(a, b) (unsigned)(((unsigned)(a)<(unsigned)(b))?(a):(b))
  111. #define MAX_unsigned(a, b) (unsigned)(((unsigned)(a)>(unsigned)(b))?(a):(b))
  112. #if 0
  113. #define MIN_int(a, b) ((b)+(((a)-(b))& -((a)<(b))))
  114. #define MAX_int(a, b) ((a)-(((a)-(b))& -((b)>(a))))
  115. /* depend on signed right shift result which depends on the compiler */
  116. #define MIN_int(a, b) ((b)+(((a)-(b))&(((a)-(b))>>(sizeof(int)*8-1))))
  117. #define MAX_int(a, b) ((a)-(((a)-(b))&(((a)-(b))>>(sizeof(int)*8-1))))
  118. #endif
  119. #define append_str(_dest,_src,_len) \
  120. do{ \
  121. memcpy( (_dest) , (_src) , (_len) ); \
  122. (_dest) += (_len) ; \
  123. }while(0); \
  124. /*! append _c char to _dest string */
  125. #define append_chr(_dest,_c) \
  126. *((_dest)++) = _c;
  127. #define is_in_str(p, in) (p < in->s + in->len && *p)
  128. /* links a value to a msgid */
  129. struct msgid_var{
  130. union{
  131. char char_val;
  132. int int_val;
  133. long long_val;
  134. }u;
  135. unsigned int msgid;
  136. };
  137. /* return the value or 0 if the msg_id doesn't match */
  138. #define get_msgid_val(var, id, type)\
  139. ((type)((type)((var).msgid!=(id))-1)&((var).u.type##_val))
  140. #define set_msgid_val(var, id, type, value)\
  141. do{\
  142. (var).msgid=(id); \
  143. (var).u.type##_val=(value); \
  144. }while(0)
  145. /* char to hex conversion table */
  146. static char fourbits2char[16] = { '0', '1', '2', '3', '4', '5',
  147. '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
  148. /* converts a str to an u. short, returns the u. short and sets *err on
  149. * error and if err!=null
  150. */
  151. static inline unsigned short str2s(const char* s, unsigned int len,
  152. int *err)
  153. {
  154. unsigned short ret;
  155. int i;
  156. unsigned char *limit;
  157. unsigned char* str;
  158. /*init*/
  159. str=(unsigned char*)s;
  160. ret=i=0;
  161. limit=str+len;
  162. for(;str<limit ;str++){
  163. if ( (*str <= '9' ) && (*str >= '0') ){
  164. ret=ret*10+*str-'0';
  165. i++;
  166. if (i>5) goto error_digits;
  167. }else{
  168. /* error unknown char */
  169. goto error_char;
  170. }
  171. }
  172. if (err) *err=0;
  173. return ret;
  174. error_digits:
  175. if (err) *err=1;
  176. return 0;
  177. error_char:
  178. if (err) *err=1;
  179. return 0;
  180. }
  181. static inline int btostr( char *p, unsigned char val)
  182. {
  183. unsigned int a,b,i =0;
  184. if ( (a=val/100)!=0 )
  185. *(p+(i++)) = a+'0'; /*first digit*/
  186. if ( (b=val%100/10)!=0 || a)
  187. *(p+(i++)) = b+'0'; /*second digit*/
  188. *(p+(i++)) = '0'+val%10; /*third digit*/
  189. return i;
  190. }
  191. #define INT2STR_MAX_LEN (19+1+1+1) /* 2^64~= 16*10^18 =>
  192. 19+1 digits + sign + \0 */
  193. /*
  194. * returns a pointer to a static buffer containing l in asciiz (with base "base") & sets len
  195. * left padded with 0 to "size"
  196. */
  197. static inline char* int2str_base_0pad(unsigned int l, int* len, int base,
  198. int size)
  199. {
  200. static char r[INT2STR_MAX_LEN];
  201. int i, j;
  202. if (base < 2) {
  203. BUG("base underflow\n");
  204. return NULL;
  205. }
  206. if (base > 36) {
  207. BUG("base overflow\n");
  208. return NULL;
  209. }
  210. i=INT2STR_MAX_LEN-2;
  211. j=i-size;
  212. r[INT2STR_MAX_LEN-1]=0; /* null terminate */
  213. do{
  214. r[i]=l%base;
  215. if (r[i]<10)
  216. r[i]+='0';
  217. else
  218. r[i]+='a'-10;
  219. i--;
  220. l/=base;
  221. }while((l || i>j) && (i>=0));
  222. if (l && (i<0)){
  223. BUG("result buffer overflow\n");
  224. }
  225. if (len) *len=(INT2STR_MAX_LEN-2)-i;
  226. return &r[i+1];
  227. }
  228. /* returns a pointer to a static buffer containing l in asciiz (with base "base") & sets len */
  229. static inline char* int2str_base(unsigned int l, int* len, int base)
  230. {
  231. return int2str_base_0pad(l, len, base, 0);
  232. }
  233. /** unsigned long to str conversion using a provided buffer.
  234. * Converts/prints an unsigned long to a string. The result buffer must be
  235. * provided and its length must be at least INT2STR_MAX_LEN.
  236. * @param l - unsigned long to be converted
  237. * @param r - pointer to result buffer
  238. * @param r_size - result buffer size, must be at least INT2STR_MAX_LEN.
  239. * @param *len - length of the written string, _without_ the terminating 0.
  240. * @return pointer _inside_ r, to the converted string (note: the string
  241. * is written from the end of the buffer and not from the start and hence
  242. * the returned pointer will most likely not be equal to r). In case of error
  243. * it returns 0 (the only error being insufficient provided buffer size).
  244. */
  245. static inline char* int2strbuf(unsigned long l, char *r, int r_size, int* len)
  246. {
  247. int i;
  248. if(unlikely(r_size<INT2STR_MAX_LEN)) {
  249. if (len)
  250. *len = 0;
  251. return 0; /* => if someone misuses it => crash (feature no. 1) */
  252. }
  253. i=INT2STR_MAX_LEN-2;
  254. r[INT2STR_MAX_LEN-1]=0; /* null terminate */
  255. do{
  256. r[i]=l%10+'0';
  257. i--;
  258. l/=10;
  259. }while(l && (i>=0));
  260. if (l && (i<0)){
  261. LOG(L_CRIT, "BUG: int2str: overflow\n");
  262. }
  263. if (len) *len=(INT2STR_MAX_LEN-2)-i;
  264. return &r[i+1];
  265. }
  266. extern char ut_buf_int2str[INT2STR_MAX_LEN];
  267. /** interger(long) to string conversion.
  268. * This version uses a static buffer (shared with sint2str()).
  269. * WARNING: other function calls might overwrite the static buffer, so
  270. * either always save the result immediately or use int2strbuf(...).
  271. * @param l - unsigned long to be converted/printed.
  272. * @param *len - will be filled with the final length (without the terminating
  273. * 0).
  274. * @return a pointer to a static buffer containing l in asciiz & sets len.
  275. */
  276. static inline char* int2str(unsigned long l, int* len)
  277. {
  278. return int2strbuf(l, ut_buf_int2str, INT2STR_MAX_LEN, len);
  279. }
  280. /** signed long to str conversion using a provided buffer.
  281. * Converts a long to a signed string. The result buffer must be provided
  282. * and its length must be at least INT2STR_MAX_LEN.
  283. * @param l - long to be converted
  284. * @param r - pointer to result buffer
  285. * @param r_size - result buffer size, must be at least INT2STR_MAX_LEN.
  286. * @param *len - length of the written string, _without_ the terminating 0.
  287. * @return pointer _inside_ r, to the converted string (note: the string
  288. * is written from the end of the buffer and not from the start and hence
  289. * the returned pointer will most likely not be equal to r). In case of error
  290. * it returns 0 (the only error being insufficient provided buffer size).
  291. */
  292. static inline char* sint2strbuf(long l, char* r, int r_size, int* len)
  293. {
  294. int sign;
  295. char *p;
  296. int p_len;
  297. sign = 0;
  298. if(l<0) {
  299. sign = 1;
  300. l = -l;
  301. }
  302. p = int2strbuf((unsigned long)l, r, r_size, &p_len);
  303. if(sign && p_len<(r_size-1)) {
  304. *(--p) = '-';
  305. p_len++;;
  306. }
  307. if (likely(len))
  308. *len = p_len;
  309. return p;
  310. }
  311. /** Signed INTeger-TO-STRing: converts a long to a string.
  312. * This version uses a static buffer, shared with int2str().
  313. * WARNING: other function calls might overwrite the static buffer, so
  314. * either always save the result immediately or use sint2strbuf(...).
  315. * @param l - long to be converted/printed.
  316. * @param *len - will be filled with the final length (without the terminating
  317. * 0).
  318. * @return a pointer to a static buffer containing l in asciiz & sets len.
  319. */
  320. static inline char* sint2str(long l, int* len)
  321. {
  322. return sint2strbuf(l, ut_buf_int2str, INT2STR_MAX_LEN, len);
  323. }
  324. #define USHORT2SBUF_MAX_LEN 5 /* 65535*/
  325. /* converts an unsigned short (16 bits) to asciiz
  326. * returns bytes written or 0 on error
  327. * the passed len must be at least USHORT2SBUF_MAX chars or error
  328. * would be returned.
  329. * (optimized for port conversion (4 or 5 digits most of the time)*/
  330. static inline int ushort2sbuf(unsigned short u, char* buf, int len)
  331. {
  332. int offs;
  333. unsigned char a, b, c, d;
  334. if (unlikely(len<USHORT2SBUF_MAX_LEN))
  335. return 0;
  336. offs=0;
  337. a=u/10000; u%=10000;
  338. buf[offs]=a+'0'; offs+=(a!=0);
  339. b=u/1000; u%=1000;
  340. buf[offs]=b+'0'; offs+=((offs|b)!=0);
  341. c=u/100; u%=100;
  342. buf[offs]=c+'0'; offs+=((offs|c)!=0);
  343. d=u/10; u%=10;
  344. buf[offs]=d+'0'; offs+=((offs|d)!=0);
  345. buf[offs]=(unsigned char)u+'0';
  346. return offs+1;
  347. }
  348. #define USHORT2STR_MAX_LEN (USHORT2SBUF_MAX_LEN+1) /* 65535\0*/
  349. /* converts an unsigned short (16 bits) to asciiz
  350. * (optimized for port conversiob (4 or 5 digits most of the time)*/
  351. static inline char* ushort2str(unsigned short u)
  352. {
  353. static char buf[USHORT2STR_MAX_LEN];
  354. int len;
  355. len=ushort2sbuf(u, buf, sizeof(buf)-1);
  356. buf[len]=0;
  357. return buf;
  358. }
  359. /* fast memchr version */
  360. static inline char* q_memchr(char* p, int c, unsigned int size)
  361. {
  362. char* end;
  363. end=p+size;
  364. for(;p<end;p++){
  365. if (*p==(unsigned char)c) return p;
  366. }
  367. return 0;
  368. }
  369. /* fast reverse char search */
  370. static inline char* q_memrchr(char* p, int c, unsigned int size)
  371. {
  372. char* end;
  373. end=p+size-1;
  374. for(;end>=p;end--) {
  375. if (*end==(unsigned char)c) return end;
  376. }
  377. return 0;
  378. }
  379. /* returns -1 on error, 1! on success (consistent with int2reverse_hex) */
  380. inline static int reverse_hex2int( char *c, int len, unsigned int* res)
  381. {
  382. char *pc;
  383. char mychar;
  384. *res=0;
  385. for (pc=c+len-1; len>0; pc--, len--) {
  386. *res <<= 4 ;
  387. mychar=*pc;
  388. if ( mychar >='0' && mychar <='9') *res+=mychar -'0';
  389. else if (mychar >='a' && mychar <='f') *res+=mychar -'a'+10;
  390. else if (mychar >='A' && mychar <='F') *res+=mychar -'A'+10;
  391. else return -1;
  392. }
  393. return 1;
  394. }
  395. inline static int int2reverse_hex( char **c, int *size, unsigned int nr )
  396. {
  397. unsigned short digit;
  398. if (*size && nr==0) {
  399. **c = '0';
  400. (*c)++;
  401. (*size)--;
  402. return 1;
  403. }
  404. while (*size && nr ) {
  405. digit = nr & 0xf ;
  406. **c= digit >= 10 ? digit + 'a' - 10 : digit + '0';
  407. nr >>= 4;
  408. (*c)++;
  409. (*size)--;
  410. }
  411. return nr ? -1 /* number not processed; too little space */ : 1;
  412. }
  413. /* double output length assumed ; does NOT zero-terminate */
  414. inline static int string2hex(
  415. /* input */ unsigned char *str, int len,
  416. /* output */ char *hex )
  417. {
  418. int orig_len;
  419. if (len==0) {
  420. *hex='0';
  421. return 1;
  422. }
  423. orig_len=len;
  424. while ( len ) {
  425. *hex=fourbits2char[(*str) >> 4];
  426. hex++;
  427. *hex=fourbits2char[(*str) & 0xf];
  428. hex++;
  429. len--;
  430. str++;
  431. }
  432. return orig_len-len;
  433. }
  434. /* portable sleep in microseconds (no interrupt handling now) */
  435. inline static void sleep_us( unsigned int nusecs )
  436. {
  437. struct timeval tval;
  438. tval.tv_sec =nusecs/1000000;
  439. tval.tv_usec=nusecs%1000000;
  440. select(0, NULL, NULL, NULL, &tval );
  441. }
  442. /* portable determination of max_path */
  443. inline static int pathmax(void)
  444. {
  445. #ifdef PATH_MAX
  446. static int pathmax=PATH_MAX;
  447. #else
  448. static int pathmax=0;
  449. #endif
  450. if (pathmax==0) { /* init */
  451. pathmax=pathconf("/", _PC_PATH_MAX);
  452. pathmax=(pathmax<=0)?PATH_MAX_GUESS:pathmax+1;
  453. }
  454. return pathmax;
  455. }
  456. inline static int hex2int(char hex_digit)
  457. {
  458. if (hex_digit>='0' && hex_digit<='9')
  459. return hex_digit-'0';
  460. if (hex_digit>='a' && hex_digit<='f')
  461. return hex_digit-'a'+10;
  462. if (hex_digit>='A' && hex_digit<='F')
  463. return hex_digit-'A'+10;
  464. /* no valid hex digit ... */
  465. LOG(L_ERR, "ERROR: hex2int: '%c' is no hex char\n", hex_digit );
  466. return -1;
  467. }
  468. /* Un-escape URI user -- it takes a pointer to original user
  469. str, as well as the new, unescaped one, which MUST have
  470. an allocated buffer linked to the 'str' structure ;
  471. (the buffer can be allocated with the same length as
  472. the original string -- the output string is always
  473. shorter (if escaped characters occur) or same-long
  474. as the original one).
  475. only printable characters are permitted
  476. <0 is returned on an unescaping error, length of the
  477. unescaped string otherwise
  478. */
  479. inline static int un_escape(str *user, str *new_user )
  480. {
  481. int i, j, value;
  482. int hi, lo;
  483. if( new_user==0 || new_user->s==0) {
  484. LOG(L_CRIT, "BUG: un_escape: called with invalid param\n");
  485. return -1;
  486. }
  487. new_user->len = 0;
  488. j = 0;
  489. for (i = 0; i < user->len; i++) {
  490. if (user->s[i] == '%') {
  491. if (i + 2 >= user->len) {
  492. LOG(L_ERR, "ERROR: un_escape: escape sequence too short in"
  493. " '%.*s' @ %d\n",
  494. user->len, user->s, i );
  495. goto error;
  496. }
  497. hi=hex2int(user->s[i + 1]);
  498. if (hi<0) {
  499. LOG(L_ERR, "ERROR: un_escape: non-hex high digit in an escape sequence in"
  500. " '%.*s' @ %d\n",
  501. user->len, user->s, i+1 );
  502. goto error;
  503. }
  504. lo=hex2int(user->s[i + 2]);
  505. if (lo<0) {
  506. LOG(L_ERR, "ERROR: non-hex low digit in an escape sequence in "
  507. "'%.*s' @ %d\n",
  508. user->len, user->s, i+2 );
  509. goto error;
  510. }
  511. value=(hi<<4)+lo;
  512. if (value < 32 || value > 126) {
  513. LOG(L_ERR, "ERROR: non-ASCII escaped character in '%.*s' @ %d\n",
  514. user->len, user->s, i );
  515. goto error;
  516. }
  517. new_user->s[j] = value;
  518. i+=2; /* consume the two hex digits, for cycle will move to the next char */
  519. } else {
  520. new_user->s[j] = user->s[i];
  521. }
  522. j++; /* good -- we translated another character */
  523. }
  524. new_user->len = j;
  525. return j;
  526. error:
  527. new_user->len = j;
  528. return -1;
  529. }
  530. /*
  531. * Convert a string to lower case
  532. */
  533. static inline void strlower(str* _s)
  534. {
  535. int i;
  536. for(i = 0; i < _s->len; i++) {
  537. _s->s[i] = tolower(_s->s[i]);
  538. }
  539. }
  540. /*
  541. * Convert a str into integer
  542. */
  543. static inline int str2int(str* _s, unsigned int* _r)
  544. {
  545. int i;
  546. *_r = 0;
  547. for(i = 0; i < _s->len; i++) {
  548. if ((_s->s[i] >= '0') && (_s->s[i] <= '9')) {
  549. *_r *= 10;
  550. *_r += _s->s[i] - '0';
  551. } else {
  552. return -1;
  553. }
  554. }
  555. return 0;
  556. }
  557. /*
  558. * Convert an str to signed integer
  559. */
  560. static inline int str2sint(str* _s, int* _r)
  561. {
  562. int i;
  563. int sign;
  564. if (_s->len == 0) return -1;
  565. *_r = 0;
  566. sign = 1;
  567. i = 0;
  568. if (_s->s[0] == '+') {
  569. i++;
  570. } else if (_s->s[0] == '-') {
  571. sign = -1;
  572. i++;
  573. }
  574. for(; i < _s->len; i++) {
  575. if ((_s->s[i] >= '0') && (_s->s[i] <= '9')) {
  576. *_r *= 10;
  577. *_r += _s->s[i] - '0';
  578. } else {
  579. return -1;
  580. }
  581. }
  582. *_r *= sign;
  583. return 0;
  584. }
  585. #ifdef SHM_MEM
  586. /**
  587. * \brief Make a copy of a str structure using shm_malloc
  588. * \param dst destination
  589. * \param src source
  590. * \return 0 on success, -1 on failure
  591. */
  592. static inline int shm_str_dup(str* dst, const str* src)
  593. {
  594. dst->s = (char*)shm_malloc(src->len);
  595. if (!dst->s) {
  596. SHM_MEM_ERROR;
  597. return -1;
  598. }
  599. memcpy(dst->s, src->s, src->len);
  600. dst->len = src->len;
  601. return 0;
  602. }
  603. #endif /* SHM_MEM */
  604. /**
  605. * \brief Make a copy of a str structure using pkg_malloc
  606. * \param dst destination
  607. * \param src source
  608. * \return 0 on success, -1 on failure
  609. */
  610. static inline int pkg_str_dup(str* dst, const str* src)
  611. {
  612. dst->s = (char*)pkg_malloc(src->len);
  613. if (dst->s==NULL)
  614. {
  615. PKG_MEM_ERROR;
  616. return -1;
  617. }
  618. memcpy(dst->s, src->s, src->len);
  619. dst->len = src->len;
  620. return 0;
  621. }
  622. /**
  623. * \brief Compare two str's case sensitive
  624. * \param str1 first str
  625. * \param str2 second str
  626. * \return 0 if both are equal, positive if str1 is greater, negative if str2 is greater, -2 on errors
  627. */
  628. static inline int str_strcmp(const str *str1, const str *str2)
  629. {
  630. if(str1==NULL || str2==NULL || str1->s ==NULL || str2->s==NULL || str1->len<0 || str2->len<0)
  631. {
  632. LM_ERR("bad parameters\n");
  633. return -2;
  634. }
  635. if (str1->len < str2->len)
  636. return -1;
  637. else if (str1->len > str2->len)
  638. return 1;
  639. else
  640. return strncmp(str1->s, str2->s, str1->len);
  641. }
  642. /**
  643. * \brief Compare two str's case insensitive
  644. * \param str1 first str
  645. * \param str2 second str
  646. * \return 0 if both are equal, positive if str1 is greater, negative if str2 is greater, -2 on errors
  647. */
  648. static inline int str_strcasecmp(const str *str1, const str *str2)
  649. {
  650. if(str1==NULL || str2==NULL || str1->s ==NULL || str2->s==NULL || str1->len<0 || str2->len<0)
  651. {
  652. LM_ERR("bad parameters\n");
  653. return -2;
  654. }
  655. if (str1->len < str2->len)
  656. return -1;
  657. else if (str1->len > str2->len)
  658. return 1;
  659. else
  660. return strncasecmp(str1->s, str2->s, str1->len);
  661. }
  662. #ifndef MIN
  663. #define MIN(x, y) ((x) < (y) ? (x) : (y))
  664. #endif
  665. #ifndef MAX
  666. #define MAX(x, y) ((x) > (y) ? (x) : (y))
  667. #endif
  668. /* INTeger-TO-Buffer-STRing : convers an unsigned long to a string
  669. * IMPORTANT: the provided buffer must be at least INT2STR_MAX_LEN size !! */
  670. static inline char* int2bstr(unsigned long l, char *s, int* len)
  671. {
  672. int i;
  673. i=INT2STR_MAX_LEN-2;
  674. s[INT2STR_MAX_LEN-1]=0;
  675. /* null terminate */
  676. do{
  677. s[i]=l%10+'0';
  678. i--;
  679. l/=10;
  680. }while(l && (i>=0));
  681. if (l && (i<0)){
  682. LM_CRIT("overflow error\n");
  683. }
  684. if (len) *len=(INT2STR_MAX_LEN-2)-i;
  685. return &s[i+1];
  686. }
  687. inline static int hexstr2int(char *c, int len, unsigned int *val)
  688. {
  689. char *pc;
  690. int r;
  691. char mychar;
  692. r=0;
  693. for (pc=c; pc<c+len; pc++) {
  694. r <<= 4 ;
  695. mychar=*pc;
  696. if ( mychar >='0' && mychar <='9') r+=mychar -'0';
  697. else if (mychar >='a' && mychar <='f') r+=mychar -'a'+10;
  698. else if (mychar >='A' && mychar <='F') r+=mychar -'A'+10;
  699. else return -1;
  700. }
  701. *val = r;
  702. return 0;
  703. }
  704. /*
  705. * Convert a str (base 10 or 16) into integer
  706. */
  707. static inline int strno2int( str *val, unsigned int *mask )
  708. {
  709. /* hexa or decimal*/
  710. if (val->len>2 && val->s[0]=='0' && val->s[1]=='x') {
  711. return hexstr2int( val->s+2, val->len-2, mask);
  712. } else {
  713. return str2int( val, mask);
  714. }
  715. }
  716. /* converts a username into uid:gid,
  717. * returns -1 on error & 0 on success */
  718. int user2uid(int* uid, int* gid, char* user);
  719. /* converts a group name into a gid
  720. * returns -1 on error, 0 on success */
  721. int group2gid(int* gid, char* group);
  722. /*
  723. * Replacement of timegm (does not exists on all platforms
  724. * Taken from
  725. * http://lists.samba.org/archive/samba-technical/2002-November/025737.html
  726. */
  727. time_t _timegm(struct tm* t);
  728. /* Convert time_t value that is relative to local timezone to UTC */
  729. time_t local2utc(time_t in);
  730. /* Convert time_t value in UTC to to value relative to local time zone */
  731. time_t utc2local(time_t in);
  732. /*
  733. * Return str as zero terminated string allocated
  734. * using pkg_malloc
  735. */
  736. char* as_asciiz(str* s);
  737. /* return system version (major.minor.minor2) as
  738. * (major<<16)|(minor)<<8|(minor2)
  739. * (if some of them are missing, they are set to 0)
  740. * if the parameters are not null they are set to the coresp. part */
  741. unsigned int get_sys_version(int* major, int* minor, int* minor2);
  742. /** Converts relative pathnames to absolute pathnames. This function returns
  743. * the full pathname of a file in parameter. If the file pathname does not
  744. * start with / then it will be converted into an absolute pathname. The
  745. * function gets the absolute directory pathname from \c base and appends \c
  746. * file to it. The first parameter can be NULL, in this case the function will
  747. * use the location of the main SER configuration file as reference.
  748. * @param base filename to be used as reference when \c file is relative. It
  749. * must be absolute. The location of the SER configuration file
  750. * will be used as reference if you set the value of this
  751. * parameter to NULL.
  752. * @param file A pathname to be converted to absolute.
  753. * @return A string containing absolute pathname, the string must be freed
  754. * with free. NULL on error.
  755. */
  756. char* get_abs_pathname(str* base, str* file);
  757. /**
  758. * search for needle in text
  759. */
  760. char *str_search(str *text, str *needle);
  761. #endif