1 /*
2  * The Initial Developer of the Original Code is International
3  * Business Machines Corporation. Portions created by IBM
4  * Corporation are Copyright (C) 2005, 2006 International Business
5  * Machines Corporation. All Rights Reserved.
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the Common Public License as published by
9  * IBM Corporation; either version 1 of the License, or (at your option)
10  * any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * Common Public License for more details.
16  *
17  * You should have received a copy of the Common Public License
18  * along with this program; if not, a copy can be viewed at
19  * http://www.opensource.org/licenses/cpl1.0.php.
20  */
21 
22 #ifndef __TPM_UTILS_H
23 #define __TPM_UTILS_H
24 
25 #ifdef ENABLE_NLS
26 #include <libintl.h>
27 #include <locale.h>
28 #define _(String) gettext(String)
29 #define N_(String) String
30 #else
31 #define setlocale(category, local)
32 #define bindtextdomain(package, localedir)
33 #define textdomain(package);
34 #define _(String) String
35 #define N_(String) String
36 #endif
37 
38 #include <string.h>
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <stdarg.h>
42 //#define GNU_SOURCE
43 #include <getopt.h>
44 
45 #ifndef BOOL
46 #define BOOL int
47 #endif
48 
49 #ifndef FALSE
50 #define FALSE 0
51 #endif
52 
53 #ifndef TRUE
54 #define TRUE (!FALSE)
55 #endif
56 
57 #define CMD_VERSION             PACKAGE_VERSION
58 
59 #define LOG_NONE                _("none")
60 #define LOG_LEVEL_NONE          0
61 #define LOG_ERROR               _("error")
62 #define LOG_LEVEL_ERROR         1
63 #define LOG_INFO                _("info")
64 #define LOG_LEVEL_INFO          2
65 #define LOG_DEBUG               _("debug")
66 #define LOG_LEVEL_DEBUG         3
67 
68 #ifdef __GNUC__
69 #define __no_optimize __attribute__((optimize("O0")))
70 #else
71 #define __no_optimize
72 #endif
73 
74 void * __no_optimize __memset(void *s, int c, size_t n);
75 
76 typedef int (*CmdOptParser)( const int aOpt, const char *aOptArg );
77 typedef void (*CmdHelpFunction)( const char *aCmd );
78 
79 void initIntlSys( );
80 
81 int genericOptHandler( int a_iNumArgs, char **a_pszArgs,
82 		       const char *a_pszShortOpts,
83 		       struct option *a_psLongOpts, int a_iNumOpts,
84 		       CmdOptParser, CmdHelpFunction );
85 char *getPlainPasswd( const char* a_pszPrompt, BOOL a_bConfirm );
86 #ifdef TSS_LIB_IS_12
87 char *_getPasswd12( const char *a_pszPrompt, int *a_iLen, BOOL a_bConfirm, BOOL a_uUseUnicode);
88 char *getPasswd12( const char *a_pszPrompt, int *a_iLen, BOOL a_bConfirm );
89 
90 #define _GETPASSWD	_getPasswd12
91 #define GETPASSWD	getPasswd12
92 #else
93 char *getPasswd( const char *a_pszPrompt, int *a_iLen, BOOL a_bConfirm );
94 
95 #define _GETPASSWD	_getPasswd
96 #define GETPASSWD	getPasswd
97 #endif
98 char *_getPasswd( const char *a_pszPrompt, int *a_iLen, BOOL a_bConfirm, BOOL a_bUseUnicode);
99 void  shredPasswd( char *a_pszPasswd );
100 char *getReply( const char *a_pszPrompt, int a_iMaxLen );
101 
102 extern int iLogLevel;
103 extern BOOL useUnicode;
104 
105 int logHex( int a_iLen, void *a_pData );
106 int logMsg( const char *a_pszFormat, ... );
107 int logDebug( const char *a_pszFormat, ... );
108 int logInfo( const char *a_pszFormat, ... );
109 int logError( const char *a_pszFormat, ... );
110 
111 int logProcess( FILE *a_pStream, const char *a_pszFormat, va_list a_vaArgs );
112 int logIt( FILE *a_pStream, const char *a_pszFormat, va_list a_vaArgs );
113 
114 void  logSuccess( const char *a_pszCmd );
115 void  logCmdOption( const char *a_pszOption, const char *a_pszDescr );
116 void  logUnicodeCmdOption( );
117 void  logGenericOptions( );
118 void  logCmdHelp( const char *a_pszCmd );
119 void  logCmdHelpEx( const char *a_pszCmd, char *a_pszArgs[], char *a_pszArgDescs[] );
120 char *logBool( BOOL aValue );
121 void  logOwnerPassCmdOption( );
122 void  logNVIndexCmdOption( );
123 
124 #endif
125