1 /*
2 ucon64_misc.h - miscellaneous functions for uCON64
3 
4 Copyright (c) 1999 - 2006              NoisyB
5 Copyright (c) 2001 - 2004, 2015 - 2021 dbjh
6 Copyright (c) 2001                     Caz
7 
8 
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
13 
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 GNU General Public License for more details.
18 
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 */
23 #ifndef UCON64_MISC_H
24 #define UCON64_MISC_H
25 
26 #ifdef  HAVE_CONFIG_H
27 #include "config.h"                             // USE_DISCMAGE
28 #endif
29 #ifdef  _MSC_VER
30 #pragma warning(push)
31 #pragma warning(disable: 4820) // 'bytes' bytes padding added after construct 'member_name'
32 #endif
33 #include <stdio.h>
34 #include <time.h>
35 #ifdef  _MSC_VER
36 #pragma warning(pop)
37 #endif
38 #include "misc/getopt2.h"                       // st_getopt2_t
39 #include "misc/itypes.h"
40 
41 
42 /*
43   UCON64_DM_VERSION_MAJOR
44   UCON64_DM_VERSION_MINOR
45   UCON64_DM_VERSION_STEP  min. version of libdiscmage supported by uCON64
46 
47   ucon64_load_discmage()  load libdiscmage
48   discmage_usage          usage for libdiscmage
49   discmage_gauge          gauge wrapper for libdiscmage
50 */
51 #ifdef  USE_DISCMAGE
52 #include "libdiscmage/libdiscmage.h"            // dm_image_t
53 
54 #define UCON64_DM_VERSION_MAJOR 0
55 #define UCON64_DM_VERSION_MINOR 0
56 #define UCON64_DM_VERSION_STEP 7
57 
58 extern const st_getopt2_t discmage_usage[];
59 extern int ucon64_load_discmage (void);
60 extern void discmage_gauge (int pos, int size);
61 #endif
62 
63 
64 /*
65   usage of miscellaneous options
66 */
67 extern const st_getopt2_t ucon64_options_usage[];
68 extern const st_getopt2_t ucon64_padding_usage[];
69 
70 
71 /*
72   uCON64 messages
73 
74   usage example: fprintf (stdout, ucon64_msg[WROTE], filename);
75 */
76 enum
77 {
78   PARPORT_ERROR = 0,
79   CONSOLE_WARNING,
80   WROTE,
81   OPEN_READ_ERROR,
82   OPEN_WRITE_ERROR,
83   READ_ERROR,
84   WRITE_ERROR,
85   BUFFER_ERROR,                                 // not enough memory
86   ROM_BUFFER_ERROR,
87   FILE_BUFFER_ERROR,
88   DAT_NOT_FOUND,
89   DAT_NOT_ENABLED,
90   READ_CONFIG_FILE,
91   NO_LIB
92 };
93 
94 extern const char *ucon64_msg[];
95 
96 /*
97   ucon64_file_handler() handles backups (before modifying the ROM) and ROMs
98                         inside archives. Read the comment at the header to
99                         see how it and the flags work
100   remove_temp_file()    remove possible temp file created by ucon64_file_handler()
101   ucon64_output_fname()
102   ucon64_gauge()        wrapper for misc.c/gauge()
103   ucon64_testpad()      test if ROM is padded
104   ucon64_testsplit()    test if ROM is split
105                           optionally a callback function can be used for specific
106                           testing
107   ucon64_configfile()   configfile handling
108   ucon64_rename()       DAT or internal header based rename
109   ucon64_e()            emulator "frontend"
110   ucon64_pattern()      change file based on patterns specified in pattern_fname
111 */
112 #define OF_FORCE_BASENAME 1
113 #define OF_FORCE_SUFFIX   2
114 
115 extern int ucon64_file_handler (char *dest, char *src, unsigned int flags);
116 extern void remove_temp_file (void);
117 extern char *ucon64_output_fname (char *requested_fname, unsigned int flags);
118 extern int ucon64_gauge (time_t init_time, size_t pos, size_t size);
119 extern int64_t ucon64_testpad (const char *filename);
120 extern int ucon64_testsplit (const char *filename,
121                              void (*testsplit_cb) (const char *, void *),
122                              void *cb_data);
123 extern int ucon64_set_property_array (const char *org_configfile);
124 extern int ucon64_rename (int mode);
125 extern int ucon64_e (void);
126 extern int ucon64_pattern (const char *pattern_fname);
127 
128 
129 /*
130   Some general file stuff that MUST NOT and WILL NOT be written again and again
131 
132   ucon64_fread()    same as fread but takes start and src is a filename
133   ucon64_fwrite()   same as fwrite but takes start and dest is a filename; mode
134                       is the same as fopen() modes
135   ucon64_fgetc()    same as fgetc but takes filename instead of FILE and a pos
136   ucon64_fputc()    same as fputc but takes filename instead of FILE and a pos
137                       buf,s,bs,b,f,m == buffer,start,blksize,blks,filename,mode
138   ucon64_bswap16_n() bswap16() n bytes of buffer
139   ucon64_fbswap16() bswap16() len bytes of file from start
140   ucon64_fwswap32() wswap32() len bytes of file from start
141   ucon64_dump()     file oriented wrapper for memdump() (uses the same flags)
142   ucon64_find()     file oriented wrapper for memsearch() (uses the same flags)
143   ucon64_replace()  like ucon64_find(), but copies replacement string to every match
144   ucon64_chksum()   file oriented wrapper for chksum()
145                       if (!sha1) {sha1 won't be calculated!}
146   ucon64_filefile() compare file with ucon64.fname for similarities or differences
147   ucon64_split()    split file
148 */
149 #define ucon64_fgetc(f, p)           (quick_io_c(0, p, f, "rb"))
150 #define ucon64_fputc(f, p, b, m)     (quick_io_c(b, p, f, m))
151 #define ucon64_fread(b, s, l, f)     (quick_io(b, s, l, f, "rb"))
152 #define ucon64_fwrite(b, s, l, f, m) (quick_io((void *) b, s, l, f, m))
153 
154 extern size_t ucon64_bswap16_n (void *buffer, size_t n);
155 extern void ucon64_fbswap16 (const char *fname, uint64_t start, uint64_t len);
156 extern void ucon64_fwswap32 (const char *fname, uint64_t start, uint64_t len);
157 extern void ucon64_dump (FILE *output, const char *filename, uint64_t start,
158                          uint64_t len, unsigned int flags);
159 // be sure the following constants don't conflict with the MEMCMP2_* constants
160 #define UCON64_FIND_QUIET   (1U << 30)
161 #define UCON64_FIND_REPLACE (1U << 31) // should only be used by ucon64_replace()
162 extern int64_t ucon64_find (const char *filename, uint64_t start, uint64_t len,
163                             const char *search, size_t searchlen,
164                             unsigned int flags);
165 extern int64_t ucon64_replace (const char *filename, uint64_t start, uint64_t len,
166                                const char *search, size_t searchlen,
167                                const char *replace, size_t replacelen,
168                                unsigned int flags);
169 extern void ucon64_chksum (char *sha1, char *md5, unsigned int *crc32, // uint16_t *crc16,
170                            const char *filename, uint64_t file_size,
171                            uint64_t start);
172 extern void ucon64_filefile (const char *filename1, uint64_t start1,
173                              uint64_t start2, int similar);
174 extern int ucon64_split (uint64_t part_size);
175 #endif // #ifndef UCON64_MISC_H
176