+FILE *f;
+char buf[32+140*8+1];
+unsigned char bin[140]={
+ 0x06, /* UDH length */
+ 0x05, /* IEI */
+ 0x04, /* IEDL */
+ 0x15, 0x83, /* dest port (group gfx) */
+ 0x00, 0x00 /* src port (unused) */
+ };
+size_t got,r,w;
+ssize_t chars,bits;
+char gsmnetf[10];
+int sizex,sizey,bit;
+
+#define WORD(n) (((unsigned char)buf[(n)])|(((unsigned char)buf[(n)+1])<<8))
+
+ if (!(f=fopen(logoname,"rb")))
+ error("^!Cannot open logo file \"%s\" for r/o",logoname);
+ got=fread(buf,1,sizeof(buf),f);
+ if (got>=20 && !memcmp(buf,"NOL",4)) {
+ VARPRINTF2(gsmnetf,"%03.3u%02.2u",WORD(6),WORD(8));
+ assert(strlen(gsmnetf)==5);
+ r=10;
+ if (verbose>=1) error(".Reading NOL file \"%s\", GSMnet \"%s\", word@4=%d..",
+ logoname,gsmnetf,WORD(4));
+ }
+ else if (got>=16 && !memcmp(buf,"NGG",4)) {
+ r=6;
+ if (verbose>=1) error(".Reading NGG file \"%s\", word@4=%d..",
+ logoname,WORD(4));
+ }
+ else error("!Unknown file format of logo file \"%s\"",logoname);
+ if (!strcasecmp(gsmnet,WORD_NET)) {
+ if (!*gsmnetf) error("!NOL network code detection requested but NOL file not loaded, please specify network code");
+ gsmnet=gsmnetf;
+ }
+ if (!strcasecmp(gsmnet,WORD_GROUP) || !*gsmnet) {
+ error("\nSending logo as: group graphics");
+ gsmnet=NULL;
+ }
+ else {
+ error("\nSending logo as: operator logo for \"%s\"",gsmnet);
+ bin[4]=0x82; /* dest port 0x1582 */
+ }
+
+ sizex=WORD(r); sizey=WORD(r+2);
+ if (verbose>=2) error(".Magic words: @+4=%d, @+6=%d, @+8=%d",
+ WORD(r+4),WORD(r+6),WORD(r+8));
+ r+=10;
+ if (sizex<1 || sizex>255
+ || sizey<1 || sizey>255) error("!Invalid size: %dx%d",sizex,sizey);
+ chars=((bits=sizex*sizey)+7)/8;
+ if (r+bits>got) error("!Logo file \"%s\" too short - actual=%d, need(%dx%d)=%d",
+ logoname,got,sizex,sizey,r+chars);
+ else if (r+bits<got)
+ if (verbose>=1) error("Ignoring trailing garbage in \"%s\", used only %d bytes",logoname,r+bits);
+ if ((got=(7+(gsmnet?3:0)+4+chars))>140)
+ error("!SMS size would be %d bytes but 140 is maximum",got);
+ w=7;
+ if (gsmnet) {
+ bin[w++]=((gsmnet[1]&0x0F)<<4)|(gsmnet[0]&0x0F);
+ bin[w++]=0xF0 |(gsmnet[2]&0x0F);
+ bin[w++]=((gsmnet[4]&0x0F)<<4)|(gsmnet[3]&0x0F);
+ }
+ bin[w++]=0x00; /* RFU by Nokia */
+ bin[w++]=sizex; bin[w++]=sizey;
+ bin[w++]=0x01; /* one B/W plane */
+ while (chars--) {
+ bin[w]=0;
+ for (bit=0x80;(bits>0) && (bit>0);bits--,bit>>=1) {
+ if (buf[r]!='0' && buf[r]!='1')
+ error("!Invalid character (neither '0' nor '1')in logo file \"%s\" at offset 0x%X",
+ logoname,r);
+ if (buf[r++]=='1') bin[w]|=bit;