-
Notifications
You must be signed in to change notification settings - Fork 1
/
play.c
794 lines (654 loc) · 23.8 KB
/
play.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
/*
* play.c: movie playback
*
* Copyright 2005
* See the COPYING file for more information on licensing and use.
*
* This file contains all functions relating to playing back a movie.
*/
#include <windows.h>
#include <winsock2.h>
#include <stdio.h>
#include <fcntl.h>
#include <io.h>
#include <sys/types.h>
#include <dirent.h>
#include <stdlib.h>
#include <process.h>
#include "zlib.h"
#include "tibiamovie.h"
/* these variable names are embarassing :P */
int sockPlayListenCharacter = -1;
int sockPlayClientCharacter = -1;
int sockPlayListenServer = -1;
int sockPlayClientServer = -1;
char playFilename[512];
gzFile fpPlay = NULL;
int bytesPlayed = 0;
unsigned int msPlayed = 0;
unsigned int msTotal = 0;
int *playMarkers = NULL;
int playMarkersCnt = 0;
int playSpeed = 1;
int abortPlayThread = 0;
int fastForwarding = 0;
int playRec = 0;
int playing = 0;
int nextPacket = 0;
int numPackets = 0;
int curPacket = 0;
void PlayListen(int port, int *sock)
{
struct hostent *host;
struct sockaddr_in addr;
*sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (!(host = gethostbyname("127.0.0.1"))) {
return;
}
memcpy((char *)&addr.sin_addr, host->h_addr, host->h_length);
addr.sin_family = host->h_addrtype;
addr.sin_port = htons(port);
if (bind(*sock, (struct sockaddr *)&addr, sizeof(struct sockaddr)) < 0) {
return;
}
if (listen(*sock, 5) < 0) {
return;
}
WSAAsyncSelect(*sock, wMain, WM_SOCKET_PLAY, FD_ACCEPT | FD_READ | FD_CLOSE);
return;
}
void PlayStart(void)
{
playing = 0;
PlayListen(TIBIAPORT, &sockPlayListenCharacter);
PlayListen(7172, &sockPlayListenServer);
return;
}
void PlayEnd(void)
{
closesocket(sockPlayListenCharacter);
closesocket(sockPlayClientCharacter);
closesocket(sockPlayListenServer);
closesocket(sockPlayClientServer);
playing = 0;
/* if the play thread is still going, break on the next iteration */
abortPlayThread = 1;
return;
}
void PlayAccept(int fromsock, int *tosock)
{
struct sockaddr_in addr;
int temp = sizeof(struct sockaddr_in);
if ((*tosock = accept(fromsock, (struct sockaddr *)&addr, &temp)) < 0)
return;
return;
}
struct filelist {
char *filename;
int version;
};
typedef struct filelist FILELIST;
int files_cmp(const void *x, const void *y)
{
FILELIST *dx = *(FILELIST **)x;
FILELIST *dy = *(FILELIST **)y;
char *cdx;
char *cdy;
cdx = dx->filename;
cdy = dy->filename;
return strcasecmp(cdx, cdy);
}
void PlaySendMotd(void)
{
DIR *dir;
struct dirent *e;
int filecount = 0;
char buf[10240];
char motd[1024];
int motdlen;
int port = 7172;
int premdays;
int len;
short l, l2;
char *pos = buf;
gzFile fp = NULL;
FILELIST **files = NULL;
int cnt;
short version = 0, tibiaversion = 0;
dir = opendir(".");
while ((e = readdir(dir)) != 0) {
l = strlen(e->d_name);
if (l > 4 && (strcasecmp(e->d_name + l - 4, ".tmv") == 0 || strcasecmp(e->d_name + l - 4, ".rec") == 0)) {
if ((fp = gzopen(e->d_name, "rb")) == NULL)
continue;
if (gzread(fp, &version, 2) <= 0)
continue;
gzread(fp, &tibiaversion, 2);
gzclose(fp);
if (compatmode && tibiaversion != TibiaVersionFound)
continue;
filecount++;
}
}
closedir(dir);
if (filecount > 0) {
files = (FILELIST **)malloc(sizeof(FILELIST *) * filecount);
memset(files, 0, sizeof(FILELIST *) * filecount);
for (cnt = 0; cnt < filecount; cnt++)
files[cnt] = (FILELIST *)malloc(sizeof(FILELIST));
}
premdays = filecount;
cnt = 0;
dir = opendir(".");
while ((e = readdir(dir)) != 0) {
l = strlen(e->d_name);
if (l > 4 && (strcasecmp(e->d_name + l - 4, ".tmv") == 0 || strcasecmp(e->d_name + l - 4, ".rec") == 0)) {
if ((fp = gzopen(e->d_name, "rb")) == NULL)
continue;
if (gzread(fp, &version, 2) <= 0)
continue;
gzread(fp, &tibiaversion, 2);
gzclose(fp);
if (compatmode && tibiaversion != TibiaVersionFound)
continue;
if (files[cnt]) {
files[cnt]->filename = (char *)malloc(sizeof(char) * (strlen(e->d_name) + 1));
strcpy(files[cnt]->filename, e->d_name);
files[cnt]->version = tibiaversion;
cnt++;
}
}
}
closedir(dir);
qsort(files, filecount, sizeof(FILELIST *), files_cmp);
/* fill in the first two bytes later on with the packet length */
pos += 2;
/* still don't know what this means */
*pos++ = 0x14;
/* send TibiaMovie's MOTD */
sprintf(motd, "123\nWelcome to TibiaMovie!\n");
motdlen = strlen(motd);
memcpy(pos, &motdlen, 2); pos += 2;
memcpy(pos, motd, motdlen); pos += motdlen;
/* still don't know what this means */
*pos++ = 0x64;
/* number of "characters" (ie. files) we want to display to the client */
*pos++ = filecount;
/* display all files to the client */
for (cnt = 0; cnt < filecount; cnt++) {
short tibiaversion = 0;
l = strlen(files[cnt]->filename);
tibiaversion = files[cnt]->version;
/* send the length of the filename */
memcpy(pos, &l, 2); pos += 2;
/* send the file itself */
memcpy(pos, files[cnt]->filename, l); pos += l;
/* if the tibia version is 0 for some reason, send a default string as the server name */
if (tibiaversion == 0) {
*pos++ = 10;
*pos++ = 0x00;
strcpy(pos, "TibiaMovie"); pos += 10;
}
else {
char buf2[20];
sprintf(buf2, "%.02f", tibiaversion / 100.0);
l2 = strlen(buf2);
/* send the length of the server string */
memcpy(pos, &l2, 2); pos += 2;
/* send the server string */
memcpy(pos, buf2, l2); pos += l2;
}
/* send the IP address (127.0.0.1) */
*pos++ = 127; *pos++ = 0; *pos++ = 0; *pos++ = 1;
/* send the port */
memcpy(pos, &port, 2); pos += 2;
}
/* send the number of premium days remaining */
memcpy(pos, &premdays, 2); pos += 2;
/* buf = start of buffer, pos = end of buffer, so pos - buf is the length, and subtract
* 2 for the first two bytes that we're about to set */
len = pos - buf - 2;
memcpy(&buf[0], &len, 2);
/* finally! send the payload */
send(sockPlayClientCharacter, buf, len + 2, 0);
for (cnt = 0; cnt < filecount; cnt++) {
if (files && files[cnt]) {
free(files[cnt]->filename);
free(files[cnt]);
}
}
if (files)
free(files);
return;
}
void PlayFindMarkers(void)
{
unsigned char buf[65500];
short version, tibiaversion;
int msPlayed = 0;
int msTotal;
int chunk;
int delay;
unsigned short len;
gzFile fpFind;
playMarkersCnt = 0;
if (playMarkers) {
free(playMarkers);
}
fpFind = gzopen(playFilename, "rb");
/* set the TibiaMovie revision */
buf[0] = gzgetc(fpFind);
buf[1] = gzgetc(fpFind);
memcpy(&version, &buf[0], 2);
/* set the Tibia version */
buf[0] = gzgetc(fpFind);
buf[1] = gzgetc(fpFind);
memcpy(&tibiaversion, &buf[0], 2);
/* set the duration (in milliseconds) of the movie */
buf[0] = gzgetc(fpFind);
buf[1] = gzgetc(fpFind);
buf[2] = gzgetc(fpFind);
buf[3] = gzgetc(fpFind);
memcpy(&msTotal, &buf[0], 4);
while (1) {
chunk = gzgetc(fpFind);
if (chunk == EOF)
break;
if (chunk == RECORD_CHUNK_DATA) {
numPackets++;
/* get the delay to pause after the packet is sent (in milliseconds) */
buf[0] = gzgetc(fpFind);
buf[1] = gzgetc(fpFind);
buf[2] = gzgetc(fpFind);
buf[3] = gzgetc(fpFind);
memcpy(&delay, &buf[0], 4);
if (delay < 1000000)
msPlayed += delay;
/* length of the packet */
buf[0] = gzgetc(fpFind);
buf[1] = gzgetc(fpFind);
memcpy(&len, &buf[0], 2);
gzread(fpFind, buf, len);
}
else if (chunk == RECORD_CHUNK_MARKER) {
if (!playMarkers) {
playMarkers = (int *)malloc(sizeof(int) * 1);
}
else {
playMarkers = (int *)realloc(playMarkers, sizeof(int) * (playMarkersCnt + 1));
}
playMarkers[playMarkersCnt] = msPlayed;
playMarkersCnt++;
}
}
gzclose(fpFind);
}
void Play(void *nothing)
{
unsigned char buf[65500];
unsigned int delay;
unsigned short len;
int llen;
int ch;
int quit = 0;
int sent;
int chunk;
int numpacks = 0;
short version;
short tibiaversion;
int c;
len = strlen(playFilename);
if (strcasecmp(playFilename + len - 4, ".rec") == 0)
playRec = 1;
else
playRec = 0;
numPackets = 0;
curPacket = 0;
if (!playRec) {
PlayFindMarkers();
}
else {
playMarkersCnt = 0;
}
/* do we need this? */
/* Sleep(1000); */
fpPlay = gzopen(playFilename, "rb");
playing = 1;
if (fpPlay) {
if (!playRec) {
/* set the TibiaMovie revision */
buf[0] = gzgetc(fpPlay);
buf[1] = gzgetc(fpPlay);
memcpy(&version, &buf[0], 2);
/* set the Tibia version */
buf[0] = gzgetc(fpPlay);
buf[1] = gzgetc(fpPlay);
memcpy(&tibiaversion, &buf[0], 2);
/* set the duration (in milliseconds) of the movie */
buf[0] = gzgetc(fpPlay);
buf[1] = gzgetc(fpPlay);
buf[2] = gzgetc(fpPlay);
buf[3] = gzgetc(fpPlay);
memcpy(&msTotal, &buf[0], 4);
}
else {
int seek = 0;
/* don't really know what 0x03, 0x01 means? */
buf[0] = gzgetc(fpPlay);
buf[1] = gzgetc(fpPlay);
memcpy(&version, &buf[0], 2);
/* they store in seconds, so multiply by 1000 */
buf[0] = gzgetc(fpPlay);
buf[1] = gzgetc(fpPlay);
buf[2] = gzgetc(fpPlay);
buf[3] = gzgetc(fpPlay);
memcpy(&numpacks, &buf[0], 4);
seek = 6;
for (c = 0; c < numpacks; c++) {
buf[0] = gzgetc(fpPlay);
buf[1] = gzgetc(fpPlay);
buf[2] = gzgetc(fpPlay);
buf[3] = gzgetc(fpPlay);
seek += 4;
memcpy(&llen, &buf[0], 4);
len = (short)llen;
buf[0] = gzgetc(fpPlay);
buf[1] = gzgetc(fpPlay);
buf[2] = gzgetc(fpPlay);
buf[3] = gzgetc(fpPlay);
seek += 4;
memcpy(&delay, &buf[0], 4);
if (c == numpacks - 1) {
/* last pack */
msTotal = delay;
gzseek(fpPlay, 6, SEEK_SET);
break;
}
seek += len;
gzseek(fpPlay, seek, SEEK_SET);
}
}
while (abortPlayThread == 0) {
int threadplaySpeed = playSpeed;
int threadnextPacket = 0;
int frame = 0;
/* the word "CHUNK" is added to debug-style .tmv files, this ignores it */
/* getc(fpPlay);getc(fpPlay);getc(fpPlay);getc(fpPlay);getc(fpPlay); */
ch = gzgetc(fpPlay);
/* check for EOF right off */
if (ch == EOF)
break;
chunk = ch;
if (chunk == RECORD_CHUNK_DATA || playRec) { /* if this is a data chunk */
if (!playRec) {
/* get the delay to pause after the packet is sent (in milliseconds) */
buf[0] = gzgetc(fpPlay);
buf[1] = gzgetc(fpPlay);
buf[2] = gzgetc(fpPlay);
buf[3] = gzgetc(fpPlay);
memcpy(&delay, &buf[0], 4);
/* length of the packet */
buf[0] = gzgetc(fpPlay);
buf[1] = gzgetc(fpPlay);
memcpy(&len, &buf[0], 2);
}
else {
buf[0] = ch;
buf[1] = gzgetc(fpPlay);
buf[2] = gzgetc(fpPlay);
buf[3] = gzgetc(fpPlay);
memcpy(&llen, &buf[0], 4);
len = (short)llen;
buf[0] = gzgetc(fpPlay);
buf[1] = gzgetc(fpPlay);
buf[2] = gzgetc(fpPlay);
buf[3] = gzgetc(fpPlay);
memcpy(&delay, &buf[0], 4);
delay = delay - msPlayed;
}
for (c = 0; c < len && c < 30000; c++) {
ch = gzgetc(fpPlay);
if (ch == EOF) {
quit = 1;
break;
}
/* don't know what i was smoking when i did this, but it sure is ugly! */
else if (ch == '&') {
ch = gzgetc(fpPlay);
if (ch == 'm') {
ch = gzgetc(fpPlay);
if (ch == 'i') {
ch = gzgetc(fpPlay);
if (ch == 'd') {
ch = gzgetc(fpPlay);
if (ch == 'd') {
ch = gzgetc(fpPlay);
if (ch == 'o') {
ch = gzgetc(fpPlay);
if (ch == 't') {
ch = gzgetc(fpPlay);
if (ch == ';') {
ch = 0xb7;
}
else { gzseek(fpPlay, gztell(fpPlay) - 7, SEEK_SET); ch = '&'; }
}
else { gzseek(fpPlay, gztell(fpPlay) - 6, SEEK_SET); ch = '&'; }
}
else { gzseek(fpPlay, gztell(fpPlay) - 5, SEEK_SET); ch = '&'; }
}
else { gzseek(fpPlay, gztell(fpPlay) - 4, SEEK_SET); ch = '&'; }
}
else { gzseek(fpPlay, gztell(fpPlay) - 3, SEEK_SET); ch = '&'; }
}
else { gzseek(fpPlay, gztell(fpPlay) - 2, SEEK_SET); ch = '&'; }
}
else {
gzseek(fpPlay, gztell(fpPlay) - 1, SEEK_SET);
ch = '&';
}
}
buf[c] = ch;
}
/* EOF was detected whilst looping the packet, or the delay was too big..
* 15 minutes (900 seconds) in milliseconds is 900,000 ms, so a delay of
* a million or above just cannot happen due to timeouts, i hope :P */
if (quit || delay > 1000000)
break;
if (playSpeed == 0) {
/* simulate a "pause" effect */
while (playSpeed == 0 && mode == MODE_PLAY && nextPacket == 0) {
/* this is safe, we're in our own thread! */
Sleep(100);
}
}
threadplaySpeed = playSpeed;
threadnextPacket = nextPacket;
#define DELAY_UPDATE 1000
if (threadnextPacket) {
nextPacket = 0;
threadnextPacket = 0;
msPlayed += delay;
frame = 1;
}
else if (threadplaySpeed == 0) {
break;
}
else if (delay / threadplaySpeed < DELAY_UPDATE) {
Sleep(delay / UMAX(1, threadplaySpeed));
msPlayed += delay;
}
else {
int ndelay = delay;
while (ndelay >= DELAY_UPDATE) {
threadplaySpeed = playSpeed;
if (threadplaySpeed == 0)
while (playSpeed == 0 && nextPacket == 0 && mode == MODE_PLAY) {
/* this is safe, we're in our own thread! */
Sleep(100);
}
threadplaySpeed = playSpeed;
threadnextPacket = nextPacket;
if (threadnextPacket) {
nextPacket = 0;
break;
}
else {
ndelay -= DELAY_UPDATE;
Sleep(DELAY_UPDATE / UMAX(1, threadplaySpeed));
msPlayed += DELAY_UPDATE;
}
InvalidateRect(wMain, NULL, TRUE);
}
if (ndelay > 0) {
if (threadnextPacket == 0)
Sleep(ndelay / UMAX(1, threadplaySpeed));
msPlayed += ndelay;
}
}
sent = send(sockPlayClientServer, buf, len, 0);
curPacket++;
if (sent == -1)
break;
/* first packet, insert our own status message */
if (bytesPlayed == 0 && playRec == 0) {
short mylen;
char msgbuf[512];
memcpy(&mylen, &buf[0], 2);
/* whilst this interferes with the tibia protocol by injecting our
* own message (harmless anyway), this should still be legal as
* since it's only in playback it doesn't interact with the tibia
* servers whatsoever.
*/
if (mylen == len - 2) {
sprintf(msgbuf, "Recorded in Tibia %.02f using TibiaMovie (%d). "
"Get TibiaMovie at tibiamovie.sourceforge.net/",
tibiaversion / 100.0, version);
SendStatusMessage(sockPlayClientServer, msgbuf);
bytesPlayed += 13;
}
}
bytesPlayed += len + 7;
/* update "bytes played" in the window, but don't update too often otherwise we get flickering */
if (GetTickCount() > lastDraw + 200 || frame) {
InvalidateRect(wMain, NULL, TRUE);
lastDraw = GetTickCount();
}
} /* end data chunk check */
else if (chunk == RECORD_CHUNK_MARKER) {
if (fastForwarding == 1) {
/* marker detected, reset the play speed if the button was pressed */
playSpeed = 1;
fastForwarding = 0;
EnableWindow(btnGoToMarker, 1);
}
bytesPlayed++;
}
}
gzclose(fpPlay);
}
if (fastForwarding == 1) {
playSpeed = 1;
fastForwarding = 0;
EnableWindow(btnGoToMarker, 1);
}
playing = 0;
msPlayed = msTotal;
InvalidateRect(wMain, NULL, TRUE);
closesocket(sockPlayClientServer);
_endthread();
return;
}
void DoSocketPlay(HWND hwnd, int wEvent, int wError, int sock)
{
unsigned char buf[65535];
int cnt = 0;
if (sock == sockPlayListenCharacter && wEvent == FD_ACCEPT) {
PlayAccept(sock, &sockPlayClientCharacter);
PlaySendMotd();
return;
}
if (sock == sockPlayListenServer && wEvent == FD_ACCEPT) {
bytesPlayed = 0;
abortPlayThread = 0;
msPlayed = 0;
msTotal = 0;
fastForwarding = 0;
playFilename[0] = '\0';
PlayAccept(sock, &sockPlayClientServer);
return;
}
if (sock == sockPlayClientCharacter && wEvent == FD_READ) {
while (recv(sock, buf, 4096, 0) > 0);
return;
}
if (sock == sockPlayClientServer && wEvent == FD_READ) {
short filelen;
char filebuf[512];
while ((cnt = recv(sock, buf, 4096, 0)) > 0) {
/* only want the first packet, ie. the filename, so make sure nothing has been
* played yet, assuring this IS the first packet */
if (bytesPlayed == 0 && cnt > 16) {
ZeroMemory(filebuf, sizeof(filebuf));
if (!oldtibia) {
memcpy(&filelen, &buf[12], 2);
memcpy(filebuf, &buf[14], filelen);
}
else {
memcpy(&filelen, &buf[8], 2);
memcpy(filebuf, &buf[10], filelen);
}
strcpy(playFilename, filebuf);
_beginthread(Play, 0, NULL);
}
}
}
return;
}
void SendStatusMessage(int sock, char *message)
{
char out[1024];
char *pout = out;
short packlen = 0;
short messagelen = 0;
messagelen = strlen(message);
packlen = messagelen + 4;
memcpy(pout, &packlen, 2);
pout += 2;
*pout++ = 0xb4;
*pout++ = 0x14;
memcpy(pout, &messagelen, 2);
pout += 2;
memcpy(pout, message, messagelen);
send(sock, out, packlen + 2, 0);
}
char *duration(unsigned int dur, char *dest)
{
char *dst = dest;
char buf[5];
int d, h, m, s;
*dest = 0;
s = dur % 60;
m = (dur / 60) % 60;
h = (dur / 60 / 60) % 24;
d = (dur / 60 / 60 / 24);
if (d) {
sprintf(buf, "%dd ", d);
strcat(dst, buf);
}
if (h) {
sprintf(buf, "%dh ", h);
strcat(dst, buf);
}
if (m) {
sprintf(buf, "%dm ", m);
strcat(dst, buf);
}
if (s) {
sprintf(buf, "%ds ", s);
strcat(dst, buf);
}
while (*++dst);
dst--;
*dst = 0;
return dest;
}