Skip to content

Commit adda029

Browse files
author
Marcos Del Sol Vives
authored
Merge pull request #7 from ynsta/copy
Copy function update
2 parents 0d2b873 + 84c326e commit adda029

File tree

4 files changed

+141
-21
lines changed

4 files changed

+141
-21
lines changed

amiibo.c

+16-17
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
#include "mbedtls/md.h"
1111
#include "mbedtls/aes.h"
1212
#include <errno.h>
13-
#include <endian.h>
13+
#include "portable_endian.h"
1414

1515

1616
#define HMAC_POS_DATA 0x008
@@ -158,23 +158,22 @@ bool nfc3d_amiibo_load_keys(nfc3d_amiibo_keys * amiiboKeys, const char * path) {
158158
}
159159

160160

161-
void nfc3d_amiibo_copy_app_data(uint8_t * dst, const uint8_t * src) {
161+
void nfc3d_amiibo_copy_app_data(const uint8_t * src, uint8_t * dst) {
162162

163-
uint16_t *ami_nb_wr = (uint16_t*)(dst + 0x29);
164-
uint16_t *cfg_nb_wr = (uint16_t*)(dst + 0xB4);
163+
uint16_t *ami_nb_wr = (uint16_t*)(dst + 0x29);
164+
uint16_t *cfg_nb_wr = (uint16_t*)(dst + 0xB4);
165165

166-
/* increment write counters */
167-
*ami_nb_wr = htobe16(be16toh(*ami_nb_wr) + 1);
168-
*cfg_nb_wr = htobe16(be16toh(*cfg_nb_wr) + 1);
169-
170-
/* copy flags */
171-
dst[0x2C] = src[0x2C];
172-
/* copy programID */
173-
memcpy(dst + 0xAC, src + 0xAC, 8);
174-
/* copy AppID */
175-
memcpy(dst + 0xB6, src + 0xB6, 4);
176-
177-
/* copy AppData */
178-
memcpy(dst + 0xDC, src + 0xDC, 216);
166+
/* increment write counters */
167+
*ami_nb_wr = htobe16(be16toh(*ami_nb_wr) + 1);
168+
*cfg_nb_wr = htobe16(be16toh(*cfg_nb_wr) + 1);
179169

170+
/* copy flags */
171+
dst[0x2C] = src[0x2C];
172+
/* copy programID */
173+
memcpy(dst + 0xAC, src + 0xAC, 8);
174+
/* copy AppID */
175+
memcpy(dst + 0xB6, src + 0xB6, 4);
176+
/* copy AppData */
177+
memcpy(dst + 0xDC, src + 0xDC, 216);
180178
}
179+

amiitool.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ int main(int argc, char ** argv) {
3939
self = argv[0];
4040

4141
char * infile = NULL;
42-
char * savefile = NULL;
42+
char * savefile = NULL;
4343
char * outfile = NULL;
4444
char * keyfile = NULL;
4545
char op = '\0';
@@ -144,7 +144,7 @@ int main(int argc, char ** argv) {
144144
}
145145
}
146146

147-
nfc3d_amiibo_copy_app_data(plain_base, plain_save);
147+
nfc3d_amiibo_copy_app_data(plain_save, plain_base);
148148
nfc3d_amiibo_pack(&amiiboKeys, plain_base, modified);
149149
}
150150

@@ -169,4 +169,4 @@ int main(int argc, char ** argv) {
169169
fclose(f);
170170

171171
return 0;
172-
}
172+
}

include/nfc3d/amiibo.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,6 @@ typedef struct {
2424
bool nfc3d_amiibo_unpack(const nfc3d_amiibo_keys * amiiboKeys, const uint8_t * tag, uint8_t * plain);
2525
void nfc3d_amiibo_pack(const nfc3d_amiibo_keys * amiiboKeys, const uint8_t * plain, uint8_t * tag);
2626
bool nfc3d_amiibo_load_keys(nfc3d_amiibo_keys * amiiboKeys, const char * path);
27-
void nfc3d_amiibo_copy_app_data(uint8_t * dst, const uint8_t * src);
27+
void nfc3d_amiibo_copy_app_data(const uint8_t * src, uint8_t * dst);
2828

2929
#endif

portable_endian.h

+121
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
// "License": Public Domain
2+
// I, Mathias Panzenböck, place this file hereby into the public domain. Use it at your own risk for whatever you like.
3+
// In case there are jurisdictions that don't support putting things in the public domain you can also consider it to
4+
// be "dual licensed" under the BSD, MIT and Apache licenses, if you want to. This code is trivial anyway. Consider it
5+
// an example on how to get the endian conversion functions on different platforms.
6+
7+
#ifndef PORTABLE_ENDIAN_H__
8+
#define PORTABLE_ENDIAN_H__
9+
10+
#if (defined(_WIN16) || defined(_WIN32) || defined(_WIN64)) && !defined(__WINDOWS__)
11+
12+
# define __WINDOWS__
13+
14+
#endif
15+
16+
#if defined(__linux__) || defined(__CYGWIN__)
17+
18+
# include <endian.h>
19+
20+
#elif defined(__APPLE__)
21+
22+
# include <libkern/OSByteOrder.h>
23+
24+
# define htobe16(x) OSSwapHostToBigInt16(x)
25+
# define htole16(x) OSSwapHostToLittleInt16(x)
26+
# define be16toh(x) OSSwapBigToHostInt16(x)
27+
# define le16toh(x) OSSwapLittleToHostInt16(x)
28+
29+
# define htobe32(x) OSSwapHostToBigInt32(x)
30+
# define htole32(x) OSSwapHostToLittleInt32(x)
31+
# define be32toh(x) OSSwapBigToHostInt32(x)
32+
# define le32toh(x) OSSwapLittleToHostInt32(x)
33+
34+
# define htobe64(x) OSSwapHostToBigInt64(x)
35+
# define htole64(x) OSSwapHostToLittleInt64(x)
36+
# define be64toh(x) OSSwapBigToHostInt64(x)
37+
# define le64toh(x) OSSwapLittleToHostInt64(x)
38+
39+
# define __BYTE_ORDER BYTE_ORDER
40+
# define __BIG_ENDIAN BIG_ENDIAN
41+
# define __LITTLE_ENDIAN LITTLE_ENDIAN
42+
# define __PDP_ENDIAN PDP_ENDIAN
43+
44+
#elif defined(__OpenBSD__)
45+
46+
# include <sys/endian.h>
47+
48+
#elif defined(__NetBSD__) || defined(__FreeBSD__) || defined(__DragonFly__)
49+
50+
# include <sys/endian.h>
51+
52+
# define be16toh(x) betoh16(x)
53+
# define le16toh(x) letoh16(x)
54+
55+
# define be32toh(x) betoh32(x)
56+
# define le32toh(x) letoh32(x)
57+
58+
# define be64toh(x) betoh64(x)
59+
# define le64toh(x) letoh64(x)
60+
61+
#elif defined(__WINDOWS__)
62+
63+
# include <windows.h>
64+
65+
# if BYTE_ORDER == LITTLE_ENDIAN
66+
67+
# if defined(_MSC_VER)
68+
# include <stdlib.h>
69+
# define htobe16(x) _byteswap_ushort(x)
70+
# define htole16(x) (x)
71+
# define be16toh(x) _byteswap_ushort(x)
72+
# define le16toh(x) (x)
73+
74+
# define htobe32(x) _byteswap_ulong(x)
75+
# define htole32(x) (x)
76+
# define be32toh(x) _byteswap_ulong(x)
77+
# define le32toh(x) (x)
78+
79+
# define htobe64(x) _byteswap_uint64(x)
80+
# define htole64(x) (x)
81+
# define be64toh(x) _byteswap_uint64(x)
82+
# define le64toh(x) (x)
83+
84+
# elif defined(__GNUC__) || defined(__clang__)
85+
86+
# define htobe16(x) __builtin_bswap16(x)
87+
# define htole16(x) (x)
88+
# define be16toh(x) __builtin_bswap16(x)
89+
# define le16toh(x) (x)
90+
91+
# define htobe32(x) __builtin_bswap32(x)
92+
# define htole32(x) (x)
93+
# define be32toh(x) __builtin_bswap32(x)
94+
# define le32toh(x) (x)
95+
96+
# define htobe64(x) __builtin_bswap64(x)
97+
# define htole64(x) (x)
98+
# define be64toh(x) __builtin_bswap64(x)
99+
# define le64toh(x) (x)
100+
# else
101+
# error platform not supported
102+
# endif
103+
104+
# else
105+
106+
# error byte order not supported
107+
108+
# endif
109+
110+
# define __BYTE_ORDER BYTE_ORDER
111+
# define __BIG_ENDIAN BIG_ENDIAN
112+
# define __LITTLE_ENDIAN LITTLE_ENDIAN
113+
# define __PDP_ENDIAN PDP_ENDIAN
114+
115+
#else
116+
117+
# error platform not supported
118+
119+
#endif
120+
121+
#endif

0 commit comments

Comments
 (0)