1 
2 /***************************************************************************
3  *                    __            __ _ ___________                       *
4  *                    \ \          / /| |____   ____|                      *
5  *                     \ \        / / | |    | |                           *
6  *                      \ \  /\  / /  | |    | |                           *
7  *                       \ \/  \/ /   | |    | |                           *
8  *                        \  /\  /    | |    | |                           *
9  *                         \/  \/     |_|    |_|                           *
10  *                                                                         *
11  *                           Wiimms ISO Tools                              *
12  *                         http://wit.wiimm.de/                            *
13  *                                                                         *
14  ***************************************************************************
15  *                                                                         *
16  *   This file is part of the WIT project.                                 *
17  *   Visit http://wit.wiimm.de/ for project details and sources.           *
18  *                                                                         *
19  *   Copyright (c) 2009-2013 by Dirk Clemens <wiimm@wiimm.de>              *
20  *                                                                         *
21  ***************************************************************************
22  *                                                                         *
23  *   This program is free software; you can redistribute it and/or modify  *
24  *   it under the terms of the GNU General Public License as published by  *
25  *   the Free Software Foundation; either version 2 of the License, or     *
26  *   (at your option) any later version.                                   *
27  *                                                                         *
28  *   This program is distributed in the hope that it will be useful,       *
29  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
30  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
31  *   GNU General Public License for more details.                          *
32  *                                                                         *
33  *   See file gpl-2.0.txt or http://www.gnu.org/licenses/gpl-2.0.txt       *
34  *                                                                         *
35  ***************************************************************************/
36 
37 #ifndef TOOLS_H
38 #define TOOLS_H
39 
40 //
41 ///////////////////////////////////////////////////////////////////////////////
42 ///////////////			    setup			///////////////
43 ///////////////////////////////////////////////////////////////////////////////
44 
45 #include "libwbfs_os.h"		// os dependent definitions
46 #include "libwbfs_defaults.h"	// default settings
47 
48 //
49 ///////////////////////////////////////////////////////////////////////////////
50 ///////////////			  constants			///////////////
51 ///////////////////////////////////////////////////////////////////////////////
52 
53 #define KB_SI 1000
54 #define MB_SI (1000*1000)
55 #define GB_SI (1000*1000*1000)
56 #define TB_SI (1000ull*1000*1000*1000)
57 #define PB_SI (1000ull*1000*1000*1000*1000)
58 #define EB_SI (1000ull*1000*1000*1000*1000*1000)
59 
60 #define KiB 1024
61 #define MiB (1024*1024)
62 #define GiB (1024*1024*1024)
63 #define TiB (1024ull*1024*1024*1024)
64 #define PiB (1024ull*1024*1024*1024*1024)
65 #define EiB (1024ull*1024*1024*1024*1024*1024)
66 
67 //
68 ///////////////////////////////////////////////////////////////////////////////
69 ///////////////		low level endian conversions		///////////////
70 ///////////////////////////////////////////////////////////////////////////////
71 
72 // convert big endian data to a number in host format
73 u16 be16 ( const void * be_data_ptr );
74 u32 be24 ( const void * be_data_ptr );
75 u32 be32 ( const void * be_data_ptr );
76 u64 be64 ( const void * be_data_ptr );
77 
78 // convert little endian data to a number in host format
79 u16 le16 ( const void * le_data_ptr );
80 u32 le24 ( const void * le_data_ptr );
81 u32 le32 ( const void * le_data_ptr );
82 u64 le64 ( const void * le_data_ptr );
83 
84 // convert u64 from/to network byte order
85 be64_t hton64 ( u64    data );
86 u64    ntoh64 ( be64_t data );
87 
88 //
89 ///////////////////////////////////////////////////////////////////////////////
90 ///////////////			error messages			///////////////
91 ///////////////////////////////////////////////////////////////////////////////
92 
93 enumError wd_print_error
94 (
95     ccp		func,		// calling function, use macro __FUNCTION__
96     ccp		file,		// source file, use macro __FILE__
97     uint	line,		// line number of source file, use macro __LINE__
98     enumError	err,		// error code
99     ccp		format,		// NULL or format string for fprintf() function.
100     ...				// parameters for 'format'
101 
102 ) __attribute__ ((__format__(__printf__,5,6)));
103 
104 //
105 ///////////////////////////////////////////////////////////////////////////////
106 ///////////////			aligning			///////////////
107 ///////////////////////////////////////////////////////////////////////////////
108 
109 // align macros for 2^n values
110 
111 #define ALIGN32(d,a) ((d)+((a)-1)&~(u32)((a)-1))
112 #define ALIGN64(d,a) ((d)+((a)-1)&~(u64)((a)-1))
113 
114 //-----------------------------------------------------------------------------
115 
116 u32 wd_align32
117 (
118     u32		number,		// object of aligning
119     u32		align,		// NULL or valid align factor
120     int		align_mode	// <0: round down, =0: round math, >0 round up
121 );
122 
123 //-----------------------------------------------------------------------------
124 
125 u64 wd_align64
126 (
127     u64		number,		// object of aligning
128     u64		align,		// NULL or valid align factor
129     int		align_mode	// <0: round down, =0: round math, >0 round up
130 );
131 
132 //-----------------------------------------------------------------------------
133 
134 u64 wd_align_part
135 (
136     u64		number,		// object of aligning
137     u64		align,		// NULL or valid align factor
138     bool	is_gamecube	// hint for automatic calculation (align==0)
139 );
140 
141 //
142 ///////////////////////////////////////////////////////////////////////////////
143 ///////////////			print size			///////////////
144 ///////////////////////////////////////////////////////////////////////////////
145 
146 typedef enum wd_size_mode_t
147 {
148     //----- modes => used as index for wd_size_tab_1000[] & wd_size_tab_1024[]
149 
150     WD_SIZE_DEFAULT	= 0,	// special default value, fall back to AUTO
151     WD_SIZE_AUTO,		// select unit automatically
152 
153     WD_SIZE_BYTES,		// force output in bytes
154     WD_SIZE_K,			// force output in KB or KiB (kilo,kibi)
155     WD_SIZE_M,			// force output in MB or MiB (mega,mebi)
156     WD_SIZE_G,			// force output in GB or GiB (giga,gibi)
157     WD_SIZE_T,			// force output in TB or TiB (tera,tebi)
158     WD_SIZE_P,			// force output in PB or PiB (peta,pebi)
159     WD_SIZE_E,			// force output in EB or EiB (exa, exbi)
160 				// zetta/zebi & yotta/yobi not supported because >2^64
161 
162     WD_SIZE_HD_SECT,		// force output as multiples of HD sector size (=512)
163     WD_SIZE_WD_SECT,		// force output as multiples of WD sector size (=32768)
164     WD_SIZE_GC,			// force output as float as multiples of GC discs
165     WD_SIZE_WII,		// force output as float as multiples of Wii discs
166 
167     WD_SIZE_N_MODES,		// number of modes
168 
169     //----- flags
170 
171     WD_SIZE_F_1000	= 0x010,  // force output in SI units (kB=1000, MB=1000000,...)
172     WD_SIZE_F_1024	= 0x020,  // force output in IEC units (KiB=1024, MiB=1024*1024,...)
173     WD_SIZE_F_AUTO_UNIT	= 0x040,  // suppress output of unit for non AUTO mode
174     WD_SIZE_F_NO_UNIT	= 0x080,  // suppress allways output of unit
175 
176     //----- masks
177 
178     WD_SIZE_M_MODE	= 0x00f,  // mask for modes
179     WD_SIZE_M_BASE	= 0x030,  // mask for base
180     WD_SIZE_M_ALL	= 0x0ff,  // all relevant bits
181 
182 } wd_size_mode_t;
183 
184 //-----------------------------------------------------------------------------
185 
186 extern ccp wd_size_tab_1000[WD_SIZE_N_MODES+1];
187 extern ccp wd_size_tab_1024[WD_SIZE_N_MODES+1];
188 
189 //-----------------------------------------------------------------------------
190 
191 ccp wd_get_size_unit // get a unit for column headers
192 (
193     wd_size_mode_t	mode,		// print mode
194     ccp			if_invalid	// output for invalid modes
195 );
196 
197 //-----------------------------------------------------------------------------
198 
199 int wd_get_size_fw // get a good value field width
200 (
201     wd_size_mode_t	mode,		// print mode
202     int			min_fw		// minimal fw => return max(calc_fw,min_fw);
203 					// this value is also returned for invalid modes
204 );
205 
206 //-----------------------------------------------------------------------------
207 
208 char * wd_print_size
209 (
210     char		* buf,		// result buffer
211 					// NULL: use a local circulary static buffer
212     size_t		buf_size,	// size of 'buf', ignored if buf==NULL
213     u64			size,		// size to print
214     bool		aligned,	// true: print exact 8 chars for num+unit
215     wd_size_mode_t	mode		// print mode
216 );
217 
218 //-----------------------------------------------------------------------------
219 
220 char * wd_print_size_1000
221 (
222     char		* buf,		// result buffer
223 					// NULL: use a local circulary static buffer
224     size_t		buf_size,	// size of 'buf', ignored if buf==NULL
225     u64			size,		// size to print
226     bool		aligned		// true: print exact 4+4 chars for num+unit
227 );
228 
229 //-----------------------------------------------------------------------------
230 
231 char * wd_print_size_1024
232 (
233     char		* buf,		// result buffer
234 					// NULL: use a local circulary static buffer
235     size_t		buf_size,	// size of 'buf', ignored if buf==NULL
236     u64			size,		// size to print
237     bool		aligned		// true: print exact 4+4 chars for num+unit
238 );
239 
240 //
241 ///////////////////////////////////////////////////////////////////////////////
242 ///////////////			printing helpers		///////////////
243 ///////////////////////////////////////////////////////////////////////////////
244 
245 int wd_normalize_indent
246 (
247     int			indent		// base value to normalize
248 );
249 
250 //-----------------------------------------------------------------------------
251 
252 void wd_print_byte_tab
253 (
254     FILE		* f,		// valid output file
255     int			indent,		// indention of the output
256     const u8		* tab,		// valid pointer to byte table
257     u32			used,		// print minimal 'used' values of 'tab'
258     u32			size,		// size of 'tab'
259     u32			addr_factor,	// each 'tab' element represents 'addr_factor' bytes
260     const char		chartab[256],	// valid pointer to a char table
261     bool		print_all	// false: ignore const lines
262 );
263 
264 //-----------------------------------------------------------------------------
265 
266 char * GetCircBuf // never returns NULL
267 (
268     u32		buf_size	// wanted buffer size
269 );
270 
271 //
272 ///////////////////////////////////////////////////////////////////////////////
273 ///////////////			string helpers			///////////////
274 ///////////////////////////////////////////////////////////////////////////////
275 
276 extern const char EmptyString[]; // ""
277 extern const char MinusString[]; // "-"
278 
279 //-----
280 
281 // frees string if str is not EmptyString|MinusString
282 void FreeString ( ccp str );
283 
284 //-----
285 
286 // StringCopy(), StringCopyE(), StringCat*()
287 //	RESULT: end of copied string pointing to NULL.
288 //	'src*' may be a NULL pointer.
289 
290 char * StringCopyS ( char * buf, size_t bufsize, ccp src );
291 char * StringCat2S ( char * buf, size_t bufsize, ccp src1, ccp src2 );
292 char * StringCat3S ( char * buf, size_t bufsize, ccp src1, ccp src2, ccp src3 );
293 
294 char * StringCopyE ( char * buf, ccp buf_end, ccp src );
295 char * StringCat2E ( char * buf, ccp buf_end, ccp src1, ccp src2 );
296 char * StringCat3E ( char * buf, ccp buf_end, ccp src1, ccp src2, ccp src3 );
297 
298 //-----
299 
300 //-----------------------------------------------------
301 // Format of version number: AABBCCDD = A.BB | A.BB.CC
302 // If D != 0x00 && D != 0xff => append: 'beta' D
303 //-----------------------------------------------------
304 
305 char * PrintVersion
306 (
307     char		* buf,		// result buffer
308 					// If NULL, a local circulary static buffer is used
309     size_t		buf_size,	// size of 'buf', ignored if buf==NULL
310     u32			version		// version number to print
311 );
312 
313 //
314 ///////////////////////////////////////////////////////////////////////////////
315 ///////////////			fake functions			///////////////
316 ///////////////////////////////////////////////////////////////////////////////
317 
318 unsigned char * wbfs_sha1_fake
319 	( const unsigned char *d, size_t n, unsigned char *md );
320 
321 //
322 ///////////////////////////////////////////////////////////////////////////////
323 ///////////////			    E N D			///////////////
324 ///////////////////////////////////////////////////////////////////////////////
325 
326 #endif // TOOLS_H
327