Diffstat (limited to 'gammu/emb/common/service/gsmmisc.c') (more/less context) (ignore whitespace changes)
-rw-r--r-- | gammu/emb/common/service/gsmmisc.c | 262 |
1 files changed, 262 insertions, 0 deletions
diff --git a/gammu/emb/common/service/gsmmisc.c b/gammu/emb/common/service/gsmmisc.c new file mode 100644 index 0000000..6959a22 --- a/dev/null +++ b/gammu/emb/common/service/gsmmisc.c | |||
@@ -0,0 +1,262 @@ | |||
1 | /* (c) 2002-2004 by Marcin Wiacek */ | ||
2 | |||
3 | #include <string.h> | ||
4 | #include <stdlib.h> | ||
5 | #include <sys/stat.h> | ||
6 | #ifdef WIN32 | ||
7 | # include <io.h> | ||
8 | # include <fcntl.h> | ||
9 | #endif | ||
10 | |||
11 | #include "../misc/coding/coding.h" | ||
12 | #include "../gsmcomon.h" | ||
13 | #include "gsmmisc.h" | ||
14 | |||
15 | struct keys_table_position { | ||
16 | char whatchar; | ||
17 | int whatcode; | ||
18 | }; | ||
19 | |||
20 | static struct keys_table_position Keys[] = { | ||
21 | {'m',GSM_KEY_MENU}, {'M',GSM_KEY_MENU}, | ||
22 | {'n',GSM_KEY_NAMES}, {'N',GSM_KEY_NAMES}, | ||
23 | {'p',GSM_KEY_POWER}, {'P',GSM_KEY_POWER}, | ||
24 | {'u',GSM_KEY_UP}, {'U',GSM_KEY_UP}, | ||
25 | {'d',GSM_KEY_DOWN}, {'D',GSM_KEY_DOWN}, | ||
26 | {'+',GSM_KEY_INCREASEVOLUME},{'-',GSM_KEY_DECREASEVOLUME}, | ||
27 | {'1',GSM_KEY_1}, {'2',GSM_KEY_2},{'3',GSM_KEY_3}, | ||
28 | {'4',GSM_KEY_4}, {'5',GSM_KEY_5},{'6',GSM_KEY_6}, | ||
29 | {'7',GSM_KEY_7}, {'8',GSM_KEY_8},{'9',GSM_KEY_9}, | ||
30 | {'*',GSM_KEY_ASTERISK}, {'0',GSM_KEY_0},{'#',GSM_KEY_HASH}, | ||
31 | {'g',GSM_KEY_GREEN}, {'G',GSM_KEY_GREEN}, | ||
32 | {'r',GSM_KEY_RED}, {'R',GSM_KEY_RED}, | ||
33 | {' ',0} | ||
34 | }; | ||
35 | |||
36 | GSM_Error MakeKeySequence(char *text, GSM_KeyCode *KeyCode, int *Length) | ||
37 | { | ||
38 | int i,j; | ||
39 | unsigned char key; | ||
40 | |||
41 | for (i=0;i<(int)(strlen(text));i++) { | ||
42 | key = text[i]; | ||
43 | KeyCode[i] = GSM_KEY_NONE; | ||
44 | j = 0; | ||
45 | while (Keys[j].whatchar!=' ') { | ||
46 | if (Keys[j].whatchar==key) { | ||
47 | KeyCode[i]=Keys[j].whatcode; | ||
48 | break; | ||
49 | } | ||
50 | j++; | ||
51 | } | ||
52 | if (KeyCode[i] == GSM_KEY_NONE) { | ||
53 | *Length = i; | ||
54 | return ERR_NOTSUPPORTED; | ||
55 | } | ||
56 | } | ||
57 | *Length = i; | ||
58 | return ERR_NONE; | ||
59 | } | ||
60 | |||
61 | GSM_Error GSM_ReadFile(char *FileName, GSM_File *File) | ||
62 | { | ||
63 | int i = 1000; | ||
64 | FILE *file; | ||
65 | struct statfileinfo; | ||
66 | |||
67 | if (FileName[0] == 0x00) return ERR_UNKNOWN; | ||
68 | file = fopen(FileName,"rb"); | ||
69 | if (file == NULL) return ERR_CANTOPENFILE; | ||
70 | |||
71 | free(File->Buffer); | ||
72 | File->Buffer = NULL; | ||
73 | File->Used = 0; | ||
74 | while (i == 1000) { | ||
75 | File->Buffer = realloc(File->Buffer,File->Used + 1000); | ||
76 | i = fread(File->Buffer+File->Used,1,1000,file); | ||
77 | File->Used = File->Used + i; | ||
78 | } | ||
79 | File->Buffer = realloc(File->Buffer,File->Used); | ||
80 | fclose(file); | ||
81 | |||
82 | File->ModifiedEmpty = true; | ||
83 | if (stat(FileName,&fileinfo) == 0) { | ||
84 | File->ModifiedEmpty = false; | ||
85 | dbgprintf("File info read correctly\n"); | ||
86 | //st_mtime is time of last modification of file | ||
87 | Fill_GSM_DateTime(&File->Modified, fileinfo.st_mtime); | ||
88 | File->Modified.Year = File->Modified.Year + 1900; | ||
89 | dbgprintf("FileTime: %02i-%02i-%04i %02i:%02i:%02i\n", | ||
90 | File->Modified.Day,File->Modified.Month,File->Modified.Year, | ||
91 | File->Modified.Hour,File->Modified.Minute,File->Modified.Second); | ||
92 | } | ||
93 | |||
94 | return ERR_NONE; | ||
95 | } | ||
96 | |||
97 | static void GSM_JADFindLine(GSM_File File, char *Name, char *Value) | ||
98 | { | ||
99 | unsigned char Line[2000]; | ||
100 | int Pos = 0; | ||
101 | |||
102 | Value[0] = 0; | ||
103 | |||
104 | while (1) { | ||
105 | MyGetLine(File.Buffer, &Pos, Line, File.Used); | ||
106 | if (strlen(Line) == 0) break; | ||
107 | if (!strncmp(Line,Name,strlen(Name))) { | ||
108 | Pos = strlen(Name); | ||
109 | while (Line[Pos] == 0x20) Pos++; | ||
110 | strcpy(Value,Line+Pos); | ||
111 | return; | ||
112 | } | ||
113 | } | ||
114 | } | ||
115 | |||
116 | GSM_Error GSM_JADFindData(GSM_File File, char *Vendor, char *Name, char *JAR, char *Version, int *Size) | ||
117 | { | ||
118 | char Size2[200]; | ||
119 | |||
120 | GSM_JADFindLine(File, "MIDlet-Vendor:", Vendor); | ||
121 | if (Vendor[0] == 0x00) return ERR_FILENOTSUPPORTED; | ||
122 | dbgprintf("Vendor: \"%s\"\n",Vendor); | ||
123 | |||
124 | GSM_JADFindLine(File, "MIDlet-Name:", Name); | ||
125 | if (Name[0] == 0x00) return ERR_FILENOTSUPPORTED; | ||
126 | dbgprintf("Name: \"%s\"\n",Name); | ||
127 | |||
128 | GSM_JADFindLine(File, "MIDlet-Jar-URL:", JAR); | ||
129 | if (JAR[0] == 0x00) return ERR_FILENOTSUPPORTED; | ||
130 | dbgprintf("JAR file URL: \"%s\"\n",JAR); | ||
131 | |||
132 | GSM_JADFindLine(File, "MIDlet-Jar-Size:", Size2); | ||
133 | *Size = -1; | ||
134 | if (Size2[0] == 0x00) return ERR_FILENOTSUPPORTED; | ||
135 | dbgprintf("JAR size: \"%s\"\n",Size2); | ||
136 | (*Size) = atoi(Size2); | ||
137 | |||
138 | GSM_JADFindLine(File, "MIDlet-Version:", Version); | ||
139 | dbgprintf("Version: \"%s\"\n",Version); | ||
140 | |||
141 | return ERR_NONE; | ||
142 | } | ||
143 | |||
144 | void GSM_IdentifyFileFormat(GSM_File *File) | ||
145 | { | ||
146 | File->Type = GSM_File_Other; | ||
147 | if (File->Used > 2) { | ||
148 | if (memcmp(File->Buffer, "BM",2)==0) { | ||
149 | File->Type = GSM_File_Image_BMP; | ||
150 | } else if (memcmp(File->Buffer, "GIF",3)==0) { | ||
151 | File->Type = GSM_File_Image_GIF; | ||
152 | } else if (File->Buffer[0] == 0x00 && File->Buffer[1] == 0x00) { | ||
153 | File->Type = GSM_File_Image_WBMP; | ||
154 | } else if (memcmp(File->Buffer+1, "PNG",3)==0) { | ||
155 | File->Type = GSM_File_Image_PNG; | ||
156 | } else if (File->Buffer[0] == 0xFF && File->Buffer[1] == 0xD8) { | ||
157 | File->Type = GSM_File_Image_JPG; | ||
158 | } else if (memcmp(File->Buffer, "MThd",4)==0) { | ||
159 | File->Type = GSM_File_Sound_MIDI; | ||
160 | } else if (File->Buffer[0] == 0x00 && File->Buffer[1] == 0x02) { | ||
161 | File->Type = GSM_File_Sound_NRT; | ||
162 | } | ||
163 | } | ||
164 | } | ||
165 | |||
166 | void SaveVCALDateTime(char *Buffer, int *Length, GSM_DateTime *Date, char *Start) | ||
167 | { | ||
168 | if (Start != NULL) { | ||
169 | *Length+=sprintf(Buffer+(*Length), "%s:",Start); | ||
170 | } | ||
171 | *Length+=sprintf(Buffer+(*Length), "%04d%02d%02dT%02d%02d%02d%c%c", | ||
172 | Date->Year, Date->Month, Date->Day, | ||
173 | Date->Hour, Date->Minute, Date->Second,13,10); | ||
174 | } | ||
175 | |||
176 | void ReadVCALDateTime(char *Buffer, GSM_DateTime *dt) | ||
177 | { | ||
178 | char year[5]="", month[3]="", day[3]="", hour[3]="", minute[3]="", second[3]=""; | ||
179 | |||
180 | memset(dt,0,sizeof(dt)); | ||
181 | |||
182 | strncpy(year, Buffer, 4); | ||
183 | strncpy(month, Buffer+4, 2); | ||
184 | strncpy(day, Buffer+6, 2); | ||
185 | strncpy(hour, Buffer+9,2); | ||
186 | strncpy(minute, Buffer+11,2); | ||
187 | strncpy(second, Buffer+13,2); | ||
188 | |||
189 | /* FIXME: Should check ranges... */ | ||
190 | dt->Year= atoi(year); | ||
191 | dt->Month= atoi(month); | ||
192 | dt->Day = atoi(day); | ||
193 | dt->Hour= atoi(hour); | ||
194 | dt->Minute= atoi(minute); | ||
195 | dt->Second= atoi(second); | ||
196 | /* FIXME */ | ||
197 | dt->Timezone= 0; | ||
198 | } | ||
199 | |||
200 | void SaveVCALText(char *Buffer, int *Length, char *Text, char *Start) | ||
201 | { | ||
202 | char buffer[1000]; | ||
203 | |||
204 | if (UnicodeLength(Text) != 0) { | ||
205 | EncodeUTF8QuotedPrintable(buffer,Text); | ||
206 | if (UnicodeLength(Text)==strlen(buffer)) { | ||
207 | *Length+=sprintf(Buffer+(*Length), "%s:%s%c%c",Start,DecodeUnicodeString(Text),13,10); | ||
208 | } else { | ||
209 | *Length+=sprintf(Buffer+(*Length), "%s;CHARSET=UTF-8;ENCODING=QUOTED-PRINTABLE:%s%c%c",Start,buffer,13,10); | ||
210 | } | ||
211 | } | ||
212 | } | ||
213 | |||
214 | bool ReadVCALText(char *Buffer, char *Start, char *Value) | ||
215 | { | ||
216 | unsigned char buff[200]; | ||
217 | |||
218 | Value[0] = 0x00; | ||
219 | Value[1] = 0x00; | ||
220 | |||
221 | strcpy(buff,Start); | ||
222 | strcat(buff,":"); | ||
223 | if (!strncmp(Buffer,buff,strlen(buff))) { | ||
224 | EncodeUnicode(Value,Buffer+strlen(Start)+1,strlen(Buffer)-(strlen(Start)+1)); | ||
225 | dbgprintf("ReadVCalText is \"%s\"\n",DecodeUnicodeConsole(Value)); | ||
226 | return true; | ||
227 | } | ||
228 | /* SE T68i */ | ||
229 | strcpy(buff,Start); | ||
230 | strcat(buff,";ENCODING=QUOTED-PRINTABLE:"); | ||
231 | if (!strncmp(Buffer,buff,strlen(buff))) { | ||
232 | DecodeUTF8QuotedPrintable(Value,Buffer+strlen(Start)+27,strlen(Buffer)-(strlen(Start)+27)); | ||
233 | dbgprintf("ReadVCalText is \"%s\"\n",DecodeUnicodeConsole(Value)); | ||
234 | return true; | ||
235 | } | ||
236 | strcpy(buff,Start); | ||
237 | strcat(buff,";CHARSET=UTF-8;ENCODING=QUOTED-PRINTABLE:"); | ||
238 | if (!strncmp(Buffer,buff,strlen(buff))) { | ||
239 | DecodeUTF8QuotedPrintable(Value,Buffer+strlen(Start)+41,strlen(Buffer)-(strlen(Start)+41)); | ||
240 | dbgprintf("ReadVCalText is \"%s\"\n",DecodeUnicodeConsole(Value)); | ||
241 | return true; | ||
242 | } | ||
243 | strcpy(buff,Start); | ||
244 | strcat(buff,";CHARSET=UTF-8:"); | ||
245 | if (!strncmp(Buffer,buff,strlen(buff))) { | ||
246 | DecodeUTF8(Value,Buffer+strlen(Start)+15,strlen(Buffer)-(strlen(Start)+15)); | ||
247 | dbgprintf("ReadVCalText is \"%s\"\n",DecodeUnicodeConsole(Value)); | ||
248 | return true; | ||
249 | } | ||
250 | strcpy(buff,Start); | ||
251 | strcat(buff,";CHARSET=UTF-7:"); | ||
252 | if (!strncmp(Buffer,buff,strlen(buff))) { | ||
253 | DecodeUTF7(Value,Buffer+strlen(Start)+15,strlen(Buffer)-(strlen(Start)+15)); | ||
254 | dbgprintf("ReadVCalText is \"%s\"\n",DecodeUnicodeConsole(Value)); | ||
255 | return true; | ||
256 | } | ||
257 | return false; | ||
258 | } | ||
259 | |||
260 | /* How should editor hadle tabs in this file? Add editor commands here. | ||
261 | * vim: noexpandtab sw=8 ts=8 sts=8: | ||
262 | */ | ||