1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <ctype.h>
5
6 #include "general.h"
7 #include "fileoutput.h"
8
9 /*
10
11 Ringtone Tools - Copyright 2001-2005 Michael Kohn (mike@mikekohn.net)
12 This falls under the Kohnian license. Please read
13 it at http://ringtonetools.mikekohn.net/
14
15 This program is NOT opensourced. You may not use any part
16 of this program for your own software.
17
18 */
19
get_bits(unsigned char * buffer,int * ptr,int * bitptr,int bits)20 int get_bits(unsigned char *buffer, int *ptr, int *bitptr, int bits)
21 {
22 unsigned int holding;
23 int i;
24
25 holding=(buffer[*ptr]<<8)+buffer[*ptr+1];
26 i=(holding>>(16-(bits+(*bitptr)))) & ((1<<bits)-1);
27
28 *bitptr=*bitptr+bits;
29 if (*bitptr>7)
30 {
31 *bitptr=*bitptr-8;
32 (*ptr)++;
33 }
34
35 /*
36 printf("i=0x%x ptr=%d bitptr=%d bits=%d\n",i,*ptr,*bitptr,bits);
37 */
38
39 return i;
40 }
41
42
parse_ringtone(FILE * out,int out_type,unsigned char * buffer,int ptr,struct note_t * note)43 int parse_ringtone(FILE *out,int out_type,unsigned char *buffer,int ptr,struct note_t *note)
44 {
45 int bitptr;
46 int k,t,x;
47 int patterns,pattern_id,count;
48 int hflag;
49
50 /*
51 for (t=0; t<ptr; t++)
52 {
53 printf("%x\n",buffer[t]);
54 }
55 */
56
57 hflag=0;
58 bitptr=0; t=0;
59
60 k=get_bits(buffer,&t,&bitptr,8);
61 get_bits(buffer,&t,&bitptr,8);
62 k=get_bits(buffer,&t,&bitptr,7);
63 k=get_bits(buffer,&t,&bitptr,3);
64
65 if (k!=1 && k!=2)
66 {
67 printf("Unsupported songtype %d\n",k);
68 return 0;
69 }
70
71 k=get_bits(buffer,&t,&bitptr,4);
72
73 for (x=0; x<k; x++)
74 {
75 note->songname[x]=get_bits(buffer,&t,&bitptr,8);
76 }
77 note->songname[x]=0;
78
79 patterns=get_bits(buffer,&t,&bitptr,8);
80
81 while(t<ptr)
82 {
83 if (patterns==0) break;
84
85 get_bits(buffer,&t,&bitptr,3);
86 get_bits(buffer,&t,&bitptr,2);
87 note->loop=get_bits(buffer,&t,&bitptr,4);
88 count=get_bits(buffer,&t,&bitptr,8);
89
90 /*
91 printf("count=%d\n",count);
92 */
93
94 for (x=0; x<count; x++)
95 {
96 if (t>=ptr) break;
97
98 k=get_bits(buffer,&t,&bitptr,3);
99 /*
100 printf("command=%d\n",k);
101 */
102 if (k==0)
103 {
104 pattern_id=get_bits(buffer,&t,&bitptr,2);
105 }
106 else
107 if (k==1)
108 {
109 if (hflag==0)
110 {
111 header_route(out,note,10,out_type);
112 hflag=1;
113 }
114
115 note->tone=get_bits(buffer,&t,&bitptr,4);
116 note->length=get_bits(buffer,&t,&bitptr,3);
117 note->modifier=get_bits(buffer,&t,&bitptr,2);
118
119 note_route(out,note,10,out_type);
120 }
121 else
122 if (k==2)
123 {
124 note->scale=get_bits(buffer,&t,&bitptr,2);
125 if (note->scale>0) note->scale--;
126 }
127 else
128 if (k==3)
129 {
130 note->style=get_bits(buffer,&t,&bitptr,2);
131 }
132 else
133 if (k==4)
134 {
135 k=get_bits(buffer,&t,&bitptr,5);
136 note->bpm=reverse_tempo(k);
137 if (hflag==1) tempo_route(out,note,10,out_type);
138 }
139 else
140 if (k==5)
141 {
142 note->volume=get_bits(buffer,&t,&bitptr,4);
143 }
144 }
145
146 if (t>=ptr) break;
147
148 patterns--;
149 }
150
151 footer_route(out,note,10,out_type);
152
153 return 0;
154 }
155
parse_group_icon(FILE * out,int out_type,unsigned char * buffer,int ptr,struct note_t * note)156 int parse_group_icon(FILE *out,int out_type,unsigned char *buffer,int ptr,struct note_t *note)
157 {
158 int t,k,p;
159
160 /*
161 #ifdef DEBUG
162 for (t=0; t<ptr; t++)
163 {
164 printf("%x\n",buffer[t]);
165 }
166 #endif
167 */
168
169 if (buffer[0]!=0x30 || buffer[1]!=0x00)
170 {
171 printf("\nSCKL: Illegal header %d %d\n",buffer[0],buffer[1]);
172 return 0;
173 }
174
175 note->width=buffer[2];
176 note->height=buffer[3];
177
178 /* note->bitplanes=buffer[4]; */
179
180 logo_header_route(out,note,out_type);
181
182 p=0;
183 for (k=5; k<ptr; k++)
184 {
185 /* ring_stack[stackptr++]=buffer[k]; */
186 for (t=7; t>=0; t--)
187 {
188 if (((buffer[k]>>t)&1)==0)
189 { note->picture[p++]=0; }
190 else
191 { note->picture[p++]=0xffffff; }
192 }
193 }
194
195 logo_footer_route(out,note,out_type);
196
197 return 0;
198 }
199
reorder(unsigned char * buffer,short int * messages,int curr,int ptr)200 int reorder(unsigned char *buffer,short int *messages, int curr, int ptr)
201 {
202 int data[8192];
203 int t,r;
204 int s,e,p;
205
206 p=0;
207
208 for (t=0; t<curr; t++)
209 {
210 s=messages[t*2];
211 e=messages[t*2+1];
212 /*
213 if (t==curr-1)
214 { e=ptr; }
215 else
216 { e=messages[t+1]; }
217 */
218
219 /* printf("in reorder %d %d\n",s,e); */
220 for (r=s; r<e; r++)
221 {
222 /* printf("%x\n",buffer[r]); */
223 data[p++]=buffer[r];
224 }
225 }
226
227 for(r=0; r<ptr; r++)
228 {
229 buffer[r]=data[r];
230 /* printf("%x\n",buffer[r]); */
231 }
232
233 return 0;
234 }
235
read_sckl_header(FILE * in,int * port,int * id,int * count,int * mess)236 int read_sckl_header(FILE *in, int *port, int *id, int *count, int *mess)
237 {
238 unsigned char buffer[5];
239 int ch;
240
241 *port=0;
242
243 while((ch=getc(in))!=EOF)
244 {
245 if (ch=='\n' || ch=='\r' || ch==' ' || ch=='\t') continue;
246 break;
247 }
248
249 if (ch==EOF) return -1;
250 if (ch=='/')
251 {
252 getc(in);
253 read_chars(in,buffer,4);
254 if (strcasecmp(buffer,"SCKL")!=0) return 0;
255 read_chars(in,buffer,4);
256 *port=atoi(buffer);
257 read_chars(in,buffer,4);
258 read_chars(in,buffer,2);
259 *id=atoi(buffer);
260 read_chars(in,buffer,2);
261 *count=atoi(buffer);
262 read_chars(in,buffer,2);
263 *mess=atoi(buffer);
264 }
265 else
266 {
267 ungetc(ch,in);
268 if (ch=='0')
269 {
270 *port=1581;
271 }
272 else
273 if (ch=='3')
274 {
275 *port=1583;
276 }
277
278 (*id)=0;
279 (*mess)++;
280 (*count)++;
281 }
282
283 return *port;
284 }
285
parse_sckl(FILE * in,FILE * out,int out_type,struct note_t * note)286 int parse_sckl(FILE *in, FILE *out, int out_type, struct note_t *note)
287 {
288 unsigned char buffer[8192];
289 short int messages[512];
290 int ch;
291 int port,id,count,mess;
292 int i,p,c,m;
293 int curr,ptr;
294
295 curr=0;
296 ptr=0;
297
298 port=0;
299 id=0;
300 count=0;
301
302 while(1)
303 {
304 ch=read_sckl_header(in,&p,&i,&c,&m);
305 if (ch==0)
306 {
307 printf("Unknown header\n");
308 break;
309 }
310 else
311 if (ch==-1)
312 { break; }
313
314 if (m>c)
315 {
316 printf("Error: More messages than maxiumum in header.\n");
317 return 0;
318 }
319
320 if (port==0)
321 {
322 port=p;
323 count=c;
324 id=i;
325 }
326 else
327 {
328 if (port!=p)
329 {
330 printf("Ports don't match. Exiting..\n");
331 return 0;
332 }
333
334 if (id!=i)
335 {
336 printf("ID's don't match. Exiting..\n");
337 return 0;
338 }
339
340 if (count!=c)
341 {
342 printf("Message counts don't match. Exiting..\n");
343 return 0;
344 }
345 }
346
347 mess=m;
348 messages[(mess-1)*2]=ptr;
349 curr++;
350
351 while((ch=getc(in))!=EOF)
352 {
353 if (ch==' ' || ch=='\t' || ch=='\n' || ch=='\r') continue;
354 if (ch=='/')
355 {
356 ungetc(ch,in);
357 break;
358 }
359
360 p=2;
361 m=0;
362
363 while(p>0)
364 {
365 ch=tolower(ch);
366
367 if (ch=='a') c=10;
368 else
369 if (ch=='b') c=11;
370 else
371 if (ch=='c') c=12;
372 else
373 if (ch=='d') c=13;
374 else
375 if (ch=='e') c=14;
376 else
377 if (ch=='f') c=15;
378 else
379 { c=ch-'0'; }
380
381 m=(m<<4)+c;
382 p--;
383 if (p==1) ch=getc(in);
384 }
385
386 /* printf("%x\n",m); */
387 buffer[ptr++]=m;
388 }
389
390 messages[(mess-1)*2+1]=ptr;
391 }
392
393 if (count!=curr)
394 {
395 printf("Missing messages!\n");
396 return 0;
397 }
398
399 reorder(buffer,messages,curr,ptr);
400
401 if (port==1583)
402 {
403 parse_group_icon(out,out_type,buffer,ptr,note);
404 }
405 else
406 if (port==1581)
407 {
408 parse_ringtone(out,out_type,buffer,ptr,note);
409 }
410 else
411 {
412 printf("Unsupported port. Exiting.\n");
413 return 0;
414 }
415
416 return 0;
417 }
418
419
parse_ott(FILE * in,FILE * out,int out_type,struct note_t * note)420 int parse_ott(FILE *in, FILE *out, int out_type, struct note_t *note)
421 {
422 unsigned char buffer[16738];
423 int ptr,ch;
424
425 for (ptr=0; ptr<16738; ptr++)
426 {
427 ch=getc(in);
428 if (ch==EOF) break;
429 buffer[ptr]=ch;
430 }
431
432 parse_ringtone(out,out_type,buffer,ptr,note);
433
434 return 0;
435 }
436
437