1 
2 /* -----------------------------------------------------------------------------------------------------------
3 Software License for The Fraunhofer FDK AAC Codec Library for Android
4 
5 � Copyright  1995 - 2013 Fraunhofer-Gesellschaft zur F�rderung der angewandten Forschung e.V.
6   All rights reserved.
7 
8  1.    INTRODUCTION
9 The Fraunhofer FDK AAC Codec Library for Android ("FDK AAC Codec") is software that implements
10 the MPEG Advanced Audio Coding ("AAC") encoding and decoding scheme for digital audio.
11 This FDK AAC Codec software is intended to be used on a wide variety of Android devices.
12 
13 AAC's HE-AAC and HE-AAC v2 versions are regarded as today's most efficient general perceptual
14 audio codecs. AAC-ELD is considered the best-performing full-bandwidth communications codec by
15 independent studies and is widely deployed. AAC has been standardized by ISO and IEC as part
16 of the MPEG specifications.
17 
18 Patent licenses for necessary patent claims for the FDK AAC Codec (including those of Fraunhofer)
19 may be obtained through Via Licensing (www.vialicensing.com) or through the respective patent owners
20 individually for the purpose of encoding or decoding bit streams in products that are compliant with
21 the ISO/IEC MPEG audio standards. Please note that most manufacturers of Android devices already license
22 these patent claims through Via Licensing or directly from the patent owners, and therefore FDK AAC Codec
23 software may already be covered under those patent licenses when it is used for those licensed purposes only.
24 
25 Commercially-licensed AAC software libraries, including floating-point versions with enhanced sound quality,
26 are also available from Fraunhofer. Users are encouraged to check the Fraunhofer website for additional
27 applications information and documentation.
28 
29 2.    COPYRIGHT LICENSE
30 
31 Redistribution and use in source and binary forms, with or without modification, are permitted without
32 payment of copyright license fees provided that you satisfy the following conditions:
33 
34 You must retain the complete text of this software license in redistributions of the FDK AAC Codec or
35 your modifications thereto in source code form.
36 
37 You must retain the complete text of this software license in the documentation and/or other materials
38 provided with redistributions of the FDK AAC Codec or your modifications thereto in binary form.
39 You must make available free of charge copies of the complete source code of the FDK AAC Codec and your
40 modifications thereto to recipients of copies in binary form.
41 
42 The name of Fraunhofer may not be used to endorse or promote products derived from this library without
43 prior written permission.
44 
45 You may not charge copyright license fees for anyone to use, copy or distribute the FDK AAC Codec
46 software or your modifications thereto.
47 
48 Your modified versions of the FDK AAC Codec must carry prominent notices stating that you changed the software
49 and the date of any change. For modified versions of the FDK AAC Codec, the term
50 "Fraunhofer FDK AAC Codec Library for Android" must be replaced by the term
51 "Third-Party Modified Version of the Fraunhofer FDK AAC Codec Library for Android."
52 
53 3.    NO PATENT LICENSE
54 
55 NO EXPRESS OR IMPLIED LICENSES TO ANY PATENT CLAIMS, including without limitation the patents of Fraunhofer,
56 ARE GRANTED BY THIS SOFTWARE LICENSE. Fraunhofer provides no warranty of patent non-infringement with
57 respect to this software.
58 
59 You may use this FDK AAC Codec software or modifications thereto only for purposes that are authorized
60 by appropriate patent licenses.
61 
62 4.    DISCLAIMER
63 
64 This FDK AAC Codec software is provided by Fraunhofer on behalf of the copyright holders and contributors
65 "AS IS" and WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, including but not limited to the implied warranties
66 of merchantability and fitness for a particular purpose. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
67 CONTRIBUTORS BE LIABLE for any direct, indirect, incidental, special, exemplary, or consequential damages,
68 including but not limited to procurement of substitute goods or services; loss of use, data, or profits,
69 or business interruption, however caused and on any theory of liability, whether in contract, strict
70 liability, or tort (including negligence), arising in any way out of the use of this software, even if
71 advised of the possibility of such damage.
72 
73 5.    CONTACT INFORMATION
74 
75 Fraunhofer Institute for Integrated Circuits IIS
76 Attention: Audio and Multimedia Departments - FDK AAC LL
77 Am Wolfsmantel 33
78 91058 Erlangen, Germany
79 
80 www.iis.fraunhofer.de/amm
81 amm-info@iis.fraunhofer.de
82 ----------------------------------------------------------------------------------------------------------- */
83 
84 /**************************  Fraunhofer IIS FDK SysLib  **********************
85 
86    Author(s):
87    Description: command line parser
88 
89 ******************************************************************************/
90 
91 
92 
93 #define _CRT_SECURE_NO_WARNINGS
94 
95 #include <stdarg.h>
96 #include <stdio.h>
97 #include <string.h>
98 #include <stdlib.h>
99 #include <ctype.h>
100 
101 #include "cmdl_parser.h"
102 #include "genericStds.h"
103 
104 
105 
106 /************************ interface ************************/
107 
108 static INT  GetArgFromString(INT argc, TEXTCHAR* argv[], TEXTCHAR* search_string, TEXTCHAR type, TEXTCHAR* found_string , INT* switches_used);
109 static void RemoveWhiteSpace(const TEXTCHAR* pReqArgs, TEXTCHAR* Removed);
110 static INT CheckArg(TEXTCHAR* arg,  TEXTCHAR* str, UINT numArgs, TEXTCHAR type, TEXTCHAR* current_str);
111 static INT CheckForUnusedSwitches(INT argc, /*TEXTCHAR* argv[],*/ INT* switches_used);
112 static INT ParseString(TEXTCHAR* str, INT*, TEXTCHAR*, TEXTCHAR*);
113 static void GetNumberOfArgs(TEXTCHAR* str, INT* nArgs);
114 
115 static
removeQuotes(char * str)116 void removeQuotes(char *str)
117 {
118   if (str[0] == '"') {
119     FDKstrcpy(str, str+1);
120     str[FDKstrlen(str)-1] = 0;
121   }
122 }
123 
124 
125 /********************** implementation *********************/
126 
IIS_ScanCmdl(INT argc,TEXTCHAR * argv[],const TEXTCHAR * str,...)127 INT IIS_ScanCmdl(INT argc, TEXTCHAR* argv[], const TEXTCHAR* str, ...)
128 {
129   INT i              = 0;
130   INT found_and_set  = 0;
131   INT nArgs          = 0;
132   INT* switches_used = 0;
133   INT*   b_str_opt   = 0;
134   TEXTCHAR*  s_str       = 0;
135   TEXTCHAR*  c_str_type  = 0;
136   TEXTCHAR*  str_clean   = 0;
137 
138   va_list ap;
139 
140   if (argc == 0 || argc == 1)
141   {
142     FDKprintf("No command line arguments\n");
143     goto bail;
144   }
145 
146   str_clean  = (TEXTCHAR*)  FDKcalloc((unsigned int)_tcslen(str), sizeof(TEXTCHAR));
147   if (str_clean == NULL) {
148     FDKprintf("Error allocating memory line %d, file %s\n",  __LINE__, __FILE__);
149     return 0;
150   }
151 
152   RemoveWhiteSpace(str, str_clean );
153   GetNumberOfArgs(str_clean, &nArgs);
154 
155   b_str_opt  = (INT*)   FDKcalloc(nArgs,    sizeof(INT));
156   s_str      = (TEXTCHAR*)  FDKcalloc(nArgs*CMDL_MAX_ARGC, sizeof(TEXTCHAR) );
157   c_str_type = (TEXTCHAR*)  FDKcalloc(nArgs,    sizeof(TEXTCHAR));
158   switches_used = (INT*) FDKcalloc(argc, sizeof(INT));
159 
160   if (b_str_opt == NULL || s_str == NULL || c_str_type == NULL || switches_used == NULL) {
161     FDKprintf("Error allocating memory line %d, file %s\n",  __LINE__, __FILE__);
162     goto bail;
163   }
164 
165   if ( ParseString( str_clean, b_str_opt, s_str, c_str_type )) {
166     goto bail;
167   }
168 
169   va_start(ap, str);
170 
171   for ( i = 0; i < nArgs; i++ )
172     {
173       TEXTCHAR arg[CMDL_MAX_STRLEN] = {L'\0'};
174       TEXTCHAR* p_arg = arg;
175       TEXTCHAR* current_str = &(s_str[i*CMDL_MAX_ARGC]);
176 
177       if (GetArgFromString(argc, argv, current_str, c_str_type[i], arg, switches_used )
178           && !b_str_opt[i] )
179         {
180 #ifdef _UNICODE
181           _ftprintf(stderr, _TEXT("\n\nError: Parsing argument for required switch '%ls'.\n" ), current_str);
182 #else
183           _ftprintf(stderr, _TEXT("\n\nError: Parsing argument for required switch '%s'.\n" ), current_str);
184 #endif
185           found_and_set = 0;
186           goto bail;
187         }
188       if (CheckArg(p_arg, s_str, nArgs, c_str_type[i], current_str))
189         {
190           goto bail;
191         }
192 
193       switch (c_str_type[i] )
194         {
195         case 's':
196           {
197             TEXTCHAR* tmp;
198             tmp = va_arg(ap, TEXTCHAR*);
199 
200             if ( arg[0] == '\0' )
201               break;
202 
203             _tcsncpy( tmp, arg, CMDL_MAX_STRLEN );
204             /* Remove quotes. Windows Mobile Workaround. */
205             removeQuotes(tmp);
206             found_and_set++;
207             break;
208           }
209         case 'd':
210           {
211             INT* tmp = va_arg(ap, INT*);
212 
213             if ( arg[0] == '\0' )
214               break;
215 
216             *tmp = _tcstol(arg, NULL, 0);
217             found_and_set++;
218             break;
219           }
220         case 'c':
221           {
222             char* tmp = va_arg(ap, char*);
223 
224             if ( arg[0] == '\0' )
225               break;
226 
227             *tmp = *arg;
228             found_and_set++;
229             break;
230           }
231         case 'u':
232           {
233             UCHAR* tmp = va_arg(ap, UCHAR*);
234 
235             if ( arg[0] == '\0' )
236               break;
237 
238             *tmp = _tstoi(arg);
239             found_and_set++;
240             break;
241           }
242         case 'f':
243           {
244             float* tmp = (float*) va_arg( ap,double*);
245 
246             if ( arg[0] == '\0' )
247               break;
248 
249             *tmp = (float) _tstof(arg);
250             found_and_set++;
251             break;
252           }
253         case 'y': // support 'data type double'
254           {
255             double* tmp = (double*) va_arg( ap,double*);
256             // use sscanf instead _tstof because of gcc
257             //_tstof(arg,"%lf",tmp); // '%lf' reads as double
258             *tmp = _tstof(arg); // '%lf' reads as double
259             found_and_set++;
260             break;
261           }
262         case '1':
263           {
264 
265             INT* tmp = va_arg( ap, INT*);
266 
267             if ( arg[0] == '\0' )
268               break;
269 
270             *tmp = 1;
271             found_and_set++;
272             break;
273           }
274 
275         default:
276           FDKprintfErr("Bug: unsupported data identifier \"%c\"\n", c_str_type[i]);
277           break;
278 
279         }
280 
281     }
282 
283   va_end(ap);
284 
285   CheckForUnusedSwitches(argc, /*argv,*/ switches_used);
286 
287 bail:
288   if (b_str_opt)     FDKfree(b_str_opt);
289   if (s_str)         FDKfree(s_str);
290   if (c_str_type)    FDKfree(c_str_type);
291   if (str_clean)     FDKfree(str_clean);
292   if (switches_used) FDKfree(switches_used);
293 
294   return found_and_set;
295 }
296 
297 
GetNumberOfArgs(TEXTCHAR * str,INT * nArgs)298 void GetNumberOfArgs(TEXTCHAR* str, INT* nArgs)
299 {
300   UINT i = 0;
301   for ( i = 0; i < _tcslen(str); ++i )
302     {
303       if ( str[i] == '%')
304         *nArgs+= 1;
305     }
306 
307 }
308 
ParseString(TEXTCHAR * str,INT * b_str_opt,TEXTCHAR * s_str,TEXTCHAR * c_str_type)309 INT ParseString(TEXTCHAR* str, INT* b_str_opt, TEXTCHAR* s_str, TEXTCHAR* c_str_type )
310 {
311     UINT i = 0;
312     INT argCounter = 0;
313 
314     TEXTCHAR* str_start = 0;
315     TEXTCHAR* str_stop = 0;
316 
317 
318     str_start = str;
319     str_stop = str_start;
320 
321     for ( i = 0; i < _tcslen(str) - 1; ++i )
322       {
323         if ( str[i] == '%' )  /* We have an Argument */
324           {
325             if ( argCounter )
326               {
327                 if ( b_str_opt[argCounter-1] )
328                   str_start = str_stop + 3;
329 
330                 else
331                   str_start = str_stop + 2;
332               }
333 
334             /* Save Argument type */
335             c_str_type[argCounter] = str[i+1];
336 
337             if ( *str_start == '(' ) /* Optional Argument */
338               {
339                 b_str_opt[argCounter] = 1;
340                 str_start++;
341               }
342 
343             /* Save Argument */
344             str[i] = '\0';
345 
346             _tcsncpy(&(s_str[argCounter*CMDL_MAX_ARGC]), str_start, CMDL_MAX_ARGC );
347 
348             str[i] = '%';
349 
350             str_stop = &(str[i]);
351 
352             if ( b_str_opt[argCounter] )
353               {
354                 if ( i+2 > ( _tcslen(str) -1 ))
355                   {
356                     _ftprintf(stderr,_TEXT("\n\nInternal Parser Error: Strlen Problem\n") );
357                     return 1;
358                   }
359                 if ( str[i+2] != ')' )
360                   {
361                     _ftprintf(stderr,_TEXT("\n\nInternal Parser Error: Missing bracket ')'\n") );
362                     return 1;
363                   }
364 
365               }
366 
367 
368             argCounter++;
369           }
370 
371 
372       }
373 
374     return 0;
375   }
376 
377 
378 
379 
RemoveWhiteSpace(const TEXTCHAR * pReqArgs,TEXTCHAR * pRemoved)380 void RemoveWhiteSpace(const TEXTCHAR* pReqArgs, TEXTCHAR* pRemoved)
381 {
382   UINT i = 0;
383   INT k = 0;
384   UINT len = (UINT)_tcslen(pReqArgs);
385 
386 
387   for ( i = 0; i < len; ++i )
388     {
389 
390       if ( pReqArgs[i] != ' ' )
391         {
392           pRemoved[k] = pReqArgs[i];
393           k++;
394         }
395     }
396 }
397 
398 
GetArgFromString(INT argc,TEXTCHAR * argv[],TEXTCHAR * search_string,TEXTCHAR type,TEXTCHAR * found_string,INT * sw_used)399 INT GetArgFromString(INT argc, TEXTCHAR* argv[], TEXTCHAR* search_string, TEXTCHAR type, TEXTCHAR* found_string, INT* sw_used )
400 {
401   INT i = 0;
402 
403   for (i = 1; i < argc; ++i ) {
404     if ( !_tcscmp(search_string, argv[i]) ) /* Strings are equal */
405       {
406         if ( type == '1' ) /* Switch without argument */
407         {
408           _tcsncpy( found_string, _TEXT("1"), 1);
409           sw_used[i] = 1;
410           return 0;
411 
412         }
413 
414         if ( i == (argc - 1))  /* We have %s or %d but are finished*/
415           return 1;
416 
417         if ( _tcslen(argv[i+1]) > CMDL_MAX_STRLEN )
418           {
419 #ifdef _UNICODE
420             _ftprintf (stderr,_TEXT("Warning: Ignoring argument for switch '%ls'. "), search_string );
421 #else
422             _ftprintf (stderr,_TEXT("Warning: Ignoring argument for switch '%s'. "), search_string );
423 #endif
424             _ftprintf (stderr,_TEXT("Argument is too LONG.\n") );
425             return 1;
426           }
427         else
428         {
429           _tcsncpy( found_string, argv[i+1], CMDL_MAX_STRLEN);
430           sw_used[i] = 1;
431           sw_used[i+1] = 1;
432           return 0;
433         }
434       }
435   }
436   return 1;
437 }
438 
439 
440 
CheckArg(TEXTCHAR * arg,TEXTCHAR * str,UINT numArgs,TEXTCHAR type,TEXTCHAR * cur_str)441 INT CheckArg(TEXTCHAR* arg, TEXTCHAR* str, UINT numArgs, TEXTCHAR type, TEXTCHAR* cur_str)
442 {
443   UINT i = 0;
444 
445   /* No argument given-> return */
446   if (arg[0] == '\0')
447     return 0;
448 
449 
450   /* Check if arg is switch */
451   for ( i = 0; i < numArgs; ++i )
452     {
453       if (!_tcscmp(arg, &(str[i*CMDL_MAX_ARGC])))
454         {
455 #ifdef _UNICODE
456           _ftprintf(stderr, _TEXT("\n\nError: Argument '%ls' for switch '%ls' is not valid \n" ), arg, cur_str );
457 #else
458           _ftprintf(stderr, _TEXT("\n\nError: Argument '%s' for switch '%s' is not valid \n" ), arg, cur_str );
459 #endif
460           return 1;
461         }
462 
463     }
464   /* Check if type is %d but a string is given */
465 
466   for ( i = 0; i < _tcslen(arg); ++i )
467     {
468       if ( (type == 'd') && !_istdigit(arg[i]) && arg[i] != 'x' && arg[i] != '-')
469         {
470 #ifdef _UNICODE
471           _ftprintf(stderr, _TEXT("\n\nError: Argument '%ls' for switch '%ls' is not a valid number.\n" ), arg, cur_str);
472 #else
473           _ftprintf(stderr, _TEXT("\n\nError: Argument '%s' for switch '%s' is not a valid number.\n" ), arg, cur_str);
474 #endif
475           return 1;
476         }
477     }
478 
479 
480   return 0;
481 }
482 
483 
CheckForUnusedSwitches(INT argc,INT * switches_used)484 INT CheckForUnusedSwitches(INT argc, /*TEXTCHAR* argv[],*/ INT* switches_used)
485 {
486   INT i = 0;
487 
488   for( i = 1; i < argc; ++i )
489     {
490       if ( !switches_used[i] )
491         {
492           ++i;
493         }
494     }
495 
496   return 0;
497 }
498 
499 
500 
501 static char line[CMDL_MAX_STRLEN*CMDL_MAX_ARGC];
502 static char *argv_ptr[CMDL_MAX_ARGC];
503 #ifdef CMDFILE_PREFIX
504 static char tmp[256]; /* this array is used to store the prefix and the filepath/name */
505 #endif
506 
IIS_ProcessCmdlList(const char * param_filename,int (* pFunction)(int,TEXTCHAR **))507 int IIS_ProcessCmdlList(const char* param_filename, int (*pFunction)(int, TEXTCHAR**))
508 {
509   /* static to reduce required stack size */
510 
511   FDKFILE *config_fp;
512   int argc;
513   char *line_ptr;
514 
515 #ifdef CMDFILE_PREFIX
516   FDKstrcpy(tmp, CMDFILE_PREFIX);
517   FDKstrcpy(tmp+FDKstrlen(CMDFILE_PREFIX), param_filename);
518   /* Open the file with command lines */
519   config_fp = FDKfopen(tmp, "r");
520 #else
521   /* Open the file with command lines */
522   config_fp = FDKfopen(param_filename, "r");
523 #endif
524 
525   if(config_fp == NULL)
526   {
527 #ifdef CMDFILE_PREFIX
528     FDKprintf("\ncould not open config file %s", tmp);
529 #else
530     FDKprintf("\ncould not open config file %s", param_filename);
531 #endif
532     return 1;
533   }
534 
535   /* Obtain a command line from config file */
536   while (FDKfgets(line, CMDL_MAX_STRLEN*CMDL_MAX_ARGC, config_fp) != NULL)
537   {
538     argc = 1;
539 
540     /* Eat \n */
541     line_ptr =  (char*)FDKstrchr(line, '\n');
542     if (line_ptr != NULL)
543       *line_ptr = ' ';
544 
545     line_ptr = line;
546 
547     /* Scan the line and put the command line params into argv */
548     do {
549       /* Skip consecutive blanks. */
550       while (*line_ptr == ' ' && line_ptr < line+CMDL_MAX_STRLEN)
551         line_ptr++;
552       /* Assign argument. */
553       argv_ptr[argc] = line_ptr;
554       /* Get pointer to next blank. */
555       line_ptr = (char*)FDKstrchr(line_ptr, ' ');
556       /*  */
557       if (line_ptr != NULL) {
558         /* Null terminate */
559         *line_ptr = 0;
560         /* Skip former blank (now null character) */
561         line_ptr++;
562         /* Advance argument counter */
563       }
564       argc++;
565     } while ( line_ptr != NULL && argc < CMDL_MAX_ARGC);
566 
567     /* call "would be main()" */
568     if (argc > 2 && *argv_ptr[1] != '#' && FDKstrlen(argv_ptr[1])>1)
569     {
570       int retval;
571 
572       retval = (*pFunction)(argc, argv_ptr);
573 
574       FDKprintf("main returned %d\n", retval);
575     }
576   }
577 
578   FDKfclose(config_fp);
579   return 0;
580 }
581 
582