fixes entering bootload messages to be less scary
[RRG-proxmark3.git] / client / src / fileutils.h
blobc6918542523c900a0b93360fb15ba2b2c0fe8361
1 //-----------------------------------------------------------------------------
2 // Copyright (C) Proxmark3 contributors. See AUTHORS.md for details.
3 //
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // See LICENSE.txt for the text of the license.
15 //-----------------------------------------------------------------------------
17 #ifndef FILEUTILS_H
18 #define FILEUTILS_H
20 #include <inttypes.h>
21 #include <stdio.h>
22 #include <string.h>
23 #include <stdlib.h>
24 #include <sys/stat.h>
25 #include <stdarg.h>
26 #include "ui.h"
27 #include "emv/emvjson.h"
28 #include "mifare/mifare4.h"
29 #include "mifare/mifarehost.h"
30 #include "cmdhfmfu.h"
32 #include "protocols.h" // iclass defines
33 #include "cmdhftopaz.h" // TOPAZ defines
34 #include "mifare/mifaredefault.h" // MFP / AES defines
36 typedef union {
37 void *v;
38 uint8_t *bytes;
39 mfu_dump_t *mfu;
40 topaz_tag_t *topaz;
41 iso14a_mf_extdump_t *mfc;
42 iso14a_mf_dump_ev1_t *mfc_ev1;
43 } udata_t;
45 typedef enum {
46 jsfRaw,
47 jsfCardMemory,
48 jsfMfc_v2,
49 jsfMfc_v3,
50 jsfMfuMemory,
51 jsfHitag,
52 jsfIclass,
53 jsf14b,
54 jsf14b_v2,
55 jsf15,
56 jsf15_v2,
57 jsf15_v3,
58 jsf15_v4,
59 jsfLegic,
60 jsfLegic_v2,
61 jsfT55x7,
62 jsfT5555,
63 jsfMfPlusKeys,
64 jsfCustom,
65 jsfMfDesfireKeys,
66 jsfEM4x05,
67 jsfEM4x69,
68 jsfEM4x50,
69 jsfFido,
70 jsfFudan,
71 jsfTopaz,
72 jsfLto,
73 jsfCryptorf,
74 jsfNDEF,
75 } JSONFileType;
77 typedef enum {
78 BIN = 0,
79 EML,
80 JSON,
81 DICTIONARY,
82 MCT,
83 FLIPPER,
84 } DumpFileType_t;
86 typedef enum {
87 MFU_DF_UNKNOWN,
88 MFU_DF_PLAINBIN,
89 MFU_DF_OLDBIN,
90 MFU_DF_NEWBIN
91 } mfu_df_e;
93 typedef enum {
94 NFC_DF_UNKNOWN,
95 NFC_DF_MFC,
96 NFC_DF_MFU,
97 NFC_DF_MFDES,
98 NFC_DF_14_3A,
99 NFC_DF_14_3B,
100 NFC_DF_14_4A,
101 NFC_DF_PICOPASS,
102 } nfc_df_e;
104 int fileExists(const char *filename);
106 // set a path in the path list g_session.defaultPaths
107 bool setDefaultPath(savePaths_t pathIndex, const char *path);
109 char *newfilenamemcopy(const char *preferredName, const char *suffix);
110 char *newfilenamemcopyEx(const char *preferredName, const char *suffix, savePaths_t e_save_path);
111 void truncate_filename(char *fn, uint16_t maxlen);
115 * @brief Utility function to save data to a binary file. This method takes a preferred name, but if that
116 * file already exists, it tries with another name until it finds something suitable.
117 * E.g. dumpdata-15.txt
119 * @param preferredName
120 * @param suffix the file suffix. Including the ".".
121 * @param data The binary data to write to the file
122 * @param datalen the length of the data
123 * @return 0 for ok, 1 for failz
125 int saveFile(const char *preferredName, const char *suffix, const void *data, size_t datalen);
127 /** STUB
128 * @brief Utility function to save JSON data to a file. This method takes a preferred name, but if that
129 * file already exists, it tries with another name until it finds something suitable.
130 * E.g. dumpdata-15.json
132 * @param preferredName
133 * @param ftype type of file.
134 * @param data The binary data to write to the file
135 * @param datalen the length of the data
136 * @return 0 for ok, 1 for failz
138 int saveFileJSON(const char *preferredName, JSONFileType ftype, uint8_t *data, size_t datalen, void (*callback)(json_t *));
139 int saveFileJSONex(const char *preferredName, JSONFileType ftype, uint8_t *data, size_t datalen, bool verbose, void (*callback)(json_t *), savePaths_t e_save_path);
140 int saveFileJSONroot(const char *preferredName, void *root, size_t flags, bool verbose);
141 int saveFileJSONrootEx(const char *preferredName, const void *root, size_t flags, bool verbose, bool overwrite);
142 /** STUB
143 * @brief Utility function to save WAVE data to a file. This method takes a preferred name, but if that
144 * file already exists, it tries with another name until it finds something suitable.
145 * E.g. dumpdata-15.wav
147 * @param preferredName
148 * @param data The binary data to write to the file
149 * @param datalen the length of the data
150 * @return 0 for ok
152 int saveFileWAVE(const char *preferredName, const int *data, size_t datalen);
154 /** STUB
155 * @brief Utility function to save PM3 data to a file. This method takes a preferred name, but if that
156 * file already exists, it tries with another name until it finds something suitable.
157 * E.g. dump_trace.pm3
159 * @param preferredName
160 * @param data The binary data to write to the file
161 * @param datalen the length of the data
162 * @return 0 for ok
164 int saveFilePM3(const char *preferredName, int *data, size_t datalen);
167 * @brief Utility function to save a keydump into a binary file.
169 * @param preferredName
170 * @param sectorsCnt the used sectors
171 * @param e_sector the keys in question
172 * @return 0 for ok, 1 for failz
174 int createMfcKeyDump(const char *preferredName, uint8_t sectorsCnt, const sector_t *e_sector);
177 * @brief Utility function to load data from a binary file. This method takes a preferred name.
178 * E.g. dumpdata-15.bin, tries to search for it, and allocated memory.
180 * @param preferredName
181 * @param suffix the file suffix. Including the ".".
182 * @param data The data array to store the loaded bytes from file
183 * @param datalen the number of bytes loaded from file
184 * @return PM3_SUCCESS for ok, PM3_E* for failz
186 int loadFile_safe(const char *preferredName, const char *suffix, void **pdata, size_t *datalen);
187 int loadFile_safeEx(const char *preferredName, const char *suffix, void **pdata, size_t *datalen, bool verbose);
189 * @brief Utility function to load data from a textfile (EML). This method takes a preferred name.
190 * E.g. dumpdata-15.txt
192 * @param preferredName
193 * @param data The data array to store the loaded bytes from file
194 * @param datalen the number of bytes loaded from file
195 * @return 0 for ok, 1 for failz
197 int loadFileEML_safe(const char *preferredName, void **pdata, size_t *datalen);
200 * @brief Utility function to load data from a textfile (MCT). This method takes a preferred name.
201 * E.g. dumpdata-15.mct
203 * @param preferredName
204 * @param data The data array to store the loaded bytes from file
205 * @param datalen the number of bytes loaded from file
206 * @return 0 for ok, 1 for failz
208 int loadFileMCT_safe(const char *preferredName, void **pdata, size_t *datalen);
211 * @brief Utility function to load data from a textfile (NFC). This method takes a preferred name.
212 * E.g. dumpdata-15.nfc
214 * @param preferredName
215 * @param data The data array to store the loaded bytes from file
216 * @param maxdatalen maximum size of data array in bytes
217 * @param datalen the number of bytes loaded from file
218 * @param ft
219 * @return 0 for ok, 1 for failz
221 int loadFileNFC_safe(const char *preferredName, void *data, size_t maxdatalen, size_t *datalen, nfc_df_e ft);
224 * @brief Utility function to load data from a JSON textfile. This method takes a preferred name.
225 * E.g. dumpdata-15.json
227 * @param preferredName
228 * @param data The data array to store the loaded bytes from file
229 * @param maxdatalen maximum size of data array in bytes
230 * @param datalen the number of bytes loaded from file
231 * @return 0 for ok, 1 for failz
233 int loadFileJSON(const char *preferredName, void *data, size_t maxdatalen, size_t *datalen, void (*callback)(json_t *));
234 int loadFileJSONex(const char *preferredName, void *data, size_t maxdatalen, size_t *datalen, bool verbose, void (*callback)(json_t *));
235 int loadFileJSONroot(const char *preferredName, void **proot, bool verbose);
238 * @brief Utility function to load data from a DICTIONARY textfile. This method takes a preferred name.
239 * E.g. mfc_default_keys.dic
241 * @param preferredName
242 * @param data The data array to store the loaded bytes from file
243 * @param datalen the number of bytes loaded from file. may be NULL
244 * @param keylen the number of bytes a key per row is
245 * @param keycnt key count that lays in data. may be NULL
246 * @return 0 for ok, 1 for failz
248 int loadFileDICTIONARY(const char *preferredName, void *data, size_t *datalen, uint8_t keylen, uint32_t *keycnt);
251 * @brief Utility function to load data from a DICTIONARY textfile. This method takes a preferred name.
252 * E.g. mfc_default_keys.dic
253 * can be executed several times for big dictionaries and checks length of buffer
255 * @param preferredName
256 * @param data The data array to store the loaded bytes from file
257 * @param maxdatalen maximum size of data array in bytes
258 * @param datalen the number of bytes loaded from file. may be NULL
259 * @param keylen the number of bytes a key per row is
260 * @param keycnt key count that lays in data. may be NULL
261 * @param startFilePosition start position in dictionary file. used for big dictionaries.
262 * @param endFilePosition in case we have keys in file and maxdatalen reached it returns current key position in file. may be NULL
263 * @param verbose print messages if true
264 * @return 0 for ok, 1 for failz
266 int loadFileDICTIONARYEx(const char *preferredName, void *data, size_t maxdatalen, size_t *datalen, uint8_t keylen, uint32_t *keycnt,
267 size_t startFilePosition, size_t *endFilePosition, bool verbose);
270 * @brief Utility function to load data safely from a DICTIONARY textfile. This method takes a preferred name.
271 * E.g. mfc_default_keys.dic
273 * @param preferredName
274 * @param pdata A pointer to a pointer (for reverencing the loaded dictionary)
275 * @param keylen the number of bytes a key per row is
276 * @return 0 for ok, 1 for failz
278 int loadFileDICTIONARY_safe(const char *preferredName, void **pdata, uint8_t keylen, uint32_t *keycnt);
280 int loadFileBinaryKey(const char *preferredName, const char *suffix, void **keya, void **keyb, size_t *alen, size_t *blen);
283 * @brief Utility function to check and convert plain mfu dump format to new mfu binary format.
284 * plain dumps doesn't have any extra data, like version, signature etc.
285 * @param dump pointer to loaded dump to check and convert format
286 * @param dumplen the number of bytes loaded dump and converted
287 * @param verbose - extra debug output
288 * @return PM3_SUCCESS for ok, PM3_ESOFT for fails
290 int convert_mfu_dump_format(uint8_t **dump, size_t *dumplen, bool verbose);
291 mfu_df_e detect_mfu_dump_format(uint8_t **dump, bool verbose);
292 int detect_nfc_dump_format(const char *preferredName, nfc_df_e *dump_type, bool verbose);
294 int searchAndList(const char *pm3dir, const char *ext);
295 int searchFile(char **foundpath, const char *pm3dir, const char *searchname, const char *suffix, bool silent);
299 * @brief detects if file is of a supported filetype based on extension
300 * @param filename
301 * @return
303 DumpFileType_t get_filetype(const char *filename);
307 * @brief load dump file into a data array dynamically allocated
308 * @param fn
309 * @param pdump pointer to loaded dump
310 * @param dumplen the number of bytes loaded from dump file
311 * @param maxdumplen maximum size of data array in bytes (JSON files)
312 * @return PM3_SUCCESS if OK
314 int pm3_load_dump(const char *fn, void **pdump, size_t *dumplen, size_t maxdumplen);
317 /** STUB
318 * @brief Utility function to save data to three file files (BIN/JSON).
319 * It also tries to save according to user preferences set dump folder paths.
320 * E.g. dumpdata.bin
321 * E.g. dumpdata.json
323 * @param fn
324 * @param d The binary data to write to the file
325 * @param n the length of the data
326 * @param jsft json format type for the different memory cards (MFC, MFUL, LEGIC, 14B, 15, ICLASS etc)
327 * @return PM3_SUCCESS if OK
329 int pm3_save_dump(const char *fn, uint8_t *d, size_t n, JSONFileType jsft);
331 /** STUB
332 * @brief Utility function to save data to three file files (BIN/JSON).
333 * It also tries to save according to user preferences set dump folder paths.
334 * E.g. dumpdata.bin
335 * E.g. dumpdata.json
337 * This function is dedicated for MIFARE CLASSIC dumps. Checking for 4 or 7 byte UID in indata.
338 * Saves the corrected data in the json file
340 * @param fn
341 * @param d The binary data to write to the file
342 * @param n the length of the data
343 * @param jsft json format type for the different memory cards (MFC, MFUL, LEGIC, 14B, 15, ICLASS etc)
344 * @return PM3_SUCCESS if OK
346 int pm3_save_mf_dump(const char *fn, uint8_t *d, size_t n, JSONFileType jsft);
347 #endif // FILEUTILS_H