5 A Linux/Unix toolset and driver for Nokia mobile phones.
7 Copyright (C) 1999, 2000 Hugh Blemings & Pavel JanÃk ml.
9 Released under the terms of the GNU GPL, see file COPYING for more details.
11 Functions to read and write common file types.
24 #include "gsm-common.h"
25 #include "gsm-ringtones.h"
26 #include "gsm-bitmaps.h"
27 #include "files/gsm-filetypes.h"
28 #include "files/midifile.h"
29 #include "gsm-coding.h"
31 #include "newmodules/n7110.h"
32 #include "newmodules/n6110.h"
41 * Fills vCalendar time string into GSM_DateTime structure
44 * dt: datetime structure
45 * time: string in format yyyymmddThhmmss
49 int GetvCalTime(GSM_DateTime *dt, char *time)
51 char year[5]="", month[3]="", day[3]="", hour[3]="", minute[3]="", second[3]="";
52 dt->Year=dt->Month=dt->Day=dt->Hour=dt->Minute=dt->Second=dt->Timezone=0;
54 strncpy(year, time, 4);
55 strncpy(month, time+4, 2);
56 strncpy(day, time+6, 2);
57 strncpy(hour, time+9, 2);
58 strncpy(minute, time+11, 2);
59 strncpy(second, time+13, 2);
61 /* FIXME: Should check ranges... */
63 dt->Month=atoi(month);
66 dt->Minute=atoi(minute);
67 dt->Second=atoi(second);
77 * Fills calendar data from strings into calendar note
80 * note: calendar note structure
81 * type: type of calendar note
82 * text: text or phonenumber
83 * time: string in format yyyymmddThhmmss
88 int FillCalendarNote(GSM_CalendarNote *note, char *type,
89 char *text, char *time, char *alarm)
91 GetvCalTime(¬e->Time, time);
92 GetvCalTime(¬e->Alarm, alarm);
96 strncpy(note->Text, text, MAX_CALENDAR_TEXT_LENGTH);
97 strcpy(note->Phone, ""); /* correct in most cases */
99 /* FIXME: Handle additional strings, maybe from configuration file */
101 if(!strcmp(type, "PHONE CALL"))
103 strncpy(note->Phone, text, MAX_CALENDAR_PHONE_LENGTH);
106 else if(!strcmp(type, "MEETING"))
107 note->Type=GCN_MEETING;
108 else if(!strcmp(type, "SPECIAL OCCASION"))
109 note->Type=GCN_BIRTHDAY;
111 note->Type=GCN_REMINDER;
117 * GSM_ReadVCalendarFile
119 * Reads vCalendar file
122 * FileName: name of vCalendar file
123 * cnote: pointer to calendar note
124 * number: number in file of calendar note to read
128 GSM_Error GSM_ReadVCalendarFile(char *FileName, GSM_CalendarNote *cnote, int *number)
131 char type[21]="", text[40]="", time[16]="", alarm[16]="";
141 char *Line, OLine[1024], BackLine[1024];
145 file=fopen(FileName, "r");
148 fprintf(stderr, _("File cannot be opened!\n"));
150 return GE_CANTOPENFILE;
153 /* Go through data from file. */
154 while (GetLine(file, Line, sizeof(OLine))!=-1) {
156 strcpy(BackLine, Line);
160 if (!strcmp(Line,"BEGIN:VCALENDAR"))
164 if (!strcmp(Line,"BEGIN:VEVENT")) {
168 if (!strcmp(Line,"END:VCALENDAR"))
172 if (veventcounter==*number) {
173 if (!strncmp(Line,"CATEGORIES:",11)) {
174 strncpy(type,Line+11,strlen(Line)-11);
175 type[strlen(Line)-11]=0;
177 if (!strncmp(Line,"DESCRIPTION:",12)) {
178 strncpy(phone,Line+12,strlen(Line)-12);
179 phone[strlen(Line)-12]=0;
181 if (!strncmp(Line,"SUMMARY:",8)) {
182 strncpy(text,Line+8,strlen(Line)-8);
183 text[strlen(Line)-8]=0;
185 if (!strncmp(Line,"SUMMARY;CHARSET=UTF-8;ENCODING=QUOTED-PRINTABLE:",48)) {
186 DecodeUTF8(text,Line+48,strlen(Line)-48);
187 text[strlen(Line)-48]=0;
189 if (!strncmp(Line,"DTSTART:",8)) {
190 strncpy(time,Line+8,strlen(Line)-8);
191 time[strlen(Line)-8]=0;
193 if (!strncmp(Line,"DALARM:",7)) {
194 strncpy(alarm,Line+7,strlen(Line)-7);
195 alarm[strlen(Line)-7]=0;
199 if (!strncmp(Line,"RECURR:",7)) {
200 recurr=mem_to_int(Line+7,strlen(Line)-7);
203 if (!strncmp(Line,"RRULE:D1 :",9)) {
206 if (!strncmp(Line,"RRULE:W1 :",9)) {
209 if (!strncmp(Line,"RRULE:W2 :",9)) {
212 if (!strncmp(Line,"RRULE:YD1 :",10)) {
216 if (!strncmp(Line,"ALTYPE:",7)) {
217 altype=(!strncmp("TONE",Line+7,4)) ? 0x00 : 0x01;
220 if (!strcmp(Line,"END:VEVENT")) {
221 if (veventcounter==*number) NoteOK=true;
231 *number=veventcounter;
233 fprintf(stdout,_("Note not found in VCalendarfile\n"));
238 FillCalendarNote(cnote, type, text, time, alarm);
240 cnote->Recurrance = recurr*24; /* it was in days. I convert in hours */
241 cnote->AlarmType = altype;
243 if( strcmp( phone, "" ) ) { /* Invert data if CALL ... */
244 strcpy( cnote->Text, phone );
245 strcpy( cnote->Phone, text ); // alread FillCalendar does it ..
253 GSM_Error GSM_ReadBinRingtoneFile(char *FileName, GSM_BinRingtone *ringtone)
258 file = fopen(FileName, "rb");
261 return(GE_CANTOPENFILE);
263 ringtone->length=fread(ringtone->frame, 1, 500, file);
267 if (ringtone->frame[0]!=0x00 || ringtone->frame[1]!=0x00 ||
268 ringtone->frame[2]!=0x0C || ringtone->frame[3]!=0x01)
269 return GE_NOTSUPPORTED;
272 while (ringtone->frame[i]!=0x00) {
273 ringtone->name[i-5]=ringtone->frame[i];
276 ringtone->name[i-5]=0x00;
281 /* Function to convert scale field in to correct number. */
282 int GetRTTLDuration (char *num)
289 case 1: duration=128; break;
290 case 2: duration= 64; break;
291 case 4: duration= 32; break;
292 case 8: duration= 16; break;
293 case 16: duration= 8; break;
294 case 32: duration= 4; break;
301 int GetRTTLScale (char *num)
304 /* This may well need improving. */
308 if ((atoi(num))<4) scale=(atoi(num));
309 if ((atoi(num))>4) scale=(atoi(num))-4;
314 GSM_Error GSM_ReadRingtoneFile(char *FileName, GSM_Ringtone *ringtone)
317 unsigned char buffer[300];
319 GSM_Filetypes filetype=RTTL;
321 file = fopen(FileName, "rb");
324 return(GE_CANTOPENFILE);
326 fread(buffer, 1, 4, file); /* Read the header of the file. */
328 /* Attempt to identify filetype */
330 if (memcmp(buffer, "MThd",3)==0) /* MIDI files have 'MThd' at the start */
333 if (buffer[0]==0xc7 && buffer[1]==0x45 && buffer[2]==0xc1 && buffer[3]==0x53)
334 filetype=COMMUNICATOR;
336 if (strstr(FileName,".ott")) filetype=OTT; /* OTT files saved by NCDS3 */
344 error=loadrttl(file,ringtone);
348 error=loadott(file,ringtone);
352 error=loadcommunicator(file,ringtone);
357 error=loadmid(FileName,ringtone);
360 error=GE_INVALIDFILEFORMAT;
367 GSM_Error loadott(FILE *file, GSM_Ringtone *ringtone)
372 i=fread(Buffer, 1, 2000, file);
374 if (!feof(file)) return GE_TOOLONG;
376 return GSM_UnPackRingtone(ringtone, Buffer, i);
379 GSM_Error loadcommunicator(FILE *file, GSM_Ringtone *ringtone)
384 i=fread(Buffer, 1, 4000, file);
386 if (!feof(file)) return GE_TOOLONG;
390 if (Buffer[j]==0x00 && Buffer[j+1]==0x02 &&
391 Buffer[j+2]==0x4a && Buffer[j+3]==0x3a) break;
392 if (j==i-4) return GE_INTERNALERROR;
397 return GSM_UnPackRingtone(ringtone, Buffer+j, i-j);
400 /* TODO: spaces should not be interpreted */
401 /* Note: ringtone have to be in one line (without 0x13 and 0x10 chars) */
402 GSM_Error loadrttl(FILE *file, GSM_Ringtone *ringtone)
406 u8 DefNoteScale=2, DefNoteDuration=4;
408 u8 DefNoteStyle=ContinuousStyle;
410 unsigned char buffer[2000];
411 unsigned char *def, *notes, *ptr;
413 ringtone->Loop=15; //default value
415 fread(buffer, 2000, 1, file);
417 /* This is for buggy RTTTL ringtones without name. */
418 if (buffer[0] != RTTTL_SEP[0]) {
419 strtok(buffer, RTTTL_SEP);
420 sprintf(ringtone->name, "%s", buffer);
421 def=strtok(NULL, RTTTL_SEP);
422 notes=strtok(NULL, RTTTL_SEP);
425 sprintf(ringtone->name, "GNOKII");
426 def=strtok(buffer, RTTTL_SEP);
427 notes=strtok(NULL, RTTTL_SEP);
430 ptr=strtok(def, ", ");
432 /* Parsing the <defaults> section. */
438 DefNoteDuration=GetRTTLDuration(ptr+2);
442 DefNoteScale=GetRTTLScale(ptr+2);
446 DefNoteTempo=atoi(ptr+2);
450 ringtone->Loop=atoi(ptr+2);
457 DefNoteStyle=ContinuousStyle;
461 DefNoteStyle=NaturalStyle;
465 DefNoteStyle=StaccatoStyle;
471 DefNoteStyle=ContinuousStyle;
475 DefNoteStyle=NaturalStyle;
479 DefNoteStyle=StaccatoStyle;
485 ptr=strtok(NULL,", ");
489 printf("DefNoteDuration=%d\n", DefNoteDuration);
490 printf("DefNoteScale=%d\n", DefNoteScale);
493 ptr=strtok(notes, ", ");
495 /* Parsing the <note-command>+ section. */
496 while (ptr && NrNote<MAX_RINGTONE_NOTES) {
501 DefNoteScale=GetRTTLScale(ptr+2);
508 DefNoteStyle=ContinuousStyle;
512 DefNoteStyle=NaturalStyle;
516 DefNoteStyle=StaccatoStyle;
522 DefNoteStyle=ContinuousStyle;
526 DefNoteStyle=NaturalStyle;
530 DefNoteStyle=StaccatoStyle;
536 ringtone->notes[NrNote].duration=GetRTTLDuration(ptr);
537 if (ringtone->notes[NrNote].duration==0)
538 ringtone->notes[NrNote].duration=DefNoteDuration;
540 /* Skip all numbers in duration specification. */
545 /* B or b is not in specs, but I decided to put it, because
546 it's in some RTTL files. It's the same to H note */
547 if ((*ptr=='B') || (*ptr=='b')) ringtone->notes[NrNote].note=12;
548 else if ((*ptr=='H') || (*ptr=='h')) ringtone->notes[NrNote].note=12;
549 else if ((*ptr>='a') && (*ptr<='g')) ringtone->notes[NrNote].note=((*ptr-'a')*2)+10;
550 else if ((*ptr>='A') && (*ptr<='G')) ringtone->notes[NrNote].note=((*ptr-'A')*2)+10;
551 else ringtone->notes[NrNote].note=255;
553 if ((ringtone->notes[NrNote].note>13)&&(ringtone->notes[NrNote].note!=255))
554 ringtone->notes[NrNote].note-=14;
559 ringtone->notes[NrNote].note++;
560 if ((ringtone->notes[NrNote].note==5) || (ringtone->notes[NrNote].note==13))
561 ringtone->notes[NrNote].note++;
565 /* Check for dodgy rttl */
566 /* [<special-duration>] */
568 ringtone->notes[NrNote].duration*=1.5;
573 if (ringtone->notes[NrNote].note!=255) {
575 ringtone->notes[NrNote].note+=GetRTTLScale(ptr)*14;
578 ringtone->notes[NrNote].note+=DefNoteScale*14;
581 /* [<special-duration>] */
583 ringtone->notes[NrNote].duration*=1.5;
588 ringtone->notes[NrNote].style=DefNoteStyle;
591 ringtone->notes[NrNote].tempo=DefNoteTempo;
597 ptr=strtok(NULL, ", ");
600 ringtone->NrNotes=NrNote;
605 GSM_Error GSM_SaveRingtoneFile(char *FileName, GSM_Ringtone *ringtone)
611 file = fopen(FileName, "wb");
614 return(GE_CANTOPENFILE);
616 if (strstr(FileName,".ott"))
618 saveott(file, ringtone);
621 if (strstr(FileName,".mid"))
623 savemid(file, ringtone);
627 if (!done) saverttl(file, ringtone);
634 void saveott(FILE *file, GSM_Ringtone *ringtone)
640 GSM_PackRingtone(ringtone, Buffer, &i);
642 fwrite(Buffer, 1, i, file);
645 void saverttl(FILE *file, GSM_Ringtone *ringtone)
647 u8 DefNoteScale=2, DefNoteDuration=4;
649 u8 DefNoteStyle=ContinuousStyle;
655 /* Saves ringtone name */
656 fprintf(file,_("%s:"),ringtone->name);
658 /* Find the most frequently used duration and use this for the default */
660 for (i=0;i<6;i++) buffer[i]=0;
661 for (i=0;i<ringtone->NrNotes;i++) {
662 switch (ringtone->notes[i].duration) {
663 case 192:buffer[0]++; break;
664 case 128:buffer[0]++; break;
665 case 96:buffer[1]++; break;
666 case 64:buffer[1]++; break;
667 case 48:buffer[2]++; break;
668 case 32:buffer[2]++; break;
669 case 24:buffer[3]++; break;
670 case 16:buffer[3]++; break;
671 case 12:buffer[4]++; break;
672 case 8:buffer[4]++; break;
673 case 6:buffer[5]++; break;
674 case 4:buffer[5]++; break;
678 /* Now find the most frequently used */
687 /* Finally convert and save the default duration */
690 case 0: DefNoteDuration=128; fprintf(file, _("d=1,")); break;
691 case 1: DefNoteDuration= 64; fprintf(file, _("d=2,")); break;
692 case 2: DefNoteDuration= 32; fprintf(file, _("d=4,")); break;
693 case 3: DefNoteDuration= 16; fprintf(file, _("d=8,")); break;
694 case 4: DefNoteDuration= 8; fprintf(file,_("d=16,")); break;
695 case 5: DefNoteDuration= 4; fprintf(file,_("d=32,")); break;
696 default: DefNoteDuration= 16; fprintf(file, _("d=8,")); break;
700 /* Find the most frequently used scale and use this for the default */
702 for (i=0;i<6;i++) buffer[i]=0;
703 for (i=0;i<ringtone->NrNotes;i++) {
704 if (ringtone->notes[i].note!=255) {
705 buffer[ringtone->notes[i].note/14]++;
716 if (ringtone->NrNotes!=0) {
717 DefNoteTempo=ringtone->notes[0].tempo;
718 DefNoteStyle=ringtone->notes[0].style;
721 /* Save the default scale */
722 fprintf(file,_("o=%i,"),DefNoteScale+4);
724 switch (DefNoteStyle) {
725 case StaccatoStyle: fprintf(file,_("s=S,")); break;
726 case NaturalStyle : fprintf(file,_("s=N,")); break;
729 /* Save the default tempo */
730 fprintf(file,_("b=%i,"),DefNoteTempo);
732 /* Save the default loop */
733 fprintf(file,_("l=%i:"),ringtone->Loop);
736 printf("DefNoteDuration=%d\n", DefNoteDuration);
737 printf("DefNoteScale=%d\n", DefNoteScale);
738 printf("Number of notes=%d\n",ringtone->NrNotes);
741 /* Now loop round for each note */
743 for (i=0;i<ringtone->NrNotes;i++) {
744 CurrentNote=ringtone->notes[i].note;
746 if (ringtone->notes[i].style!=DefNoteStyle) {
747 DefNoteStyle=ringtone->notes[i].style;
748 switch (DefNoteStyle) {
749 case StaccatoStyle : fprintf(file,_("s=S")); break;
750 case NaturalStyle : fprintf(file,_("s=N")); break;
751 case ContinuousStyle: fprintf(file,_("s=C")); break;
753 /* And a separator before next note */
754 if (i!=ringtone->NrNotes-1)
755 fprintf(file,_(","));
758 if (ringtone->notes[i].tempo!=DefNoteTempo) {
759 DefNoteTempo=ringtone->notes[i].tempo;
760 fprintf(file,_("b=%i"),DefNoteTempo);
761 if (i!=ringtone->NrNotes-1)
762 fprintf(file,_(","));
765 /* This note has a duration different than the default. We must save it */
766 if (ringtone->notes[i].duration!=DefNoteDuration) {
767 switch (ringtone->notes[i].duration) {
768 case 192: fprintf(file, _("1")); break; //192=128*1.5
769 case 128: fprintf(file, _("1")); break;
770 case 96: fprintf(file, _("2")); break; //96=64*1.5
771 case 64: fprintf(file, _("2")); break;
772 case 48: fprintf(file, _("4")); break; //48=32*1.5
773 case 32: fprintf(file, _("4")); break;
774 case 24: fprintf(file, _("8")); break; //24=16*1.5
775 case 16: fprintf(file, _("8")); break;
776 case 12: fprintf(file,_("16")); break; //12=8*1.5
777 case 8: fprintf(file,_("16")); break;
778 case 6: fprintf(file,_("32")); break; //6=4*1.5
779 case 4: fprintf(file,_("32")); break;
785 /* Now save the actual note */
786 switch (GSM_GetNote(CurrentNote)) {
787 case Note_C :fprintf(file,_("c"));break;
788 case Note_Cis:fprintf(file,_("c#"));break;
789 case Note_D :fprintf(file,_("d"));break;
790 case Note_Dis:fprintf(file,_("d#"));break;
791 case Note_E :fprintf(file,_("e"));break;
792 case Note_F :fprintf(file,_("f"));break;
793 case Note_Fis:fprintf(file,_("f#"));break;
794 case Note_G :fprintf(file,_("g"));break;
795 case Note_Gis:fprintf(file,_("g#"));break;
796 case Note_A :fprintf(file,_("a"));break;
797 case Note_Ais:fprintf(file,_("a#"));break;
798 case Note_H :fprintf(file,_("h"));break;
799 default :fprintf(file,_("p"));break; //Pause ?
802 /* Saving info about special duration */
803 if (ringtone->notes[i].duration==128*1.5 ||
804 ringtone->notes[i].duration==64*1.5 ||
805 ringtone->notes[i].duration==32*1.5 ||
806 ringtone->notes[i].duration==16*1.5 ||
807 ringtone->notes[i].duration==8*1.5 ||
808 ringtone->notes[i].duration==4*1.5)
809 fprintf(file,_("."));
811 /* This note has a scale different than the default, so save it */
812 if ( (CurrentNote!=255) && (CurrentNote/14!=DefNoteScale))
813 fprintf(file,_("%i"),(CurrentNote/14)+4);
815 /* And a separator before next note */
816 if (i!=ringtone->NrNotes-1)
817 fprintf(file,_(","));
822 void WriteVarLen(char* midifile, int* current, long value)
826 buffer = value & 0x7f;
828 while (value >>= 7) {
831 buffer += (value & 0x7f);
835 midifile[(*current)++] = buffer;
845 /* FIXME: need adding tempo before each note and scale too ? */
846 void savemid(FILE* file, GSM_Ringtone *ringtone)
848 char midifile[3000] = { 0x4D, 0x54, 0x68, 0x64, // MThd
849 0x00, 0x00, 0x00, 0x06, // chunk length
850 0x00, 0x00, // format 0
851 0x00, 0x01, // one track
852 0x00, 0x20, // 32 per quarter note
853 0x4D, 0x54, 0x72, 0x6B, // MTrk
854 0x00, 0x00, 0x00, 0x00, // chunk length
855 0x00, 0xFF, 0x51, 0x03, // tempo meta event
856 0x00, 0x00, 0x00 // 3 bytes for us for a quarter note
859 //{ "c", "c#", "d", "d#", "e", "f", "f#", "g", "g#", "a", "a#", "h" };
861 { 0, 1, 2, 3, 4, 4, 5, 6, 7, 8, 9, 10 , 11, 11 };
865 int current = 26, i, note, pause = 0;
866 bool notesexisting = false;
868 /* FIXME: we need add tempo before each note or so... */
869 long duration=60000000/63; // us for a quarter note
870 if (ringtone->NrNotes!=0)
871 duration=60000000/ringtone->notes[0].tempo;
872 midifile[current++] = duration >> 16;
873 midifile[current++] = duration >> 8;
874 midifile[current++] = duration;
876 for (i = 0; i < ringtone->NrNotes; i++) {
878 note = ringtone->notes[i].note;
879 if (note == 255) { // readmid does not read pauses at the beginning
882 pause += ringtone->notes[i].duration;
884 WriteVarLen(midifile,¤t,pause);
886 midifile[current++]=0x00; // pause
887 midifile[current++]=0x00;
893 notesexisting = true;
894 note = 48+12*((note/14)%4) + midinotes[note%14];
896 WriteVarLen(midifile,¤t,pause);
898 midifile[current++]=0x90; // note on
899 midifile[current++]=note;
900 midifile[current++]=0x64; // forte
902 WriteVarLen(midifile,¤t,ringtone->notes[i].duration);
903 midifile[current++]=0x80; // note off
904 midifile[current++]=note;
905 midifile[current++]=0x64;
911 WriteVarLen(midifile,¤t,pause);
912 midifile[current++]=0x00; // pause
913 midifile[current++]=0x00; //
915 midifile[current++] = 0x00;
916 midifile[current++] = 0xFF; // track end
917 midifile[current++] = 0x2F;
918 midifile[current++] = 0x00;
919 midifile[length++] = (current-start) >> 8;
920 midifile[length++] = current-start;
922 fwrite(midifile,1,current,file);
925 GSM_Error GSM_ReadBitmapFile(char *FileName, GSM_Bitmap *bitmap)
929 unsigned char buffer[300];
931 GSM_Filetypes filetype=None;
933 file = fopen(FileName, "rb");
936 return(GE_CANTOPENFILE);
938 fread(buffer, 1, 9, file); /* Read the header of the file. */
940 /* Attempt to identify filetype */
942 if (memcmp(buffer, "NOL",3)==0) { /* NOL files have 'NOL' at the start */
944 } else if (memcmp(buffer, "NGG",3)==0) { /* NGG files have 'NGG' at the start */
946 } else if (memcmp(buffer, "FORM",4)==0) { /* NSL files have 'FORM' at the start */
948 } else if (memcmp(buffer, "NLM",3)==0) { /* NLM files have 'NLM' at the start */
950 } else if (memcmp(buffer, "BM",2)==0) { /* BMP, I61 and GGP files have 'BM' at the start */
952 } else if (memcmp(buffer, "/* XPM */",9)==0) { /* XPM files have 'XPM' at the start */
954 } else filetype=None;
956 if (strstr(FileName,".otb")) filetype=OTA; /* OTA files saved by NCDS3 */
963 case NOL: error=loadnol(file,bitmap); fclose(file); break;
964 case NGG: error=loadngg(file,bitmap); fclose(file); break;
965 case NSL: error=loadnsl(file,bitmap); fclose(file); break;
966 case NLM: error=loadnlm(file,bitmap); fclose(file); break;
967 case OTA: error=loadota(file,bitmap); fclose(file); break;
968 case BMP: error=loadbmp(file,bitmap); fclose(file); break;
970 case XPMF:fclose(file);error=loadxpm(FileName,bitmap);break;
972 default : error=GE_INVALIDFILEFORMAT;
980 GSM_Error loadxpm(char *filename, GSM_Bitmap *bitmap)
986 error=XpmReadFileToXpmImage(filename,&image,&info);
989 case XpmColorError: return GE_WRONGCOLORS;break;
990 case XpmColorFailed: return GE_WRONGCOLORS;break;
991 case XpmOpenFailed: return GE_CANTOPENFILE;break;
992 case XpmFileInvalid: return GE_INVALIDFILEFORMAT;break;
993 case XpmSuccess: break;
996 if (image.ncolors!=2) {
997 printf("Wrong number of colors\n");
998 return GE_WRONGNUMBEROFCOLORS;
1001 if ((image.height==48) && (image.width==84)) {
1002 bitmap->type=GSM_StartupLogo;
1004 else if ((image.height==65) && (image.width==96)) {
1005 bitmap->type=GSM_7110StartupLogo;
1007 else if ((image.height==60) && (image.width==96)) {
1008 bitmap->type=GSM_6210StartupLogo;
1010 else if ((image.height==28) && (image.width==72)) {
1011 bitmap->type=GSM_PictureImage;
1013 else if ((image.height==14) && (image.width==72)) {
1014 bitmap->type=GSM_CallerLogo;
1018 printf("Invalid Image Size (%dx%d).\n",image.width,image.height);
1020 return GE_INVALIDIMAGESIZE;
1023 bitmap->height=image.height;
1024 bitmap->width=image.width;
1025 bitmap->size=GSM_GetBitmapSize(bitmap);
1027 GSM_ClearBitmap(bitmap);
1029 for(y=0;y<image.height;y++) {
1030 for(x=0;x<image.width;x++) {
1031 if (image.data[y*image.width+x]==0) GSM_SetPointBitmap(bitmap,x,y);
1040 /* Based on the article from the Polish Magazine "Bajtek" 11/92 */
1041 /* Marcin-Wiacek@Topnet.PL */
1042 GSM_Error loadbmp(FILE *file, GSM_Bitmap *bitmap)
1044 unsigned char buffer[34];
1046 int w,h,pos,y,x,i,sizeimage;
1048 fread(buffer, 1, 34, file); //required part of header
1050 h=buffer[22]+256*buffer[21]; //height of image in the file
1051 w=buffer[18]+256*buffer[17]; //width of image in the file
1053 printf("Image Size in BMP file: %dx%d\n",w,h);
1056 bitmap->type=GSM_7110StartupLogo;
1060 if (h==48 && w==84) {
1063 bitmap->type=GSM_StartupLogo;
1065 if (h==60 && w==96) {
1068 bitmap->type=GSM_6210StartupLogo;
1070 if (h==14 && w==72) {
1073 bitmap->type=GSM_CallerLogo;
1075 if (h==28 && w==72) {
1078 bitmap->type=GSM_PictureImage;
1080 if (h==21 && w==78) {
1083 bitmap->type=GSM_7110OperatorLogo;
1086 bitmap->size=GSM_GetBitmapSize(bitmap);
1088 GSM_ClearBitmap(bitmap);
1091 printf("Number of colors in BMP file: ");
1092 switch (buffer[28]) {
1093 case 1:printf("2 (supported by gnokii)\n");break;
1094 case 4:printf("16 (not supported by gnokii)\n");break;
1095 case 8:printf("256 (not supported by gnokii)\n");break;
1096 case 24:printf("True Color (not supported by gnokii)\n");break;
1097 default:printf("unknown\n");break;
1100 if (buffer[28]!=1) {
1101 printf("Wrong number of colors\n"); //we support only 2 colors images !
1102 return GE_WRONGNUMBEROFCOLORS;
1106 printf("Compression in BMP file: ");
1107 switch (buffer[30]) {
1108 case 0:printf("no compression (supported by gnokii)\n");break;
1109 case 1:printf("RLE8 (not supported by gnokii)\n");break;
1110 case 2:printf("RLE4 (not supported by gnokii)\n");break;
1111 default:printf("unknown\n");break;
1114 if (buffer[30]!=0) {
1116 printf("Subformat not supported\n"); //we don't support RLE compression
1118 return GE_SUBFORMATNOTSUPPORTED;
1122 fread(buffer, 1, pos, file); //rest of header (if exists) and color palette
1125 printf("First color in BMP file: %i %i %i ",buffer[pos-8], buffer[pos-7], buffer[pos-6]);
1126 if (buffer[pos-8]==0 && buffer[pos-7]==0 && buffer[pos-6]==0) printf("(white)");
1127 if (buffer[pos-8]==0xFF && buffer[pos-7]==0xFF && buffer[pos-6]==0xFF) printf("(black)");
1128 if (buffer[pos-8]==102 && buffer[pos-7]==204 && buffer[pos-6]==102) printf("(green)");
1131 printf("Second color in BMP file: %i %i %i ",buffer[pos-4], buffer[pos-3], buffer[pos-2]);
1132 if (buffer[pos-4]==0 && buffer[pos-3]==0 && buffer[pos-2]==0) printf("(white)");
1133 if (buffer[pos-4]==0xFF && buffer[pos-3]==0xFF && buffer[pos-2]==0xFF) printf("(black)");
1137 if (buffer[pos-8]!=0 || buffer[pos-7]!=0 || buffer[pos-6]!=0) first_white=false;
1141 for (y=h-1;y>=0;y--) { //lines are written from the last to the first
1144 if (pos==7) { //new byte !
1145 fread(buffer, 1, 1, file);
1148 if(i==5) i=1; //each line is written in multiply of 4 bytes
1150 if (x<=bitmap->width && y<=bitmap->height) { //we have top left corner !
1152 if ((buffer[0]&(1<<pos))<=0) GSM_SetPointBitmap(bitmap,x,y);
1154 if ((buffer[0]&(1<<pos))>0) GSM_SetPointBitmap(bitmap,x,y);
1158 if (pos<0) pos=7; //going to new byte
1160 pos=7; //going to new byte
1162 while (i!=5) //each line is written in multiply of 4 bytes
1164 fread(buffer, 1, 1, file);
1172 printf("Data size in BMP file: %i\n",sizeimage);
1178 GSM_Error loadnol(FILE *file, GSM_Bitmap *bitmap)
1181 unsigned char buffer[2000];
1184 bitmap->type=GSM_OperatorLogo;
1186 fread(buffer, 1, 6, file);
1187 fread(buffer, 1, 4, file);
1188 sprintf(bitmap->netcode, "%d %02d", buffer[0]+256*buffer[1], buffer[2]);
1190 fread(buffer, 1, 4, file); /* Width and height of the icon. */
1191 bitmap->width=buffer[0];
1192 bitmap->height=buffer[2];
1193 bitmap->size=GSM_GetBitmapSize(bitmap);
1195 if ((bitmap->height!=14) || (bitmap->width!=72)) {
1197 printf("Invalid Image Size (%dx%d).\n",bitmap->width,bitmap->height);
1199 return GE_INVALIDIMAGESIZE;
1202 fread(buffer, 1, 6, file); /* Unknown bytes. */
1204 for (i=0; i<bitmap->size; i++) {
1205 if (fread(buffer, 1, 8, file)==8) {
1206 bitmap->bitmap[i]=0;
1208 if (buffer[7-j] == '1')
1209 bitmap->bitmap[i]|=(1<<j);
1212 return (GE_TOOSHORT);
1216 /* Some programs writes here fileinfo */
1217 if (fread(buffer, 1, 1, file)==1) {
1218 fprintf(stdout, _("Fileinfo: %c"),buffer[0]);
1219 while (fread(buffer, 1, 1, file)==1) {
1220 if (buffer[0]!=0x0A) fprintf(stdout,_("%c"),buffer[0]);
1222 fprintf(stdout, _("\n"));
1229 GSM_Error loadngg(FILE *file, GSM_Bitmap *bitmap)
1232 unsigned char buffer[2000];
1235 bitmap->type=GSM_CallerLogo;
1237 fread(buffer, 1, 6, file);
1238 fread(buffer, 1, 4, file); /* Width and height of the icon. */
1239 bitmap->width=buffer[0];
1240 bitmap->height=buffer[2];
1241 bitmap->size=GSM_GetBitmapSize(bitmap);
1243 if ((bitmap->height!=14) || (bitmap->width!=72)) {
1245 printf("Invalid Image Size (%dx%d).\n",bitmap->width,bitmap->height);
1247 return GE_INVALIDIMAGESIZE;
1250 fread(buffer, 1, 6, file); /* Unknown bytes. */
1252 for (i=0; i<bitmap->size; i++) {
1253 if (fread(buffer, 1, 8, file)==8){
1254 bitmap->bitmap[i]=0;
1256 if (buffer[7-j] == '1')
1257 bitmap->bitmap[i]|=(1<<j);
1260 return(GE_TOOSHORT);
1264 /* Some programs writes here fileinfo */
1265 if (fread(buffer, 1, 1, file)==1) {
1266 fprintf(stdout, _("Fileinfo: %c"),buffer[0]);
1267 while (fread(buffer, 1, 1, file)==1) {
1268 if (buffer[0]!=0x0A) fprintf(stdout,_("%c"),buffer[0]);
1270 fprintf(stdout, _("\n"));
1277 GSM_Error loadnsl(FILE *file, GSM_Bitmap *bitmap)
1280 unsigned char block[6],buffer[505];
1285 while (fread(block,1,6,file)==6) {
1287 block_size=block[4]*256+block[5];
1290 fprintf(stdout,_("Block %c%c%c%c, size %i\n"),block[0],block[1],block[2],block[3],block_size);
1293 if (!strncmp(block, "FORM", 4)) {
1295 fprintf(stdout,_(" File ID\n"));
1299 if (block_size>504) return(GE_INVALIDFILEFORMAT);
1301 if (block_size!=0) {
1303 fread(buffer,1,block_size,file);
1304 buffer[block_size]=0; //if it's string, we end it with \0
1307 if (!strncmp(block, "VERS", 4)) fprintf(stdout,_(" File saved by: %s\n"),buffer);
1308 if (!strncmp(block, "MODL", 4)) fprintf(stdout,_(" Logo saved from: %s\n"),buffer);
1309 if (!strncmp(block, "COMM", 4)) fprintf(stdout,_(" Phone was connected to COM port: %s\n"),buffer);
1312 if (!strncmp(block, "NSLD", 4)) {
1313 bitmap->type=GSM_StartupLogo;
1316 bitmap->size=GSM_GetBitmapSize(bitmap);
1318 memcpy(bitmap->bitmap,buffer,bitmap->size);
1321 fprintf(stdout,_(" Startup logo (size %i)\n"),block_size);
1328 if (bitmap->size==0) return(GE_TOOSHORT);
1333 GSM_Error loadnlm (FILE *file, GSM_Bitmap *bitmap)
1335 unsigned char buffer[1000];
1339 fread(buffer,1,5,file);
1340 fread(buffer,1,1,file);
1342 switch (buffer[0]) {
1343 case 0x00: bitmap->type=GSM_OperatorLogo; break;
1344 case 0x01: bitmap->type=GSM_CallerLogo; break;
1345 case 0x02: bitmap->type=GSM_StartupLogo; break;
1346 case 0x03: bitmap->type=GSM_PictureImage; break;
1348 return(GE_SUBFORMATNOTSUPPORTED);
1351 fread(buffer,1,4,file);
1352 bitmap->width=buffer[1];
1353 bitmap->height=buffer[2];
1355 if (bitmap->type==GSM_StartupLogo && bitmap->width==96 && bitmap->height==65)
1356 bitmap->type=GSM_7110StartupLogo;
1357 if (bitmap->type==GSM_StartupLogo && bitmap->width==96 && bitmap->height==60)
1358 bitmap->type=GSM_6210StartupLogo;
1359 if (bitmap->type==GSM_OperatorLogo && bitmap->width==78 && bitmap->height==21)
1360 bitmap->type=GSM_7110OperatorLogo;
1362 bitmap->size=GSM_GetBitmapSize(bitmap);
1364 division=div(bitmap->width,8);
1365 if (division.rem!=0) division.quot++; /* For startup logos */
1367 if (fread(buffer,1,(division.quot*bitmap->height),file)!=(division.quot*bitmap->height))
1368 return(GE_TOOSHORT);
1370 GSM_ClearBitmap(bitmap);
1373 for (y=0;y<bitmap->height;y++) {
1374 for (x=0;x<bitmap->width;x++) {
1375 if ((buffer[pos]&(1<<pos2))>0) GSM_SetPointBitmap(bitmap,x,y);
1377 if (pos2<0) {pos2=7;pos++;} //going to new byte
1379 if (pos2!=7) {pos2=7;pos++;} //for startup logos-new line means new byte
1385 GSM_Error loadota(FILE *file, GSM_Bitmap *bitmap)
1390 fread(buffer,1,4,file);
1392 bitmap->width=buffer[1];
1393 bitmap->height=buffer[2];
1395 if ((bitmap->height==48) && (bitmap->width==84)) {
1396 bitmap->type=GSM_StartupLogo;
1398 else if ((bitmap->height==14) && (bitmap->width==72)) {
1399 bitmap->type=GSM_CallerLogo;
1403 printf("Invalid Image Size (%dx%d).\n",bitmap->width,bitmap->height);
1405 return GE_INVALIDIMAGESIZE;
1408 bitmap->size=GSM_GetBitmapSize(bitmap);
1410 if (fread(bitmap->bitmap,1,bitmap->size,file)!=bitmap->size)
1411 return(GE_TOOSHORT);
1416 GSM_Error GSM_SaveBitmapFile(char *FileName, GSM_Bitmap *bitmap)
1422 file = fopen(FileName, "wb");
1425 return(GE_CANTOPENFILE);
1427 if (strstr(FileName,".xpm")) { savexpm(file, bitmap); done=true; }
1428 if (strstr(FileName,".nlm")) { savenlm(file, bitmap); done=true; }
1429 if (strstr(FileName,".ngg")) { savengg(file, bitmap); done=true; }
1430 if (strstr(FileName,".nsl")) { savensl(file, bitmap); done=true; }
1431 if (strstr(FileName,".otb")) { saveota(file, bitmap); done=true; }
1432 if (strstr(FileName,".nol")) { savenol(file, bitmap); done=true; }
1433 if (strstr(FileName,".bmp") ||
1434 strstr(FileName,".ggp") ||
1435 strstr(FileName,".i61"))
1437 savebmp(file, bitmap);
1443 switch (bitmap->type) {
1444 case GSM_CallerLogo : savengg(file, bitmap); break;
1445 case GSM_OperatorLogo : savenol(file, bitmap); break;
1446 case GSM_7110OperatorLogo: savebmp(file, bitmap); break;
1447 case GSM_7110StartupLogo : savebmp(file, bitmap); break;
1448 case GSM_6210StartupLogo : savebmp(file, bitmap); break;
1449 case GSM_StartupLogo : savensl(file, bitmap); break;
1450 case GSM_PictureImage : savenlm(file, bitmap); break;
1451 case GSM_WelcomeNoteText : break;
1452 case GSM_DealerNoteText : break;
1453 case GSM_None : break;
1462 void savexpm(FILE *file, GSM_Bitmap *bitmap)
1466 fprintf(file,_("/* XPM */\n"));
1467 fprintf(file,_("static char * ala_xpm[] = {\n"));
1468 fprintf(file,_("\"%i %i 2 1\",\n"),bitmap->width,bitmap->height);
1469 fprintf(file,_("\". s c m #000000 g4 #000000 g #000000 c #000000\",\n"));
1470 fprintf(file,_("\"# s c m #ffffff g4 #ffffff g #ffffff c #ffffff\",\n"));
1472 for (y=0;y<bitmap->height;y++) {
1473 fprintf(file,_("\""));
1474 for (x=0;x<bitmap->width;x++)
1475 if (GSM_IsPointBitmap(bitmap,x,y))
1476 fprintf(file,_("."));
1478 fprintf(file,_("#"));
1479 fprintf(file,_("\""));
1480 if (y==bitmap->height-1)
1481 fprintf(file,_("};\n"));
1483 fprintf(file,_(",\n"));
1487 /* Based on the article from the Polish Magazine "Bajtek" 11/92 */
1488 /* Marcin-Wiacek@Topnet.PL */
1489 void savebmp(FILE *file, GSM_Bitmap *bitmap)
1491 int x,y,pos,i,sizeimage;
1492 unsigned char buffer[1];
1495 unsigned char header[]={
1496 /*1'st header*/ 'B','M', /* BMP file ID */
1497 0x00,0x00,0x00,0x00, /* Size of file */
1498 0x00,0x00, /* Reserved for future use */
1499 0x00,0x00, /* Reserved for future use */
1500 62,0x00,0x00,0x00, /* Offset for image data */
1502 /*2'nd header*/ 40,0x00,0x00,0x00, /* Length of this part of header */
1503 0x00,0x00,0x00,0x00, /* Width of image */
1504 0x00,0x00,0x00,0x00, /* Height of image */
1505 1,0x00, /* How many planes in target device */
1506 1,0x00, /* How many colors in image. 1 means 2^1=2 colors */
1507 0x00,0x00,0x00,0x00, /* Type of compression. 0 means no compression */
1508 /*Sometimes */ 0x00,0x00,0x00,0x00, /* Size of part with image data */
1509 /*ttttttt...*/ 0xE8,0x03,0x00,0x00, /* XPelsPerMeter */
1510 /*hhiiiiissss*/ 0xE8,0x03,0x00,0x00, /* YPelsPerMeter */
1511 /*part of header*/2,0x00,0x00,0x00, /* How many colors from palette is used */
1512 /*doesn't exist*/ 0x00,0x00,0x00,0x00, /* How many colors from palette is required to display image. 0 means all */
1514 /*Color palette*/ 0x00,0x00,0x00, /* First color in palette in Blue, Green, Red. Here white */
1515 0x00, /* Each color in palette is end by 4'th byte */
1516 102,204,102, /* Second color in palette in Blue, Green, Red. Here green */
1517 0x00}; /* Each color in palette is end by 4'th byte */
1519 header[22]=bitmap->height;
1520 header[18]=bitmap->width;
1524 for (y=bitmap->height-1;y>=0;y--) { //lines are written from the last to the first
1526 for (x=0;x<bitmap->width;x++) {
1527 if (pos==7) { //new byte !
1528 if (x!=0) sizeimage++;
1530 if(i==5) i=1; //each line is written in multiply of 4 bytes
1533 if (pos<0) pos=7; //going to new byte
1535 pos=7; //going to new byte
1538 while (i!=5) //each line is written in multiply of 4 bytes
1546 printf("Data size in BMP file: %i\n",sizeimage);
1548 division=div(sizeimage,256);
1549 header[35]=division.quot;
1550 header[34]=sizeimage-(division.quot*256);
1552 sizeimage=sizeimage+sizeof(header);
1554 printf("Size of BMP file: %i\n",sizeimage);
1556 division=div(sizeimage,256);
1557 header[3]=division.quot;
1558 header[2]=sizeimage-(division.quot*256);
1560 fwrite(header,1,sizeof(header),file);
1563 for (y=bitmap->height-1;y>=0;y--) { //lines are written from the last to the first
1565 for (x=0;x<bitmap->width;x++) {
1566 if (pos==7) { //new byte !
1567 if (x!=0) fwrite(buffer, 1, sizeof(buffer), file);
1569 if(i==5) i=1; //each line is written in multiply of 4 bytes
1572 if (!GSM_IsPointBitmap(bitmap,x,y)) buffer[0]|=(1<<pos);
1574 if (pos<0) pos=7; //going to new byte
1576 pos=7; //going to new byte
1577 fwrite(buffer, 1, sizeof(buffer), file);
1579 while (i!=5) //each line is written in multiply of 4 bytes
1582 fwrite(buffer, 1, sizeof(buffer), file);
1589 void savengg(FILE *file, GSM_Bitmap *bitmap)
1592 char header[]={'N','G','G',0x00,0x01,0x00,
1593 0x00,0x00, /* Width */
1594 0x00,0x00, /* Height */
1595 0x01,0x00,0x01,0x00,
1596 0x00, /* Unknown.Can't be checksum - for */
1597 /* the same logo files can be different */
1606 GSM_ResizeBitmap(©,GSM_CallerLogo);
1608 header[6]=copy.width;
1609 header[8]=copy.height;
1611 fwrite(header,1,sizeof(header),file);
1613 for (i=0; i<copy.size; i++) {
1615 if ((copy.bitmap[i]&(1<<j))>0) {
1620 fwrite(buffer,1,8,file);
1624 void savenol(FILE *file, GSM_Bitmap *bitmap)
1627 char header[]={'N','O','L',0x00,0x01,0x00,
1628 0x00,0x00, /* MCC */
1629 0x00,0x00, /* MNC */
1630 0x00,0x00, /* Width */
1631 0x00,0x00, /* Height */
1632 0x01,0x00,0x01,0x00,
1633 0x00, /* Unknown.Can't be checksum - for */
1634 /* the same logo files can be different */
1637 int i,j,country,net;
1642 GSM_ResizeBitmap(©,GSM_OperatorLogo);
1644 sscanf(copy.netcode, "%d %d", &country, &net);
1646 header[6]=country%256;
1647 header[7]=country/256;
1650 header[10]=copy.width;
1651 header[12]=copy.height;
1653 fwrite(header,1,sizeof(header),file);
1655 for (i=0; i<copy.size; i++) {
1657 if ((copy.bitmap[i]&(1<<j))>0) {
1662 fwrite(buffer,1,8,file);
1666 void savensl(FILE *file, GSM_Bitmap *bitmap)
1669 u8 header[]={'F','O','R','M', 0x01,0xFE, /* File ID block, size 1*256+0xFE=510*/
1670 'N','S','L','D', 0x01,0xF8}; /* Startup Logo block, size 1*256+0xF8=504*/
1675 GSM_ResizeBitmap(©,GSM_StartupLogo);
1677 fwrite(header,1,sizeof(header),file);
1679 fwrite(copy.bitmap,1,copy.size,file);
1682 void saveota(FILE *file, GSM_Bitmap *bitmap)
1685 char header[]={0x01,
1693 header[1]=copy.width;
1694 header[2]=copy.height;
1696 fwrite(header,1,sizeof(header),file);
1698 fwrite(copy.bitmap,1,copy.size,file);
1701 void savenlm(FILE *file, GSM_Bitmap *bitmap)
1704 char header[]={'N','L','M', /* Nokia Logo Manager file ID. */
1707 0x00, /* 0x00 (OP), 0x01 (CLI), 0x02 (Startup), 0x03 (Picture)*/
1708 0x00, /* Number of images inside file - 1. 0x01==2 images, 0x03==4 images, etc. */
1713 unsigned char buffer[1000];
1720 switch (copy.type) {
1721 case GSM_OperatorLogo : header[5]=0x00; break;
1722 case GSM_7110OperatorLogo: header[5]=0x00; break;
1723 case GSM_CallerLogo : header[5]=0x01; break;
1724 case GSM_StartupLogo : header[5]=0x02; break;
1725 case GSM_7110StartupLogo : header[5]=0x02; break;
1726 case GSM_6210StartupLogo : header[5]=0x02; break;
1727 case GSM_PictureImage : header[5]=0x03; break;
1728 case GSM_WelcomeNoteText : break;
1729 case GSM_DealerNoteText : break;
1730 case GSM_None : break;
1733 header[7]=copy.width;
1734 header[8]=copy.height;
1737 for (y=0;y<copy.height;y++) {
1738 for (x=0;x<copy.width;x++) {
1739 if (pos2==7) buffer[pos]=0;
1741 if (GSM_IsPointBitmap(©,x,y)) buffer[pos]|=(1<<pos2);
1744 if (pos2<0) {pos2=7;pos++;} //going to new line
1746 if (pos2!=7) {pos2=7;pos++;} //for startup logos - new line with new byte
1749 division=div(copy.width,8);
1750 if (division.rem!=0) division.quot++; /* For startup logos */
1752 fwrite(header,1,sizeof(header),file);
1754 fwrite(buffer,1,(division.quot*copy.height),file);
1757 /* mode == 0 -> overwrite
1759 * mode == 2 -> append
1761 int GSM_SaveTextFile(char *FileName, char *text, int mode)
1766 if (mode == 2) file = fopen(FileName, "a");
1767 else file = fopen(FileName, "w");
1769 if (!file) return -1;
1771 fprintf(file, "%s\n\n", text);
1778 GSM_Error GSM_SaveBackupFile(char *FileName, GSM_Backup *backup)
1783 file = fopen(FileName, "wb");
1785 if (!file) return(GE_CANTOPENFILE);
1787 savelmb(file, backup);
1794 void savelmbstartupentry(FILE *file, GSM_Bitmap startup, GSM_Bitmap text, bool available)
1796 /* Welcome note and logo header block */
1797 char req[1000] = {'W','E','L',' ', /*block identifier*/
1798 00,00, /*block data size*/
1799 0x02,00,00,00,00,00,
1801 0x02}; /*number of blocks (like in 6110 frame)*/
1807 count=count+N6110_MakeStartupLogoFrame(req+13,startup);
1811 req[count++]=strlen(text.text);
1812 memcpy(req+count,text.text,strlen(text.text));
1813 count=count+strlen(text.text);
1815 req[4]=(count-12)%256;
1816 req[5]=(count-12)/256;
1818 fwrite(req, 1, count, file);
1821 void savelmbopentry(FILE *file, GSM_Bitmap bitmap)
1823 /* Operator logo header block */
1824 char req[500] = {'O','L','G',' ', /*block identifier*/
1825 0x88,00, /*block data size*/
1826 0x02,00,00,00,00,00,
1832 count=count+N6110_MakeOperatorLogoFrame(req+13,bitmap);
1834 if (bitmap.width!=0x48) {
1835 req[4]=(count-7)%256;
1836 req[5]=(count-7)/256;
1838 for (i=1;i<=5;i++) req[count++]=0;
1840 req[17]=req[17]+5; //we fix size of logo block
1842 req[4]=(count-12)%256;
1843 req[5]=(count-12)/256;
1846 fwrite(req, 1, count, file);
1849 /* Work in progress ! */
1850 void savelmbspeedentry(FILE *file, GSM_SpeedDial speed)
1852 /* Speed dial header block */
1853 char req[] = {'S','P','D',' ', /*block identifier*/
1854 0x03,00, /*block data size*/
1856 00, /*number of speed dial*/
1859 00, /*number of speed dial*/
1860 03, /*memory type. ME=02;SM=03*/
1861 00}; /*number of location assigned to speed dial*/
1863 req[8]=req[12]=speed.Number;
1865 if (speed.MemoryType==GMT_ME) req[13]=2; //memory type=GMT_ME
1867 req[14]=speed.Location;
1869 fwrite(req, 1, 15, file);
1872 void savelmbcallerentry(FILE *file, GSM_Bitmap bitmap)
1874 char req[500] = {'C','G','R',' ', /*block identifier*/
1875 00,00, /*block data size*/
1877 00, /*group number=0,1,etc.*/
1882 req[8]=bitmap.number;
1884 count=count+N6110_MakeCallerGroupFrame(req+12,bitmap);
1888 req[4]=(count-12)%256;
1889 req[5]=(count-12)/256;
1891 fwrite(req, 1, count, file);
1894 void savelmbpbkentry(FILE *file, GSM_PhonebookEntry entry)
1896 char req[500] = {'P','B','E','2', /*block identifier*/
1897 00,00, /*block data size*/
1899 00,00, /*position of phonebook entry*/
1900 03, /*memory type. ME=02;SM=03*/
1903 00,00, /*position of phonebook entry*/
1904 03, /*memory type. ME=02;SM=03*/
1907 int count = 16, blocks;
1909 req[9]=req[13] = (entry.Location >> 8);
1910 req[8]=req[12] = entry.Location & 0xff;
1912 if (entry.MemoryType==GMT_ME) req[10]=req[14]=2;
1914 count=count+N7110_EncodePhonebookFrame(req+16, entry, &blocks);
1916 req[4]=(count-12)%256;
1917 req[5]=(count-12)/256;
1919 fwrite(req, 1, count, file);
1922 void savelmb(FILE *file, GSM_Backup *backup)
1926 char LMBHeader[] = {'L','M','B',' '}; /*file identifier*/
1928 /* Phonebook header block */
1929 char PBKHeader[] = {'P','B','K',' ', /*block identifier*/
1930 0x08,00, /*block data size*/
1932 03, /*memory type. ME=02;SM=03*/
1935 00,00, /*size of phonebook*/
1936 14, /*max length of each position*/
1939 fwrite(LMBHeader, 1, sizeof(LMBHeader), file); /* Write the header of the file. */
1941 if (backup->SIMPhonebookUsed!=0) {
1942 PBKHeader[12]=backup->SIMPhonebookSize%256;
1943 PBKHeader[13]=backup->SIMPhonebookSize/256;
1944 fwrite(PBKHeader, 1, sizeof(PBKHeader), file);
1946 for (i=0;i<backup->SIMPhonebookUsed;i++)
1947 savelmbpbkentry(file, backup->SIMPhonebook[i]);
1950 if (backup->PhonePhonebookUsed!=0) {
1951 PBKHeader[8]=2; //memory type=GMT_ME
1952 PBKHeader[12]=backup->PhonePhonebookSize%256;
1953 PBKHeader[13]=backup->PhonePhonebookSize/256;
1954 PBKHeader[14]=0x16; //max size of one entry
1955 fwrite(PBKHeader, 1, sizeof(PBKHeader), file);
1957 for (i=0;i<backup->PhonePhonebookUsed;i++)
1958 savelmbpbkentry(file, backup->PhonePhonebook[i]);
1961 if (backup->CallerAvailable)
1962 for (i=0;i<5;i++) savelmbcallerentry(file,backup->CallerGroups[i]);
1964 // if (backup->SpeedAvailable)
1965 // for (i=0;i<8;i++) savelmbspeedentry(file,backup->SpeedDials[i]);
1967 if (backup->OperatorLogoAvailable) savelmbopentry(file,backup->OperatorLogo);
1969 savelmbstartupentry(file,backup->StartupLogo,backup->StartupText,backup->StartupLogoAvailable);
1972 void loadlmbcallerentry(unsigned char *buffer, unsigned char *buffer2, GSM_Backup *backup, int number)
1976 backup->CallerAvailable=true;
1979 fprintf(stdout, _(" Number %i, name \""), buffer2[0]);
1980 for (i=0;i<buffer2[1];i++) {
1981 fprintf(stdout, _("%c"), buffer2[i+2]);
1983 fprintf(stdout, _("\"\n"));
1985 fprintf(stdout,_(" Ringtone ID=%i\n"), buffer2[i+2]);
1987 if (buffer2[i+3]==1) fprintf(stdout,_(" Logo enabled\n"));
1988 else fprintf(stdout,_(" Logo disabled\n"));
1991 backup->CallerGroups[number].number=buffer2[0];
1992 backup->CallerGroups[number].type=GSM_CallerLogo;
1994 for (i=0;i<buffer2[1];i++) {
1995 backup->CallerGroups[number].text[i]=buffer2[i+2];
1997 backup->CallerGroups[number].text[buffer2[1]]=0;
1999 backup->CallerGroups[number].ringtone=buffer2[i+2];
2001 backup->CallerGroups[number].enabled=false;
2002 if (buffer2[i+3]==1) backup->CallerGroups[number].enabled=true;
2004 backup->CallerGroups[number].width=buffer2[i+7];
2005 backup->CallerGroups[number].height=buffer2[i+8];
2007 backup->CallerGroups[number].size=GSM_GetBitmapSize(&backup->CallerGroups[number]);
2009 memcpy(backup->CallerGroups[number].bitmap,buffer2+i+10,backup->CallerGroups[number].size);
2012 fprintf(stdout, _(" Caller logo"));
2013 fprintf(stdout, _(" (size %ix%i - %i bytes)\n"),
2014 backup->CallerGroups[number].width, backup->CallerGroups[number].height, backup->CallerGroups[number].size);
2015 GSM_PrintBitmap(&backup->CallerGroups[number]);
2020 void loadlmbopentry(unsigned char *buffer, unsigned char *buffer2, GSM_Backup *backup)
2022 backup->OperatorLogoAvailable=true;
2024 DecodeNetworkCode(buffer2+1, backup->OperatorLogo.netcode);
2026 backup->OperatorLogo.text[0]=0;
2028 backup->OperatorLogo.width=buffer2[7];
2029 backup->OperatorLogo.height=buffer2[8];
2031 backup->OperatorLogo.type=GSM_OperatorLogo;
2032 if (backup->OperatorLogo.width==78) backup->OperatorLogo.type=GSM_7110OperatorLogo;
2034 backup->OperatorLogo.size=GSM_GetBitmapSize(&backup->OperatorLogo);
2037 fprintf(stdout, _(" GSM operator logo (size %ix%i - %i bytes) for %s (%s) network.\n"),
2038 backup->OperatorLogo.width, backup->OperatorLogo.height,
2039 backup->OperatorLogo.size, backup->OperatorLogo.netcode,
2040 GSM_GetNetworkName(backup->OperatorLogo.netcode));
2043 memcpy(backup->OperatorLogo.bitmap,buffer2+10,backup->OperatorLogo.size);
2046 GSM_PrintBitmap(&backup->OperatorLogo);
2051 void loadlmbpbkentry(unsigned char *buffer, unsigned char *buffer2, GSM_Backup *backup)
2053 GSM_PhonebookEntry pbk;
2056 fprintf(stdout,_(" Memory : "));
2057 switch(buffer[10]) {
2058 case 2: fprintf(stdout, _("(internal)\n"));break;
2059 case 3: fprintf(stdout, _("(sim)\n"));break;
2060 default: fprintf(stdout, _("(unknown)\n"));break;
2062 fprintf(stdout,_(" Location : %i\n"),buffer2[0]+buffer2[1]*256);
2066 pbk.Group = 5; /* 5 = no group as 6110 */
2069 pbk.SubEntriesCount = 0;
2071 N7110_DecodePhonebookFrame(&pbk,buffer2+4,(buffer[4]+buffer[5]*256)-4);
2073 pbk.MemoryType=GMT_SM;
2074 if (buffer[10]==2) pbk.MemoryType=GMT_ME;
2076 pbk.Location=buffer2[0]+256*buffer2[1];
2078 if (buffer[10]==2) backup->PhonePhonebook[backup->PhonePhonebookUsed++]=pbk;
2079 else backup->SIMPhonebook [backup->SIMPhonebookUsed++] =pbk;
2082 void loadlmbstartupentry(unsigned char *buffer, unsigned char *buffer2, GSM_Backup *backup)
2090 for (i=0;i<buffer2[0];i++) {
2091 switch (buffer2[j++]) {
2093 backup->StartupLogoAvailable=true;
2094 backup->StartupLogo.height=buffer2[j++];
2095 backup->StartupLogo.width=buffer2[j++];
2096 backup->StartupLogo.text[0]=0;
2097 backup->StartupLogo.type=GSM_StartupLogo;
2098 switch (backup->StartupLogo.height) {
2099 case 65:backup->StartupLogo.type=GSM_7110StartupLogo;break;
2100 case 60:backup->StartupLogo.type=GSM_6210StartupLogo;break;
2102 backup->StartupLogo.size=GSM_GetBitmapSize(&backup->StartupLogo);
2105 fprintf(stdout, _(" Block 1 - startup logo (size %ix%i - %i bytes)\n"),
2106 backup->StartupLogo.width, backup->StartupLogo.height, backup->StartupLogo.size);
2109 memcpy(backup->StartupLogo.bitmap,buffer2+j,backup->StartupLogo.size);
2111 GSM_PrintBitmap(&backup->StartupLogo);
2113 j=j+backup->StartupLogo.size;
2119 fprintf(stdout, _(" Block 2 - welcome note \""));
2120 for (z=0;z<buffer2[j];z++) fprintf(stdout, _("%c"),buffer2[j+z+1]);
2121 fprintf(stdout, _("\"\n"));
2126 fprintf(stdout, _(" Unknown block %02x\n"),buffer2[j]);
2133 GSM_Error loadlmb(FILE *file, GSM_Backup *backup)
2140 unsigned char buffer[12], buffer2[1000]; //for data from file
2142 fread(buffer, 1, 4, file); /* Read the header of the file. */
2144 while (fread(buffer, 1, 12, file)==12) { //while we have something to read
2147 /* Info about block in the file */
2148 fprintf(stdout, _("Block \""));
2149 for (i=0;i<4;i++) {fprintf(stdout, _("%c"),buffer[i]);}
2150 fprintf(stdout, _("\" ("));
2151 if (memcmp(buffer, "PBK ",4)==0) {fprintf(stdout, _("Phonebook")); } else {
2152 if (memcmp(buffer, "PBE2",4)==0) {fprintf(stdout, _("Phonebook entry"));} else {
2153 if (memcmp(buffer, "CGR ",4)==0) {fprintf(stdout, _("Caller group")); } else {
2154 if (memcmp(buffer, "SPD ",4)==0) {fprintf(stdout, _("Speed dial")); } else {
2155 if (memcmp(buffer, "OLG ",4)==0) {fprintf(stdout, _("Operator logo")); } else {
2156 if (memcmp(buffer, "WEL ",4)==0) {fprintf(stdout, _("Startup logo and welcome text")); } else {
2157 fprintf(stdout, _("uknown - ignored"));}}}}}}
2159 fprintf(stdout, _(") - length %i\n"), buffer[4]+buffer[5]*256);
2162 fread(buffer2, 1, buffer[4]+buffer[5]*256, file); //reading block data
2165 if (memcmp(buffer, "PBK ",4)==0) { //phonebook
2166 fprintf(stdout, _(" Size of phonebook %i, type %i "),(buffer2[0]+buffer2[1]*256),buffer[8]);
2168 case 2 : fprintf(stdout, _("(internal)"));break;
2169 case 3 : fprintf(stdout, _("(sim)")) ;break;
2170 default: fprintf(stdout, _("(unknown)")) ;break;
2172 fprintf(stdout, _(", length of each position - %i\n"),buffer2[2]);
2176 if (memcmp(buffer, "PBE2",4)==0) //phonebook entry
2177 loadlmbpbkentry(buffer,buffer2,backup);
2179 if (memcmp(buffer, "CGR ",4)==0) { //caller groups
2180 loadlmbcallerentry(buffer,buffer2,backup,number);
2184 if (memcmp(buffer, "OLG ",4)==0) //operator logo
2185 loadlmbopentry(buffer,buffer2,backup);
2187 if (memcmp(buffer, "WEL ",4)==0) //welcome blocks
2188 loadlmbstartupentry(buffer,buffer2,backup);
2194 GSM_Error GSM_ReadBackupFile(char *FileName, GSM_Backup *backup)
2198 unsigned char buffer[300];
2200 GSM_Filetypes filetype=None;
2202 file = fopen(FileName, "rb");
2204 if (!file) return(GE_CANTOPENFILE);
2206 fread(buffer, 1, 4, file); /* Read the header of the file. */
2208 /* Attempt to identify filetype */
2210 if (memcmp(buffer, "LMB ",4)==0) { /* LMB files have 'LMB ' at the start */
2212 } else filetype=None;
2218 backup->PhonePhonebookUsed=0;
2219 backup->SIMPhonebookUsed=0;
2220 backup->StartupLogoAvailable=false;
2221 backup->StartupText.text[0]=0;
2222 backup->OperatorLogoAvailable=false;
2223 backup->CallerAvailable=false;
2224 backup->SpeedAvailable=false;
2227 case LMB: error=loadlmb(file,backup); fclose(file); break;
2228 default : error=GE_INVALIDFILEFORMAT;
2234 GSM_Error GSM_SaveBinRingtoneFile(char *FileName, GSM_BinRingtone *ringtone)
2239 file = fopen(FileName, "wb");
2241 if (!file) return(GE_CANTOPENFILE);
2243 fwrite(ringtone->frame, 1, ringtone->length, file);