-rw-r--r-- | gammu/emb/common/misc/coding/coding.c | 177 | ||||
-rw-r--r-- | gammu/emb/common/misc/coding/coding.h | 27 | ||||
-rw-r--r-- | gammu/emb/common/misc/coding/md5.c | 2 | ||||
-rw-r--r-- | gammu/emb/common/misc/misc.c | 19 | ||||
-rw-r--r-- | gammu/emb/common/misc/misc.h | 8 |
5 files changed, 209 insertions, 24 deletions
diff --git a/gammu/emb/common/misc/coding/coding.c b/gammu/emb/common/misc/coding/coding.c index 62543ac..b30b645 100644 --- a/gammu/emb/common/misc/coding/coding.c +++ b/gammu/emb/common/misc/coding/coding.c @@ -1,3 +1,7 @@ /* (c) 2002-2004 by Marcin Wiacek, Michal Cihar and others */ -/* based on some work from MyGnokii and Gnokii */ +/* based on some work from MyGnokii (www.mwiacek.com) */ +/* based on some work from Gnokii (www.gnokii.org) + * (C) 1999-2000 Hugh Blemings & Pavel Janik ml. (C) 2001-2004 Pawel Kot + * GNU GPL version 2 or later + */ @@ -18,2 +22,151 @@ +/* function changes #10 #13 chars to \n \r */ +char *EncodeUnicodeSpecialChars(unsigned char *buffer) +{ + int Pos=0, Pos2=0; + static unsigned char Buf[20000]; + + while (buffer[Pos*2]!=0x00 || buffer[Pos*2+1]!=0x00) { + if (buffer[Pos*2] == 0x00 && buffer[Pos*2+1] == 10) { + Buf[Pos2*2] = 0x00; + Buf[Pos2*2+1] = '\\'; + Pos2++; + Buf[Pos2*2] = 0x00; + Buf[Pos2*2+1] = 'n'; + Pos2++; + } else if (buffer[Pos*2] == 0x00 && buffer[Pos*2+1] == 13) { + Buf[Pos2*2] = 0x00; + Buf[Pos2*2+1] = '\\'; + Pos2++; + Buf[Pos2*2] = 0x00; + Buf[Pos2*2+1] = 'r'; + Pos2++; + } else if (buffer[Pos*2] == 0x00 && buffer[Pos*2+1] == '\\') { + Buf[Pos2*2] = 0x00; + Buf[Pos2*2+1] = '\\'; + Pos2++; + Buf[Pos2*2] = 0x00; + Buf[Pos2*2+1] = '\\'; + Pos2++; + } else { + Buf[Pos2*2] = buffer[Pos*2]; + Buf[Pos2*2+1] = buffer[Pos*2+1]; + Pos2++; + } + Pos++; + } + Buf[Pos2*2] = 0; + Buf[Pos2*2+1] = 0; + return Buf; +} + +/* function changes #10 #13 chars to \n \r */ +char *EncodeSpecialChars(unsigned char *buffer) +{ + int Pos=0, Pos2=0; + static unsigned char Buf[10000]; + + while (buffer[Pos]!=0x00) { + switch (buffer[Pos]) { + case 10: + Buf[Pos2++] = '\\'; + Buf[Pos2++] = 'n'; + break; + case 13: + Buf[Pos2++] = '\\'; + Buf[Pos2++] = 'r'; + break; + case '\\': + Buf[Pos2++] = '\\'; + Buf[Pos2++] = '\\'; + break; + default: + Buf[Pos2++] = buffer[Pos]; + } + Pos++; + } + Buf[Pos2] = 0; + return Buf; +} + +char *DecodeUnicodeSpecialChars(unsigned char *buffer) +{ + int Pos=0, Pos2=0, level=0; + static unsigned char Buf[10000]; + + while (buffer[Pos*2]!=0x00 || buffer[Pos*2+1]!=0x00) { + Buf[Pos2*2] = buffer[Pos*2]; + Buf[Pos2*2+1] = buffer[Pos*2+1]; + switch (level) { + case 0: + if (buffer[Pos*2] == 0x00 && buffer[Pos*2+1] == '\\') { + level = 1; + } else { + Pos2++; + } + break; + case 1: + if (buffer[Pos*2] == 0x00 && buffer[Pos*2+1] == 'n') { + Buf[Pos2*2] = 0; + Buf[Pos2*2+1] = 10; + } + if (buffer[Pos*2] == 0x00 && buffer[Pos*2+1] == 'r') { + Buf[Pos2*2] = 0; + Buf[Pos2*2+1] = 13; + } + if (buffer[Pos*2] == 0x00 && buffer[Pos*2+1] == '\\') { + Buf[Pos2*2] = 0; + Buf[Pos2*2+1] = '\\'; + } + Pos2++; + level = 0; + } + Pos++; + } + Buf[Pos2*2] = 0; + Buf[Pos2*2+1] = 0; + return Buf; +} + +char *DecodeSpecialChars(unsigned char *buffer) +{ + int Pos=0, Pos2=0, level=0; + static unsigned char Buf[10000]; + + while (buffer[Pos]!=0x00) { + Buf[Pos2] = buffer[Pos]; + switch (level) { + case 0: + if (buffer[Pos] == '\\') { + level = 1; + } else { + Pos2++; + } + break; + case 1: + if (buffer[Pos] == 'n') Buf[Pos2] = 10; + if (buffer[Pos] == 'r') Buf[Pos2] = 13; + if (buffer[Pos] == '\\') Buf[Pos2] = '\\'; + Pos2++; + level = 0; + } + Pos++; + } + Buf[Pos2] = 0; + return Buf; +} + +char *mystrcasestr(unsigned const char *a, unsigned const char *b) +{ + unsigned char A[2000], B[200]; + int i; + + memset(A,0,sizeof(A)); + memset(B,0,sizeof(B)); + for (i=0;i<(int)strlen(a);i++) A[i] = tolower(a[i]); + for (i=0;i<(int)strlen(b);i++) B[i] = tolower(b[i]); + + return strstr(A,B); +} + unsigned int UnicodeLength(const unsigned char *str) @@ -588,4 +741,4 @@ void GSM_UnpackSemiOctetNumber(unsigned char *retval, unsigned char *Number, boo - switch (Number[1]) { - case NUMBER_ALPHANUMERIC: + switch ((Number[1] & 112)) { + case (NUMBER_ALPHANUMERIC_NUMBERING_PLAN_UNKNOWN & 112): if (length > 6) length++; @@ -595,3 +748,3 @@ void GSM_UnpackSemiOctetNumber(unsigned char *retval, unsigned char *Number, boo break; - case NUMBER_INTERNATIONAL: + case (NUMBER_INTERNATIONAL_NUMBERING_PLAN_ISDN & 112): dbgprintf("International number\n"); @@ -633,3 +786,3 @@ int GSM_PackSemiOctetNumber(unsigned char *Number, unsigned char *Output, bool s /* Checking for format number */ - format = NUMBER_UNKNOWN; + format = NUMBER_UNKNOWN_NUMBERING_PLAN_ISDN; for (i=0;i<length;i++) { @@ -637,6 +790,6 @@ int GSM_PackSemiOctetNumber(unsigned char *Number, unsigned char *Output, bool s if (i==0 && buffer[i]=='+') { - format=NUMBER_INTERNATIONAL; + format=NUMBER_INTERNATIONAL_NUMBERING_PLAN_ISDN; } else { /*char is not number. It must be alphanumeric*/ - if (!isdigit(buffer[i])) format=NUMBER_ALPHANUMERIC; + if (!isdigit(buffer[i])) format=NUMBER_ALPHANUMERIC_NUMBERING_PLAN_UNKNOWN; } @@ -652,3 +805,3 @@ int GSM_PackSemiOctetNumber(unsigned char *Number, unsigned char *Output, bool s switch (format) { - case NUMBER_ALPHANUMERIC: + case NUMBER_ALPHANUMERIC_NUMBERING_PLAN_UNKNOWN: length=GSM_PackSevenBitsToEight(0, buffer, Output+1, strlen(buffer))*2; @@ -656,3 +809,3 @@ int GSM_PackSemiOctetNumber(unsigned char *Number, unsigned char *Output, bool s break; - case NUMBER_INTERNATIONAL: + case NUMBER_INTERNATIONAL_NUMBERING_PLAN_ISDN: length--; @@ -921,3 +1074,3 @@ bool mystrncasecmp(unsigned const char *a, unsigned const char *b, int num) - num--; + if (num == 0) num = -1; @@ -941,3 +1094,3 @@ bool mywstrncasecmp(unsigned const char *a, unsigned const char *b, int num) - num--; + if (num == 0) num = -1; @@ -1017,3 +1170,3 @@ int mytowlower(wchar_t c) -unsigned char *mystrstr (const unsigned char *haystack, const unsigned char *needle) +unsigned char *mywstrstr (const unsigned char *haystack, const unsigned char *needle) { diff --git a/gammu/emb/common/misc/coding/coding.h b/gammu/emb/common/misc/coding/coding.h index d0c334d..4cf0038 100644 --- a/gammu/emb/common/misc/coding/coding.h +++ b/gammu/emb/common/misc/coding/coding.h @@ -5,2 +5,6 @@ +#if defined(_MSC_VER) && defined(__cplusplus) + extern "C" { +#endif + #include <stdlib.h> @@ -15,3 +19,3 @@ typedef int wint_t; bool mywstrncasecmp (unsigned const char *a, unsigned const char *b, int num); -unsigned char *mystrstr (unsigned const char *haystack, unsigned const char *needle); +unsigned char *mywstrstr (unsigned const char *haystack, unsigned const char *needle); bool mywstrncmp (unsigned const char *a, unsigned const char *b, int num); @@ -37,2 +41,5 @@ void EncodeUnicodeSpecialNOKIAChars (unsigned char *dest, const unsigned char +char *EncodeUnicodeSpecialChars (unsigned char *buffer); +char *DecodeUnicodeSpecialChars (unsigned char *buffer); + /* ------------------------------- BCD ------------------------------------- */ @@ -88,3 +95,3 @@ typedef enum { */ - NUMBER_UNKNOWN = 0x81, + NUMBER_UNKNOWN_NUMBERING_PLAN_ISDN = 0x81, /** @@ -92,3 +99,3 @@ typedef enum { */ - NUMBER_INTERNATIONAL = 0x91, + NUMBER_INTERNATIONAL_NUMBERING_PLAN_ISDN = 0x91, /** @@ -96,3 +103,3 @@ typedef enum { */ - NUMBER_ALPHANUMERIC = 0xD0 + NUMBER_ALPHANUMERIC_NUMBERING_PLAN_UNKNOWN = 0xD0 @@ -124,5 +131,13 @@ void StringToDouble (char *text, double *d); -bool mystrncasecmp (unsigned const char *a, unsigned const char *b, int num); +bool mystrncasecmp (unsigned const char *a, unsigned const char *b, int num); +char *mystrcasestr (unsigned const char *a, unsigned const char *b); -void MyGetLine(unsigned char *Buffer, int *Pos, unsigned char *OutBuffer, int MaxLen); +void MyGetLine (unsigned char *Buffer, int *Pos, unsigned char *OutBuffer, int MaxLen); + +char *EncodeSpecialChars(unsigned char *buffer); +char *DecodeSpecialChars(unsigned char *buffer); + +#if defined(_MSC_VER) && defined(__cplusplus) + } +#endif diff --git a/gammu/emb/common/misc/coding/md5.c b/gammu/emb/common/misc/coding/md5.c index 30fe33f..abb61be 100644 --- a/gammu/emb/common/misc/coding/md5.c +++ b/gammu/emb/common/misc/coding/md5.c @@ -1,2 +1,2 @@ -/* Taken from ReHash (see http://www.reichlsoft.de.vu/) and released +/* Taken from ReHash (www.reichlsoft.de.vu) and released * under GPL/LGPL with permission from ReHash author diff --git a/gammu/emb/common/misc/misc.c b/gammu/emb/common/misc/misc.c index c2f09e4..7227e7b 100644 --- a/gammu/emb/common/misc/misc.c +++ b/gammu/emb/common/misc/misc.c @@ -13,2 +13,5 @@ #endif +#if defined(linux) || defined(__linux) || defined(__linux__) +# include <sys/utsname.h> +#endif @@ -232,7 +235,9 @@ char *OSDate (GSM_DateTime dt) bool CheckDate(GSM_DateTime *date) -{ - /* FIXME: This could also check if day is correct for selected month */ +{ + const unsigned int days[]={31,29,31,30,31,30,31,31,30,31,30,31}; + + /* FIXME: This could also check for leap years */ return date->Year != 0 && - date->Month >= 1 && date->Month <= 12 && - date->Day >= 1 && date->Day <= 31; + date->Month >= 1 && date->Month <= 12 && + date->Day >= 1 && date->Day <= days[date->Month]; } @@ -453,2 +458,5 @@ char *GetOS(void) #endif +#if defined(linux) || defined(__linux) || defined(__linux__) + struct utsname Ver; +#endif static char Buffer[100] = {0x00}; @@ -522,3 +530,4 @@ char *GetOS(void) #elif defined(linux) || defined(__linux) || defined(__linux__) - sprintf(Buffer, "Linux"); + uname(&Ver); + sprintf(Buffer, "Linux, kernel %s",Ver.release); #elif defined(__FreeBSD__) diff --git a/gammu/emb/common/misc/misc.h b/gammu/emb/common/misc/misc.h index 8b46170..c461001 100644 --- a/gammu/emb/common/misc/misc.h +++ b/gammu/emb/common/misc/misc.h @@ -5,2 +5,6 @@ +#if defined(_MSC_VER) && defined(__cplusplus) + extern "C" { +#endif + #include <stdio.h> @@ -132,2 +136,6 @@ char *GetOS(void); +#if defined(_MSC_VER) && defined(__cplusplus) + } +#endif + #endif |