Merge pull request #2654 from Antiklesys/master
[RRG-proxmark3.git] / client / src / fileutils.h
blobae04b8265d7c4985f863ec5fc447264960106f31
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 jsfFM11RF08SNonces,
76 jsfFM11RF08SNoncesWithData
77 } JSONFileType;
79 typedef enum {
80 BIN = 0,
81 EML,
82 JSON,
83 DICTIONARY,
84 MCT,
85 FLIPPER,
86 } DumpFileType_t;
88 typedef enum {
89 MFU_DF_UNKNOWN,
90 MFU_DF_PLAINBIN,
91 MFU_DF_OLDBIN,
92 MFU_DF_NEWBIN
93 } mfu_df_e;
95 typedef enum {
96 NFC_DF_UNKNOWN,
97 NFC_DF_MFC,
98 NFC_DF_MFU,
99 NFC_DF_MFDES,
100 NFC_DF_14_3A,
101 NFC_DF_14_3B,
102 NFC_DF_14_4A,
103 NFC_DF_PICOPASS,
104 } nfc_df_e;
106 int fileExists(const char *filename);
108 // set a path in the path list g_session.defaultPaths
109 bool setDefaultPath(savePaths_t pathIndex, const char *path);
111 char *newfilenamemcopy(const char *preferredName, const char *suffix);
112 char *newfilenamemcopyEx(const char *preferredName, const char *suffix, savePaths_t e_save_path);
113 void truncate_filename(char *fn, uint16_t maxlen);
117 * @brief Utility function to save data to a binary file. This method takes a preferred name, but if that
118 * file already exists, it tries with another name until it finds something suitable.
119 * E.g. dumpdata-15.txt
121 * @param preferredName
122 * @param suffix the file suffix. Including the ".".
123 * @param data The binary data to write to the file
124 * @param datalen the length of the data
125 * @return 0 for ok, 1 for failz
127 int saveFile(const char *preferredName, const char *suffix, const void *data, size_t datalen);
128 int saveFileEx(const char *preferredName, const char *suffix, const void *data, size_t datalen, savePaths_t e_save_path);
130 /** STUB
131 * @brief Utility function to save JSON data to a file. This method takes a preferred name, but if that
132 * file already exists, it tries with another name until it finds something suitable.
133 * E.g. dumpdata-15.json
135 * @param preferredName
136 * @param ftype type of file.
137 * @param data The binary data to write to the file
138 * @param datalen the length of the data
139 * @return 0 for ok, 1 for failz
141 int saveFileJSON(const char *preferredName, JSONFileType ftype, uint8_t *data, size_t datalen, void (*callback)(json_t *));
142 int saveFileJSONex(const char *preferredName, JSONFileType ftype, uint8_t *data, size_t datalen, bool verbose, void (*callback)(json_t *), savePaths_t e_save_path);
143 int saveFileJSONroot(const char *preferredName, void *root, size_t flags, bool verbose);
144 int saveFileJSONrootEx(const char *preferredName, const void *root, size_t flags, bool verbose, bool overwrite, savePaths_t e_save_path);
145 int prepareJSON(json_t *root, JSONFileType ftype, uint8_t *data, size_t datalen, bool verbose, void (*callback)(json_t *));
146 char *sprintJSON(JSONFileType ftype, uint8_t *data, size_t datalen, bool verbose, void (*callback)(json_t *));
147 /** STUB
148 * @brief Utility function to save WAVE data to a file. This method takes a preferred name, but if that
149 * file already exists, it tries with another name until it finds something suitable.
150 * E.g. dumpdata-15.wav
152 * @param preferredName
153 * @param data The binary data to write to the file
154 * @param datalen the length of the data
155 * @return 0 for ok
157 int saveFileWAVE(const char *preferredName, const int *data, size_t datalen);
159 /** STUB
160 * @brief Utility function to save PM3 data to a file. This method takes a preferred name, but if that
161 * file already exists, it tries with another name until it finds something suitable.
162 * E.g. dump_trace.pm3
164 * @param preferredName
165 * @param data The binary data to write to the file
166 * @param datalen the length of the data
167 * @return 0 for ok
169 int saveFilePM3(const char *preferredName, int *data, size_t datalen);
172 * @brief Utility function to save a keydump into a binary file.
174 * @param preferredName
175 * @param sectorsCnt the used sectors
176 * @param e_sector the keys in question
177 * @return 0 for ok, 1 for failz
179 int createMfcKeyDump(const char *preferredName, uint8_t sectorsCnt, const sector_t *e_sector);
182 * @brief Utility function to load data from a binary file. This method takes a preferred name.
183 * E.g. dumpdata-15.bin, tries to search for it, and allocated memory.
185 * @param preferredName
186 * @param suffix the file suffix. Including the ".".
187 * @param data The data array to store the loaded bytes from file
188 * @param datalen the number of bytes loaded from file
189 * @return PM3_SUCCESS for ok, PM3_E* for failz
191 int loadFile_safe(const char *preferredName, const char *suffix, void **pdata, size_t *datalen);
192 int loadFile_safeEx(const char *preferredName, const char *suffix, void **pdata, size_t *datalen, bool verbose);
194 * @brief Utility function to load data from a textfile (EML). This method takes a preferred name.
195 * E.g. dumpdata-15.txt
197 * @param preferredName
198 * @param data The data array to store the loaded bytes from file
199 * @param datalen the number of bytes loaded from file
200 * @return 0 for ok, 1 for failz
202 int loadFileEML_safe(const char *preferredName, void **pdata, size_t *datalen);
205 * @brief Utility function to load data from a textfile (MCT). This method takes a preferred name.
206 * E.g. dumpdata-15.mct
208 * @param preferredName
209 * @param data The data array to store the loaded bytes from file
210 * @param datalen the number of bytes loaded from file
211 * @return 0 for ok, 1 for failz
213 int loadFileMCT_safe(const char *preferredName, void **pdata, size_t *datalen);
216 * @brief Utility function to load data from a textfile (NFC). This method takes a preferred name.
217 * E.g. dumpdata-15.nfc
219 * @param preferredName
220 * @param data The data array to store the loaded bytes from file
221 * @param maxdatalen maximum size of data array in bytes
222 * @param datalen the number of bytes loaded from file
223 * @param ft
224 * @return 0 for ok, 1 for failz
226 int loadFileNFC_safe(const char *preferredName, void *data, size_t maxdatalen, size_t *datalen, nfc_df_e ft);
229 * @brief Utility function to load data from a JSON textfile. This method takes a preferred name.
230 * E.g. dumpdata-15.json
232 * @param preferredName
233 * @param data The data array to store the loaded bytes from file
234 * @param maxdatalen maximum size of data array in bytes
235 * @param datalen the number of bytes loaded from file
236 * @return 0 for ok, 1 for failz
238 int loadFileJSON(const char *preferredName, void *data, size_t maxdatalen, size_t *datalen, void (*callback)(json_t *));
239 int loadFileJSONex(const char *preferredName, void *data, size_t maxdatalen, size_t *datalen, bool verbose, void (*callback)(json_t *));
240 int loadFileJSONroot(const char *preferredName, void **proot, bool verbose);
243 * @brief Utility function to load data from a DICTIONARY textfile. This method takes a preferred name.
244 * E.g. mfc_default_keys.dic
246 * @param preferredName
247 * @param data The data array to store the loaded bytes from file
248 * @param datalen the number of bytes loaded from file. may be NULL
249 * @param keylen the number of bytes a key per row is
250 * @param keycnt key count that lays in data. may be NULL
251 * @return 0 for ok, 1 for failz
253 int loadFileDICTIONARY(const char *preferredName, void *data, size_t *datalen, uint8_t keylen, uint32_t *keycnt);
256 * @brief Utility function to load data from a DICTIONARY textfile. This method takes a preferred name.
257 * E.g. mfc_default_keys.dic
258 * can be executed several times for big dictionaries and checks length of buffer
260 * @param preferredName
261 * @param data The data array to store the loaded bytes from file
262 * @param maxdatalen maximum size of data array in bytes
263 * @param datalen the number of bytes loaded from file. may be NULL
264 * @param keylen the number of bytes a key per row is
265 * @param keycnt key count that lays in data. may be NULL
266 * @param startFilePosition start position in dictionary file. used for big dictionaries.
267 * @param endFilePosition in case we have keys in file and maxdatalen reached it returns current key position in file. may be NULL
268 * @param verbose print messages if true
269 * @return 0 for ok, 1 for failz
271 int loadFileDICTIONARYEx(const char *preferredName, void *data, size_t maxdatalen, size_t *datalen, uint8_t keylen, uint32_t *keycnt,
272 size_t startFilePosition, size_t *endFilePosition, bool verbose);
275 * @brief Utility function to load data safely from a DICTIONARY textfile. This method takes a preferred name.
276 * E.g. mfc_default_keys.dic
278 * @param preferredName
279 * @param pdata A pointer to a pointer (for reverencing the loaded dictionary)
280 * @param keylen the number of bytes a key per row is
281 * @return 0 for ok, 1 for failz
283 int loadFileDICTIONARY_safe(const char *preferredName, void **pdata, uint8_t keylen, uint32_t *keycnt);
285 int loadFileDICTIONARY_safe_ex(const char *preferredName, const char *suffix, void **pdata, uint8_t keylen, uint32_t *keycnt, bool verbose);
287 int loadFileBinaryKey(const char *preferredName, const char *suffix, void **keya, void **keyb, size_t *alen, size_t *blen);
290 * @brief Utility function to check and convert plain mfu dump format to new mfu binary format.
291 * plain dumps doesn't have any extra data, like version, signature etc.
292 * @param dump pointer to loaded dump to check and convert format
293 * @param dumplen the number of bytes loaded dump and converted
294 * @param verbose - extra debug output
295 * @return PM3_SUCCESS for ok, PM3_ESOFT for fails
297 int convert_mfu_dump_format(uint8_t **dump, size_t *dumplen, bool verbose);
298 mfu_df_e detect_mfu_dump_format(uint8_t **dump, bool verbose);
299 int detect_nfc_dump_format(const char *preferredName, nfc_df_e *dump_type, bool verbose);
301 int searchAndList(const char *pm3dir, const char *ext);
302 int searchFile(char **foundpath, const char *pm3dir, const char *searchname, const char *suffix, bool silent);
306 * @brief detects if file is of a supported filetype based on extension
307 * @param filename
308 * @return
310 DumpFileType_t get_filetype(const char *filename);
314 * @brief load dump file into a data array dynamically allocated
315 * @param fn
316 * @param pdump pointer to loaded dump
317 * @param dumplen the number of bytes loaded from dump file
318 * @param maxdumplen maximum size of data array in bytes (JSON files)
319 * @return PM3_SUCCESS if OK
321 int pm3_load_dump(const char *fn, void **pdump, size_t *dumplen, size_t maxdumplen);
324 /** STUB
325 * @brief Utility function to save data to three file files (BIN/JSON).
326 * It also tries to save according to user preferences set dump folder paths.
327 * E.g. dumpdata.bin
328 * E.g. dumpdata.json
330 * @param fn
331 * @param d The binary data to write to the file
332 * @param n the length of the data
333 * @param jsft json format type for the different memory cards (MFC, MFUL, LEGIC, 14B, 15, ICLASS etc)
334 * @return PM3_SUCCESS if OK
336 int pm3_save_dump(const char *fn, uint8_t *d, size_t n, JSONFileType jsft);
338 /** STUB
339 * @brief Utility function to save data to three file files (BIN/JSON).
340 * It also tries to save according to user preferences set dump folder paths.
341 * E.g. dumpdata.bin
342 * E.g. dumpdata.json
344 * This function is dedicated for MIFARE CLASSIC dumps. Checking for 4 or 7 byte UID in indata.
345 * Saves the corrected data in the json file
347 * @param fn
348 * @param d The binary data to write to the file
349 * @param n the length of the data
350 * @param jsft json format type for the different memory cards (MFC, MFUL, LEGIC, 14B, 15, ICLASS etc)
351 * @return PM3_SUCCESS if OK
353 int pm3_save_mf_dump(const char *fn, uint8_t *d, size_t n, JSONFileType jsft);
355 /** STUB
356 * @brief Utility function to save FM11RF08S recovery data.
358 * @param fn
359 * @param d iso14a_fm11rf08s_nonces_with_data_t structure
360 * @param n the length of the structure
361 * @param with_data does the structure contain data blocks?
362 * @return PM3_SUCCESS if OK
364 int pm3_save_fm11rf08s_nonces(const char *fn, iso14a_fm11rf08s_nonces_with_data_t *d, bool with_data);
365 #endif // FILEUTILS_H