7 290.rec contains the tick value 4 times, followed by the 4 byte key
8 to decrypt each instance, for a total of 20 bytes.
11 // load the contents of 290.rec and store in value_out. Returns 0 on success.
12 // If there is no such file or an error occurs, writes 0 to value_out.
13 bool niku_load(uint32_t *value_out
)
17 uint32_t *result
= (uint32_t *)buffer
;
20 const char *fname
= getfname();
21 fp
= fopen(fname
, "rb");
24 stat("niku_load: couldn't open file '%s'", fname
);
25 if (value_out
) *value_out
= 0;
29 fread(buffer
, 20, 1, fp
);
34 uint8_t key
= buffer
[i
+16];
40 buffer
[j
+3] -= (key
/ 2);
43 if ((result
[0] != result
[1]) || \
44 (result
[0] != result
[2]) || \
45 (result
[0] != result
[3]))
47 stat("niku_load: value mismatch; '%s' corrupt", fname
);
48 if (value_out
) *value_out
= 0;
52 stat("niku_load: loaded value 0x%x from %s", *result
, fname
);
53 if (value_out
) *value_out
= *result
;
59 // save the timestamp in value to 290.rec.
60 bool niku_save(uint32_t value
)
63 uint32_t *buf_dword
= (uint32_t *)buf_byte
;
72 buf_byte
[16] = random(0, 255);
73 buf_byte
[17] = random(0, 255);
74 buf_byte
[18] = random(0, 255);
75 buf_byte
[19] = random(0, 255);
80 uint8_t *ptr
= (uint8_t *)&buf_dword
[i
];
81 uint8_t key
= buf_byte
[i
+16];
89 const char *fname
= getfname();
90 FILE *fp
= fopen(fname
, "wb");
93 staterr("niku_save: failed to open '%s'", fname
);
97 fwrite(buf_byte
, 20, 1, fp
);
100 stat("niku_save: wrote value 0x%08x", value
);
105 void c------------------------------() {}
108 static const char *getfname()
110 // might do stuff with sprintf here to place it in other subdirectories, etc.
111 // is just here for expansion.