int RingCommadFromPython(PyObject * dict, GSM_RingCommand * cmd) { int i; char *s; i = GetIntFromDict(dict, "Value"); if (i == INT_INVALID) return 0; cmd->Value = i; s = GetCharFromDict(dict, "Type"); if (s == NULL) return 0; cmd->Type = StringToRingCommandType(s); if (cmd->Type == 0) return 0; i = GetIntFromDict(dict, "Tempo"); if (i == INT_INVALID) return 0; cmd->Note.Tempo = i; i = GetIntFromDict(dict, "Scale"); if (i == INT_INVALID) return 0; cmd->Note.Scale = IntToRingNoteScale(i); if (cmd->Note.Scale == 0) return 0; s = GetCharFromDict(dict, "Style"); if (s == NULL) return 0; cmd->Note.Style = StringToRingNoteStyle(s); if (cmd->Note.Style == ENUM_INVALID) return 0; s = GetCharFromDict(dict, "Note"); if (s == NULL) return 0; cmd->Note.Note = StringToRingNoteNote(s); if (cmd->Note.Note == ENUM_INVALID) return 0; s = GetCharFromDict(dict, "DurationSpec"); if (s == NULL) return 0; cmd->Note.DurationSpec = StringToRingNoteDurationSpec(s); if (cmd->Note.DurationSpec == ENUM_INVALID) return 0; s = GetCharFromDict(dict, "Duration"); if (s == NULL) return 0; cmd->Note.Duration = StringToRingNoteDuration(s); if (cmd->Note.Duration == ENUM_INVALID) return 0; return 1; }
int MemoryEntryFromPython(PyObject * dict, GSM_MemoryEntry * entry, int needs_location) { PyObject *o; PyObject *item; Py_ssize_t len, data_len; Py_ssize_t i; char *type, *location; char valuetype; const char *bmptype; if (!PyDict_Check(dict)) { PyErr_Format(PyExc_ValueError, "Memory entry is not a dictionary"); return 0; } memset(entry, 0, sizeof(GSM_MemoryEntry)); entry->Location = GetIntFromDict(dict, "Location"); if (entry->Location == INT_INVALID) { if (needs_location) { return 0; } PyErr_Clear(); } entry->MemoryType = GetMemoryTypeFromDict(dict, "MemoryType"); if (entry->MemoryType == ENUM_INVALID) { if (needs_location) { return 0; } PyErr_Clear(); } o = PyDict_GetItemString(dict, "Entries"); if (o == NULL) { PyErr_Format(PyExc_ValueError, "Can not get string value for key Entries"); return 0; } if (!PyList_Check(o)) { PyErr_Format(PyExc_ValueError, "Key Entries doesn't contain list"); return 0; } len = PyList_Size(o); if (len > GSM_PHONEBOOK_ENTRIES) { pyg_warning("Using just %i entries from list!", GSM_PHONEBOOK_ENTRIES); len = GSM_PHONEBOOK_ENTRIES; } entry->EntriesNum = len; for (i = 0; i < len; i++) { item = PyList_GetItem(o, i); if (item == NULL) return 0; if (!PyDict_Check(item)) { PyErr_Format(PyExc_ValueError, "Element %" PY_FORMAT_SIZE_T "d in Entries is not dictionary", i); return 0; } type = GetCharFromDict(item, "Type"); if (type == NULL) return 0; location = GetCharFromDict(item, "Location"); if (location == NULL) { entry->Entries[i].Location = PBK_Location_Unknown; } else { if (strcmp(location, "Home") == 0) { entry->Entries[i].Location = PBK_Location_Home; } else if (strcmp(location, "Work") == 0) { entry->Entries[i].Location = PBK_Location_Work; } else { entry->Entries[i].Location = PBK_Location_Unknown; } } /* Zero everything, just to be sure */ entry->Entries[i].Text[0] = 0; entry->Entries[i].Text[1] = 0; entry->Entries[i].SMSList[0] = 0; entry->Entries[i].Number = 0; /* Get VoiceTag flag */ entry->Entries[i].VoiceTag = GetIntFromDict(item, "VoiceTag"); if (entry->Entries[i].VoiceTag == INT_INVALID) { entry->Entries[i].VoiceTag = 0; PyErr_Clear(); } /* Get AddError flag */ entry->Entries[i].AddError = GetIntFromDict(item, "AddError"); if (entry->Entries[i].AddError == INT_INVALID) { entry->Entries[i].AddError = ERR_NONE; PyErr_Clear(); } if (strcmp("Number_General", type) == 0) { valuetype = 't'; entry->Entries[i].EntryType = PBK_Number_General; } else if (strcmp("Number_Mobile_Work", type) == 0) { valuetype = 't'; entry->Entries[i].EntryType = PBK_Number_Mobile; entry->Entries[i].Location = PBK_Location_Work; } else if (strcmp("Number_Mobile_Home", type) == 0) { valuetype = 't'; entry->Entries[i].EntryType = PBK_Number_Mobile; entry->Entries[i].Location = PBK_Location_Home; } else if (strcmp("Number_Mobile", type) == 0) { valuetype = 't'; entry->Entries[i].EntryType = PBK_Number_Mobile; } else if (strcmp("Number_Work", type) == 0) { valuetype = 't'; entry->Entries[i].EntryType = PBK_Number_General; entry->Entries[i].Location = PBK_Location_Work; } else if (strcmp("Number_Fax", type) == 0) { valuetype = 't'; entry->Entries[i].EntryType = PBK_Number_Fax; } else if (strcmp("Number_Home", type) == 0) { valuetype = 't'; entry->Entries[i].EntryType = PBK_Number_General; entry->Entries[i].Location = PBK_Location_Home; } else if (strcmp("Number_Pager", type) == 0) { valuetype = 't'; entry->Entries[i].EntryType = PBK_Number_Pager; } else if (strcmp("Number_Other", type) == 0) { valuetype = 't'; entry->Entries[i].EntryType = PBK_Number_Other; } else if (strcmp("Number_Messaging", type) == 0) { valuetype = 't'; entry->Entries[i].EntryType = PBK_Number_Messaging; } else if (strcmp("Number_Video", type) == 0) { valuetype = 't'; entry->Entries[i].EntryType = PBK_Number_Video; } else if (strcmp("Text_Note", type) == 0) { valuetype = 't'; entry->Entries[i].EntryType = PBK_Text_Note; } else if (strcmp("Text_Postal", type) == 0) { valuetype = 't'; entry->Entries[i].EntryType = PBK_Text_Postal; } else if (strcmp("Text_WorkPostal", type) == 0) { valuetype = 't'; entry->Entries[i].EntryType = PBK_Text_Postal; entry->Entries[i].Location = PBK_Location_Work; } else if (strcmp("Text_Email", type) == 0) { valuetype = 't'; entry->Entries[i].EntryType = PBK_Text_Email; } else if (strcmp("Text_Email2", type) == 0) { valuetype = 't'; entry->Entries[i].EntryType = PBK_Text_Email2; } else if (strcmp("Text_URL", type) == 0) { valuetype = 't'; entry->Entries[i].EntryType = PBK_Text_URL; } else if (strcmp("Date", type) == 0) { valuetype = 'd'; entry->Entries[i].EntryType = PBK_Date; } else if (strcmp("LastModified", type) == 0) { valuetype = 'd'; entry->Entries[i].EntryType = PBK_LastModified; } else if (strcmp("Caller_Group", type) == 0) { valuetype = 'n'; entry->Entries[i].EntryType = PBK_Caller_Group; } else if (strcmp("Text_Name", type) == 0) { valuetype = 't'; entry->Entries[i].EntryType = PBK_Text_Name; } else if (strcmp("Text_LastName", type) == 0) { valuetype = 't'; entry->Entries[i].EntryType = PBK_Text_LastName; } else if (strcmp("Text_FirstName", type) == 0) { valuetype = 't'; entry->Entries[i].EntryType = PBK_Text_FirstName; } else if (strcmp("Text_SecondName", type) == 0) { valuetype = 't'; entry->Entries[i].EntryType = PBK_Text_SecondName; } else if (strcmp("Text_NickName", type) == 0) { valuetype = 't'; entry->Entries[i].EntryType = PBK_Text_NickName; } else if (strcmp("Text_FormalName", type) == 0) { valuetype = 't'; entry->Entries[i].EntryType = PBK_Text_FormalName; } else if (strcmp("Text_NameSuffix", type) == 0) { valuetype = 't'; entry->Entries[i].EntryType = PBK_Text_NameSuffix; } else if (strcmp("Text_NamePrefix", type) == 0) { valuetype = 't'; entry->Entries[i].EntryType = PBK_Text_NamePrefix; } else if (strcmp("Text_Company", type) == 0) { valuetype = 't'; entry->Entries[i].EntryType = PBK_Text_Company; } else if (strcmp("Text_JobTitle", type) == 0) { valuetype = 't'; entry->Entries[i].EntryType = PBK_Text_JobTitle; } else if (strcmp("Category", type) == 0) { valuetype = 'o'; entry->Entries[i].EntryType = PBK_Category; } else if (strcmp("Private", type) == 0) { valuetype = 'n'; entry->Entries[i].EntryType = PBK_Private; } else if (strcmp("Text_StreetAddress", type) == 0) { valuetype = 't'; entry->Entries[i].EntryType = PBK_Text_StreetAddress; } else if (strcmp("Text_City", type) == 0) { valuetype = 't'; entry->Entries[i].EntryType = PBK_Text_City; } else if (strcmp("Text_State", type) == 0) { valuetype = 't'; entry->Entries[i].EntryType = PBK_Text_State; } else if (strcmp("Text_Zip", type) == 0) { valuetype = 't'; entry->Entries[i].EntryType = PBK_Text_Zip; } else if (strcmp("Text_Country", type) == 0) { valuetype = 't'; entry->Entries[i].EntryType = PBK_Text_Country; } else if (strcmp("Text_WorkStreetAddress", type) == 0) { valuetype = 't'; entry->Entries[i].EntryType = PBK_Text_StreetAddress; entry->Entries[i].Location = PBK_Location_Work; } else if (strcmp("Text_WorkCity", type) == 0) { valuetype = 't'; entry->Entries[i].EntryType = PBK_Text_City; entry->Entries[i].Location = PBK_Location_Work; } else if (strcmp("Text_WorkState", type) == 0) { valuetype = 't'; entry->Entries[i].EntryType = PBK_Text_State; entry->Entries[i].Location = PBK_Location_Work; } else if (strcmp("Text_WorkZip", type) == 0) { valuetype = 't'; entry->Entries[i].EntryType = PBK_Text_Zip; entry->Entries[i].Location = PBK_Location_Work; } else if (strcmp("Text_WorkCountry", type) == 0) { valuetype = 't'; entry->Entries[i].EntryType = PBK_Text_Country; entry->Entries[i].Location = PBK_Location_Work; } else if (strcmp("Text_Custom1", type) == 0) { valuetype = 't'; entry->Entries[i].EntryType = PBK_Text_Custom1; } else if (strcmp("Text_Custom2", type) == 0) { valuetype = 't'; entry->Entries[i].EntryType = PBK_Text_Custom2; } else if (strcmp("Text_Custom3", type) == 0) { valuetype = 't'; entry->Entries[i].EntryType = PBK_Text_Custom3; } else if (strcmp("Text_Custom4", type) == 0) { valuetype = 't'; entry->Entries[i].EntryType = PBK_Text_Custom4; } else if (strcmp("Text_LUID", type) == 0) { valuetype = 't'; entry->Entries[i].EntryType = PBK_Text_LUID; } else if (strcmp("Text_VOIP", type) == 0) { valuetype = 't'; entry->Entries[i].EntryType = PBK_Text_VOIP; } else if (strcmp("Text_SWIS", type) == 0) { valuetype = 't'; entry->Entries[i].EntryType = PBK_Text_SWIS; } else if (strcmp("Text_WVID", type) == 0) { valuetype = 't'; entry->Entries[i].EntryType = PBK_Text_WVID; } else if (strcmp("Text_SIP", type) == 0) { valuetype = 't'; entry->Entries[i].EntryType = PBK_Text_SIP; } else if (strcmp("Text_DTMF", type) == 0) { valuetype = 't'; entry->Entries[i].EntryType = PBK_Text_DTMF; } else if (strcmp("Text_UserID", type) == 0) { valuetype = 't'; entry->Entries[i].EntryType = PBK_Text_UserID; } else if (strcmp("Text_PictureName", type) == 0) { valuetype = 't'; entry->Entries[i].EntryType = PBK_Text_PictureName; } else if (strcmp("RingtoneID", type) == 0) { valuetype = 'n'; entry->Entries[i].EntryType = PBK_RingtoneID; } else if (strcmp("PictureID", type) == 0) { valuetype = 'n'; entry->Entries[i].EntryType = PBK_PictureID; } else if (strcmp("CallLength", type) == 0) { valuetype = 'c'; entry->Entries[i].EntryType = PBK_CallLength; } else if (strcmp("PushToTalkID", type) == 0) { valuetype = 't'; entry->Entries[i].EntryType = PBK_PushToTalkID; } else if (strcmp("Photo", type) == 0) { valuetype = 'p'; entry->Entries[i].EntryType = PBK_Photo; } else { PyErr_Format(PyExc_ValueError, "Element %" PY_FORMAT_SIZE_T "d in Entries has bad type: %s", i, type); return 0; } switch (valuetype) { case 'o': entry->Entries[i].Number = GetIntFromDict(item, "Value"); if (entry->Entries[i].Number == INT_INVALID) { PyErr_Clear(); if (!CopyStringFromDict (item, "Value", GSM_PHONEBOOK_TEXT_LENGTH, entry->Entries[i].Text)) return 0; } break; case 'n': entry->Entries[i].Number = GetIntFromDict(item, "Value"); if (entry->Entries[i].Number == INT_INVALID) return 0; break; case 'c': entry->Entries[i].CallLength = GetIntFromDict(item, "Value"); if (entry->Entries[i].CallLength == INT_INVALID) return 0; break; case 't': if (!CopyStringFromDict (item, "Value", GSM_PHONEBOOK_TEXT_LENGTH, entry->Entries[i].Text)) return 0; break; case 'd': entry->Entries[i].Date = GetDateTimeFromDict(item, "Value"); if (entry->Entries[i].Date.Year == -1) return 0; break; case 'p': entry->Entries[i].Picture.Buffer = (unsigned char *) GetCStringLengthFromDict(item, "Value", &data_len); entry->Entries[i].Picture.Length = data_len; if (entry->Entries[i].Picture.Buffer == NULL) { return 0; } bmptype = GetCStringFromDict(item, "PictureType"); if (strcmp(bmptype, "BMP") == 0) { entry->Entries[i].Picture.Type = PICTURE_BMP; } else if (strcmp(bmptype, "GIF") == 0) { entry->Entries[i].Picture.Type = PICTURE_GIF; } else if (strcmp(bmptype, "JPG") == 0) { entry->Entries[i].Picture.Type = PICTURE_JPG; } else if (strcmp(bmptype, "ICN") == 0) { entry->Entries[i].Picture.Type = PICTURE_ICN; } else if (strcmp(bmptype, "PNG") == 0) { entry->Entries[i].Picture.Type = PICTURE_PNG; } else { entry->Entries[i].Picture.Type = 0; } break; } } /* end for */ return 1; }
int TodoFromPython(PyObject * dict, GSM_ToDoEntry * entry, int needs_location) { PyObject *o; PyObject *item; Py_ssize_t len; Py_ssize_t i; char *p; char *t; char *type; char valuetype; if (!PyDict_Check(dict)) { PyErr_Format(PyExc_ValueError, "Todo entry is not a dictionary"); return 0; } memset(entry, 0, sizeof(GSM_ToDoEntry)); if (needs_location) { entry->Location = GetIntFromDict(dict, "Location"); if (entry->Location == INT_INVALID) return 0; } t = GetCharFromDict(dict, "Type"); if (t == NULL) return 0; entry->Type = StringToCalendarType(t); free(t); if (entry->Type == 0) return 0; p = GetCharFromDict(dict, "Priority"); if (p == NULL) return 0; entry->Priority = StringToTodoPriority(p); free(p); if (entry->Priority == GSM_Priority_INVALID) return 0; o = PyDict_GetItemString(dict, "Entries"); if (o == NULL) { PyErr_Format(PyExc_ValueError, "Can not get string value for key Values"); return 0; } if (!PyList_Check(o)) { PyErr_Format(PyExc_ValueError, "Key Values doesn't contain list"); return 0; } len = PyList_Size(o); if (len > GSM_TODO_ENTRIES) { pyg_warning("Using just %i entries from list!", GSM_TODO_ENTRIES); len = GSM_TODO_ENTRIES; } entry->EntriesNum = len; for (i = 0; i < len; i++) { item = PyList_GetItem(o, i); if (item == NULL) return 0; if (!PyDict_Check(item)) { PyErr_Format(PyExc_ValueError, "Element %" PY_FORMAT_SIZE_T "d in Values is not dictionary", i); return 0; } type = GetCharFromDict(item, "Type"); if (type == NULL) return 0; if (strcmp("END_DATETIME", type) == 0) { valuetype = 'd'; entry->Entries[i].EntryType = TODO_END_DATETIME; } else if (strcmp("START_DATETIME", type) == 0) { valuetype = 'd'; entry->Entries[i].EntryType = TODO_START_DATETIME; } else if (strcmp("COMPLETED_DATETIME", type) == 0) { valuetype = 'd'; entry->Entries[i].EntryType = TODO_COMPLETED_DATETIME; } else if (strcmp("COMPLETED", type) == 0) { valuetype = 'n'; entry->Entries[i].EntryType = TODO_COMPLETED; } else if (strcmp("ALARM_DATETIME", type) == 0) { valuetype = 'd'; entry->Entries[i].EntryType = TODO_ALARM_DATETIME; } else if (strcmp("SILENT_ALARM_DATETIME", type) == 0) { valuetype = 'd'; entry->Entries[i].EntryType = TODO_SILENT_ALARM_DATETIME; } else if (strcmp("LAST_MODIFIED", type) == 0) { valuetype = 'd'; entry->Entries[i].EntryType = TODO_LAST_MODIFIED; } else if (strcmp("LUID", type) == 0) { valuetype = 't'; entry->Entries[i].EntryType = TODO_LUID; } else if (strcmp("LOCATION", type) == 0) { valuetype = 't'; entry->Entries[i].EntryType = TODO_LOCATION; } else if (strcmp("DESCRIPTION", type) == 0) { valuetype = 't'; entry->Entries[i].EntryType = TODO_DESCRIPTION; } else if (strcmp("TEXT", type) == 0) { valuetype = 't'; entry->Entries[i].EntryType = TODO_TEXT; } else if (strcmp("PRIVATE", type) == 0) { valuetype = 'n'; entry->Entries[i].EntryType = TODO_PRIVATE; } else if (strcmp("CATEGORY", type) == 0) { valuetype = 'n'; entry->Entries[i].EntryType = TODO_CATEGORY; } else if (strcmp("CONTACTID", type) == 0) { valuetype = 'n'; entry->Entries[i].EntryType = TODO_CONTACTID; } else if (strcmp("PHONE", type) == 0) { valuetype = 't'; entry->Entries[i].EntryType = TODO_PHONE; } else { PyErr_Format(PyExc_ValueError, "Element %" PY_FORMAT_SIZE_T "d in Values has bad type: %s", i, type); free(type); return 0; } free(type); switch (valuetype) { case 'n': entry->Entries[i].Number = GetIntFromDict(item, "Value"); if (entry->Entries[i].Number == INT_INVALID) return 0; break; case 't': if (!CopyStringFromDict (item, "Value", GSM_PHONEBOOK_TEXT_LENGTH, entry->Entries[i].Text)) return 0; break; case 'd': entry->Entries[i].Date = GetDateTimeFromDict(item, "Value"); if (entry->Entries[i].Date.Year == -1) return 0; break; } } /* end for */ return 1; }
int CalendarFromPython(PyObject * dict, GSM_CalendarEntry * entry, int needs_location) { PyObject *o; PyObject *item; Py_ssize_t len; Py_ssize_t i; char *t; char *type; char valuetype; if (!PyDict_Check(dict)) { PyErr_Format(PyExc_ValueError, "Calendar entry is not a dictionary"); return 0; } memset(entry, 0, sizeof(GSM_CalendarEntry)); if (needs_location) { entry->Location = GetIntFromDict(dict, "Location"); if (entry->Location == INT_INVALID) return 0; } t = GetCharFromDict(dict, "Type"); if (t == NULL) return 0; entry->Type = StringToCalendarType(t); free(t); if (entry->Type == 0) return 0; o = PyDict_GetItemString(dict, "Entries"); if (o == NULL) { PyErr_Format(PyExc_ValueError, "Can not get string value for key Values"); return 0; } if (!PyList_Check(o)) { PyErr_Format(PyExc_ValueError, "Key Values doesn't contain list"); return 0; } len = PyList_Size(o); if (len > GSM_CALENDAR_ENTRIES) { pyg_warning("Using just %i entries from list!", GSM_CALENDAR_ENTRIES); len = GSM_CALENDAR_ENTRIES; } entry->EntriesNum = len; for (i = 0; i < len; i++) { item = PyList_GetItem(o, i); if (item == NULL) return 0; if (!PyDict_Check(item)) { PyErr_Format(PyExc_ValueError, "Element %" PY_FORMAT_SIZE_T "d in Values is not dictionary", i); return 0; } type = GetCharFromDict(item, "Type"); if (type == NULL) return 0; if (strcmp("START_DATETIME", type) == 0) { valuetype = 'd'; entry->Entries[i].EntryType = CAL_START_DATETIME; } else if (strcmp("END_DATETIME", type) == 0) { valuetype = 'd'; entry->Entries[i].EntryType = CAL_END_DATETIME; } else if (strcmp("TONE_ALARM_DATETIME", type) == 0) { valuetype = 'd'; entry->Entries[i].EntryType = CAL_TONE_ALARM_DATETIME; } else if (strcmp("SILENT_ALARM_DATETIME", type) == 0) { valuetype = 'd'; entry->Entries[i].EntryType = CAL_SILENT_ALARM_DATETIME; } else if (strcmp("LAST_MODIFIED", type) == 0) { valuetype = 'd'; entry->Entries[i].EntryType = CAL_LAST_MODIFIED; } else if (strcmp("REPEAT_STARTDATE", type) == 0) { valuetype = 'D'; entry->Entries[i].EntryType = CAL_REPEAT_STARTDATE; } else if (strcmp("REPEAT_STOPDATE", type) == 0) { valuetype = 'D'; entry->Entries[i].EntryType = CAL_REPEAT_STOPDATE; } else if (strcmp("TEXT", type) == 0) { valuetype = 't'; entry->Entries[i].EntryType = CAL_TEXT; } else if (strcmp("DESCRIPTION", type) == 0) { valuetype = 't'; entry->Entries[i].EntryType = CAL_DESCRIPTION; } else if (strcmp("LUID", type) == 0) { valuetype = 't'; entry->Entries[i].EntryType = CAL_LUID; } else if (strcmp("LOCATION", type) == 0) { valuetype = 't'; entry->Entries[i].EntryType = CAL_LOCATION; } else if (strcmp("PHONE", type) == 0) { valuetype = 't'; entry->Entries[i].EntryType = CAL_PHONE; } else if (strcmp("PRIVATE", type) == 0) { valuetype = 'n'; entry->Entries[i].EntryType = CAL_PRIVATE; } else if (strcmp("CONTACTID", type) == 0) { valuetype = 'n'; entry->Entries[i].EntryType = CAL_CONTACTID; } else if (strcmp("REPEAT_DAYOFWEEK", type) == 0) { valuetype = 'n'; entry->Entries[i].EntryType = CAL_REPEAT_DAYOFWEEK; } else if (strcmp("REPEAT_DAY", type) == 0) { valuetype = 'n'; entry->Entries[i].EntryType = CAL_REPEAT_DAY; } else if (strcmp("REPEAT_WEEKOFMONTH", type) == 0) { valuetype = 'n'; entry->Entries[i].EntryType = CAL_REPEAT_WEEKOFMONTH; } else if (strcmp("REPEAT_MONTH", type) == 0) { valuetype = 'n'; entry->Entries[i].EntryType = CAL_REPEAT_MONTH; } else if (strcmp("REPEAT_FREQUENCY", type) == 0) { valuetype = 'n'; entry->Entries[i].EntryType = CAL_REPEAT_FREQUENCY; } else if (strcmp("REPEAT_COUNT", type) == 0) { valuetype = 'n'; entry->Entries[i].EntryType = CAL_REPEAT_COUNT; } else if (strcmp("REPEAT_DAYOFYEAR", type) == 0) { valuetype = 'n'; entry->Entries[i].EntryType = CAL_REPEAT_DAYOFYEAR; } else { PyErr_Format(PyExc_ValueError, "Element %" PY_FORMAT_SIZE_T "d in Values has bad type: %s", i, type); free(type); return 0; } free(type); switch (valuetype) { case 'n': entry->Entries[i].Number = GetIntFromDict(item, "Value"); if (entry->Entries[i].Number == INT_INVALID) return 0; break; case 't': if (!CopyStringFromDict (item, "Value", GSM_PHONEBOOK_TEXT_LENGTH, entry->Entries[i].Text)) return 0; break; case 'D': entry->Entries[i].Date = GetDateFromDict(item, "Value"); if (entry->Entries[i].Date.Year == -1) return 0; break; case 'd': entry->Entries[i].Date = GetDateTimeFromDict(item, "Value"); if (entry->Entries[i].Date.Year == -1) return 0; break; } /* Clear AddError flag */ entry->Entries[i].AddError = ERR_NONE; } /* end for */ return 1; }
int BitmapFromPython(PyObject * dict, GSM_Bitmap * entry) { char *s; int i, j; int w, h, cols, chars; char c, black = 0, transp = 0; char buffer[1000]; int x, y; PyObject *o; PyObject *item; int len; if (!PyDict_Check(dict)) { PyErr_Format(PyExc_ValueError, "Bitmap is not a dictionary"); return 0; } memset(entry, 0, sizeof(GSM_Bitmap)); s = GetCharFromDict(dict, "Type"); if (s == NULL) return 0; entry->Type = StringToBitmapType(s); if (entry->Type == 0) return 0; s = GetCharFromDict(dict, "NetworkCode"); if (s == NULL) { PyErr_Clear(); entry->NetworkCode[0] = 0; } else { mystrncpy(entry->NetworkCode, s, 6); } i = GetIntFromDict(dict, "Location"); if (i == INT_INVALID) { PyErr_Clear(); } else { entry->Location = i; } i = GetIntFromDict(dict, "BitmapEnabled"); if (i == INT_INVALID) { PyErr_Clear(); } else { entry->BitmapEnabled = i; } i = GetIntFromDict(dict, "DefaultName"); if (i == INT_INVALID) { PyErr_Clear(); } else { entry->DefaultName = i; } i = GetIntFromDict(dict, "DefaultBitmap"); if (i == INT_INVALID) { PyErr_Clear(); } else { entry->DefaultBitmap = i; } i = GetIntFromDict(dict, "DefaultRingtone"); if (i == INT_INVALID) { PyErr_Clear(); } else { entry->DefaultRingtone = i; } i = GetIntFromDict(dict, "RingtoneID"); if (i == INT_INVALID) { PyErr_Clear(); } else { entry->RingtoneID = i; } i = GetIntFromDict(dict, "ID"); if (i == INT_INVALID) { PyErr_Clear(); } else { entry->ID = i; } if (!CopyStringFromDict (dict, "Text", GSM_BITMAP_TEXT_LENGTH, entry->Text)) { PyErr_Clear(); entry->Text[0] = 0; entry->Text[1] = 0; } if (!CopyStringFromDict (dict, "Sender", GSM_MAX_NUMBER_LENGTH, entry->Sender)) { PyErr_Clear(); entry->Text[0] = 0; entry->Text[1] = 0; } o = PyDict_GetItemString(dict, "XPM"); if (o == NULL) { PyErr_Format(PyExc_ValueError, "Could not get XPM for bitmap!"); return 0; } if (!PyList_Check(o)) { PyErr_Format(PyExc_ValueError, "XPM isn't list!"); return 0; } len = PyList_Size(o); if (len < 3) { PyErr_Format(PyExc_ValueError, "XPM list too small!"); return 0; } #define GetString(s, x) \ item = PyList_GetItem(o, x);\ if (!PyString_Check(item)) {\ PyErr_Format(PyExc_ValueError, "XPM contains something different than string!");\ return 0;\ }\ \ s = PyString_AsString(item);\ if (s == NULL) {\ PyErr_Format(PyExc_ValueError, "XPM contains something different than string!");\ return 0;\ } GetString(s, 0); if (sscanf(s, "%d %d %d %d", &w, &h, &cols, &chars) != 4) { PyErr_Format(PyExc_ValueError, "Bad first XPM row"); return 0; } if (chars != 1 || cols != 2) { PyErr_Format(PyExc_ValueError, "Only two color XPM with one char per pixel supported"); return 0; } if (w > 255 || h > 255 || w < 0 || h < 0 || w * h / 8 > GSM_BITMAP_SIZE) { PyErr_Format(PyExc_ValueError, "Bad size of bitmap"); return 0; } entry->BitmapWidth = w; entry->BitmapHeight = h; for (i = 1; i < 3; i++) { GetString(s, i); if (sscanf(s, "%c c %999s", &c, buffer) != 2) { PyErr_Format(PyExc_ValueError, "Can not parse XPM line: '%s'", s); return 0; } j = 0; while (buffer[j] != 0) { buffer[j] = tolower((int)buffer[j]); j++; } if (strcmp(buffer, "none") == 0 || strcmp(buffer, "#fff") == 0 || strcmp(buffer, "#ffffff") == 0 || strcmp(buffer, "white") == 0) transp = c; else if (strcmp(buffer, "#000") == 0 || strcmp(buffer, "#000000") == 0 || strcmp(buffer, "black") == 0) black = c; else { PyErr_Format(PyExc_ValueError, "Only black and write are supported (guven was %s)", buffer); return 0; } } if (black == 0 || transp == 0) { PyErr_Format(PyExc_ValueError, "At least one XPM color was not found"); return 0; } for (y = 0; y < h; y++) { GetString(s, y + 3); if ((ssize_t) strlen(s) != w) { PyErr_Format(PyExc_ValueError, "XPM line has bad length: '%s'", s); return 0; } for (x = 0; x < w; x++) { if (s[x] == black) GSM_SetPointBitmap(entry, x, y); else if (s[x] == transp) GSM_ClearPointBitmap(entry, x, y); else { PyErr_Format(PyExc_ValueError, "Bad character in XPM data: '%c'", s[x]); return 0; } } } return 1; }
int FileFromPython(PyObject * dict, GSM_File * file, gboolean check) { char *s; GSM_DateTime nulldt = { 0, 0, 0, 0, 0, 0, 0 }; Py_ssize_t i; if (!PyDict_Check(dict)) { PyErr_Format(PyExc_ValueError, "File is not a dictionary"); return 0; } memset(file, 0, sizeof(file)); if ((file->Used = GetIntFromDict(dict, "Used")) == INT_INVALID) { // We don't need Used in all cases, we can get size from buffer PyErr_Clear(); } if (!CopyStringFromDict(dict, "Name", 300, file->Name)) { if (check) { return 0; } else { PyErr_Clear(); } } if ((i = GetIntFromDict(dict, "Folder")) == INT_INVALID) { if (check) { return 0; } else { PyErr_Clear(); } } else { file->Folder = i; } if ((file->Level = GetIntFromDict(dict, "Level")) == INT_INVALID) { if (check) { return 0; } else { PyErr_Clear(); } } s = GetCharFromDict(dict, "Type"); if (s == NULL) { file->Type = 0; if (check) { return 0; } else { PyErr_Clear(); } } else { file->Type = StringToFileType(s); if (file->Type == ENUM_INVALID) return 0; } if (!CopyStringFromDict(dict, "ID_FullName", 800, file->ID_FullName)) { if (check) { return 0; } else { PyErr_Clear(); } } s = GetDataFromDict(dict, "Buffer", &i); if (s == NULL) { file->Buffer = NULL; file->Used = 0; if (check) { return 0; } else { PyErr_Clear(); } } else { if (file->Used == INT_INVALID) { file->Used = i; } else if (file->Used != (size_t) i) { PyErr_Format(PyExc_ValueError, "Used and Buffer size mismatch in File (%li != %" PY_FORMAT_SIZE_T "i)!", (long)file->Used, i); return 0; } file->Buffer = (unsigned char *)malloc(i * sizeof(unsigned char)); if (file->Buffer == NULL) { PyErr_Format(PyExc_MemoryError, "Not enough memory to allocate buffer (wanted %li bytes)", (long)(i * sizeof(unsigned char))); return 0; } memcpy(file->Buffer, s, i); } file->Modified = GetDateTimeFromDict(dict, "Modified"); if (file->Modified.Year == -1) { file->Modified = nulldt; file->ModifiedEmpty = TRUE; PyErr_Clear(); } else { file->ModifiedEmpty = FALSE; } if ((i = GetIntFromDict(dict, "Protected")) == INT_INVALID) { if (check) { free(file->Buffer); file->Buffer = NULL; return 0; } else { PyErr_Clear(); } } else { file->Protected = i; } if ((i = GetIntFromDict(dict, "ReadOnly")) == INT_INVALID) { if (check) { free(file->Buffer); file->Buffer = NULL; return 0; } else { PyErr_Clear(); } } else { file->ReadOnly = i; } if ((i = GetIntFromDict(dict, "Hidden")) == INT_INVALID) { if (check) { free(file->Buffer); file->Buffer = NULL; return 0; } else { PyErr_Clear(); } } else { file->Hidden = i; } if ((i = GetIntFromDict(dict, "System")) == INT_INVALID) { if (check) { free(file->Buffer); file->Buffer = NULL; return 0; } else { PyErr_Clear(); } } else { file->System = i; } return 1; }
/* ----------------------------------------------------------------------------- l2tpvpn_listen - called by vpnd to setup listening socket ----------------------------------------------------------------------------- */ int l2tpvpn_listen(void) { char *errstr; if (listen_sockfd <= 0) return -1; //set_flag(listen_sockfd, kerneldebug & 1, L2TP_FLAG_DEBUG); set_flag(listen_sockfd, 1, L2TP_FLAG_CONTROL); set_flag(listen_sockfd, !opt_noipsec, L2TP_FLAG_IPSEC); /* unknown src and dst addresses */ any_address.sin_len = sizeof(any_address); any_address.sin_family = AF_INET; any_address.sin_port = 0; any_address.sin_addr.s_addr = INADDR_ANY; /* bind the socket in the kernel with L2TP port */ listen_address.sin_len = sizeof(listen_address); listen_address.sin_family = AF_INET; listen_address.sin_port = htons(L2TP_UDP_PORT); listen_address.sin_addr.s_addr = INADDR_ANY; l2tp_set_ouraddress(listen_sockfd, (struct sockaddr *)&listen_address); our_address = listen_address; /* add security policies */ if (!opt_noipsec) { CFStringRef auth_method; CFStringRef string; CFDataRef data; uint32_t natt_multiple_users; /* get authentication method from the IPSec dict */ auth_method = CFDictionaryGetValue(ipsec_settings, kRASPropIPSecAuthenticationMethod); if (!isString(auth_method)) auth_method = kRASValIPSecAuthenticationMethodSharedSecret; /* get setting for nat traversal multiple user support - default is enabled for server */ GetIntFromDict(ipsec_settings, kRASPropIPSecNattMultipleUsersEnabled, &natt_multiple_users, 1); ipsec_dict = IPSecCreateL2TPDefaultConfiguration( (struct sockaddr *)&our_address, (struct sockaddr *)&any_address, NULL, auth_method, 0, natt_multiple_users, 0); /* set the authentication information */ if (CFEqual(auth_method, kRASValIPSecAuthenticationMethodSharedSecret)) { string = CFDictionaryGetValue(ipsec_settings, kRASPropIPSecSharedSecret); if (isString(string)) CFDictionarySetValue(ipsec_dict, kRASPropIPSecSharedSecret, string); else if (isData(string) && ((CFDataGetLength((CFDataRef)string) % sizeof(UniChar)) == 0)) { CFStringEncoding encoding; data = (CFDataRef)string; #if __BIG_ENDIAN__ encoding = (*(CFDataGetBytePtr(data) + 1) == 0x00) ? kCFStringEncodingUTF16LE : kCFStringEncodingUTF16BE; #else // __LITTLE_ENDIAN__ encoding = (*(CFDataGetBytePtr(data) ) == 0x00) ? kCFStringEncodingUTF16BE : kCFStringEncodingUTF16LE; #endif string = CFStringCreateWithBytes(NULL, (const UInt8 *)CFDataGetBytePtr(data), CFDataGetLength(data), encoding, FALSE); CFDictionarySetValue(ipsec_dict, kRASPropIPSecSharedSecret, string); CFRelease(string); } string = CFDictionaryGetValue(ipsec_settings, kRASPropIPSecSharedSecretEncryption); if (isString(string)) CFDictionarySetValue(ipsec_dict, kRASPropIPSecSharedSecretEncryption, string); } else if (CFEqual(auth_method, kRASValIPSecAuthenticationMethodCertificate)) { data = CFDictionaryGetValue(ipsec_settings, kRASPropIPSecLocalCertificate); if (isData(data)) CFDictionarySetValue(ipsec_dict, kRASPropIPSecLocalCertificate, data); } if (IPSecApplyConfiguration(ipsec_dict, &errstr) || IPSecInstallPolicies(ipsec_dict, -1, &errstr)) { vpnlog(LOG_ERR, "L2TP plugin: cannot configure secure transport (%s).\n", errstr); IPSecRemoveConfiguration(ipsec_dict, &errstr); CFRelease(ipsec_dict); ipsec_dict = 0; return -1; } /* set IPSec Key management to prefer most recent key */ if (IPSecSetSecurityAssociationsPreference(&key_preference, 0)) vpnlog(LOG_ERR, "L2TP plugin: cannot set IPSec Key management preference (error %d)\n", errno); sick_timeleft = IPSEC_SICK_TIME; ping_timeleft = 0; } return listen_sockfd; }