+/* Convert the user sequence into a list of colour index specifiers */
+static unsigned long *expand_seq(unsigned long *out, const char *seq)
+{
+ int i;
+
+ for (i = 0; i < 9; i++) {
+ out[i] = 0x30303 * (seq[i]-'0') + 0x20100;
+ }
+
+ return out;
+}
+
+static unsigned long decode_size(char *size_spec)
+{
+ unsigned long w, h;
+ char c = 0;
+ size_t n;
+
+ n = strspn(size_spec, "0123456789");
+ switch (size_spec[n]) {
+ default:
+ goto err_invalid;
+ case 0: case 'x': case 'X':
+ c = size_spec[n];
+ size_spec[n] = 0;
+ }
+
+ w = strtoul(size_spec, NULL, 10);
+ size_spec[n] = c;
+ if (w > 0xffff)
+ goto err_range;
+
+ if (!c) {
+ h = w;
+ } else {
+ n = strspn(size_spec += n+1, "0123456789");
+ if (size_spec[n])
+ goto err_invalid;
+
+ h = strtoul(size_spec, NULL, 10);
+ if (h > 0xffff)
+ goto err_range;
+ }
+
+ return (h << 16) | w;
+err_range:
+ fprintf(stderr, "%s: %s: %s\n", progname, size_spec, strerror(ERANGE));
+ return 0;
+err_invalid:
+ fprintf(stderr, "%s: %s: %s\n", progname, size_spec,
+ "invalid size specification");
+ return 0;
+}
+
+static unsigned long *find_icon(unsigned long size, unsigned long *ewmhicon)
+{
+ unsigned long w = size & 0xffff, h = (size >> 16) & 0xffff;
+ unsigned long i;
+
+ if (!size)
+ return NULL;
+
+ for (i = 0; i < EWMH_ICON_NELEM-2;) {
+ unsigned long icon_w = ewmhicon[i];
+ unsigned long icon_h = ewmhicon[i+1];
+
+ if (w == icon_w && h == icon_h) {
+ i += 2;
+ break;
+ }
+
+ assert(icon_w < ULONG_MAX / icon_h);
+ assert(i < ULONG_MAX - icon_w*icon_h);
+ i += 2 + icon_w*icon_h;
+ }
+
+ if (i < EWMH_ICON_NELEM)
+ return &ewmhicon[i];
+
+ fprintf(stderr, "%s: error: no %lux%lu icon found\n", progname, w, h);
+ return NULL;
+}
+