textual
[RRG-proxmark3.git] / client / src / fileutils.h
blob74f00e24390d59212a200ad394f86413ae7ed2e1
1 /*****************************************************************************
2 * WARNING
4 * THIS CODE IS CREATED FOR EXPERIMENTATION AND EDUCATIONAL USE ONLY.
6 * USAGE OF THIS CODE IN OTHER WAYS MAY INFRINGE UPON THE INTELLECTUAL
7 * PROPERTY OF OTHER PARTIES, SUCH AS INSIDE SECURE AND HID GLOBAL,
8 * AND MAY EXPOSE YOU TO AN INFRINGEMENT ACTION FROM THOSE PARTIES.
10 * THIS CODE SHOULD NEVER BE USED TO INFRINGE PATENTS OR INTELLECTUAL PROPERTY RIGHTS.
12 *****************************************************************************
14 * This file is part of loclass. It is a reconstructon of the cipher engine
15 * used in iClass, and RFID techology.
17 * The implementation is based on the work performed by
18 * Flavio D. Garcia, Gerhard de Koning Gans, Roel Verdult and
19 * Milosch Meriac in the paper "Dismantling IClass".
21 * Copyright (C) 2014 Martin Holst Swende
23 * This is free software: you can redistribute it and/or modify
24 * it under the terms of the GNU General Public License version 2 as published
25 * by the Free Software Foundation, or, at your option, any later version.
27 * This file is distributed in the hope that it will be useful,
28 * but WITHOUT ANY WARRANTY; without even the implied warranty of
29 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30 * GNU General Public License for more details.
32 * You should have received a copy of the GNU General Public License
33 * along with loclass. If not, see <http://www.gnu.org/licenses/>.
36 ****************************************************************************/
38 #ifndef FILEUTILS_H
39 #define FILEUTILS_H
41 #include <inttypes.h>
42 #include <stdio.h>
43 #include <string.h>
44 #include <stdlib.h>
45 #include <sys/stat.h>
46 #include <stdarg.h>
47 #include "ui.h"
48 #include "emv/emvjson.h"
49 #include "mifare/mifare4.h"
50 #include "mifare/mifarehost.h"
51 #include "cmdhfmfu.h"
53 typedef enum {
54 jsfRaw,
55 jsfCardMemory,
56 jsfMfuMemory,
57 jsfHitag,
58 jsfIclass,
59 jsf14b,
60 jsf15,
61 jsfLegic,
62 jsfT55x7,
63 jsfT5555,
64 jsfMfPlusKeys,
65 jsfCustom,
66 jsfMfDesfireKeys,
67 jsfEM4x05,
68 jsfEM4x69,
69 jsfEM4x50,
70 jsfFido,
71 } JSONFileType;
73 typedef enum {
74 BIN = 0,
75 EML,
76 JSON,
77 DICTIONARY,
78 } DumpFileType_t;
80 int fileExists(const char *filename);
81 //bool create_path(const char *dirname);
82 bool setDefaultPath(savePaths_t pathIndex, const char *Path); // set a path in the path list session.defaultPaths
84 char *newfilenamemcopy(const char *preferredName, const char *suffix);
86 /**
87 * @brief Utility function to save data to a binary file. This method takes a preferred name, but if that
88 * file already exists, it tries with another name until it finds something suitable.
89 * E.g. dumpdata-15.txt
91 * @param preferredName
92 * @param suffix the file suffix. Including the ".".
93 * @param data The binary data to write to the file
94 * @param datalen the length of the data
95 * @return 0 for ok, 1 for failz
97 int saveFile(const char *preferredName, const char *suffix, const void *data, size_t datalen);
99 /**
100 * @brief Utility function to save data to a textfile (EML). This method takes a preferred name, but if that
101 * file already exists, it tries with another name until it finds something suitable.
102 * E.g. dumpdata-15.txt
104 * @param preferredName
105 * @param data The binary data to write to the file
106 * @param datalen the length of the data
107 * @param blocksize the length of one row
108 * @return 0 for ok, 1 for failz
110 int saveFileEML(const char *preferredName, uint8_t *data, size_t datalen, size_t blocksize);
112 /** STUB
113 * @brief Utility function to save JSON data to a file. This method takes a preferred name, but if that
114 * file already exists, it tries with another name until it finds something suitable.
115 * E.g. dumpdata-15.json
117 * @param preferredName
118 * @param ftype type of file.
119 * @param data The binary data to write to the file
120 * @param datalen the length of the data
121 * @return 0 for ok, 1 for failz
123 int saveFileJSON(const char *preferredName, JSONFileType ftype, uint8_t *data, size_t datalen, void (*callback)(json_t *));
124 int saveFileJSONex(const char *preferredName, JSONFileType ftype, uint8_t *data, size_t datalen, bool verbose, void (*callback)(json_t *));
125 int saveFileJSONroot(const char *preferredName, void *root, size_t flags, bool verbose);
126 int saveFileJSONrootEx(const char *preferredName, void *root, size_t flags, bool verbose, bool overwrite);
127 /** STUB
128 * @brief Utility function to save WAVE 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.wav
132 * @param preferredName
133 * @param data The binary data to write to the file
134 * @param datalen the length of the data
135 * @return 0 for ok
137 int saveFileWAVE(const char *preferredName, int *data, size_t datalen);
139 /** STUB
140 * @brief Utility function to save PM3 data to a file. This method takes a preferred name, but if that
141 * file already exists, it tries with another name until it finds something suitable.
142 * E.g. dump_trace.pm3
144 * @param preferredName
145 * @param data The binary data to write to the file
146 * @param datalen the length of the data
147 * @return 0 for ok
149 int saveFilePM3(const char *preferredName, int *data, size_t datalen);
152 * @brief Utility function to save a keydump into a binary file.
154 * @param preferredName
155 * @param sectorsCnt the used sectors
156 * @param e_sector the keys in question
157 * @return 0 for ok, 1 for failz
159 int createMfcKeyDump(const char *preferredName, uint8_t sectorsCnt, sector_t *e_sector);
162 * @brief Utility function to load data from a binary file. This method takes a preferred name.
163 * E.g. dumpdata-15.bin
165 * @param preferredName
166 * @param suffix the file suffix. Including the ".".
167 * @param data The data array to store the loaded bytes from file
168 * @param maxdatalen the number of bytes that your data array has
169 * @param datalen the number of bytes loaded from file
170 * @return PM3_SUCCESS for ok, PM3_E* for failz
172 int loadFile(const char *preferredName, const char *suffix, void *data, size_t maxdatalen, size_t *datalen);
176 * @brief Utility function to load data from a binary file. This method takes a preferred name.
177 * E.g. dumpdata-15.bin, tries to search for it, and allocated memory.
179 * @param preferredName
180 * @param suffix the file suffix. Including the ".".
181 * @param data The data array to store the loaded bytes from file
182 * @param datalen the number of bytes loaded from file
183 * @return PM3_SUCCESS for ok, PM3_E* for failz
185 int loadFile_safe(const char *preferredName, const char *suffix, void **pdata, size_t *datalen);
186 int loadFile_safeEx(const char *preferredName, const char *suffix, void **pdata, size_t *datalen, bool verbose);
188 * @brief Utility function to load data from a textfile (EML). This method takes a preferred name.
189 * E.g. dumpdata-15.txt
191 * @param preferredName
192 * @param data The data array to store the loaded bytes from file
193 * @param datalen the number of bytes loaded from file
194 * @return 0 for ok, 1 for failz
196 int loadFileEML(const char *preferredName, void *data, size_t *datalen);
197 int loadFileEML_safe(const char *preferredName, void **pdata, size_t *datalen);
200 * @brief Utility function to load data from a JSON textfile. This method takes a preferred name.
201 * E.g. dumpdata-15.json
203 * @param preferredName
204 * @param data The data array to store the loaded bytes from file
205 * @param maxdatalen maximum size of data array in bytes
206 * @param datalen the number of bytes loaded from file
207 * @return 0 for ok, 1 for failz
209 int loadFileJSON(const char *preferredName, void *data, size_t maxdatalen, size_t *datalen, void (*callback)(json_t *));
210 int loadFileJSONex(const char *preferredName, void *data, size_t maxdatalen, size_t *datalen, bool verbose, void (*callback)(json_t *));
211 int loadFileJSONroot(const char *preferredName, void **proot, bool verbose);
214 * @brief Utility function to load data from a DICTIONARY textfile. This method takes a preferred name.
215 * E.g. mfc_default_keys.dic
217 * @param preferredName
218 * @param data The data array to store the loaded bytes from file
219 * @param datalen the number of bytes loaded from file. may be NULL
220 * @param keylen the number of bytes a key per row is
221 * @param keycnt key count that lays in data. may be NULL
222 * @return 0 for ok, 1 for failz
224 int loadFileDICTIONARY(const char *preferredName, void *data, size_t *datalen, uint8_t keylen, uint32_t *keycnt);
227 * @brief Utility function to load data from a DICTIONARY textfile. This method takes a preferred name.
228 * E.g. mfc_default_keys.dic
229 * can be executed several times for big dictionaries and checks length of buffer
231 * @param preferredName
232 * @param data The data array to store the loaded bytes from file
233 * @param maxdatalen maximum size of data array in bytes
234 * @param datalen the number of bytes loaded from file. may be NULL
235 * @param keylen the number of bytes a key per row is
236 * @param keycnt key count that lays in data. may be NULL
237 * @param startFilePosition start position in dictionary file. used for big dictionaries.
238 * @param endFilePosition in case we have keys in file and maxdatalen reached it returns current key position in file. may be NULL
239 * @param verbose print messages if true
240 * @return 0 for ok, 1 for failz
242 int loadFileDICTIONARYEx(const char *preferredName, void *data, size_t maxdatalen, size_t *datalen, uint8_t keylen, uint32_t *keycnt,
243 size_t startFilePosition, size_t *endFilePosition, bool verbose);
246 * @brief Utility function to load data safely from a DICTIONARY textfile. This method takes a preferred name.
247 * E.g. mfc_default_keys.dic
249 * @param preferredName
250 * @param pdata A pointer to a pointer (for reverencing the loaded dictionary)
251 * @param keylen the number of bytes a key per row is
252 * @return 0 for ok, 1 for failz
254 int loadFileDICTIONARY_safe(const char *preferredName, void **pdata, uint8_t keylen, uint32_t *keycnt);
257 typedef enum {
258 MFU_DF_UNKNOWN,
259 MFU_DF_PLAINBIN,
260 MFU_DF_OLDBIN,
261 MFU_DF_NEWBIN
262 } mfu_df_e;
264 * @brief Utility function to check and convert plain mfu dump format to new mfu binary format.
265 * plain dumps doesn't have any extra data, like version, signature etc.
266 * @param dump pointer to loaded dump to check and convert format
267 * @param dumplen the number of bytes loaded dump and converted
268 * @param verbose - extra debug output
269 * @return PM3_SUCCESS for ok, PM3_ESOFT for fails
271 int convert_mfu_dump_format(uint8_t **dump, size_t *dumplen, bool verbose);
272 mfu_df_e detect_mfu_dump_format(uint8_t **dump, size_t *dumplen, bool verbose);
274 int searchAndList(const char *pm3dir, const char *ext);
275 int searchFile(char **foundpath, const char *pm3dir, const char *searchname, const char *suffix, bool silent);
279 * @brief detects if file is of a supported filetype based on extension
280 * @param filename
281 * @return
283 DumpFileType_t getfiletype(const char *filename);
284 #endif // FILEUTILS_H