Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
stenzek
GitHub Repository: stenzek/duckstation
Path: blob/master/dep/rcheevos/include/rc_hash.h
4804 views
1
#ifndef RC_HASH_H
2
#define RC_HASH_H
3
4
#include <stddef.h>
5
#include <stdio.h>
6
#include <stdint.h>
7
8
#include "rc_consoles.h"
9
10
RC_BEGIN_C_DECLS
11
12
struct rc_hash_iterator;
13
14
/* ===================================================== */
15
16
typedef void (RC_CCONV *rc_hash_message_callback_deprecated)(const char*);
17
18
/* specifies a function to call when an error occurs to display the error message */
19
/* [deprecated] set callbacks in rc_hash_iterator_t */
20
RC_EXPORT void RC_CCONV rc_hash_init_error_message_callback(rc_hash_message_callback_deprecated callback);
21
22
/* specifies a function to call for verbose logging */
23
/* [deprecated] set callbacks in rc_hash_iterator_t */
24
RC_EXPORT void rc_hash_init_verbose_message_callback(rc_hash_message_callback_deprecated callback);
25
26
/* ===================================================== */
27
28
/* opens a file */
29
typedef void* (RC_CCONV *rc_hash_filereader_open_file_handler)(const char* path_utf8);
30
31
/* moves the file pointer - standard fseek parameters */
32
typedef void (RC_CCONV *rc_hash_filereader_seek_handler)(void* file_handle, int64_t offset, int origin);
33
34
/* locates the file pointer */
35
typedef int64_t (RC_CCONV *rc_hash_filereader_tell_handler)(void* file_handle);
36
37
/* reads the specified number of bytes from the file starting at the read pointer.
38
* returns the number of bytes actually read.
39
*/
40
typedef size_t (RC_CCONV *rc_hash_filereader_read_handler)(void* file_handle, void* buffer, size_t requested_bytes);
41
42
/* closes the file */
43
typedef void (RC_CCONV *rc_hash_filereader_close_file_handler)(void* file_handle);
44
45
typedef struct rc_hash_filereader
46
{
47
rc_hash_filereader_open_file_handler open;
48
rc_hash_filereader_seek_handler seek;
49
rc_hash_filereader_tell_handler tell;
50
rc_hash_filereader_read_handler read;
51
rc_hash_filereader_close_file_handler close;
52
} rc_hash_filereader_t;
53
54
/* [deprecated] set callbacks in rc_hash_iterator_t */
55
RC_EXPORT void RC_CCONV rc_hash_init_custom_filereader(struct rc_hash_filereader* reader);
56
57
/* ===================================================== */
58
59
#ifndef RC_HASH_NO_DISC
60
61
#define RC_HASH_CDTRACK_FIRST_DATA ((uint32_t)-1) /* the first data track (skip audio tracks) */
62
#define RC_HASH_CDTRACK_LAST ((uint32_t)-2) /* the last data/audio track */
63
#define RC_HASH_CDTRACK_LARGEST ((uint32_t)-3) /* the largest data/audio track */
64
#define RC_HASH_CDTRACK_FIRST_OF_SECOND_SESSION ((uint32_t)-4) /* the first data/audio track of the second session */
65
66
/* opens a track from the specified file. see the RC_HASH_CDTRACK_ defines for special tracks.
67
* returns a handle to be passed to the other functions, or NULL if the track could not be opened.
68
*/
69
typedef void* (RC_CCONV *rc_hash_cdreader_open_track_handler)(const char* path, uint32_t track);
70
typedef void* (RC_CCONV* rc_hash_cdreader_open_track_iterator_handler)(const char* path, uint32_t track, const struct rc_hash_iterator* iterator);
71
72
/* attempts to read the specified number of bytes from the file starting at the specified absolute sector.
73
* returns the number of bytes actually read.
74
*/
75
typedef size_t (RC_CCONV *rc_hash_cdreader_read_sector_handler)(void* track_handle, uint32_t sector, void* buffer, size_t requested_bytes);
76
77
/* closes the track handle */
78
typedef void (RC_CCONV *rc_hash_cdreader_close_track_handler)(void* track_handle);
79
80
/* gets the absolute sector index for the first sector of a track */
81
typedef uint32_t(RC_CCONV *rc_hash_cdreader_first_track_sector_handler)(void* track_handle);
82
83
typedef struct rc_hash_cdreader
84
{
85
rc_hash_cdreader_open_track_handler open_track;
86
rc_hash_cdreader_read_sector_handler read_sector;
87
rc_hash_cdreader_close_track_handler close_track;
88
rc_hash_cdreader_first_track_sector_handler first_track_sector;
89
rc_hash_cdreader_open_track_iterator_handler open_track_iterator;
90
} rc_hash_cdreader_t;
91
92
RC_EXPORT void RC_CCONV rc_hash_get_default_cdreader(struct rc_hash_cdreader* cdreader);
93
/* [deprecated] don't set callbacks in rc_hash_iterator_t */
94
RC_EXPORT void RC_CCONV rc_hash_init_default_cdreader(void);
95
/* [deprecated] set callbacks in rc_hash_iterator_t */
96
RC_EXPORT void RC_CCONV rc_hash_init_custom_cdreader(struct rc_hash_cdreader* reader);
97
98
#endif /* RC_HASH_NO_DISC */
99
100
#ifndef RC_HASH_NO_ENCRYPTED
101
102
/* specifies a function called to obtain a 3DS CIA decryption normal key.
103
* this key would be derived from slot0x3DKeyX and the common key specified by the passed index.
104
* the normal key should be written in big endian format
105
* returns non-zero on success, or zero on failure.
106
*/
107
typedef int (RC_CCONV *rc_hash_3ds_get_cia_normal_key_func)(uint8_t common_key_index, uint8_t out_normal_key[16]);
108
/* [deprecated] set callbacks in rc_hash_iterator_t */
109
RC_EXPORT void RC_CCONV rc_hash_init_3ds_get_cia_normal_key_func(rc_hash_3ds_get_cia_normal_key_func func);
110
111
/* specifies a function called to obtain 3DS NCCH decryption normal keys.
112
* the primary key will always use slot0x2CKeyX and the passed primary KeyY.
113
* the secondary key will use the KeyX slot passed
114
* the secondary KeyY will be identical to the primary keyY if the passed program id is NULL
115
* if the program id is not null, then the secondary KeyY will be obtained with "seed crypto"
116
* with "seed crypto" the 8 byte program id can be used to obtain a 16 byte "seed" within the seeddb.bin firmware file
117
* the primary KeyY then the seed will then be hashed with SHA256, and the upper 16 bytes of the digest will be the secondary KeyY used
118
* the normal keys should be written in big endian format
119
* returns non-zero on success, or zero on failure.
120
*/
121
typedef int (RC_CCONV *rc_hash_3ds_get_ncch_normal_keys_func)(uint8_t primary_key_y[16], uint8_t secondary_key_x_slot, uint8_t* optional_program_id,
122
uint8_t out_primary_key[16], uint8_t out_secondary_key[16]);
123
/* [deprecated] set callbacks in rc_hash_iterator_t */
124
RC_EXPORT void RC_CCONV rc_hash_init_3ds_get_ncch_normal_keys_func(rc_hash_3ds_get_ncch_normal_keys_func func);
125
126
#endif /* RC_HASH_NO_ENCRYPTED */
127
128
/* ===================================================== */
129
130
typedef void (RC_CCONV* rc_hash_message_callback_func)(const char*, const struct rc_hash_iterator* iterator);
131
132
typedef struct rc_hash_callbacks {
133
rc_hash_message_callback_func verbose_message;
134
rc_hash_message_callback_func error_message;
135
136
rc_hash_filereader_t filereader;
137
#ifndef RC_HASH_NO_DISC
138
rc_hash_cdreader_t cdreader;
139
#endif
140
141
#ifndef RC_HASH_NO_ENCRYPTED
142
struct rc_hash_encryption_callbacks {
143
rc_hash_3ds_get_cia_normal_key_func get_3ds_cia_normal_key;
144
rc_hash_3ds_get_ncch_normal_keys_func get_3ds_ncch_normal_keys;
145
} encryption;
146
#endif
147
} rc_hash_callbacks_t;
148
149
/* data for rc_hash_iterate
150
*/
151
typedef struct rc_hash_iterator {
152
const uint8_t* buffer;
153
size_t buffer_size;
154
uint8_t consoles[12];
155
int index;
156
const char* path;
157
void* userdata;
158
159
rc_hash_callbacks_t callbacks;
160
} rc_hash_iterator_t;
161
162
/* initializes a rc_hash_iterator
163
* - path must be provided
164
* - if buffer and buffer_size are provided, path may be a filename (i.e. for something extracted from a zip file)
165
*/
166
RC_EXPORT void RC_CCONV rc_hash_initialize_iterator(rc_hash_iterator_t* iterator, const char* path, const uint8_t* buffer, size_t buffer_size);
167
168
/* releases resources associated to a rc_hash_iterator
169
*/
170
RC_EXPORT void RC_CCONV rc_hash_destroy_iterator(rc_hash_iterator_t* iterator);
171
172
/* generates the next hash for the data in the rc_hash_iterator.
173
* returns non-zero if a hash was generated, or zero if no more hashes can be generated for the data.
174
*/
175
RC_EXPORT int RC_CCONV rc_hash_iterate(char hash[33], rc_hash_iterator_t* iterator);
176
177
/* generates a hash for the data in the rc_hash_iterator.
178
* returns non-zero if a hash was generated.
179
*/
180
RC_EXPORT int RC_CCONV rc_hash_generate(char hash[33], uint32_t console_id, const rc_hash_iterator_t* iterator);
181
182
/* ===================================================== */
183
184
/* generates a hash from a block of memory.
185
* returns non-zero on success, or zero on failure.
186
*/
187
/* [deprecated] use rc_hash_generate instead */
188
RC_EXPORT int RC_CCONV rc_hash_generate_from_buffer(char hash[33], uint32_t console_id, const uint8_t* buffer, size_t buffer_size);
189
190
/* generates a hash from a file.
191
* returns non-zero on success, or zero on failure.
192
*/
193
/* [deprecated] use rc_hash_generate instead */
194
RC_EXPORT int RC_CCONV rc_hash_generate_from_file(char hash[33], uint32_t console_id, const char* path);
195
196
/* ===================================================== */
197
198
RC_END_C_DECLS
199
200
#endif /* RC_HASH_H */
201
202