On Tuesday 19 March 2013 13:57:04 Thomas Kluyver wrote:
> On 19 March 2013 13:28, David Faure <[email protected]> wrote:
> > The other would be to write code that detects the cases where the database
> > has
> > values such that (value & mask) != value, and fixing the database to
> > specify
> > (value & mask) as value from now on. This would allow implementations to
> > avoid
> > having to mask the value at runtime, which would lead to a minor speedup
> > (and
> > to the spec being correct after all).
> > Such code would be easy to write, as part of any of the existing
> > implementations, I would think.
>
> Yes, I think that sounds reasonable, although of course implementations
> will need to support the existing data for some time, even if newer
> versions of shared-mime-info fix that.
I don't see that point. I'm talking about fixing the shared-mime-info data to
have more useful expected values, this won't break existing implementations at
all.
What I meant by "Such code" and "the existing implementations" was to add a
check in one implementation and use that to detect the weird expected values.
But you've already done that apparently, by manual inspection.
You're right though, removing the masking of the value in the implementations
cannot be done for quite some time, even if we adjust the data today.
Still, at some point this will be useful :)
> The downside is that
> update-mime-database is written in C, and as I found yesterday, I'm lousy
> at fixing C code. (Aside: this is an occasionally used script where
> performance isn't that important - would it make sense to write it in
> Python rather than C?)
Not my code, I can't comment on that. But IMHO let's not start a language
flamewar. It's there and it works.
> I've just inspected the values I have. There aren't many rules using masks
> at all. Of those that are, 5 need the mask applied, in all cases because
> they use a placeholder character where the mask has a null byte.
>
> - application/x-core, application/x-sharedlib and
> application/vnd.adobe.photoshop use spaces
> - image/bmp uses lowercase 'x'
> - application/vnd.corel-draw uses an uppercase 'X'
Ah, so this leads to more readable magic than using '\000' in the value field.
But indeed, update-mime-database could take care of sanitizing the value in
the generated output.
OK, done for int values too, which caught one more case:
<mime-type type="image/x-sigma-x3f">
<match value="0x00FF00FF" type="little32" offset="4"
mask="0xFF00FF00"/>
I wonder if it's intended, i.e. the FF in the value field mean nothing...
OK, http://www.photofo.com/downloads/x3f-raw-format.pdf says this is correct.
The goal is to catch a version number like 0x00010003, for 1.3.
And done for strings too (I'm not C/glib programmer either, I'm rather a
C++/Qt guy, so this should be reviewed by glib people) ;)
Attached is the diff (after hex-dumping) of the generated magic files.
--
David Faure, [email protected], http://www.davidfaure.fr
Working on KDE, in particular KDE Frameworks 5
--- /tmp/orig 2013-04-19 18:40:06.029954219 +0200
+++ /tmp/new 2013-04-19 18:40:04.252954246 +0200
@@ -22,7 +22,7 @@
0000150: 6b65 725d 0a3e 3630 3d00 0844 6174 6150 ker].>60=..DataP
0000160: 6c6b 720a 5b38 303a 6170 706c 6963 6174 lkr.[80:applicat
0000170: 696f 6e2f 766e 642e 636f 7265 6c2d 6472 ion/vnd.corel-dr
-0000180: 6177 5d0a 3e38 3d00 0843 4452 5876 7273 aw].>8=..CDRXvrs
+0000180: 6177 5d0a 3e38 3d00 0843 4452 0076 7273 aw].>8=..CDR.vrs
0000190: 6e26 ffff ff00 ffff ffff 0a5b 3830 3a61 n&.........[80:a
00001a0: 7070 6c69 6361 7469 6f6e 2f76 6e64 2e6b pplication/vnd.k
00001b0: 6465 2e6b 7061 7469 656e 6365 2e73 6176 de.kpatience.sav
@@ -955,7 +955,7 @@
0003ba0: 2d63 6f6d 7072 6573 735d 0a3e 303d 0002 -compress].>0=..
0003bb0: 1f9d 0a5b 3530 3a61 7070 6c69 6361 7469 ...[50:applicati
0003bc0: 6f6e 2f78 2d63 6f72 655d 0a3e 303d 0011 on/x-core].>0=..
-0003bd0: 7f45 4c46 2020 2020 2020 2020 2020 2020 .ELF
+0003bd0: 7f45 4c46 0000 0000 0000 0000 0000 0000 .ELF............
0003be0: 0426 ffff ffff 0000 0000 0000 0000 0000 .&..............
0003bf0: 0000 ff0a 3e30 3d00 047f 454c 460a 313e ....>0=...ELF.1>
0003c00: 353d 0001 010a 323e 3136 3d00 0204 000a 5=....2>16=.....
@@ -1218,8 +1218,8 @@
0004c10: 0002 0300 0a3e 303d 0004 7f45 4c46 0a31 .....>0=...ELF.1
0004c20: 3e35 3d00 0102 0a32 3e31 363d 0002 0003 >5=....2>16=....
0004c30: 0a3e 303d 0002 8301 0a31 3e32 323d 0002 .>0=.....1>22=..
-0004c40: 0020 2600 300a 3e30 3d00 117f 454c 4620 . &.0.>0=...ELF
-0004c50: 2020 2020 2020 2020 2020 2003 26ff ffff .&...
+0004c40: 0020 2600 300a 3e30 3d00 117f 454c 4600 . &.0.>0=...ELF.
+0004c50: 0000 0000 0000 0000 0000 0003 26ff ffff ............&...
0004c60: ff00 0000 0000 0000 0000 0000 00ff 0a5b ...............[
0004c70: 3530 3a61 7070 6c69 6361 7469 6f6e 2f78 50:application/x
0004c80: 2d73 6865 6c6c 7363 7269 7074 5d0a 3e31 -shellscript].>1
@@ -1401,7 +1401,7 @@
0005780: 5d0a 3e30 3d00 0458 4d46 5f0a 3e30 3d00 ].>0=..XMF_.>0=.
0005790: 0c58 4d46 5f32 2e30 3000 0000 020a 5b35 .XMF_2.00.....[5
00057a0: 303a 696d 6167 652f 626d 705d 0a3e 303d 0:image/bmp].>0=
-00057b0: 0008 424d 7878 7878 0000 26ff ff00 0000 ..BMxxxx..&.....
+00057b0: 0008 424d 0000 0000 0000 26ff ff00 0000 ..BM......&.....
00057c0: 00ff ff0a 3e30 3d00 0242 4d0a 313e 3134 ....>0=..BM.1>14
00057d0: 3d00 010c 0a31 3e31 343d 0001 400a 313e =....1>[email protected]>
00057e0: 3134 3d00 0128 0a5b 3530 3a69 6d61 6765 14=..(.[50:image
@@ -1425,7 +1425,7 @@
0005900: 3e30 3d00 0449 492a 000a 5b35 303a 696d >0=..II*..[50:im
0005910: 6167 652f 766e 642e 6164 6f62 652e 7068 age/vnd.adobe.ph
0005920: 6f74 6f73 686f 705d 0a3e 303d 000a 3842 otoshop].>0=..8B
-0005930: 5053 2020 0000 0000 26ff ffff ff00 00ff PS ....&.......
+0005930: 5053 0000 0000 0000 26ff ffff ff00 00ff PS......&.......
0005940: ffff ff0a 5b35 303a 696d 6167 652f 766e ....[50:image/vn
0005950: 642e 646a 7675 5d0a 3e30 3d00 0841 5426 d.djvu].>0=..AT&
0005960: 5446 4f52 4d0a 313e 3132 3d00 0444 4a56 TFORM.1>12=..DJV
@@ -1499,8 +1499,8 @@
0005da0: 782d 7175 6963 6b74 696d 655d 0a3e 343d x-quicktime].>4=
0005db0: 0004 6964 6174 0a5b 3530 3a69 6d61 6765 ..idat.[50:image
0005dc0: 2f78 2d73 6967 6d61 2d78 3366 5d0a 3e30 /x-sigma-x3f].>0
-0005dd0: 3d00 0446 4f56 620a 313e 343d 0004 ff00 =..FOVb.1>4=....
-0005de0: ff00 2600 ff00 ff0a 5b35 303a 696d 6167 ..&.....[50:imag
+0005dd0: 3d00 0446 4f56 620a 313e 343d 0004 0000 =..FOVb.1>4=....
+0005de0: 0000 2600 ff00 ff0a 5b35 303a 696d 6167 ..&.....[50:imag
0005df0: 652f 782d 736b 656e 6369 6c5d 0a3e 303d e/x-skencil].>0=
0005e00: 0008 2323 536b 6574 6368 0a5b 3530 3a69 ..##Sketch.[50:i
0005e10: 6d61 6765 2f78 2d73 756e 2d72 6173 7465 mage/x-sun-raste
diff --git a/update-mime-database.c b/update-mime-database.c
index bd28ce0..bdf35f6 100644
--- a/update-mime-database.c
+++ b/update-mime-database.c
@@ -1147,21 +1147,6 @@ static void parse_int_value(int bytes, const char *in, const char *in_mask,
return;
}
- for (b = 0; b < bytes; b++)
- {
- int shift = (big_endian ? (bytes - b - 1) : b) * 8;
- g_string_append_c(parsed_value, (value >> shift) & 0xff);
- }
-
- if ((bytes == 1 && (value & ~0xff)) ||
- (bytes == 2 && (value & ~0xffff)))
- {
- g_set_error(error, MIME_ERROR, 0,
- "Number out-of-range (%lx should fit in %d bytes)",
- value, bytes);
- return;
- }
-
if (in_mask)
{
int b;
@@ -1183,6 +1168,8 @@ static void parse_int_value(int bytes, const char *in, const char *in_mask,
return;
}
+ value &= mask;
+
out_mask = g_new(char, bytes);
for (b = 0; b < bytes; b++)
{
@@ -1191,6 +1178,21 @@ static void parse_int_value(int bytes, const char *in, const char *in_mask,
}
}
+ for (b = 0; b < bytes; b++)
+ {
+ int shift = (big_endian ? (bytes - b - 1) : b) * 8;
+ g_string_append_c(parsed_value, (value >> shift) & 0xff);
+ }
+
+ if ((bytes == 1 && (value & ~0xff)) ||
+ (bytes == 2 && (value & ~0xffff)))
+ {
+ g_set_error(error, MIME_ERROR, 0,
+ "Number out-of-range (%lx should fit in %d bytes)",
+ value, bytes);
+ return;
+ }
+
*parsed_mask = out_mask;
}
@@ -1275,9 +1277,14 @@ static void parse_value(const char *type, const char *in, const char *in_mask,
else if (strcmp(type, "string") == 0)
{
getstr(in, parsed_value);
- if (in_mask)
+ if (in_mask) {
+ int i;
*parsed_mask = parse_string_mask(in_mask,
parsed_value->len, error);
+ for (i = 0; i < parsed_value->len; ++i) {
+ parsed_value->str[i] &= (*parsed_mask)[i];
+ }
+ }
}
else
g_assert_not_reached();
_______________________________________________
xdg mailing list
[email protected]
http://lists.freedesktop.org/mailman/listinfo/xdg