1 // license:BSD-3-Clause
2 // copyright-holders:Raphael Nabet
3 /****************************************************************************
4
5 macutil.cpp
6
7 Imgtool Utility code for manipulating certain Apple/Mac data structures
8 and conventions
9
10 *****************************************************************************/
11
12 #include "macutil.h"
13 #include "timeconv.h"
14
15
16 typedef util::arbitrary_clock<std::uint32_t, 1904, 1, 1, 0, 0, 0, std::ratio<1, 1> > classic_mac_clock;
17
18 //-------------------------------------------------
19 // mac_crack_time
20 //-------------------------------------------------
21
mac_crack_time(uint32_t t)22 imgtool::datetime mac_crack_time(uint32_t t)
23 {
24 classic_mac_clock::duration d(t);
25 std::chrono::time_point<classic_mac_clock> tp(d);
26 return imgtool::datetime(imgtool::datetime::datetime_type::LOCAL, tp);
27 }
28
29
30 //-------------------------------------------------
31 // mac_setup_time
32 //-------------------------------------------------
33
mac_setup_time(const imgtool::datetime & t)34 uint32_t mac_setup_time(const imgtool::datetime &t)
35 {
36 auto mac_time_point = classic_mac_clock::from_arbitrary_time_point(t.time_point());
37 return mac_time_point.time_since_epoch().count();
38 }
39
40
41 //-------------------------------------------------
42 // mac_setup_time
43 //-------------------------------------------------
44
mac_setup_time(time_t t)45 uint32_t mac_setup_time(time_t t)
46 {
47 imgtool::datetime dt(imgtool::datetime::datetime_type::LOCAL, t);
48 return mac_setup_time(dt);
49 }
50
51
52 //-------------------------------------------------
53 // mac_time_now
54 //-------------------------------------------------
55
mac_time_now(void)56 uint32_t mac_time_now(void)
57 {
58 imgtool::datetime dt = imgtool::datetime::now(imgtool::datetime::datetime_type::LOCAL);
59 return mac_setup_time(dt);
60 }
61
62
63 //-------------------------------------------------
64 // mac_identify_fork
65 //-------------------------------------------------
66
mac_identify_fork(const char * fork_string,mac_fork_t * fork_num)67 imgtoolerr_t mac_identify_fork(const char *fork_string, mac_fork_t *fork_num)
68 {
69 if (!strcmp(fork_string, ""))
70 *fork_num = MAC_FORK_DATA;
71 else if (!strcmp(fork_string, "RESOURCE_FORK"))
72 *fork_num = MAC_FORK_RESOURCE;
73 else
74 return IMGTOOLERR_FORKNOTFOUND;
75 return IMGTOOLERR_SUCCESS;
76 }
77
78
79
mac_suggest_transfer(mac_filecategory_t file_category,imgtool_transfer_suggestion * suggestions,size_t suggestions_length)80 void mac_suggest_transfer(mac_filecategory_t file_category, imgtool_transfer_suggestion *suggestions, size_t suggestions_length)
81 {
82 suggestions[0].viability = (file_category == MAC_FILECATEGORY_FORKED) ? SUGGESTION_RECOMMENDED : SUGGESTION_POSSIBLE;
83 suggestions[0].filter = filter_macbinary_getinfo;
84 suggestions[0].fork = NULL;
85 suggestions[0].description = NULL;
86
87 suggestions[1].viability = (file_category == MAC_FILECATEGORY_TEXT) ? SUGGESTION_RECOMMENDED : SUGGESTION_POSSIBLE;
88 suggestions[1].filter = filter_eoln_getinfo;
89 suggestions[1].fork = NULL;
90 suggestions[1].description = NULL;
91
92 suggestions[2].viability = (file_category == MAC_FILECATEGORY_DATA) ? SUGGESTION_RECOMMENDED : SUGGESTION_POSSIBLE;
93 suggestions[2].filter = NULL;
94 suggestions[2].fork = "";
95 suggestions[2].description = "Raw (data fork)";
96
97 suggestions[3].viability = SUGGESTION_POSSIBLE;
98 suggestions[3].filter = NULL;
99 suggestions[3].fork = "RESOURCE_FORK";
100 suggestions[3].description = "Raw (resource fork)";
101 }
102
103
104
pascal_from_c_string(unsigned char * pstring,size_t pstring_len,const char * cstring)105 void pascal_from_c_string(unsigned char *pstring, size_t pstring_len, const char *cstring)
106 {
107 size_t cstring_len, i;
108
109 cstring_len = strlen(cstring);
110 pstring[0] = std::min(cstring_len, pstring_len - 1);
111
112 for (i = 0; i < pstring[0]; i++)
113 pstring[1 + i] = cstring[i];
114 while(i < pstring_len - 1)
115 pstring[1 + i++] = '\0';
116 }
117