-rw-r--r-- | gammu/emb/common/misc/coding/coding.c | 177 | ||||
-rw-r--r-- | gammu/emb/common/misc/coding/coding.h | 23 | ||||
-rw-r--r-- | gammu/emb/common/misc/coding/md5.c | 2 | ||||
-rw-r--r-- | gammu/emb/common/misc/misc.c | 15 | ||||
-rw-r--r-- | gammu/emb/common/misc/misc.h | 8 |
5 files changed, 205 insertions, 20 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,30 +1,183 @@ /* (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 + */ #include <stdio.h> #include <stdlib.h> #include <string.h> #include <ctype.h> #include <locale.h> #ifndef __OpenBSD__ # include <wctype.h> #endif #ifdef WIN32 # include "windows.h" #endif #include "../misc.h" #include "coding.h" +/* 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) { unsigned int len = 0; if (str == NULL) return 0; while(str[len*2] != 0 || str[len*2+1] != 0) len++; return len; } /* Convert Unicode char saved in src to dest */ @@ -577,32 +730,32 @@ void GSM_UnpackSemiOctetNumber(unsigned char *retval, unsigned char *Number, boo unsigned char Buffer[50] = ""; int length = Number[0]; if (semioctet) { /* Convert number of semioctets to number of chars */ if (length % 2) length++; length=length / 2 + 1; } /*without leading byte with format of number*/ length--; - switch (Number[1]) { - case NUMBER_ALPHANUMERIC: + switch ((Number[1] & 112)) { + case (NUMBER_ALPHANUMERIC_NUMBERING_PLAN_UNKNOWN & 112): if (length > 6) length++; dbgprintf("Alphanumeric number, length %i\n",length); GSM_UnpackEightBitsToSeven(0, length, length, Number+2, Buffer); Buffer[length]=0; break; - case NUMBER_INTERNATIONAL: + case (NUMBER_INTERNATIONAL_NUMBERING_PLAN_ISDN & 112): dbgprintf("International number\n"); Buffer[0]='+'; DecodeBCD(Buffer+1,Number+2, length); break; default: dbgprintf("Default number %02x\n",Number[1]); DecodeBCD (Buffer, Number+2, length); break; } EncodeUnicode(retval,Buffer,strlen(Buffer)); } @@ -622,48 +775,48 @@ void GSM_UnpackSemiOctetNumber(unsigned char *retval, unsigned char *Number, boo * * 1 semioctet = 4 bits = half of byte */ int GSM_PackSemiOctetNumber(unsigned char *Number, unsigned char *Output, bool semioctet) { unsigned char format, buffer[50]; int length, i; length=UnicodeLength(Number); memcpy(buffer,DecodeUnicodeString(Number),length+1); /* Checking for format number */ - format = NUMBER_UNKNOWN; + format = NUMBER_UNKNOWN_NUMBERING_PLAN_ISDN; for (i=0;i<length;i++) { /* first byte is '+'. Number can be international */ 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; } } /** * First byte is used for saving type of number. See GSM 03.40 * section 9.1.2.5 */ Output[0]=format; /* After number type we will have number. GSM 03.40 section 9.1.2 */ switch (format) { - case NUMBER_ALPHANUMERIC: + case NUMBER_ALPHANUMERIC_NUMBERING_PLAN_UNKNOWN: length=GSM_PackSevenBitsToEight(0, buffer, Output+1, strlen(buffer))*2; if (strlen(buffer)==7) length--; break; - case NUMBER_INTERNATIONAL: + case NUMBER_INTERNATIONAL_NUMBERING_PLAN_ISDN: length--; EncodeBCD (Output+1, buffer+1, length, true); break; default: EncodeBCD (Output+1, buffer, length, true); break; } if (semioctet) return length; /* Convert number of semioctets to number of chars */ if (length % 2) length++; @@ -910,45 +1063,45 @@ void DecodeUnicodeSpecialNOKIAChars(unsigned char *dest, const unsigned char *sr } } dest[current++] = 0x00; dest[current++] = 0x00; } bool mystrncasecmp(unsigned const char *a, unsigned const char *b, int num) { int i; if (a == NULL || b == NULL) return false; - num--; + if (num == 0) num = -1; for (i = 0; i != num; i++) { if (a[i] == 0x00 && b[i] == 0x00) return true; if (a[i] == 0x00 || b[i] == 0x00) return false; if (tolower(a[i]) != tolower(b[i])) return false; } return true; } /* Compares two Unicode strings without regarding to case. * Return true, when they're equal */ bool mywstrncasecmp(unsigned const char *a, unsigned const char *b, int num) { int i; wchar_t wc,wc2; if (a == NULL || b == NULL) return false; - num--; + if (num == 0) num = -1; for (i = 0; i != num; i++) { if ((a[i*2] == 0x00 && a[i*2+1] == 0x00) && (b[i*2] == 0x00 && b[i*2+1] == 0x00)) return true; if ((a[i*2] == 0x00 && a[i*2+1] == 0x00) || (b[i*2] == 0x00 && b[i*2+1] == 0x00)) return false; wc = a[i*2+1] | (a[i*2] << 8); wc2 = b[i*2+1] | (b[i*2] << 8); if (mytowlower(wc) != mytowlower(wc2)) return false; } return true; } /* wcscmp in Mandrake 9.0 is wrong */ @@ -1006,25 +1159,25 @@ int mytowlower(wchar_t c) */ /* * The original strstr() file contains the following comment: * * My personal strstr() implementation that beats most other algorithms. * Until someone tells me otherwise, I assume that this is the * fastest implementation of strstr() in C. * I deliberately chose not to comment it. You should have at least * as much fun trying to understand it, as I had to write it :-). * * Stephen R. van den Berg, berg@pool.informatik.rwth-aachen.de */ -unsigned char *mystrstr (const unsigned char *haystack, const unsigned char *needle) +unsigned char *mywstrstr (const unsigned char *haystack, const unsigned char *needle) { /* One crazy define to convert unicode used in Gammu to standard wchar_t */ #define tolowerwchar(x) (mytowlower((wchar_t)( (((&(x))[0] & 0xff) << 8) | (((&(x))[1] & 0xff)) ))) register wchar_t b, c; if ((b = tolowerwchar(*needle)) != L'\0') { haystack -= 2; /* possible ANSI violation */ do { haystack += 2; if ((c = tolowerwchar(*haystack)) == L'\0') goto ret0; } while (c != b); 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 @@ -1,49 +1,56 @@ /* (c) 2002-2004 by Marcin Wiacek and others */ #ifndef __coding_h #define __coding_h +#if defined(_MSC_VER) && defined(__cplusplus) + extern "C" { +#endif + #include <stdlib.h> #include "../misc.h" #ifdef __OpenBSD__ typedef int wint_t; #endif /* ---------------------------- Unicode ------------------------------------ */ 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); bool myiswspace (unsigned const char *src); int mytowlower (wchar_t c); unsigned int EncodeWithUnicodeAlphabet (const unsigned char *value, wchar_t *dest); unsigned int DecodeWithUnicodeAlphabet (wchar_t value, unsigned char *dest); unsigned int UnicodeLength (const unsigned char *str); unsigned char *DecodeUnicodeString (const unsigned char *src); unsigned char *DecodeUnicodeConsole (const unsigned char *src); void DecodeUnicode (const unsigned char *src, unsigned char *dest); void EncodeUnicode (unsigned char *dest, const unsigned char *src, int len); void CopyUnicodeString (unsigned char *Dest, unsigned char *Source); void ReverseUnicodeString (unsigned char *String); void ReadUnicodeFile (unsigned char *Dest, unsigned char *Source); void DecodeUnicodeSpecialNOKIAChars (unsigned char *dest, const unsigned char *src, int len); void EncodeUnicodeSpecialNOKIAChars (unsigned char *dest, const unsigned char *src, int len); +char *EncodeUnicodeSpecialChars (unsigned char *buffer); +char *DecodeUnicodeSpecialChars (unsigned char *buffer); + /* ------------------------------- BCD ------------------------------------- */ unsigned char EncodeWithBCDAlphabet (int value); int DecodeWithBCDAlphabet (unsigned char value); void DecodeBCD (unsigned char *dest, const unsigned char *src, int len); void EncodeBCD (unsigned char *dest, const unsigned char *src, int len, bool fill); /* ------------------------------ UTF7 ------------------------------------- */ void DecodeUTF7 (unsigned char *dest, const unsigned char *src, int len); /* ------------------------------ UTF8 ------------------------------------- */ wchar_t DecodeWithUTF8Alphabet (unsigned char mychar3, unsigned char mychar4); @@ -77,33 +84,33 @@ int GSM_UnpackEightBitsToSeven (int offset, int in_length, int out_length, unsigned char *input, unsigned char *output); /* ----------------- Phone numbers according to GSM specs ------------------ */ /** * Enum to handle types of phones numbers like * specified in GSM 03.40 section 9.1.2.5 */ typedef enum { /** * Unknown number type */ - NUMBER_UNKNOWN = 0x81, + NUMBER_UNKNOWN_NUMBERING_PLAN_ISDN = 0x81, /** * International number (full number with code of country) */ - NUMBER_INTERNATIONAL = 0x91, + NUMBER_INTERNATIONAL_NUMBERING_PLAN_ISDN = 0x91, /** * Alphanumeric number (with chars too) */ - NUMBER_ALPHANUMERIC = 0xD0 + NUMBER_ALPHANUMERIC_NUMBERING_PLAN_UNKNOWN = 0xD0 /* specification give also other values */ } GSM_NumberType; void GSM_UnpackSemiOctetNumber (unsigned char *retval, unsigned char *Number, bool semioctet); int GSM_PackSemiOctetNumber (unsigned char *Number, unsigned char *Output, bool semioctet); /* ---------------------------- Bits --------------------------------------- */ void BufferAlign (unsigned char *Destination, int *CurrentBit); void BufferAlignNumber(int *CurrentBit); @@ -114,20 +121,28 @@ void GetBuffer (unsigned char *Source, int *CurrentBit, unsigned char *Destin void GetBufferInt (unsigned char *Source, int *CurrentBit, int *integer, int BitsToProcess); void GetBufferI (unsigned char *Source, int *CurrentBit, int *result, int BitsToProcess); int GetBit (unsigned char *Buffer, int BitNum); int SetBit (unsigned char *Buffer, int BitNum); int ClearBit (unsigned char *Buffer, int BitNum); /* ---------------------------- Other -------------------------------------- */ void StringToDouble (char *text, double *d); 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); +char *EncodeSpecialChars(unsigned char *buffer); +char *DecodeSpecialChars(unsigned char *buffer); + +#if defined(_MSC_VER) && defined(__cplusplus) + } +#endif + #endif /* How should editor hadle tabs in this file? Add editor commands here. * vim: noexpandtab sw=8 ts=8 sts=8: */ 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,13 +1,13 @@ -/* 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 * Dominik Reichl <dominik.reichl@t-online.de>, Germany */ /* ********************************************************************** ** MD5.cpp ** ** ** ** - Style modified by Tony Ray, January 2001 ** ** Added support for randomizing initialization constants ** ** - Style modified by Dominik Reichl, April 2003 ** ** Optimized code ** 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 @@ -2,24 +2,27 @@ /* Checking used compiler (c) 2002 by Michal Cihar */ #include <string.h> #include <ctype.h> #include <time.h> #include <stdarg.h> #include <stdio.h> #include <locale.h> #include <sys/timeb.h> #ifdef WIN32 # include "windows.h" #endif +#if defined(linux) || defined(__linux) || defined(__linux__) +# include <sys/utsname.h> +#endif #include "../gsmstate.h" #include "misc.h" /* Based on article in Polish PC-Kurier 8/1998 page 104 * Archive on http://www.pckurier.pl */ char *DayOfWeek (int year, int month, int day) { int p,q,r,w; static char DayOfWeekChar[10]; @@ -222,28 +225,30 @@ char *OSDate (GSM_DateTime dt) } } #ifdef WIN32 setlocale(LC_ALL, ".ACP"); #endif return retval2; } 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->Day >= 1 && date->Day <= days[date->Month]; } bool CheckTime(GSM_DateTime *date) { return date->Hour <= 23 && date->Hour >= 0 && date->Minute <= 59 && date->Minute >= 0 && date->Second <= 59 && date->Second >= 0; } int GetLine(FILE *File, char *Line, int count) { int num; @@ -442,24 +447,27 @@ void DumpMessage(FILE *df, Debug_Level dl, const unsigned char *message, int mes j++; } } if (j != 0) smfprintf(df, dl, "%s\n", buffer); } char *GetOS(void) { #ifdef WIN32 OSVERSIONINFOEX Ver; bool Extended = true; #endif +#if defined(linux) || defined(__linux) || defined(__linux__) + struct utsname Ver; +#endif static char Buffer[100] = {0x00}; #ifdef WIN32 memset(&Ver,sizeof(OSVERSIONINFOEX),0); Ver.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX); if (!GetVersionEx((OSVERSIONINFO *)&Ver)) { Extended = false; Ver.dwOSVersionInfoSize = sizeof(OSVERSIONINFO); if (!GetVersionEx((OSVERSIONINFO *)&Ver)) { //#ifdef _MSC_VER
// Ver.dwMajorVersion = _winmajor; @@ -511,25 +519,26 @@ char *GetOS(void) } else if (Ver.dwMajorVersion == 5 && Ver.dwMinorVersion == 2) { sprintf(Buffer,"Win 2003"); } else { sprintf(Buffer, "Windows %i.%i.%i",Ver.dwMajorVersion,Ver.dwMinorVersion,Ver.dwBuildNumber); } if (Extended && Ver.wServicePackMajor != 0) { sprintf(Buffer+strlen(Buffer)," SP%i",Ver.wServicePackMajor); } #elif defined(linux) || defined(__linux) || defined(__linux__) - sprintf(Buffer, "Linux"); + uname(&Ver); + sprintf(Buffer, "Linux, kernel %s",Ver.release); #elif defined(__FreeBSD__) sprintf(Buffer, "FreeBSD"); #elif defined(__NetBSD__) sprintf(Buffer, "NetBSD"); #elif defined(__OpenBSD__) sprintf(Buffer, "OpenBSD"); #elif defined(__GNU__) sprintf(Buffer, "GNU/Hurd"); #elif defined(sun) || defined(__sun) || defined(__sun__) # ifdef __SVR4 sprintf(Buffer, "Sun Solaris"); # else 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 @@ -1,17 +1,21 @@ /* (c) 2002-2004 by Marcin Wiacek */ #ifndef __misc_h #define __misc_h +#if defined(_MSC_VER) && defined(__cplusplus) + extern "C" { +#endif + #include <stdio.h> #include <time.h> #ifdef WIN32 # include <windows.h> #endif #include "../config.h" #ifndef __cplusplus #ifndef false # define false 0 #endif @@ -121,17 +125,21 @@ void GSM_GetCurrentDateTime (GSM_DateTime *Date); char *OSDateTime (GSM_DateTime dt, bool TimeZone); char *OSDate (GSM_DateTime dt); char *DayOfWeek (int year, int month, int day); time_t Fill_Time_T (GSM_DateTime DT, int TZ); void GetTimeDifference (unsigned long diff, GSM_DateTime *DT, bool Plus, int multi); void Fill_GSM_DateTime (GSM_DateTime *Date, time_t timet); bool CheckDate (GSM_DateTime *date); bool CheckTime (GSM_DateTime *date); char *GetCompiler(void); char *GetOS(void); +#if defined(_MSC_VER) && defined(__cplusplus) + } +#endif + #endif /* How should editor hadle tabs in this file? Add editor commands here. * vim: noexpandtab sw=8 ts=8 sts=8: */ |