summaryrefslogtreecommitdiff
path: root/src/mmsrip/main.c
blob: a575b4a88286ac434b953bc925b2b3874677a494 (plain)
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
/*
 * $RCSfile: main.c,v $
 * $Date: 2006/01/23 20:30:42 $ - $Revision: 1.32 $
 *
 * This file is distributed as a part of MMSRIP ( MMS Ripper ).
 * Copyright (c) 2005-2006 Nicolas BENOIT
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2, or (at your option) any
 * later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 */


#define _GNU_SOURCE

#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <time.h>
#include <sys/time.h>
#include "common.h"
#include "mms.h"
#include "error.h"

#ifdef HAVE_GETOPT_H
#include <getopt.h>
#endif

/*
 * options
 */
#if defined(SOLARIS) || defined(sun)
static char * options = "ahvtqko:d:g:";
#else
static char * options = "-ahvtqko:d:g:";
#endif

#ifdef HAVE_GETOPT_LONG
static struct option long_options[] = {
  {"about",   0, NULL, 'a'},
  {"version", 0, NULL, 'v'},
  {"help",    0, NULL, 'h'},
  {"test",    0, NULL, 't'},
  {"quiet",   0, NULL, 'q'},
  {"trick",   0, NULL, 'k'},
  {"output",  1, NULL, 'o'},
  {"delay",   1, NULL, 'd'},
  {"debug",   1, NULL, 'g'},
  {NULL,      0, NULL,  0 }
};
#endif

/*
 * usage
 */
void
usage ( void )
{
  fprintf ( stderr, "%s (%s) version %s\n\n", PROGRAM_SHORT_NAME, PROGRAM_FULL_NAME, PROGRAM_VERSION );
  fprintf ( stderr, "usage: %s <[-oFILE] stream url> ...\n\n", PROGRAM_SHORT_NAME );

#ifdef HAVE_GETOPT_LONG
  fprintf ( stderr, "General Options:\n\t"                                                      \
                    "-a, --about\t\tshow information about %s\n\t"                              \
                    "-h, --help\t\tshow this help\n\t"                                          \
                    "-v, --version\t\tshow version number\n\n"                                  \
                    "Program Behaviour:\n\t"                                                    \
                    "-oFILE, --output=FILE\toutput to specified file (can be repeated)\n\t"     \
                    "-gFILE, --debug=FILE\toutput debug info to specified file\n\t"             \
                    "-q, --quiet\t\tquiet mode (can be repeated)\n\t"                           \
                    "-dDELAY, --delay=DELAY\tsave the stream during DELAY seconds and exit\n\t" \
                    "-k, --trick\t\tattempt to trick recalcitrant servers\n\t"                  \
                    "-t, --test\t\ttest mode (check stream availability)\n\n", PROGRAM_SHORT_NAME );
#else
  fprintf ( stderr, "General Options:\n\t"                                       \
                    "-a\tshow information about %s\n\t"                          \
                    "-h\tshow this help\n\t"                                     \
                    "-v\tshow version number\n\n"                                \
                    "Program Behaviour:\n\t"                                     \
                    "-oFILE\toutput to specified file (can be repeated)\n\t"     \
                    "-gFILE\toutput debug info to specified file\n\t"            \
                    "-q\tquiet mode (can be repeated)\n\t"                       \
                    "-dDELAY\tsave the stream during DELAY seconds and exit\n\t" \
                    "-k\tattempt to trick recalcitrant servers\n\t"              \
                    "-t\ttest mode (check stream availability)\n\n", PROGRAM_SHORT_NAME );
#endif

  return;
}


/*
 * main
 */
int
main ( int argc,
       char *argv[] )
{
  MMS *mms;
  FILE *f;
  FILE *stddebug = NULL;
  char *output_filename;
  STREAM_LIST *stream_list;
  STREAM_LIST *block;
  char c;
  int ret = MMS_RET_SUCCESS;
  int quiet = 0;
  int trick = MMS_TRICK_DISABLED;
  int retry = 0;
  int test = 0;
  int delay = 0;
  time_t end = 0;
  float speed = 0.0f;
  struct timeval speed_last_update;
  struct timeval now;
  float elapsed_time;
  ssize_t len_written;
  uint64_t total_len_written = 0;
  uint64_t old_total_len_written = 0;

  if ( ( stream_list = (STREAM_LIST *) malloc(sizeof(STREAM_LIST)) ) == NULL )
    {
      error ( "main", "early initialization failed" );
      return 1;
    }

  stream_list->next = NULL;
  stream_list->stream = NULL;
  stream_list->output = NULL;

  block = stream_list;

#ifdef HAVE_GETOPT_LONG
  while ( ( c = getopt_long(argc, argv, options, long_options, NULL) ) != -1 )
#elif defined(SOLARIS) || defined(sun)
  /* Implementation of getopt in Solaris is a bit strange, it returns -1 even if there are still args to parse... */
  while ( optind < argc )
#else
  while ( ( c = getopt(argc, argv, options) ) != -1 )
#endif
    {
#if defined(SOLARIS) || defined(sun)
      c = getopt ( argc, argv, options );
#endif
      switch (c)
        {
        case 'h':
          {
            fprintf ( stderr, "\n" );
            usage();
            return 0;
          }

        case 'v':
          {
            fprintf ( stderr, "%s version %s\n", PROGRAM_SHORT_NAME, PROGRAM_VERSION);
            return 0;
          }

        case 'a':
          {
            fprintf ( stderr, "\n" );
            fprintf ( stderr, "%s (%s) version %s\n\n", PROGRAM_SHORT_NAME, PROGRAM_FULL_NAME, PROGRAM_VERSION);
            fprintf ( stderr, "Written by %s.\n", PROGRAM_AUTHORS );
            fprintf ( stderr, "With a lot of help from %s.\n\n", PROGRAM_HELPERS );
            fprintf ( stderr, "This program is free software; you can redistribute it and/or\nmodify it under the terms "         \
                              "of the GNU General Public License\nas published by the Free Software Foundation; either version 2" \
                              ",\nor (at your option) any later version.\n\n" );
            return 0;
          }

        case 't':
          {
            test = 1;
            break;
          }

        case 'q':
          {
            quiet += 1;
            break;
          }

        case 'k':
          {
            trick = MMS_TRICK_ENABLED;
            break;
          }

        case 'o':
          {
            if ( optarg != NULL )
              {
                if ( block->stream != NULL )
                  {
                    if ( ( block->next = malloc ( sizeof(STREAM_LIST) ) ) == NULL )
                      {
                        error ( "main", "early initialization failed" );
                        ret = MMS_RET_ERROR;
                        goto clean;
                      }

                    block = block->next;
                    block->next = NULL;
                    block->stream = NULL;
                  }

                if ( block->output != NULL )
                  free ( block->output );

                block->output = (char *) strdup ( optarg );
              }

            break;
          }

        case 'g':
          {
            if ( optarg != NULL )
              {
                if ( stddebug != NULL )
                  fclose ( stddebug );

                if ( ( stddebug = fopen ( optarg, "w" ) ) == NULL )
                  {
                    if ( quiet < 2 )
#ifdef HAVE_VSNPRINTF
                      warning ( NULL, "unable to write debug info in \'%s\'", optarg );
#else
                      warning ( NULL, "unable to write debug info in specified file" );
#endif
                  }
                else
                  {
                    fprintf ( stddebug, "%s (%s) version %s\n\n", PROGRAM_SHORT_NAME, PROGRAM_FULL_NAME, PROGRAM_VERSION);
                    fprintf ( stddebug, "--> debug log begins now\n" );
                  }
              }

            break;
          }

        case 'd':
          {
            if ( optarg != NULL )
              {
                delay = atoi( optarg );

                if ( delay < 0 )
                  delay = 0;
              }

            break;
          }


#if defined(SOLARIS) || defined(sun)
        case -1:
          {
            if ( optind >= argc )
              break;
#else
        case 1:
          {
            if ( optarg != NULL )
              {
#endif
                if ( block->stream != NULL )
                  {
                    if ( ( block->next = malloc ( sizeof(STREAM_LIST) ) ) == NULL )
                      {
                        error ( "main", "early initialization failed" );
                        ret = MMS_RET_ERROR;
                        goto clean;
                      }

                    block = block->next;
                    block->next = NULL;
                    block->output = NULL;
                  }
#if defined(SOLARIS) || defined(sun)
                /* optind is not incremented when meeting something else than an option, so we do that... */
                block->stream = (char *) strdup ( argv[optind++] );
#else
                block->stream = (char *) strdup ( optarg );
              }
#endif
            break;
          }
        }
    }

  if ( stream_list->stream == NULL )
    {
      usage ( );
      ret = MMS_RET_ERROR;
      goto clean;
    }

  if ( !quiet )
    {
      fprintf ( stderr, "\n" );
      fprintf ( stderr, "%s (%s) version %s\n\n", PROGRAM_SHORT_NAME, PROGRAM_FULL_NAME, PROGRAM_VERSION );
    }

  for ( block=stream_list; block!=NULL; block=(STREAM_LIST*)block->next )
    {
      if ( block->stream == NULL )
        {
          if ( quiet < 2 )
            {
              if ( block->output == NULL )
#ifdef HAVE_VSNPRINTF
                error ( "main", "invalid invocation of %s", PROGRAM_SHORT_NAME );
#else
                error ( "main", "invalid invocation of mmsrip" );
#endif
              else
#ifdef HAVE_VSNPRINTF
                error ( "main", "output to \'%s\' is not attached to any stream", block->output );
#else
                error ( "main", "one of the -o output is not attached to any stream" );
#endif
            }

          ret = MMS_RET_ERROR;
          goto clean;
        }

      if ( block->output == NULL )
        {
          char *tmp = strchr ( block->stream, '/' );
          char *interro_ptr = strchr ( block->stream, '?' );

          if ( interro_ptr == NULL )
            output_filename = strrchr ( block->stream, '/' );
          else
            {
              do  /* we look for the last '/' before the first '?' */
                {
                  output_filename = tmp;
                  tmp = strchr ( tmp+1, '/' );
                }
              while ( ( tmp < interro_ptr ) && ( tmp != NULL ) );
            }

          if ( output_filename == NULL )
            {
              if ( quiet < 2 )
#ifdef HAVE_VSNPRINTF
                error ( "main", "malformed url: \'%s\'", block->stream );
#else
                error ( "main", "malformed url" );
#endif

              ret = MMS_RET_ERROR;
              continue;
            }

          ++output_filename;

          if ( strlen ( output_filename ) == 0 )
            {
              if ( quiet < 2 )
#ifdef HAVE_VSNPRINTF
                error ( "main", "malformed url: \'%s\'", block->stream );
#else
                error ( "main", "malformed url" );
#endif

              ret = MMS_RET_ERROR;
              continue;
            }

          block->output = (char *) strdup ( output_filename );

          /* we clean filenames that look like 'stream.asf?digest=7Q2bjXo&provider=lala' */
          if ( ( interro_ptr = strchr(block->output,'?') ) != NULL )
            *interro_ptr = '\0';
        }
    }

  if ( ret != MMS_RET_SUCCESS )
    goto clean;

  if ( delay != 0 )
    end = time(NULL) + delay;

  for ( block=stream_list; block!=NULL; block=(STREAM_LIST*)(retry?block:block->next) )
    {
      output_filename = block->output;
      retry = 0;

      if ( !test )
        {
          if ( ( f = fopen ( output_filename, "w" ) ) == NULL )
            {
              if ( quiet < 2 )
#ifdef HAVE_VSNPRINTF
                error ( "main", "unable to write in \'%s\'", output_filename );
#else
                error ( "main", "unable to write in output file" );
#endif

              ret = MMS_RET_ERROR;
              continue;
            }
        }
      else
        f = NULL;

      if ( ( mms = mms_create ( block->stream, f, stddebug, trick, test?1:(quiet>>1) ) ) == NULL )
        {
          if ( quiet < 2 )
            error ( "main", "unable to create mms struct" );

          if ( !test )
            {
              fclose ( f );
              remove ( output_filename );
            }

          ret = MMS_RET_ERROR;
          continue;
        }

      if ( !quiet )
        fprintf ( stderr, "Connecting ...\r" );

      if ( mms_connect ( mms ) != MMS_RET_SUCCESS )
        {
          if ( quiet < 2 )
            error ( "main", "unable to connect" );

          mms_destroy ( mms );

          if ( !test )
            {
              fclose ( f );
              remove ( output_filename );
            }

          ret = MMS_RET_ERROR;
          continue;
        }

      if ( !quiet )
        fprintf ( stderr, "Handshaking ...\r" );

      if ( mms_handshake ( mms ) != MMS_RET_SUCCESS )
        {
          if ( ( quiet < 2 ) && ( !test ) )
            error ( "main", "unable to handshake" );

          mms_disconnect ( mms );
          mms_destroy ( mms );

          if ( !test )
            {
              fclose ( f );
              remove ( output_filename );
            }

          if ( !quiet )
            {
              if ( !test )
                fprintf ( stderr, "Stream \'%s\' is not good.\n\n", block->stream );
              else
                fprintf ( stderr, "Stream \'%s\' is not good.\n", block->stream );
            }

          ret = MMS_RET_ERROR;
          continue;
        }

      if ( test )
        {
          if ( !quiet )
            fprintf ( stderr, "Stream \'%s\' is available.\n", block->stream );

          mms_disconnect ( mms );
          mms_destroy ( mms );
          continue;
        }

      if ( !quiet )
        fprintf ( stderr, "Getting header ...\r" );

      if ( ( len_written = mms_write_stream_header ( mms ) ) == MMS_RET_ERROR )
        {
          if ( quiet < 2 )
            error ( "main", "unable to write stream header" );

          mms_disconnect ( mms );
          mms_destroy ( mms );
          fclose ( f );
          remove ( output_filename );
          ret = MMS_RET_ERROR;
          continue;
        }

      total_len_written = len_written;

      if ( !quiet )
        fprintf ( stderr, "Rip is about to start ...\r" );

      if ( mms_begin_rip ( mms ) != MMS_RET_SUCCESS )
        {
          if ( quiet < 2 )
            error ( "main", "unable to begin the rip" );

          mms_disconnect ( mms );
          mms_destroy ( mms );
          fclose ( f );
          remove ( output_filename );
          ret = -1;
          continue;
        }

      if ( mms->is_live == MMS_LIVE )
        {
          if ( !quiet )
            {
              
              fprintf ( stderr, "                                                            \r" );
              fprintf ( stderr, "%s: %d bytes written (--.- kbps)\r", output_filename, (ssize_t)total_len_written );
              gettimeofday ( &speed_last_update, NULL );
            }

          while ( 1 )
            {
              len_written = mms_write_stream_data ( mms );

              if ( len_written == 0 )
                break;
              else if ( len_written == MMS_RET_ERROR )
                {
                  if ( quiet < 2 )
                    error ( "main", "unable to write stream data" );

                  ret = MMS_RET_ERROR;
                  break;
                }
              else if ( len_written == MMS_RET_NO_AUTH )
                {
                  if ( trick == MMS_TRICK_DISABLED )
                    {
                      /* we retry with the trick enabled */
                      if ( quiet < 2 )
                        fprintf ( stderr, "\r*** retrying with the anti-recalcitrant servers trick enabled ***\n" );

                      trick = MMS_TRICK_ENABLED;
                      retry = 1;
                    }
                  else
                    {
                      /* it's definitely not working */
                      if ( quiet < 2 )
                        error ( "main", "unable to write stream data" );

                      ret = MMS_RET_NO_AUTH;
                    }

                  break;
                }

              total_len_written += len_written;

              if ( !quiet )
                {
                  gettimeofday ( &now, NULL );

                  if ( now.tv_sec > speed_last_update.tv_sec )
                    {
                      elapsed_time = (now.tv_sec - speed_last_update.tv_sec) + ((now.tv_usec - speed_last_update.tv_usec) / 1000000.0f);

                      if ( elapsed_time >= 1.0f )
                        {
                          speed = ( ( ((float) (total_len_written-old_total_len_written)) / elapsed_time) / 1024.0f );
                          old_total_len_written = total_len_written;
                          gettimeofday ( &speed_last_update, NULL );
                        }
                    }

                  if ( speed > 0.0f )
                    {
                      if ( (speed / 1024.0f) >= 1.0f )
                        fprintf ( stderr, "%s: %d bytes written (%.1f mbps)    \r", output_filename, (ssize_t)total_len_written, speed/1024.0f );
                      else
                        fprintf ( stderr, "%s: %d bytes written (%.1f kbps)    \r", output_filename, (ssize_t)total_len_written, speed );
                    }
                  else
                    fprintf ( stderr, "%s: %d bytes written (--.- kbps)    \r", output_filename, (ssize_t)total_len_written );
                }

              fflush ( f );

              if ( delay != 0 )
                {
                  if ( end <= time(NULL) )
                    {
                      delay = -1;
                      break;
                    }
                }
            }

          /* for a live, we should rewrite the header */

          if ( ( ret == MMS_RET_SUCCESS ) && ( !quiet ) && ( !retry ) )
            fprintf ( stderr, "%s: %d bytes written              \r", output_filename, (ssize_t)total_len_written );
        }
      else
        {
          register const double coef = 100.0 / (double) mms->expected_file_size;

          if ( !quiet )
            {
              fprintf ( stderr, "                                                            \r" );
              fprintf ( stderr, "%s: %.2f%% (--.- kbps)\r", output_filename, (double) total_len_written * coef );
              gettimeofday ( &speed_last_update, NULL );
            }

          while ( 1 )
            {
              len_written = mms_write_stream_data ( mms );

              if ( len_written == 0 )
                break;
              else if ( len_written == MMS_RET_ERROR )
                {
                  if ( quiet < 2 )
                    error ( "main", "unable to write stream data" );

                  ret = MMS_RET_ERROR;
                  break;
                }
              else if ( len_written == MMS_RET_NO_AUTH )
                {
                  if ( trick == MMS_TRICK_DISABLED )
                    {
                      /* we retry with the trick enabled */
                      if ( quiet < 2 )
                        fprintf ( stderr, "\r*** retrying with the anti-recalcitrant servers trick enabled ***\n" );

                      trick = MMS_TRICK_ENABLED;
                      retry = 1;
                    }
                  else
                    {
                      /* it's definitely not working */
                      if ( quiet < 2 )
                        error ( "main", "unable to write stream data" );

                      ret = MMS_RET_NO_AUTH;
                    }

                  break;
                }

              total_len_written += len_written;

              if ( !quiet )
                {
                  gettimeofday ( &now, NULL );

                  if ( now.tv_sec > speed_last_update.tv_sec )
                    {
                      elapsed_time = (now.tv_sec - speed_last_update.tv_sec) + ((now.tv_usec - speed_last_update.tv_usec) / 1000000.0f);

                      if ( elapsed_time >= 1.0f )
                        {
                          speed = ( ( ((float) (total_len_written-old_total_len_written)) / elapsed_time) / 1024.0f );
                          old_total_len_written = total_len_written;
                          gettimeofday ( &speed_last_update, NULL );
                        }
                    }

                  if ( speed > 0.0f )
                    {
                      if ( (speed / 1024.0f) >= 1.0f )
                        fprintf ( stderr, "%s: %.2f%% (%.1f mbps)    \r", output_filename, (double) total_len_written * coef, speed/1024.0f );
                      else
                        fprintf ( stderr, "%s: %.2f%% (%.1f kbps)    \r", output_filename, (double) total_len_written * coef, speed );
                    }
                  else
                    fprintf ( stderr, "%s: %.2f%% (--.- kbps)    \r", output_filename, (double) total_len_written * coef );
                }

              fflush ( f );

              if ( delay != 0 )
                {
                  if ( end <= time(NULL) )
                    {
                      delay = -1;
                      break;
                    }
                }
            }

          if ( ( ret == MMS_RET_SUCCESS ) && ( !quiet ) && ( delay != -1 ) && ( !retry ) )
            fprintf ( stderr, "%s: 100.00%%                 \r", output_filename );
        }

      mms_disconnect ( mms );
      mms_destroy ( mms );

      fclose ( f );

      if ( delay == -1 )
        break;
    }

 clean:
  if ( !quiet )
    fprintf ( stderr, "\n" );

  if ( stddebug != NULL )
    {
      fprintf ( stddebug, "\n\n--> debug log ends now\n" );
      fclose ( stddebug );
    }

  for ( block=stream_list; block!=NULL; )
    {
      STREAM_LIST *old;
      old = block;
      block = (STREAM_LIST *) block->next;

      if ( old->stream != NULL )
        free ( old->stream );

      if ( old->output != NULL )
        free ( old->output );

      free ( old );
    }

  return (ret<0) ? (ret*-1) : ret;
}