2 * misc.c: Miscellaneous helpful functions.
12 char UI_UPDATE[] = "";
14 void free_cfg(config_item *cfg)
18 for (i = cfg; i->type != C_END; i++)
19 if (i->type == C_STRING)
20 sfree(i->u.string.sval);
25 * The Mines (among others) game descriptions contain the location of every
26 * mine, and can therefore be used to cheat.
28 * It would be pointless to attempt to _prevent_ this form of
29 * cheating by encrypting the description, since Mines is
30 * open-source so anyone can find out the encryption key. However,
31 * I think it is worth doing a bit of gentle obfuscation to prevent
32 * _accidental_ spoilers: if you happened to note that the game ID
33 * starts with an F, for example, you might be unable to put the
34 * knowledge of those mines out of your mind while playing. So,
35 * just as discussions of film endings are rot13ed to avoid
36 * spoiling it for people who don't want to be told, we apply a
37 * keyless, reversible, but visually completely obfuscatory masking
38 * function to the mine bitmap.
40 void obfuscate_bitmap(unsigned char *bmp, int bits, int decode)
42 int bytes, firsthalf, secondhalf;
44 unsigned char *seedstart;
46 unsigned char *targetstart;
52 * My obfuscation algorithm is similar in concept to the OAEP
53 * encoding used in some forms of RSA. Here's a specification
56 * + We have a `masking function' which constructs a stream of
57 * pseudorandom bytes from a seed of some number of input
60 * + We pad out our input bit stream to a whole number of
61 * bytes by adding up to 7 zero bits on the end. (In fact
62 * the bitmap passed as input to this function will already
63 * have had this done in practice.)
65 * + We divide the _byte_ stream exactly in half, rounding the
66 * half-way position _down_. So an 81-bit input string, for
67 * example, rounds up to 88 bits or 11 bytes, and then
68 * dividing by two gives 5 bytes in the first half and 6 in
71 * + We generate a mask from the second half of the bytes, and
72 * XOR it over the first half.
74 * + We generate a mask from the (encoded) first half of the
75 * bytes, and XOR it over the second half. Any null bits at
76 * the end which were added as padding are cleared back to
77 * zero even if this operation would have made them nonzero.
79 * To de-obfuscate, the steps are precisely the same except
80 * that the final two are reversed.
82 * Finally, our masking function. Given an input seed string of
83 * bytes, the output mask consists of concatenating the SHA-1
84 * hashes of the seed string and successive decimal integers,
88 bytes = (bits + 7) / 8;
89 firsthalf = bytes / 2;
90 secondhalf = bytes - firsthalf;
92 steps[decode ? 1 : 0].seedstart = bmp + firsthalf;
93 steps[decode ? 1 : 0].seedlen = secondhalf;
94 steps[decode ? 1 : 0].targetstart = bmp;
95 steps[decode ? 1 : 0].targetlen = firsthalf;
97 steps[decode ? 0 : 1].seedstart = bmp;
98 steps[decode ? 0 : 1].seedlen = firsthalf;
99 steps[decode ? 0 : 1].targetstart = bmp + firsthalf;
100 steps[decode ? 0 : 1].targetlen = secondhalf;
102 for (i = 0; i < 2; i++) {
103 SHA_State base, final;
104 unsigned char digest[20];
106 int digestpos = 20, counter = 0;
109 SHA_Bytes(&base, steps[i].seedstart, steps[i].seedlen);
111 for (j = 0; j < steps[i].targetlen; j++) {
112 if (digestpos >= 20) {
113 sprintf(numberbuf, "%d", counter++);
115 SHA_Bytes(&final, numberbuf, strlen(numberbuf));
116 SHA_Final(&final, digest);
119 steps[i].targetstart[j] ^= digest[digestpos++];
123 * Mask off the pad bits in the final byte after both steps.
126 bmp[bits / 8] &= 0xFF & (0xFF00 >> (bits % 8));
130 /* err, yeah, these two pretty much rely on unsigned char being 8 bits.
131 * Platforms where this is not the case probably have bigger problems
132 * than just making these two work, though... */
133 char *bin2hex(const unsigned char *in, int inlen)
135 char *ret = snewn(inlen*2 + 1, char), *p = ret;
138 for (i = 0; i < inlen*2; i++) {
140 if (i % 2 == 0) v >>= 4;
141 *p++ = "0123456789abcdef"[v & 0xF];
147 unsigned char *hex2bin(const char *in, int outlen)
149 unsigned char *ret = snewn(outlen, unsigned char);
152 memset(ret, 0, outlen*sizeof(unsigned char));
153 for (i = 0; i < outlen*2; i++) {
158 if (c >= '0' && c <= '9')
160 else if (c >= 'a' && c <= 'f')
162 else if (c >= 'A' && c <= 'F')
167 ret[i / 2] |= v << (4 * (1 - (i % 2)));
172 void game_mkhighlight_specific(frontend *fe, float *ret,
173 int background, int highlight, int lowlight)
179 * Drop the background colour so that the highlight is
180 * noticeably brighter than it while still being under 1.
182 max = ret[background*3];
183 for (i = 1; i < 3; i++)
184 if (ret[background*3+i] > max)
185 max = ret[background*3+i];
186 if (max * 1.2F > 1.0F) {
187 for (i = 0; i < 3; i++)
188 ret[background*3+i] /= (max * 1.2F);
191 for (i = 0; i < 3; i++) {
193 ret[highlight * 3 + i] = ret[background * 3 + i] * 1.2F;
195 ret[lowlight * 3 + i] = ret[background * 3 + i] * 0.8F;
199 void game_mkhighlight(frontend *fe, float *ret,
200 int background, int highlight, int lowlight)
202 frontend_default_colour(fe, &ret[background * 3]);
203 game_mkhighlight_specific(fe, ret, background, highlight, lowlight);
206 static void memswap(void *av, void *bv, int size)
209 char *a = av, *b = bv;
212 int thislen = min(size, sizeof(tmpbuf));
213 memcpy(tmpbuf, a, thislen);
214 memcpy(a, b, thislen);
215 memcpy(b, tmpbuf, thislen);
222 void shuffle(void *array, int nelts, int eltsize, random_state *rs)
224 char *carray = (char *)array;
227 for (i = nelts; i-- > 1 ;) {
228 int j = random_upto(rs, i+1);
230 memswap(carray + eltsize * i, carray + eltsize * j, eltsize);
234 void draw_rect_outline(drawing *dr, int x, int y, int w, int h, int colour)
236 int x0 = x, x1 = x+w-1, y0 = y, y1 = y+h-1;
248 draw_polygon(dr, coords, 4, -1, colour);
251 void draw_rect_corners(drawing *dr, int cx, int cy, int r, int col)
253 draw_line(dr, cx - r, cy - r, cx - r, cy - r/2, col);
254 draw_line(dr, cx - r, cy - r, cx - r/2, cy - r, col);
255 draw_line(dr, cx - r, cy + r, cx - r, cy + r/2, col);
256 draw_line(dr, cx - r, cy + r, cx - r/2, cy + r, col);
257 draw_line(dr, cx + r, cy - r, cx + r, cy - r/2, col);
258 draw_line(dr, cx + r, cy - r, cx + r/2, cy - r, col);
259 draw_line(dr, cx + r, cy + r, cx + r, cy + r/2, col);
260 draw_line(dr, cx + r, cy + r, cx + r/2, cy + r, col);
263 void move_cursor(int button, int *x, int *y, int maxw, int maxh, int wrap)
267 case CURSOR_UP: dy = -1; break;
268 case CURSOR_DOWN: dy = 1; break;
269 case CURSOR_RIGHT: dx = 1; break;
270 case CURSOR_LEFT: dx = -1; break;
274 *x = (*x + dx + maxw) % maxw;
275 *y = (*y + dy + maxh) % maxh;
277 *x = min(max(*x+dx, 0), maxw - 1);
278 *y = min(max(*y+dy, 0), maxh - 1);
282 /* Used in netslide.c and sixteen.c for cursor movement around edge. */
284 int c2pos(int w, int h, int cx, int cy)
287 return cx; /* top row, 0 .. w-1 (->) */
289 return w + cy; /* R col, w .. w+h -1 (v) */
291 return w + h + (w-cx-1); /* bottom row, w+h .. w+h+w-1 (<-) */
293 return w + h + w + (h-cy-1); /* L col, w+h+w .. w+h+w+h-1 (^) */
295 assert(!"invalid cursor pos!");
296 return -1; /* not reached */
299 int c2diff(int w, int h, int cx, int cy, int button)
303 assert(IS_CURSOR_MOVE(button));
305 /* Obvious moves around edge. */
307 diff = (button == CURSOR_RIGHT) ? +1 : (button == CURSOR_LEFT) ? -1 : diff;
309 diff = (button == CURSOR_RIGHT) ? -1 : (button == CURSOR_LEFT) ? +1 : diff;
311 diff = (button == CURSOR_UP) ? +1 : (button == CURSOR_DOWN) ? -1 : diff;
313 diff = (button == CURSOR_UP) ? -1 : (button == CURSOR_DOWN) ? +1 : diff;
315 if (button == CURSOR_LEFT && cx == w && (cy == 0 || cy == h-1))
316 diff = (cy == 0) ? -1 : +1;
317 if (button == CURSOR_RIGHT && cx == -1 && (cy == 0 || cy == h-1))
318 diff = (cy == 0) ? +1 : -1;
319 if (button == CURSOR_DOWN && cy == -1 && (cx == 0 || cx == w-1))
320 diff = (cx == 0) ? -1 : +1;
321 if (button == CURSOR_UP && cy == h && (cx == 0 || cx == w-1))
322 diff = (cx == 0) ? +1 : -1;
324 debug(("cx,cy = %d,%d; w%d h%d, diff = %d", cx, cy, w, h, diff));
328 void pos2c(int w, int h, int pos, int *cx, int *cy)
332 pos = (pos + max) % max;
335 *cx = pos; *cy = -1; return;
339 *cx = w; *cy = pos; return;
343 *cx = w-pos-1; *cy = h; return;
347 *cx = -1; *cy = h-pos-1; return;
349 assert(!"invalid pos, huh?"); /* limited by % above! */
352 void draw_text_outline(drawing *dr, int x, int y, int fonttype,
353 int fontsize, int align,
354 int text_colour, int outline_colour, const char *text)
356 if (outline_colour > -1) {
357 draw_text(dr, x-1, y, fonttype, fontsize, align, outline_colour, text);
358 draw_text(dr, x+1, y, fonttype, fontsize, align, outline_colour, text);
359 draw_text(dr, x, y-1, fonttype, fontsize, align, outline_colour, text);
360 draw_text(dr, x, y+1, fonttype, fontsize, align, outline_colour, text);
362 draw_text(dr, x, y, fonttype, fontsize, align, text_colour, text);
366 /* kludge for sprintf() in Rockbox not supporting "%-8.8s" */
367 void copy_left_justified(char *buf, size_t sz, const char *str)
369 size_t len = strlen(str);
371 memset(buf, ' ', sz - 1);
372 assert(len <= sz - 1);
373 memcpy(buf, str, len);
377 /* vim: set shiftwidth=4 tabstop=8: */