1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
|
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
*
* Copyright (C) 2015 Richard Hughes <richard@hughsie.com>
*
* Licensed under the GNU Lesser General Public License Version 2.1
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
/**
* SECTION:as-inf
* @short_description: Helper functions for parsing .inf files
* @include: appstream-glib.h
* @stability: Stable
*
* These functions are used internally to libappstream-glib, and some may be
* useful to user-applications.
*/
#include "config.h"
#include <string.h>
#include <stdlib.h>
#include "as-cleanup.h"
#include "as-inf.h"
/**
* as_inf_error_quark:
*
* Return value: An error quark.
*
* Since: 0.3.7
**/
GQuark
as_inf_error_quark (void)
{
static GQuark quark = 0;
if (!quark)
quark = g_quark_from_static_string ("AsInfError");
return quark;
}
typedef struct {
GKeyFile *keyfile;
GHashTable *dict;
gboolean last_line_continuation;
gboolean last_line_continuation_ignore;
gboolean require_2nd_pass;
gchar *group;
gchar *last_key;
gchar *comment;
gchar *comment_group;
guint nokey_idx;
AsInfLoadFlags flags;
} AsInfHelper;
/**
* as_inf_make_case_insensitive:
*/
static gchar *
as_inf_make_case_insensitive (AsInfHelper *helper, const gchar *text)
{
if (helper->flags & AS_INF_LOAD_FLAG_CASE_INSENSITIVE)
return g_ascii_strdown (text, -1);
return g_strdup (text);
}
/**
* as_inf_string_isdigits:
*/
static gboolean
as_inf_string_isdigits (const gchar *str)
{
guint i;
for (i = 0; str[i] != '\0'; i++) {
if (!g_ascii_isdigit (str[i]))
return FALSE;
}
return TRUE;
}
/**
* as_inf_replace_variable:
*
* Replaces any instance of %val% with the 'val' key value found in @dict.
*/
static gchar *
as_inf_replace_variable (AsInfHelper *helper, const gchar *line, GError **error)
{
GString *new;
const gchar *tmp;
guint i;
_cleanup_strv_free_ gchar **split = NULL;
/* split up into sections of the delimiter */
new = g_string_sized_new (strlen (line));
split = g_strsplit (line, "%", -1);
for (i = 0; split[i] != NULL; i++) {
_cleanup_free_ gchar *lower = NULL;
/* the text between the substitutions */
if (i % 2 == 0) {
g_strdelimit (split[i], "\\", '/');
g_string_append (new, split[i]);
continue;
}
/* a Dirid */
if (as_inf_string_isdigits (split[i])) {
g_string_append (new, "/tmp");
continue;
}
/* replace or ignore things not (yet) in the dictionary */
lower = as_inf_make_case_insensitive (helper, split[i]);
tmp = g_hash_table_lookup (helper->dict, lower);
if (tmp == NULL) {
if (helper->flags & AS_INF_LOAD_FLAG_STRICT) {
g_set_error (error,
AS_INF_ERROR,
AS_INF_ERROR_FAILED,
"No replacement for '%s' in [Strings]",
lower);
g_string_free (new, TRUE);
return NULL;
}
g_string_append_printf (new, "%%%s%%", split[i]);
continue;
}
g_string_append (new, tmp);
}
return g_string_free (new, FALSE);
}
/**
* as_inf_get_dict:
*
* Puts all the strings in [Strings] into a hash table.
*/
static GHashTable *
as_inf_get_dict (AsInfHelper *helper, GError **error)
{
GHashTable *dict = NULL;
gchar *val;
guint i;
_cleanup_free_ gchar *lower = NULL;
_cleanup_hashtable_unref_ GHashTable *dict_tmp = NULL;
_cleanup_strv_free_ gchar **keys = NULL;
dict_tmp = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
lower = as_inf_make_case_insensitive (helper, "Strings");
keys = g_key_file_get_keys (helper->keyfile, lower, NULL, NULL);
for (i = 0; keys != NULL && keys[i] != NULL; i++) {
val = g_key_file_get_string (helper->keyfile, lower, keys[i], error);
if (val == NULL)
goto out;
g_hash_table_insert (dict_tmp, g_strdup (keys[i]), val);
}
dict = g_hash_table_ref (dict_tmp);
out:
return dict;
}
/**
* as_inf_replace_variables:
*
* Replaces any key file value with it's substitution from [Strings]
*/
static gboolean
as_inf_replace_variables (AsInfHelper *helper, GError **error)
{
guint i;
guint j;
_cleanup_strv_free_ gchar **groups = NULL;
groups = g_key_file_get_groups (helper->keyfile, NULL);
for (i = 0; groups[i] != NULL; i++) {
_cleanup_strv_free_ gchar **keys = NULL;
/* ignore the source */
if (g_strcmp0 (groups[i], "Strings") == 0)
continue;
/* fix up keys */
keys = g_key_file_get_keys (helper->keyfile, groups[i], NULL, NULL);
if (keys == NULL)
continue;
for (j = 0; keys[j] != NULL; j++) {
_cleanup_free_ gchar *data_old = NULL;
_cleanup_free_ gchar *data_new = NULL;
/* get the old data for this [group] key */
data_old = g_key_file_get_string (helper->keyfile,
groups[i],
keys[j],
NULL);
if (data_old == NULL || data_old[0] == '\0')
continue;
/* optimise a little */
if (g_strstr_len (data_old, -1, "%") == NULL)
continue;
/* do string replacement */
data_new = as_inf_replace_variable (helper,
data_old,
error);
if (data_new == NULL)
return FALSE;
if (g_strcmp0 (data_old, data_new) == 0)
continue;
g_key_file_set_string (helper->keyfile,
groups[i],
keys[j],
data_new);
}
}
return TRUE;
}
/**
* as_inf_set_last_key:
*/
static void
as_inf_set_last_key (AsInfHelper *helper, const gchar *key)
{
/* same value */
if (key == helper->last_key)
return;
g_free (helper->last_key);
helper->last_key = g_strdup (key);
}
/**
* as_inf_set_comment:
*/
static void
as_inf_set_comment (AsInfHelper *helper, const gchar *comment)
{
/* same value */
if (comment == helper->comment)
return;
g_free (helper->comment);
helper->comment = g_strdup (comment);
if (helper->comment != NULL)
g_strchug (helper->comment);
}
/**
* as_inf_strcheckchars:
*/
static gboolean
as_inf_strcheckchars (const gchar *str, const gchar *chars)
{
guint i;
guint j;
for (i = 0; str[i] != '\0'; i++) {
for (j = 0; chars[j] != '\0'; j++) {
if (str[i] == chars[j])
return TRUE;
}
}
return FALSE;
}
/**
* as_inf_strip_value:
*
* Strips off any comments and quotes from the .inf key value.
*/
static gboolean
as_inf_strip_value (AsInfHelper *helper, gchar *value, GError **error)
{
gchar *comment;
gchar *last_quote;
/* trivial case; no quotes */
if (value[0] != '"') {
comment = g_strrstr (value + 1, ";");
if (comment != NULL) {
*comment = '\0';
if (helper->comment == NULL)
as_inf_set_comment (helper, comment + 1);
g_strchomp (value + 1);
}
return TRUE;
}
/* is there only one quote? */
last_quote = g_strrstr (value, "\"");
if (last_quote == value) {
if (helper->flags & AS_INF_LOAD_FLAG_STRICT) {
g_set_error (error,
AS_INF_ERROR,
AS_INF_ERROR_FAILED,
"mismatched quotes %s", value);
return FALSE;
}
last_quote = NULL;
}
/* shift the string down */
memmove (value, value + 1, strlen (value));
/* there's a comment past the last quote */
comment = g_strrstr (value, ";");
if (comment != NULL && comment > last_quote) {
*comment = '\0';
if (helper->comment == NULL)
as_inf_set_comment (helper, comment + 1);
g_strchomp (value);
}
/* remove the last quote */
if (last_quote != NULL)
*(last_quote-1) = '\0';
return TRUE;
}
/**
* as_inf_set_group:
*/
static gboolean
as_inf_set_group (AsInfHelper *helper, const gchar *group, GError **error)
{
/* same value */
if (group == helper->group)
return TRUE;
/* maximum permitted length is 255 chars */
if (strlen (group) > 255) {
if (helper->flags & AS_INF_LOAD_FLAG_STRICT) {
g_set_error (error,
AS_INF_ERROR,
AS_INF_ERROR_FAILED,
"section name is too long: %s",
group);
return FALSE;
}
}
/* save new value */
g_free (helper->group);
helper->group = as_inf_make_case_insensitive (helper, group);
/* section names can contain double quotes */
if (!as_inf_strip_value (helper, helper->group, error))
return FALSE;
/* does this section name have any banned chars */
if (helper->flags & AS_INF_LOAD_FLAG_STRICT) {
if (g_str_has_prefix (helper->group, " ")) {
g_set_error (error,
AS_INF_ERROR,
AS_INF_ERROR_FAILED,
"section name '%s' has leading spaces",
helper->group);
return FALSE;
}
if (g_str_has_suffix (helper->group, " ")) {
g_set_error (error,
AS_INF_ERROR,
AS_INF_ERROR_FAILED,
"section name '%s' has trailing spaces",
helper->group);
return FALSE;
}
if (as_inf_strcheckchars (helper->group, "\t\n\r;\"")) {
g_set_error (error,
AS_INF_ERROR,
AS_INF_ERROR_FAILED,
"section name '%s' has invalid chars",
helper->group);
return FALSE;
}
} else {
g_strstrip (helper->group);
g_strdelimit (helper->group, "\t\n\r;\"", '_');
}
/* start with new values */
helper->last_line_continuation_ignore = FALSE;
helper->nokey_idx = 0;
/* no longer valid */
as_inf_set_last_key (helper, NULL);
/* FIXME: refusing to add a comment to a group that does not exist
* is a GLib bug! */
if (helper->comment != NULL) {
g_key_file_set_comment (helper->keyfile,
helper->group,
NULL,
helper->comment,
NULL);
as_inf_set_comment (helper, NULL);
}
return TRUE;
}
/**
* as_inf_set_key:
*/
static void
as_inf_set_key (AsInfHelper *helper, const gchar *key, const gchar *value)
{
helper->last_line_continuation_ignore = FALSE;
g_key_file_set_string (helper->keyfile, helper->group, key, value);
if (helper->comment != NULL) {
g_key_file_set_comment (helper->keyfile,
helper->group,
key,
helper->comment,
NULL);
as_inf_set_comment (helper, NULL);
}
as_inf_set_last_key (helper, key);
}
/**
* as_inf_convert_key:
*/
static void
as_inf_convert_key (gchar *key)
{
guint i;
for (i = 0; key[i] != '\0'; i++) {
if (g_ascii_isalnum (key[i]))
continue;
if (key[i] == '.' || key[i] == '%')
continue;
key[i] = '_';
}
}
/**
* as_inf_repair_utf8:
*/
static gboolean
as_inf_repair_utf8 (AsInfHelper *helper, gchar *line, GError **error)
{
guint i;
guint8 val;
/* Microsoft keeps using 0x99 as UTF-8 (R), and it's not valid */
for (i = 0; line[i] != '\0'; i++) {
val = (guint8) line[i];
if (val == 0x99) {
if (helper->flags & AS_INF_LOAD_FLAG_STRICT) {
g_set_error (error,
AS_INF_ERROR,
AS_INF_ERROR_FAILED,
"Invalid UTF-8: 0x%02x", val);
return FALSE;
}
line[i] = '?';
continue;
}
}
return TRUE;
}
/**
* as_inf_parse_line:
*/
static gboolean
as_inf_parse_line (AsInfHelper *helper, gchar *line, GError **error)
{
gchar *kvsplit;
gchar *comment;
gchar *tmp;
guint len;
gboolean ret = TRUE;
gboolean continuation = FALSE;
_cleanup_free_ gchar *key = NULL;
/* line too long */
if ((helper->flags & AS_INF_LOAD_FLAG_STRICT) > 0 &&
strlen (line) > 4096) {
ret = FALSE;
g_set_error (error,
AS_INF_ERROR,
AS_INF_ERROR_FAILED,
"line too long: %s", line);
goto out;
}
/* fix up invalid UTF-8 */
if (!as_inf_repair_utf8 (helper, line, error)) {
ret = FALSE;
goto out;
}
/* remove leading and trailing whitespace */
g_strstrip (line);
if (line[0] == '\0')
goto out;
/* remove comments */
if (line[0] == ';') {
as_inf_set_comment (helper, line + 1);
goto out;
}
/* ignore pragmas */
if (g_str_has_prefix (line, "#pragma"))
goto out;
if (g_str_has_prefix (line, "#define"))
goto out;
/* [group] */
if (line[0] == '[') {
/* remove comments */
comment = g_strstr_len (line, -1, ";");
if (comment != NULL) {
*comment = '\0';
as_inf_set_comment (helper, comment + 1);
g_strchomp (line);
}
/* find group end */
tmp = g_strrstr (line, "]");
if (tmp == NULL) {
ret = FALSE;
g_set_error (error,
AS_INF_ERROR,
AS_INF_ERROR_FAILED,
"[ but no ] : '%s'", line);
goto out;
}
*tmp = '\0';
ret = as_inf_set_group (helper, line + 1, error);
goto out;
}
/* last char continuation */
len = strlen (line);
if (line[len-1] == '\\') {
line[len-1] = '\0';
continuation = TRUE;
}
/* is a multiline key value */
if (line[len-1] == '|') {
line[len-1] = '\0';
continuation = TRUE;
}
/* if the key-value split is before the first quote */
kvsplit = g_strstr_len (line, -1, "=");
tmp = g_strstr_len (line, -1, "\"");
if ((tmp == NULL && kvsplit != NULL) ||
(kvsplit != NULL && kvsplit < tmp)) {
_cleanup_free_ gchar *key_new = NULL;
/* key=value before [group] */
if (helper->group == NULL) {
ret = FALSE;
g_set_error (error,
AS_INF_ERROR,
AS_INF_ERROR_FAILED,
"key not in group: '%s'", line);
goto out;
}
*kvsplit = '\0';
key = as_inf_make_case_insensitive (helper, line);
g_strchomp (key);
/* remove invalid chars as GKeyFile is more strict than .ini */
as_inf_convert_key (key);
/* convert key names with variables */
if (key[0] == '%') {
_cleanup_free_ gchar *key_tmp = NULL;
if (helper->dict == NULL) {
helper->require_2nd_pass = TRUE;
helper->last_line_continuation_ignore = TRUE;
goto out;
}
key_tmp = as_inf_replace_variable (helper, key, error);
if (key_tmp == NULL)
return FALSE;
if (key_tmp[0] == '%') {
key_new = g_strdup (key + 1);
g_strdelimit (key_new, "%", '\0');
g_debug ("No replacement for '%s' in [Strings] "
"using '%s'", key, key_new);
goto out;
}
/* this has to be lowercase */
key_new = as_inf_make_case_insensitive (helper, key_tmp);
as_inf_convert_key (key_new);
} else {
key_new = g_strdup (key);
}
/* remove leading and trailing quote */
g_strchug (kvsplit + 1);
if (!as_inf_strip_value (helper, kvsplit + 1, error)) {
ret = FALSE;
goto out;
}
/* add to keyfile */
as_inf_set_key (helper, key_new, kvsplit + 1);
goto out;
}
/* last_line_continuation from the last line */
if (helper->last_line_continuation) {
_cleanup_free_ gchar *old = NULL;
_cleanup_free_ gchar *new = NULL;
/* this is the 1st pass, and we have no key */
if (helper->last_line_continuation_ignore)
goto out;
/* eek, no key at all */
if (helper->last_key == NULL) {
ret = FALSE;
g_set_error (error,
AS_INF_ERROR,
AS_INF_ERROR_FAILED,
"continued last but no key for %s", line);
goto out;
}
/* get the old value of this */
old = g_key_file_get_string (helper->keyfile,
helper->group,
helper->last_key,
NULL);
if (old == NULL) {
ret = FALSE;
g_set_error (error,
AS_INF_ERROR,
AS_INF_ERROR_FAILED,
"can't continue [%s] '%s': %s",
helper->group, helper->last_key, line);
goto out;
}
/* remove leading and trailing quote */
if (!as_inf_strip_value (helper, line, error)) {
ret = FALSE;
goto out;
}
/* re-set key with new value */
new = g_strdup_printf ("%s%s", old, line);
as_inf_set_key (helper, helper->last_key, new);
goto out;
}
/* there was left-over data but with no group defined */
if (helper->group == NULL) {
ret = FALSE;
g_set_error (error,
AS_INF_ERROR,
AS_INF_ERROR_FAILED,
"fake key %s without [group]", line);
goto out;
}
/* remove leading and trailing quote */
if (!as_inf_strip_value (helper, line, error)) {
ret = FALSE;
goto out;
}
/* convert registry entries. e.g.
* "HKR,,FirmwareFilename,,firmware.bin"
* -> "HKR_FirmwareFilename"="firmware.bin"
* "HKR,,FirmwareVersion,%REG_DWORD%,0x0000000"
* -> "HKR_FirmwareVersion_0x00010001"="0x0000000" */
if (g_strcmp0 (helper->group, "Firmware_AddReg") == 0 &&
g_str_has_prefix (line, "HK")) {
guint i;
_cleanup_strv_free_ gchar **reg_split = NULL;
_cleanup_string_free_ GString *str = NULL;
str = g_string_new ("");
reg_split = g_strsplit (line, ",", -1);
for (i = 0; reg_split[i+1] != NULL; i++) {
g_strstrip (reg_split[i]);
if (reg_split[i][0] == '\0')
continue;
g_string_append_printf (str, "%s_", reg_split[i]);
}
if (str->len > 0) {
_cleanup_free_ gchar *key_tmp = NULL;
/* remove trailing '_' */
g_string_truncate (str, str->len - 1);
/* remove leading and trailing quote */
g_strchug (reg_split[i]);
if (!as_inf_strip_value (helper, reg_split[i], error)) {
ret = FALSE;
goto out;
}
if (helper->dict == NULL) {
helper->require_2nd_pass = TRUE;
helper->last_line_continuation_ignore = TRUE;
goto out;
}
key_tmp = as_inf_replace_variable (helper, str->str, error);
if (key_tmp == NULL)
return FALSE;
as_inf_set_key (helper, key_tmp, reg_split[i]);
goto out;
}
}
/* add fake key */
key = g_strdup_printf ("value%03i", helper->nokey_idx++);
as_inf_set_key (helper, key, line);
out:
helper->last_line_continuation = continuation;
return ret;
}
/**
* as_inf_helper_new:
*/
static AsInfHelper *
as_inf_helper_new (void)
{
AsInfHelper *helper;
helper = g_new0 (AsInfHelper, 1);
return helper;
}
/**
* as_inf_helper_free:
*/
static void
as_inf_helper_free (AsInfHelper *helper)
{
if (helper->dict != NULL)
g_hash_table_unref (helper->dict);
g_key_file_unref (helper->keyfile);
g_free (helper->comment);
g_free (helper->group);
g_free (helper->last_key);
g_free (helper);
}
/**
* as_inf_load_data:
* @keyfile: a #GKeyFile
* @data: the .inf file date to parse
* @flags: #AsInfLoadFlags, e.g. %AS_INF_LOAD_FLAG_NONE
* @error: A #GError or %NULL
*
* Repairs .inf file data and opens it as a keyfile.
*
* Important: The group and keynames are all forced to lower case as INF files
* are specified as case insensitve and GKeyFile *is* case sensitive.
* Any backslashes or spaces in the key name are also converted to '_'.
*
* Returns: %TRUE for success
*
* Since: 0.3.5
*/
gboolean
as_inf_load_data (GKeyFile *keyfile,
const gchar *data,
AsInfLoadFlags flags,
GError **error)
{
AsInfHelper *helper;
gboolean ret = TRUE;
guint i;
_cleanup_strv_free_ gchar **lines = NULL;
/* initialize helper */
helper = as_inf_helper_new ();
helper->flags = flags;
helper->keyfile = g_key_file_ref (keyfile);
/* verify each line, and make sane */
lines = g_strsplit (data, "\n", -1);
for (i = 0; lines[i] != NULL; i++) {
if (!as_inf_parse_line (helper, lines[i], error)) {
g_prefix_error (error,
"Failed to parse line %i: ",
i + 1);
ret = FALSE;
goto out;
}
}
/* process every key in each group for strings we can substitute */
helper->dict = as_inf_get_dict (helper, error);
if (helper->dict == NULL) {
ret = FALSE;
goto out;
}
/* lets do this all over again */
if (helper->require_2nd_pass) {
_cleanup_strv_free_ gchar **lines2 = NULL;
lines2 = g_strsplit (data, "\n", -1);
for (i = 0; lines2[i] != NULL; i++) {
if (!as_inf_parse_line (helper, lines2[i], error)) {
g_prefix_error (error,
"Failed to parse line %i: ",
i + 1);
ret = FALSE;
goto out;
}
}
}
/* replace key values */
if (!as_inf_replace_variables (helper, error)) {
ret = FALSE;
goto out;
}
out:
as_inf_helper_free (helper);
return ret;
}
typedef struct {
const gchar *id;
const gchar *name;
guint id_length;
} AsInfBOM;
/**
* as_inf_load_file:
* @keyfile: a #GKeyFile
* @filename: the .inf file to open
* @flags: #AsInfLoadFlags, e.g. %AS_INF_LOAD_FLAG_NONE
* @error: A #GError or %NULL
*
* Repairs an .inf file and opens it as a keyfile.
*
* Returns: %TRUE for success
*
* Since: 0.3.5
*/
gboolean
as_inf_load_file (GKeyFile *keyfile,
const gchar *filename,
AsInfLoadFlags flags,
GError **error)
{
const gchar *data_no_bom;
gsize len;
guint i;
_cleanup_free_ gchar *data = NULL;
AsInfBOM boms[] = { { "\x00\x00\xfe\xff", "UTF-32BE", 4 },
{ "\xff\xfe\x00\x00", "UTF-32LE", 4 },
{ "\xfe\xff", "UTF-16BE", 2 },
{ "\xff\xfe", "UTF-16LE", 2 },
{ "\xef\xbb\xbf", "UTF-8", 3 },
{ NULL, NULL, 0 } };
if (!g_file_get_contents (filename, &data, &len, error))
return FALSE;
/* detect BOM */
data_no_bom = data;
for (i = 0; boms[i].id != NULL; i++) {
/* BOM matches */
if (len < boms[i].id_length)
continue;
if (memcmp (data, boms[i].id, boms[i].id_length) != 0)
continue;
/* ignore UTF-8 BOM */
if (g_strcmp0 (boms[i].name, "UTF-8") == 0) {
data_no_bom += boms[i].id_length;
break;
}
g_set_error (error,
AS_INF_ERROR,
AS_INF_ERROR_INVALID_TYPE,
"File is encoded with %s and not supported",
boms[i].name);
return FALSE;
}
/* load data stripped of BOM */
return as_inf_load_data (keyfile, data_no_bom, flags, error);
}
/**
* as_inf_get_driver_version:
* @keyfile: a #GKeyFile
* @timestamp: the returned driverver timestamp, or %NULL
* @error: A #GError or %NULL
*
* Parses the DriverVer string into a recognisable version and timestamp;
*
* Returns: the version string, or %NULL for error.
*
* Since: 0.3.5
*/
gchar *
as_inf_get_driver_version (GKeyFile *keyfile, guint64 *timestamp, GError **error)
{
_cleanup_date_time_unref_ GDateTime *dt = NULL;
_cleanup_strv_free_ gchar **split = NULL;
_cleanup_strv_free_ gchar **dv_split = NULL;
_cleanup_free_ gchar *driver_ver = NULL;
/* get the release date and the version in case there's no metainfo */
driver_ver = g_key_file_get_string (keyfile, "Version", "DriverVer", NULL);
if (driver_ver == NULL) {
g_set_error_literal (error,
AS_INF_ERROR,
AS_INF_ERROR_NOT_FOUND,
"DriverVer is missing");
return FALSE;
}
/* split into driver date and version */
dv_split = g_strsplit (driver_ver, ",", -1);
if (g_strv_length (dv_split) != 2) {
g_set_error (error,
AS_INF_ERROR,
AS_INF_ERROR_INVALID_TYPE,
"DriverVer is invalid: %s", driver_ver);
return NULL;
}
/* split up into MM/DD/YYYY, because America */
if (timestamp != NULL) {
split = g_strsplit (dv_split[0], "/", -1);
if (g_strv_length (split) != 3) {
g_set_error (error,
AS_INF_ERROR,
AS_INF_ERROR_INVALID_TYPE,
"DriverVer date invalid: %s",
dv_split[0]);
return NULL;
}
dt = g_date_time_new_utc (atoi (split[2]),
atoi (split[0]),
atoi (split[1]),
0, 0, 0);
if (dt == NULL) {
g_set_error (error,
AS_INF_ERROR,
AS_INF_ERROR_INVALID_TYPE,
"DriverVer date invalid: %s",
dv_split[0]);
return NULL;
}
*timestamp = g_date_time_to_unix (dt);
}
return g_strdup (dv_split[1]);
}
|