소스 검색

*** empty log message ***

Andrei Pelinescu-Onciul 24 년 전
부모
커밋
7fa84b413e
2개의 변경된 파일127개의 추가작업 그리고 0개의 파일을 삭제
  1. 17 0
      str.h
  2. 110 0
      ut.h

+ 17 - 0
str.h

@@ -0,0 +1,17 @@
+/*
+ * $Id$
+ */
+
+#ifndef str_h
+#define str_h
+
+
+struct _str{
+	char* s; /* null terminated string*/
+	int len; /*string len*/
+};
+
+typedef struct _str str;
+
+
+#endif

+ 110 - 0
ut.h

@@ -0,0 +1,110 @@
+/*
+ *$Id$
+ *
+ * - various general purpose functions
+ */
+
+#ifndef ut_h
+#define ut_h
+
+#include "dprint.h"
+
+/* converts a str to an u. short, returns the u. short and sets *err on 
+ * error and if err!=null
+ * */
+static inline unsigned short str2s(unsigned char* str, unsigned int len,
+									int *err)
+{
+	unsigned short ret;
+	int i;
+	unsigned char *limit;
+	unsigned char *init;
+	
+	/*init*/
+	ret=i=0;
+	limit=str+len;
+	init=str;
+	
+	for(;str<limit ;str++){
+		if ( (*str <= '9' ) && (*str >= '0') ){
+				ret=ret*10+*str-'0';
+				i++;
+				if (i>5) goto error_digits;
+		}else{
+				//error unknown char
+				goto error_char;
+		}
+	}
+	if (err) *err=0;
+	return ret;
+	
+error_digits:
+	DBG("str2s: ERROR: too many letters in [%s]\n", init);
+	if (err) *err=1;
+	return 0;
+error_char:
+	DBG("str2s: ERROR: unexpected char %c in %s\n", *str, init);
+	if (err) *err=1;
+	return 0;
+}
+
+
+
+/* converts a str to an ipv4 address, returns the address and sets *err
+ * if error and err!=null
+ */
+static inline unsigned int str2ip(unsigned char* str, unsigned int len,
+		int * err)
+{
+	unsigned int ret;
+	int i;
+	unsigned char *limit;
+	unsigned char *init;
+
+	/*init*/
+	ret=i=0;
+	limit=str+len;
+	init=str;
+	
+	for(;str<limit ;str++){
+		if (*str=='.'){
+				i++;
+				if (i>3) goto error_dots;
+		}else if ( (*str <= '9' ) && (*str >= '0') ){
+				((unsigned char*)&ret)[i]=((unsigned char*)&ret)[i]*10+
+											*str-'0';
+		}else{
+				//error unknown char
+				goto error_char;
+		}
+	}
+	if (err) *err=0;
+	return ret;
+	
+error_dots:
+	DBG("str2ip: ERROR: too many dots in [%s]\n", init);
+	if (err) *err=1;
+	return 0;
+error_char:
+	DBG("str2ip: ERROR: unexpected char %c in %s\n", *str, init);
+	if (err) *err=1;
+	return 0;
+}
+
+
+
+/* faster memchr version */
+
+static inline char* q_memchr(char* p, int c, unsigned int size)
+{
+	char* end;
+
+	end=p+size;
+	for(;p<end;p++){
+		if (*p==(unsigned char)c) return p;
+	}
+	return 0;
+}
+	
+
+#endif