+ assert(chars==-1); assert(bits==0); assert(w==got);
+ nokiaprep(bin,w);
+#undef WORD
+}
+
+static inline void ringread(void)
+{
+FILE *f;
+unsigned char bin1[140]={
+ 6, /* UDH length */
+ 0x05, /* IEI */
+ 0x04, /* IEDL */
+ 0x15, 0x81, /* dest port (ring tones) */
+ 0x15, 0x81 /* src port (unused) */
+#define BIN1_PAYLOAD (140-7)
+ };
+unsigned char binn[140]={
+ 11, /* UDH length */
+ 0x05, /* IEI */
+ 0x04, /* IEDL */
+ 0x15, 0x81, /* dest port (ring tones) */
+ 0x15, 0x81, /* src port (unused) */
+ 0x00, 0x03, /* multipart */
+ /* 0x??, unique serial ID */
+ /* 0x??, total messages */
+ /* 0x??, message number (# from 1) */
+#define BINN_PAYLOAD (140-12)
+ };
+size_t got,want;
+int totn,fragn;
+long size;
+
+#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);
+ if ((size=getfilesize(f,logoname))==-1)
+ error("!File size is essential to continue operation");
+ if (size<0x103)
+ error("!File \"%s\" size %ld too small (must >=0x103)! Is it .000 file?",
+ logoname,size);
+ if (fseek(f,0x100,SEEK_SET))
+ error("^Seeking error on \"%s\", ignoring",logoname);
+ size-=0x100;
+ if (size<=BIN1_PAYLOAD) {
+ if ((got=fread(bin1+7,1,size,f))!=size)
+ error("^Read error on \"%s\", wanted %ld, got %d",logoname,size,got);
+ error("\nSending ring tone \"%s\" as single SMS (size %ld, max %d)",
+ ringname,size,BIN1_PAYLOAD);
+ nokiaprep(bin1,7+size);
+ }
+ else {
+ totn=(size+BINN_PAYLOAD-1)/BINN_PAYLOAD;
+ if (totn>0xFF)
+ error("!File size %ld too large for multi-SMS ring upload (max=%d)",
+ size,BINN_PAYLOAD*0xFF);
+ binn[10]=totn;
+ if (verbose>=1)
+ error("\nSending ring tone \"%s\" as %d multi-SMSes (size %ld, max %d, frag %d)",
+ ringname,totn,size,BIN1_PAYLOAD,BINN_PAYLOAD);
+ binn[9]=time(NULL)&0x100; /* rand() would be better but it is a compatibility pain */
+ if (verbose>=1)
+ error("\nUsing unique multi-SMS ID 0x%02X",(unsigned)binn[9]);
+ for (fragn=1;fragn<=totn;fragn++) {
+ binn[11]=fragn;
+ want=MIN(size,BINN_PAYLOAD);
+ if ((got=fread(binn+12,1,want,f))!=want)
+ error("^Read error on \"%s\", wanted %d, got %d",logoname,want,got);
+ nokiaprep(binn,12+want);
+ size-=want;
+ }
+ }
+ chkfclose(f,logoname);