Commit 1d7c15adf983853cf8df03dc0af411df3fa5fa07

Edward Thomson 2017-11-11T20:15:07

Merge pull request #4310 from pks-t/pks/common-parser Common parser interface

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
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
diff --git a/src/config_file.c b/src/config_file.c
index cc4e7b3..792a3de 100644
--- a/src/config_file.c
+++ b/src/config_file.c
@@ -17,6 +17,7 @@
 #include "git2/types.h"
 #include "strmap.h"
 #include "array.h"
+#include "config_parse.h"
 
 #include <ctype.h>
 #include <sys/types.h>
@@ -75,20 +76,6 @@ typedef struct git_config_file_iter {
 		 (iter) && (((tmp) = CVAR_LIST_NEXT(iter) || 1));\
 		 (iter) = (tmp))
 
-struct config_file {
-	git_oid checksum;
-	char *path;
-	git_array_t(struct config_file) includes;
-};
-
-struct reader {
-	struct config_file *file;
-	git_buf buffer;
-	char *read_ptr;
-	int line_number;
-	int eof;
-};
-
 typedef struct {
 	git_atomic refcount;
 	git_strmap *values;
@@ -107,6 +94,8 @@ typedef struct {
 	git_config_level_t level;
 	const git_repository *repo;
 
+	git_array_t(git_config_parser) readers;
+
 	bool locked;
 	git_filebuf locked_buf;
 	git_buf locked_content;
@@ -120,19 +109,13 @@ typedef struct {
 	diskfile_backend *snapshot_from;
 } diskfile_readonly_backend;
 
-static int config_read(git_strmap *values, const git_repository *repo, struct config_file *file, git_config_level_t level, int depth);
+static int config_read(git_strmap *values, const git_repository *repo, git_config_file *file, git_config_level_t level, int depth);
 static int config_write(diskfile_backend *cfg, const char *orig_key, const char *key, const regex_t *preg, const char *value);
 static char *escape_value(const char *ptr);
 
 int git_config_file__snapshot(git_config_backend **out, diskfile_backend *in);
 static int config_snapshot(git_config_backend **out, git_config_backend *in);
 
-static void set_parse_error(struct reader *reader, int col, const char *error_str)
-{
-	giterr_set(GITERR_CONFIG, "failed to parse config file: %s (in %s:%d, column %d)",
-		error_str, reader->file->path, reader->line_number, col);
-}
-
 static int config_error_readonly(void)
 {
 	giterr_set(GITERR_CONFIG, "this backend is read-only");
@@ -293,7 +276,6 @@ static int config_open(git_config_backend *cfg, git_config_level_t level, const 
 	if ((res = refcounted_strmap_alloc(&b->header.values)) < 0)
 		return res;
 
-	/* It's fine if the file doesn't exist */
 	if (!git_path_exists(b->file.path))
 		return 0;
 
@@ -307,11 +289,11 @@ static int config_open(git_config_backend *cfg, git_config_level_t level, const 
 
 static int config_is_modified(int *modified, struct config_file *file)
 {
-	struct config_file *include;
+	git_config_file *include;
 	git_buf buf = GIT_BUF_INIT;
 	git_oid hash;
 	uint32_t i;
-	int error;
+	int error = 0;
 
 	*modified = 0;
 
@@ -341,7 +323,7 @@ static int config_refresh(git_config_backend *cfg)
 {
 	diskfile_backend *b = (diskfile_backend *)cfg;
 	refcounted_strmap *values = NULL, *tmp;
-	struct config_file *include;
+	git_config_file *include;
 	int error, modified;
 	uint32_t i;
 
@@ -885,397 +867,6 @@ int git_config_file__snapshot(git_config_backend **out, diskfile_backend *in)
 	return 0;
 }
 
-static int reader_getchar_raw(struct reader *reader)
-{
-	int c;
-
-	c = *reader->read_ptr++;
-
-	/*
-	Win 32 line breaks: if we find a \r\n sequence,
-	return only the \n as a newline
-	*/
-	if (c == '\r' && *reader->read_ptr == '\n') {
-		reader->read_ptr++;
-		c = '\n';
-	}
-
-	if (c == '\n')
-		reader->line_number++;
-
-	if (c == 0) {
-		reader->eof = 1;
-		c = '\0';
-	}
-
-	return c;
-}
-
-#define SKIP_WHITESPACE (1 << 1)
-#define SKIP_COMMENTS (1 << 2)
-
-static int reader_getchar(struct reader *reader, int flags)
-{
-	const int skip_whitespace = (flags & SKIP_WHITESPACE);
-	const int skip_comments = (flags & SKIP_COMMENTS);
-	int c;
-
-	assert(reader->read_ptr);
-
-	do {
-		c = reader_getchar_raw(reader);
-	} while (c != '\n' && c != '\0' && skip_whitespace && git__isspace(c));
-
-	if (skip_comments && (c == '#' || c == ';')) {
-		do {
-			c = reader_getchar_raw(reader);
-		} while (c != '\n' && c != '\0');
-	}
-
-	return c;
-}
-
-/*
- * Read the next char, but don't move the reading pointer.
- */
-static int reader_peek(struct reader *reader, int flags)
-{
-	void *old_read_ptr;
-	int old_lineno, old_eof;
-	int ret;
-
-	assert(reader->read_ptr);
-
-	old_read_ptr = reader->read_ptr;
-	old_lineno = reader->line_number;
-	old_eof = reader->eof;
-
-	ret = reader_getchar(reader, flags);
-
-	reader->read_ptr = old_read_ptr;
-	reader->line_number = old_lineno;
-	reader->eof = old_eof;
-
-	return ret;
-}
-
-/*
- * Read and consume a line, returning it in newly-allocated memory.
- */
-static char *reader_readline(struct reader *reader, bool skip_whitespace)
-{
-	char *line = NULL;
-	char *line_src, *line_end;
-	size_t line_len, alloc_len;
-
-	line_src = reader->read_ptr;
-
-	if (skip_whitespace) {
-		/* Skip empty empty lines */
-		while (git__isspace(*line_src))
-			++line_src;
-	}
-
-	line_end = strchr(line_src, '\n');
-
-	/* no newline at EOF */
-	if (line_end == NULL)
-		line_end = strchr(line_src, 0);
-
-	line_len = line_end - line_src;
-
-	if (GIT_ADD_SIZET_OVERFLOW(&alloc_len, line_len, 1) ||
-		(line = git__malloc(alloc_len)) == NULL) {
-		return NULL;
-	}
-
-	memcpy(line, line_src, line_len);
-
-	do line[line_len] = '\0';
-	while (line_len-- > 0 && git__isspace(line[line_len]));
-
-	if (*line_end == '\n')
-		line_end++;
-
-	if (*line_end == '\0')
-		reader->eof = 1;
-
-	reader->line_number++;
-	reader->read_ptr = line_end;
-
-	return line;
-}
-
-/*
- * Consume a line, without storing it anywhere
- */
-static void reader_consume_line(struct reader *reader)
-{
-	char *line_start, *line_end;
-
-	line_start = reader->read_ptr;
-	line_end = strchr(line_start, '\n');
-	/* No newline at EOF */
-	if(line_end == NULL){
-		line_end = strchr(line_start, '\0');
-	}
-
-	if (*line_end == '\n')
-		line_end++;
-
-	if (*line_end == '\0')
-		reader->eof = 1;
-
-	reader->line_number++;
-	reader->read_ptr = line_end;
-}
-
-GIT_INLINE(int) config_keychar(int c)
-{
-	return isalnum(c) || c == '-';
-}
-
-static int parse_section_header_ext(struct reader *reader, const char *line, const char *base_name, char **section_name)
-{
-	int c, rpos;
-	char *first_quote, *last_quote;
-	git_buf buf = GIT_BUF_INIT;
-	size_t quoted_len, alloc_len, base_name_len = strlen(base_name);
-
-	/*
-	 * base_name is what came before the space. We should be at the
-	 * first quotation mark, except for now, line isn't being kept in
-	 * sync so we only really use it to calculate the length.
-	 */
-
-	first_quote = strchr(line, '"');
-	if (first_quote == NULL) {
-		set_parse_error(reader, 0, "Missing quotation marks in section header");
-		goto end_error;
-	}
-
-	last_quote = strrchr(line, '"');
-	quoted_len = last_quote - first_quote;
-
-	if (quoted_len == 0) {
-		set_parse_error(reader, 0, "Missing closing quotation mark in section header");
-		goto end_error;
-	}
-
-	GITERR_CHECK_ALLOC_ADD(&alloc_len, base_name_len, quoted_len);
-	GITERR_CHECK_ALLOC_ADD(&alloc_len, alloc_len, 2);
-
-	if (git_buf_grow(&buf, alloc_len) < 0 ||
-	    git_buf_printf(&buf, "%s.", base_name) < 0)
-		goto end_error;
-
-	rpos = 0;
-
-	line = first_quote;
-	c = line[++rpos];
-
-	/*
-	 * At the end of each iteration, whatever is stored in c will be
-	 * added to the string. In case of error, jump to out
-	 */
-	do {
-
-		switch (c) {
-		case 0:
-			set_parse_error(reader, 0, "Unexpected end-of-line in section header");
-			goto end_error;
-
-		case '"':
-			goto end_parse;
-
-		case '\\':
-			c = line[++rpos];
-
-			if (c == 0) {
-				set_parse_error(reader, rpos, "Unexpected end-of-line in section header");
-				goto end_error;
-			}
-
-		default:
-			break;
-		}
-
-		git_buf_putc(&buf, (char)c);
-		c = line[++rpos];
-	} while (line + rpos < last_quote);
-
-end_parse:
-	if (git_buf_oom(&buf))
-		goto end_error;
-
-	if (line[rpos] != '"' || line[rpos + 1] != ']') {
-		set_parse_error(reader, rpos, "Unexpected text after closing quotes");
-		git_buf_free(&buf);
-		return -1;
-	}
-
-	*section_name = git_buf_detach(&buf);
-	return 0;
-
-end_error:
-	git_buf_free(&buf);
-
-	return -1;
-}
-
-static int parse_section_header(struct reader *reader, char **section_out)
-{
-	char *name, *name_end;
-	int name_length, c, pos;
-	int result;
-	char *line;
-	size_t line_len;
-
-	line = reader_readline(reader, true);
-	if (line == NULL)
-		return -1;
-
-	/* find the end of the variable's name */
-	name_end = strrchr(line, ']');
-	if (name_end == NULL) {
-		git__free(line);
-		set_parse_error(reader, 0, "Missing ']' in section header");
-		return -1;
-	}
-
-	GITERR_CHECK_ALLOC_ADD(&line_len, (size_t)(name_end - line), 1);
-	name = git__malloc(line_len);
-	GITERR_CHECK_ALLOC(name);
-
-	name_length = 0;
-	pos = 0;
-
-	/* Make sure we were given a section header */
-	c = line[pos++];
-	assert(c == '[');
-
-	c = line[pos++];
-
-	do {
-		if (git__isspace(c)){
-			name[name_length] = '\0';
-			result = parse_section_header_ext(reader, line, name, section_out);
-			git__free(line);
-			git__free(name);
-			return result;
-		}
-
-		if (!config_keychar(c) && c != '.') {
-			set_parse_error(reader, pos, "Unexpected character in header");
-			goto fail_parse;
-		}
-
-		name[name_length++] = (char)git__tolower(c);
-
-	} while ((c = line[pos++]) != ']');
-
-	if (line[pos - 1] != ']') {
-		set_parse_error(reader, pos, "Unexpected end of file");
-		goto fail_parse;
-	}
-
-	git__free(line);
-
-	name[name_length] = 0;
-	*section_out = name;
-
-	return 0;
-
-fail_parse:
-	git__free(line);
-	git__free(name);
-	return -1;
-}
-
-static int skip_bom(struct reader *reader)
-{
-	git_bom_t bom;
-	int bom_offset = git_buf_text_detect_bom(&bom,
-		&reader->buffer, reader->read_ptr - reader->buffer.ptr);
-
-	if (bom == GIT_BOM_UTF8)
-		reader->read_ptr += bom_offset;
-
-	/* TODO: reference implementation is pretty stupid with BoM */
-
-	return 0;
-}
-
-/*
-	(* basic types *)
-	digit = "0".."9"
-	integer = digit { digit }
-	alphabet = "a".."z" + "A" .. "Z"
-
-	section_char = alphabet | "." | "-"
-	extension_char = (* any character except newline *)
-	any_char = (* any character *)
-	variable_char = "alphabet" | "-"
-
-
-	(* actual grammar *)
-	config = { section }
-
-	section = header { definition }
-
-	header = "[" section [subsection | subsection_ext] "]"
-
-	subsection = "." section
-	subsection_ext = "\"" extension "\""
-
-	section = section_char { section_char }
-	extension = extension_char { extension_char }
-
-	definition = variable_name ["=" variable_value] "\n"
-
-	variable_name = variable_char { variable_char }
-	variable_value = string | boolean | integer
-
-	string = quoted_string | plain_string
-	quoted_string = "\"" plain_string "\""
-	plain_string = { any_char }
-
-	boolean = boolean_true | boolean_false
-	boolean_true = "yes" | "1" | "true" | "on"
-	boolean_false = "no" | "0" | "false" | "off"
-*/
-
-static int strip_comments(char *line, int in_quotes)
-{
-	int quote_count = in_quotes, backslash_count = 0;
-	char *ptr;
-
-	for (ptr = line; *ptr; ++ptr) {
-		if (ptr[0] == '"' && ptr > line && ptr[-1] != '\\')
-			quote_count++;
-
-		if ((ptr[0] == ';' || ptr[0] == '#') &&
-			(quote_count % 2) == 0 &&
-			(backslash_count % 2) == 0) {
-			ptr[0] = '\0';
-			break;
-		}
-
-		if (ptr[0] == '\\')
-			backslash_count++;
-		else
-			backslash_count = 0;
-	}
-
-	/* skip any space at the end */
-	while (ptr > line && git__isspace(ptr[-1])) {
-		ptr--;
-	}
-	ptr[0] = '\0';
-
-	return quote_count;
-}
-
 static int included_path(git_buf *out, const char *dir, const char *path)
 {
 	/* From the user's home */
@@ -1285,9 +876,6 @@ static int included_path(git_buf *out, const char *dir, const char *path)
 	return git_path_join_unrooted(out, path, dir, NULL);
 }
 
-static const char *escapes = "ntb\"\\";
-static const char *escaped = "\n\t\b\"\\";
-
 /* Escape the values to write them to the file */
 static char *escape_value(const char *ptr)
 {
@@ -1305,9 +893,9 @@ static char *escape_value(const char *ptr)
 		return NULL;
 
 	while (*ptr != '\0') {
-		if ((esc = strchr(escaped, *ptr)) != NULL) {
+		if ((esc = strchr(git_config_escaped, *ptr)) != NULL) {
 			git_buf_putc(&buf, '\\');
-			git_buf_putc(&buf, escapes[esc - escaped]);
+			git_buf_putc(&buf, git_config_escapes[esc - git_config_escaped]);
 		} else {
 			git_buf_putc(&buf, *ptr);
 		}
@@ -1322,254 +910,6 @@ static char *escape_value(const char *ptr)
 	return git_buf_detach(&buf);
 }
 
-/* '\"' -> '"' etc */
-static int unescape_line(
-	char **out, bool *is_multi, const char *ptr, int quote_count)
-{
-	char *str, *fixed, *esc;
-	size_t ptr_len = strlen(ptr), alloc_len;
-
-	*is_multi = false;
-
-	if (GIT_ADD_SIZET_OVERFLOW(&alloc_len, ptr_len, 1) ||
-		(str = git__malloc(alloc_len)) == NULL) {
-		return -1;
-	}
-
-	fixed = str;
-
-	while (*ptr != '\0') {
-		if (*ptr == '"') {
-			quote_count++;
-		} else if (*ptr != '\\') {
-			*fixed++ = *ptr;
-		} else {
-			/* backslash, check the next char */
-			ptr++;
-			/* if we're at the end, it's a multiline, so keep the backslash */
-			if (*ptr == '\0') {
-				*is_multi = true;
-				goto done;
-			}
-			if ((esc = strchr(escapes, *ptr)) != NULL) {
-				*fixed++ = escaped[esc - escapes];
-			} else {
-				git__free(str);
-				giterr_set(GITERR_CONFIG, "invalid escape at %s", ptr);
-				return -1;
-			}
-		}
-		ptr++;
-	}
-
-done:
-	*fixed = '\0';
-	*out = str;
-
-	return 0;
-}
-
-static int parse_multiline_variable(struct reader *reader, git_buf *value, int in_quotes)
-{
-	char *line = NULL, *proc_line = NULL;
-	int quote_count;
-	bool multiline;
-
-	/* Check that the next line exists */
-	line = reader_readline(reader, false);
-	if (line == NULL)
-		return -1;
-
-	/* We've reached the end of the file, there is no continuation.
-	 * (this is not an error).
-	 */
-	if (line[0] == '\0') {
-		git__free(line);
-		return 0;
-	}
-
-	quote_count = strip_comments(line, !!in_quotes);
-
-	/* If it was just a comment, pretend it didn't exist */
-	if (line[0] == '\0') {
-		git__free(line);
-		return parse_multiline_variable(reader, value, quote_count);
-		/* TODO: unbounded recursion. This **could** be exploitable */
-	}
-
-	if (unescape_line(&proc_line, &multiline, line, in_quotes) < 0) {
-		git__free(line);
-		return -1;
-	}
-	/* add this line to the multiline var */
-
-	git_buf_puts(value, proc_line);
-	git__free(line);
-	git__free(proc_line);
-
-	/*
-	 * If we need to continue reading the next line, let's just
-	 * keep putting stuff in the buffer
-	 */
-	if (multiline)
-		return parse_multiline_variable(reader, value, quote_count);
-
-	return 0;
-}
-
-GIT_INLINE(bool) is_namechar(char c)
-{
-	return isalnum(c) || c == '-';
-}
-
-static int parse_name(
-	char **name, const char **value, struct reader *reader, const char *line)
-{
-	const char *name_end = line, *value_start;
-
-	*name = NULL;
-	*value = NULL;
-
-	while (*name_end && is_namechar(*name_end))
-		name_end++;
-
-	if (line == name_end) {
-		set_parse_error(reader, 0, "Invalid configuration key");
-		return -1;
-	}
-
-	value_start = name_end;
-
-	while (*value_start && git__isspace(*value_start))
-		value_start++;
-
-	if (*value_start == '=') {
-		*value = value_start + 1;
-	} else if (*value_start) {
-		set_parse_error(reader, 0, "Invalid configuration key");
-		return -1;
-	}
-
-	if ((*name = git__strndup(line, name_end - line)) == NULL)
-		return -1;
-
-	return 0;
-}
-
-static int parse_variable(struct reader *reader, char **var_name, char **var_value)
-{
-	const char *value_start = NULL;
-	char *line;
-	int quote_count;
-	bool multiline;
-
-	line = reader_readline(reader, true);
-	if (line == NULL)
-		return -1;
-
-	quote_count = strip_comments(line, 0);
-
-	/* If there is no value, boolean true is assumed */
-	*var_value = NULL;
-
-	if (parse_name(var_name, &value_start, reader, line) < 0)
-		goto on_error;
-
-	/*
-	 * Now, let's try to parse the value
-	 */
-	if (value_start != NULL) {
-		while (git__isspace(value_start[0]))
-			value_start++;
-
-		if (unescape_line(var_value, &multiline, value_start, 0) < 0)
-			goto on_error;
-
-		if (multiline) {
-			git_buf multi_value = GIT_BUF_INIT;
-			git_buf_attach(&multi_value, *var_value, 0);
-
-			if (parse_multiline_variable(reader, &multi_value, quote_count) < 0 ||
-				git_buf_oom(&multi_value)) {
-				git_buf_free(&multi_value);
-				goto on_error;
-			}
-
-			*var_value = git_buf_detach(&multi_value);
-		}
-	}
-
-	git__free(line);
-	return 0;
-
-on_error:
-	git__free(*var_name);
-	git__free(line);
-	return -1;
-}
-
-static int config_parse(
-	struct reader *reader,
-	int (*on_section)(struct reader *reader, const char *current_section, const char *line, size_t line_len, void *data),
-	int (*on_variable)(struct reader *reader, const char *current_section, char *var_name, char *var_value, const char *line, size_t line_len, void *data),
-	int (*on_comment)(struct reader *reader, const char *line, size_t line_len, void *data),
-	int (*on_eof)(struct reader *reader, const char *current_section, void *data),
-	void *data)
-{
-	char *current_section = NULL, *var_name, *var_value, *line_start;
-	char c;
-	size_t line_len;
-	int result = 0;
-
-	skip_bom(reader);
-
-	while (result == 0 && !reader->eof) {
-		line_start = reader->read_ptr;
-
-		c = reader_peek(reader, SKIP_WHITESPACE);
-
-		switch (c) {
-		case '\0': /* EOF when peeking, set EOF in the reader to exit the loop */
-			reader->eof = 1;
-			break;
-
-		case '[': /* section header, new section begins */
-			git__free(current_section);
-			current_section = NULL;
-
-			if ((result = parse_section_header(reader, &current_section)) == 0 && on_section) {
-				line_len = reader->read_ptr - line_start;
-				result = on_section(reader, current_section, line_start, line_len, data);
-			}
-			break;
-
-		case '\n': /* comment or whitespace-only */
-		case ';':
-		case '#':
-			reader_consume_line(reader);
-
-			if (on_comment) {
-				line_len = reader->read_ptr - line_start;
-				result = on_comment(reader, line_start, line_len, data);
-			}
-			break;
-
-		default: /* assume variable declaration */
-			if ((result = parse_variable(reader, &var_name, &var_value)) == 0 && on_variable) {
-				line_len = reader->read_ptr - line_start;
-				result = on_variable(reader, current_section, var_name, var_value, line_start, line_len, data);
-			}
-			break;
-		}
-	}
-
-	if (on_eof)
-		result = on_eof(reader, current_section, data);
-
-	git__free(current_section);
-	return result;
-}
-
 struct parse_data {
 	const git_repository *repo;
 	const char *file_path;
@@ -1578,7 +918,7 @@ struct parse_data {
 	int depth;
 };
 
-static int parse_include(struct reader *reader,
+static int parse_include(git_config_parser *reader,
 	struct parse_data *parse_data, const char *file)
 {
 	struct config_file *include;
@@ -1680,7 +1020,7 @@ static const struct {
 	{ "gitdir/i:", conditional_match_gitdir_i }
 };
 
-static int parse_conditional_include(struct reader *reader,
+static int parse_conditional_include(git_config_parser *reader,
 	struct parse_data *parse_data, const char *section, const char *file)
 {
 	char *condition;
@@ -1714,7 +1054,7 @@ static int parse_conditional_include(struct reader *reader,
 }
 
 static int read_on_variable(
-	struct reader *reader,
+	git_config_parser *reader,
 	const char *current_section,
 	char *var_name,
 	char *var_value,
@@ -1769,12 +1109,13 @@ static int read_on_variable(
 static int config_read(
 	git_strmap *values,
 	const git_repository *repo,
-	struct config_file *file,
+	git_config_file *file,
 	git_config_level_t level,
 	int depth)
 {
 	struct parse_data parse_data;
-	struct reader reader;
+	git_config_parser reader;
+	git_buf contents = GIT_BUF_INIT;
 	int error;
 
 	if (depth >= MAX_INCLUDE_DEPTH) {
@@ -1782,22 +1123,20 @@ static int config_read(
 		return -1;
 	}
 
-	git_buf_init(&reader.buffer, 0);
-
-	if ((error = git_futils_readbuffer(&reader.buffer, file->path)) < 0)
+	if ((error = git_futils_readbuffer(&contents, file->path)) < 0)
 		goto out;
 
-	if ((error = git_hash_buf(&file->checksum, reader.buffer.ptr, reader.buffer.size)) < 0)
+	git_parse_ctx_init(&reader.ctx, contents.ptr, contents.size);
+
+	if ((error = git_hash_buf(&file->checksum, contents.ptr, contents.size)) < 0)
 		goto out;
 
 	/* Initialize the reading position */
 	reader.file = file;
-	reader.line_number = 0;
-	reader.read_ptr = reader.buffer.ptr;
-	reader.eof = 0;
+	git_parse_ctx_init(&reader.ctx, contents.ptr, contents.size);
 
 	/* If the file is empty, there's nothing for us to do */
-	if (*reader.read_ptr == '\0')
+	if (!reader.ctx.content || *reader.ctx.content == '\0')
 		goto out;
 
 	parse_data.repo = repo;
@@ -1806,10 +1145,10 @@ static int config_read(
 	parse_data.level = level;
 	parse_data.depth = depth;
 
-	error = config_parse(&reader, NULL, read_on_variable, NULL, NULL, &parse_data);
+	error = git_config_parse(&reader, NULL, read_on_variable, NULL, NULL, &parse_data);
 
 out:
-	git_buf_free(&reader.buffer);
+	git_buf_free(&contents);
 	return error;
 }
 
@@ -1909,7 +1248,7 @@ static int write_value(struct write_data *write_data)
 }
 
 static int write_on_section(
-	struct reader *reader,
+	git_config_parser *reader,
 	const char *current_section,
 	const char *line,
 	size_t line_len,
@@ -1945,7 +1284,7 @@ static int write_on_section(
 }
 
 static int write_on_variable(
-	struct reader *reader,
+	git_config_parser *reader,
 	const char *current_section,
 	char *var_name,
 	char *var_value,
@@ -1995,7 +1334,7 @@ static int write_on_variable(
 	return write_value(write_data);
 }
 
-static int write_on_comment(struct reader *reader, const char *line, size_t line_len, void *data)
+static int write_on_comment(git_config_parser *reader, const char *line, size_t line_len, void *data)
 {
 	struct write_data *write_data;
 
@@ -2006,7 +1345,7 @@ static int write_on_comment(struct reader *reader, const char *line, size_t line
 }
 
 static int write_on_eof(
-	struct reader *reader, const char *current_section, void *data)
+	git_config_parser *reader, const char *current_section, void *data)
 {
 	struct write_data *write_data = (struct write_data *)data;
 	int result = 0;
@@ -2044,36 +1383,30 @@ static int config_write(diskfile_backend *cfg, const char *orig_key, const char 
 	int result;
 	char *orig_section, *section, *orig_name, *name, *ldot;
 	git_filebuf file = GIT_FILEBUF_INIT;
-	git_buf buf = GIT_BUF_INIT;
-	struct reader reader;
+	git_buf buf = GIT_BUF_INIT, contents = GIT_BUF_INIT;
+	git_config_parser reader;
 	struct write_data write_data;
 
 	memset(&reader, 0, sizeof(reader));
-	git_buf_init(&reader.buffer, 0);
 	reader.file = &cfg->file;
 
 	if (cfg->locked) {
-		result = git_buf_puts(&reader.buffer, git_buf_cstr(&cfg->locked_content));
+		result = git_buf_puts(&contents, git_buf_cstr(&cfg->locked_content));
 	} else {
 		/* Lock the file */
 		if ((result = git_filebuf_open(
 			     &file, cfg->file.path, GIT_FILEBUF_HASH_CONTENTS, GIT_CONFIG_FILE_MODE)) < 0) {
-			git_buf_free(&reader.buffer);
+			git_buf_free(&contents);
 			return result;
 		}
 
 		/* We need to read in our own config file */
-		result = git_futils_readbuffer(&reader.buffer, cfg->file.path);
+		result = git_futils_readbuffer(&contents, cfg->file.path);
 	}
 
 	/* Initialise the reading position */
-	if (result == GIT_ENOTFOUND) {
-		reader.read_ptr = NULL;
-		reader.eof = 1;
-		git_buf_clear(&reader.buffer);
-	} else if (result == 0) {
-		reader.read_ptr = reader.buffer.ptr;
-		reader.eof = 0;
+	if (result == 0 || result == GIT_ENOTFOUND) {
+		git_parse_ctx_init(&reader.ctx, contents.ptr, contents.size);
 	} else {
 		git_filebuf_cleanup(&file);
 		return -1; /* OS error when reading the file */
@@ -2100,7 +1433,12 @@ static int config_write(diskfile_backend *cfg, const char *orig_key, const char 
 	write_data.preg = preg;
 	write_data.value = value;
 
-	result = config_parse(&reader, write_on_section, write_on_variable, write_on_comment, write_on_eof, &write_data);
+	result = git_config_parse(&reader,
+		write_on_section,
+		write_on_variable,
+		write_on_comment,
+		write_on_eof,
+		&write_data);
 	git__free(section);
 	git__free(orig_section);
 	git_buf_free(&write_data.buffered_comment);
@@ -2122,6 +1460,7 @@ static int config_write(diskfile_backend *cfg, const char *orig_key, const char 
 
 done:
 	git_buf_free(&buf);
-	git_buf_free(&reader.buffer);
+	git_buf_free(&contents);
+	git_parse_ctx_clear(&reader.ctx);
 	return result;
 }
diff --git a/src/config_parse.c b/src/config_parse.c
new file mode 100644
index 0000000..586bba8
--- /dev/null
+++ b/src/config_parse.c
@@ -0,0 +1,519 @@
+/*
+ * Copyright (C) the libgit2 contributors. All rights reserved.
+ *
+ * This file is part of libgit2, distributed under the GNU GPL v2 with
+ * a Linking Exception. For full terms see the included COPYING file.
+ */
+
+#include "config_parse.h"
+
+#include "buf_text.h"
+
+#include <ctype.h>
+
+static void set_parse_error(git_config_parser *reader, int col, const char *error_str)
+{
+	giterr_set(GITERR_CONFIG, "failed to parse config file: %s (in %s:%"PRIuZ", column %d)",
+		error_str, reader->file->path, reader->ctx.line_num, col);
+}
+
+
+GIT_INLINE(int) config_keychar(int c)
+{
+	return isalnum(c) || c == '-';
+}
+
+static int strip_comments(char *line, int in_quotes)
+{
+	int quote_count = in_quotes, backslash_count = 0;
+	char *ptr;
+
+	for (ptr = line; *ptr; ++ptr) {
+		if (ptr[0] == '"' && ptr > line && ptr[-1] != '\\')
+			quote_count++;
+
+		if ((ptr[0] == ';' || ptr[0] == '#') &&
+			(quote_count % 2) == 0 &&
+			(backslash_count % 2) == 0) {
+			ptr[0] = '\0';
+			break;
+		}
+
+		if (ptr[0] == '\\')
+			backslash_count++;
+		else
+			backslash_count = 0;
+	}
+
+	/* skip any space at the end */
+	while (ptr > line && git__isspace(ptr[-1])) {
+		ptr--;
+	}
+	ptr[0] = '\0';
+
+	return quote_count;
+}
+
+
+static int parse_section_header_ext(git_config_parser *reader, const char *line, const char *base_name, char **section_name)
+{
+	int c, rpos;
+	char *first_quote, *last_quote;
+	git_buf buf = GIT_BUF_INIT;
+	size_t quoted_len, alloc_len, base_name_len = strlen(base_name);
+
+	/*
+	 * base_name is what came before the space. We should be at the
+	 * first quotation mark, except for now, line isn't being kept in
+	 * sync so we only really use it to calculate the length.
+	 */
+
+	first_quote = strchr(line, '"');
+	if (first_quote == NULL) {
+		set_parse_error(reader, 0, "Missing quotation marks in section header");
+		goto end_error;
+	}
+
+	last_quote = strrchr(line, '"');
+	quoted_len = last_quote - first_quote;
+
+	if (quoted_len == 0) {
+		set_parse_error(reader, 0, "Missing closing quotation mark in section header");
+		goto end_error;
+	}
+
+	GITERR_CHECK_ALLOC_ADD(&alloc_len, base_name_len, quoted_len);
+	GITERR_CHECK_ALLOC_ADD(&alloc_len, alloc_len, 2);
+
+	if (git_buf_grow(&buf, alloc_len) < 0 ||
+	    git_buf_printf(&buf, "%s.", base_name) < 0)
+		goto end_error;
+
+	rpos = 0;
+
+	line = first_quote;
+	c = line[++rpos];
+
+	/*
+	 * At the end of each iteration, whatever is stored in c will be
+	 * added to the string. In case of error, jump to out
+	 */
+	do {
+
+		switch (c) {
+		case 0:
+			set_parse_error(reader, 0, "Unexpected end-of-line in section header");
+			goto end_error;
+
+		case '"':
+			goto end_parse;
+
+		case '\\':
+			c = line[++rpos];
+
+			if (c == 0) {
+				set_parse_error(reader, rpos, "Unexpected end-of-line in section header");
+				goto end_error;
+			}
+
+		default:
+			break;
+		}
+
+		git_buf_putc(&buf, (char)c);
+		c = line[++rpos];
+	} while (line + rpos < last_quote);
+
+end_parse:
+	if (git_buf_oom(&buf))
+		goto end_error;
+
+	if (line[rpos] != '"' || line[rpos + 1] != ']') {
+		set_parse_error(reader, rpos, "Unexpected text after closing quotes");
+		git_buf_free(&buf);
+		return -1;
+	}
+
+	*section_name = git_buf_detach(&buf);
+	return 0;
+
+end_error:
+	git_buf_free(&buf);
+
+	return -1;
+}
+
+static int parse_section_header(git_config_parser *reader, char **section_out)
+{
+	char *name, *name_end;
+	int name_length, c, pos;
+	int result;
+	char *line;
+	size_t line_len;
+
+	git_parse_advance_ws(&reader->ctx);
+	line = git__strndup(reader->ctx.line, reader->ctx.line_len);
+	if (line == NULL)
+		return -1;
+
+	/* find the end of the variable's name */
+	name_end = strrchr(line, ']');
+	if (name_end == NULL) {
+		git__free(line);
+		set_parse_error(reader, 0, "Missing ']' in section header");
+		return -1;
+	}
+
+	GITERR_CHECK_ALLOC_ADD(&line_len, (size_t)(name_end - line), 1);
+	name = git__malloc(line_len);
+	GITERR_CHECK_ALLOC(name);
+
+	name_length = 0;
+	pos = 0;
+
+	/* Make sure we were given a section header */
+	c = line[pos++];
+	assert(c == '[');
+
+	c = line[pos++];
+
+	do {
+		if (git__isspace(c)){
+			name[name_length] = '\0';
+			result = parse_section_header_ext(reader, line, name, section_out);
+			git__free(line);
+			git__free(name);
+			return result;
+		}
+
+		if (!config_keychar(c) && c != '.') {
+			set_parse_error(reader, pos, "Unexpected character in header");
+			goto fail_parse;
+		}
+
+		name[name_length++] = (char)git__tolower(c);
+
+	} while ((c = line[pos++]) != ']');
+
+	if (line[pos - 1] != ']') {
+		set_parse_error(reader, pos, "Unexpected end of file");
+		goto fail_parse;
+	}
+
+	git__free(line);
+
+	name[name_length] = 0;
+	*section_out = name;
+
+	return 0;
+
+fail_parse:
+	git__free(line);
+	git__free(name);
+	return -1;
+}
+
+static int skip_bom(git_parse_ctx *parser)
+{
+	git_buf buf = GIT_BUF_INIT_CONST(parser->content, parser->content_len);
+	git_bom_t bom;
+	int bom_offset = git_buf_text_detect_bom(&bom, &buf, parser->content_len);
+
+	if (bom == GIT_BOM_UTF8)
+		git_parse_advance_chars(parser, bom_offset);
+
+	/* TODO: reference implementation is pretty stupid with BoM */
+
+	return 0;
+}
+
+/*
+	(* basic types *)
+	digit = "0".."9"
+	integer = digit { digit }
+	alphabet = "a".."z" + "A" .. "Z"
+
+	section_char = alphabet | "." | "-"
+	extension_char = (* any character except newline *)
+	any_char = (* any character *)
+	variable_char = "alphabet" | "-"
+
+
+	(* actual grammar *)
+	config = { section }
+
+	section = header { definition }
+
+	header = "[" section [subsection | subsection_ext] "]"
+
+	subsection = "." section
+	subsection_ext = "\"" extension "\""
+
+	section = section_char { section_char }
+	extension = extension_char { extension_char }
+
+	definition = variable_name ["=" variable_value] "\n"
+
+	variable_name = variable_char { variable_char }
+	variable_value = string | boolean | integer
+
+	string = quoted_string | plain_string
+	quoted_string = "\"" plain_string "\""
+	plain_string = { any_char }
+
+	boolean = boolean_true | boolean_false
+	boolean_true = "yes" | "1" | "true" | "on"
+	boolean_false = "no" | "0" | "false" | "off"
+*/
+
+/* '\"' -> '"' etc */
+static int unescape_line(
+	char **out, bool *is_multi, const char *ptr, int quote_count)
+{
+	char *str, *fixed, *esc;
+	size_t ptr_len = strlen(ptr), alloc_len;
+
+	*is_multi = false;
+
+	if (GIT_ADD_SIZET_OVERFLOW(&alloc_len, ptr_len, 1) ||
+		(str = git__malloc(alloc_len)) == NULL) {
+		return -1;
+	}
+
+	fixed = str;
+
+	while (*ptr != '\0') {
+		if (*ptr == '"') {
+			quote_count++;
+		} else if (*ptr != '\\') {
+			*fixed++ = *ptr;
+		} else {
+			/* backslash, check the next char */
+			ptr++;
+			/* if we're at the end, it's a multiline, so keep the backslash */
+			if (*ptr == '\0') {
+				*is_multi = true;
+				goto done;
+			}
+			if ((esc = strchr(git_config_escapes, *ptr)) != NULL) {
+				*fixed++ = git_config_escaped[esc - git_config_escapes];
+			} else {
+				git__free(str);
+				giterr_set(GITERR_CONFIG, "invalid escape at %s", ptr);
+				return -1;
+			}
+		}
+		ptr++;
+	}
+
+done:
+	*fixed = '\0';
+	*out = str;
+
+	return 0;
+}
+
+static int parse_multiline_variable(git_config_parser *reader, git_buf *value, int in_quotes)
+{
+	char *line = NULL, *proc_line = NULL;
+	int quote_count;
+	bool multiline;
+
+	/* Check that the next line exists */
+	git_parse_advance_line(&reader->ctx);
+	line = git__strndup(reader->ctx.line, reader->ctx.line_len);
+	if (line == NULL)
+		return -1;
+
+	/* We've reached the end of the file, there is no continuation.
+	 * (this is not an error).
+	 */
+	if (line[0] == '\0') {
+		git__free(line);
+		return 0;
+	}
+
+	quote_count = strip_comments(line, !!in_quotes);
+
+	/* If it was just a comment, pretend it didn't exist */
+	if (line[0] == '\0') {
+		git__free(line);
+		return parse_multiline_variable(reader, value, quote_count);
+		/* TODO: unbounded recursion. This **could** be exploitable */
+	}
+
+	if (unescape_line(&proc_line, &multiline, line, in_quotes) < 0) {
+		git__free(line);
+		return -1;
+	}
+	/* add this line to the multiline var */
+
+	git_buf_puts(value, proc_line);
+	git__free(line);
+	git__free(proc_line);
+
+	/*
+	 * If we need to continue reading the next line, let's just
+	 * keep putting stuff in the buffer
+	 */
+	if (multiline)
+		return parse_multiline_variable(reader, value, quote_count);
+
+	return 0;
+}
+
+GIT_INLINE(bool) is_namechar(char c)
+{
+	return isalnum(c) || c == '-';
+}
+
+static int parse_name(
+	char **name, const char **value, git_config_parser *reader, const char *line)
+{
+	const char *name_end = line, *value_start;
+
+	*name = NULL;
+	*value = NULL;
+
+	while (*name_end && is_namechar(*name_end))
+		name_end++;
+
+	if (line == name_end) {
+		set_parse_error(reader, 0, "Invalid configuration key");
+		return -1;
+	}
+
+	value_start = name_end;
+
+	while (*value_start && git__isspace(*value_start))
+		value_start++;
+
+	if (*value_start == '=') {
+		*value = value_start + 1;
+	} else if (*value_start) {
+		set_parse_error(reader, 0, "Invalid configuration key");
+		return -1;
+	}
+
+	if ((*name = git__strndup(line, name_end - line)) == NULL)
+		return -1;
+
+	return 0;
+}
+
+static int parse_variable(git_config_parser *reader, char **var_name, char **var_value)
+{
+	const char *value_start = NULL;
+	char *line;
+	int quote_count;
+	bool multiline;
+
+	git_parse_advance_ws(&reader->ctx);
+	line = git__strndup(reader->ctx.line, reader->ctx.line_len);
+	if (line == NULL)
+		return -1;
+
+	quote_count = strip_comments(line, 0);
+
+	/* If there is no value, boolean true is assumed */
+	*var_value = NULL;
+
+	if (parse_name(var_name, &value_start, reader, line) < 0)
+		goto on_error;
+
+	/*
+	 * Now, let's try to parse the value
+	 */
+	if (value_start != NULL) {
+		while (git__isspace(value_start[0]))
+			value_start++;
+
+		if (unescape_line(var_value, &multiline, value_start, 0) < 0)
+			goto on_error;
+
+		if (multiline) {
+			git_buf multi_value = GIT_BUF_INIT;
+			git_buf_attach(&multi_value, *var_value, 0);
+
+			if (parse_multiline_variable(reader, &multi_value, quote_count) < 0 ||
+				git_buf_oom(&multi_value)) {
+				git_buf_free(&multi_value);
+				goto on_error;
+			}
+
+			*var_value = git_buf_detach(&multi_value);
+		}
+	}
+
+	git__free(line);
+	return 0;
+
+on_error:
+	git__free(*var_name);
+	git__free(line);
+	return -1;
+}
+
+int git_config_parse(
+	git_config_parser *parser,
+	git_config_parser_section_cb on_section,
+	git_config_parser_variable_cb on_variable,
+	git_config_parser_comment_cb on_comment,
+	git_config_parser_eof_cb on_eof,
+	void *data)
+{
+	git_parse_ctx *ctx;
+	char *current_section = NULL, *var_name, *var_value;
+	int result = 0;
+
+	ctx = &parser->ctx;
+
+	skip_bom(ctx);
+
+	for (; ctx->remain_len > 0; git_parse_advance_line(ctx)) {
+		const char *line_start = parser->ctx.line;
+		size_t line_len = parser->ctx.line_len;
+		char c;
+
+		if (git_parse_peek(&c, ctx, GIT_PARSE_PEEK_SKIP_WHITESPACE) < 0 &&
+		    git_parse_peek(&c, ctx, 0) < 0)
+			continue;
+
+		switch (c) {
+		case '[': /* section header, new section begins */
+			git__free(current_section);
+			current_section = NULL;
+
+			if ((result = parse_section_header(parser, &current_section)) == 0 && on_section) {
+				result = on_section(parser, current_section, line_start, line_len, data);
+			}
+			break;
+
+		case '\n': /* comment or whitespace-only */
+		case ' ':
+		case '\t':
+		case ';':
+		case '#':
+			if (on_comment) {
+				result = on_comment(parser, line_start, line_len, data);
+			}
+			break;
+
+		default: /* assume variable declaration */
+			if ((result = parse_variable(parser, &var_name, &var_value)) == 0 && on_variable) {
+				result = on_variable(parser, current_section, var_name, var_value, line_start, line_len, data);
+			}
+			break;
+		}
+
+		if (result < 0)
+			goto out;
+	}
+
+	if (on_eof)
+		result = on_eof(parser, current_section, data);
+
+out:
+	git__free(current_section);
+	return result;
+}
diff --git a/src/config_parse.h b/src/config_parse.h
new file mode 100644
index 0000000..6c18639
--- /dev/null
+++ b/src/config_parse.h
@@ -0,0 +1,60 @@
+/*
+ * Copyright (C) the libgit2 contributors. All rights reserved.
+ *
+ * This file is part of libgit2, distributed under the GNU GPL v2 with
+ * a Linking Exception. For full terms see the included COPYING file.
+ */
+
+#include "common.h"
+#include "array.h"
+#include "oid.h"
+#include "parse.h"
+
+static const char *git_config_escapes = "ntb\"\\";
+static const char *git_config_escaped = "\n\t\b\"\\";
+
+typedef struct config_file {
+	git_oid checksum;
+	char *path;
+	git_array_t(struct config_file) includes;
+} git_config_file;
+
+typedef struct {
+	struct config_file *file;
+	git_parse_ctx ctx;
+} git_config_parser;
+
+typedef int (*git_config_parser_section_cb)(
+	git_config_parser *parser,
+	const char *current_section,
+	const char *line,
+	size_t line_len,
+	void *data);
+
+typedef int (*git_config_parser_variable_cb)(
+	git_config_parser *parser,
+	const char *current_section,
+	char *var_name,
+	char *var_value,
+	const char *line,
+	size_t line_len,
+	void *data);
+
+typedef int (*git_config_parser_comment_cb)(
+	git_config_parser *parser,
+	const char *line,
+	size_t line_len,
+	void *data);
+
+typedef int (*git_config_parser_eof_cb)(
+	git_config_parser *parser,
+	const char *current_section,
+	void *data);
+
+int git_config_parse(
+	git_config_parser *parser,
+	git_config_parser_section_cb on_section,
+	git_config_parser_variable_cb on_variable,
+	git_config_parser_comment_cb on_comment,
+	git_config_parser_eof_cb on_eof,
+	void *data);
diff --git a/src/diff_parse.c b/src/diff_parse.c
index 78da3b6..2838314 100644
--- a/src/diff_parse.c
+++ b/src/diff_parse.c
@@ -83,7 +83,7 @@ int git_diff_from_buffer(
 	ctx = git_patch_parse_ctx_init(content, content_len, NULL);
 	GITERR_CHECK_ALLOC(ctx);
 
-	while (ctx->remain_len) {
+	while (ctx->parse_ctx.remain_len) {
 		if ((error = git_patch_parse(&patch, ctx)) < 0)
 			break;
 
diff --git a/src/parse.c b/src/parse.c
new file mode 100644
index 0000000..6b8902c
--- /dev/null
+++ b/src/parse.c
@@ -0,0 +1,121 @@
+/*
+ * Copyright (C) the libgit2 contributors. All rights reserved.
+ *
+ * This file is part of libgit2, distributed under the GNU GPL v2 with
+ * a Linking Exception. For full terms see the included COPYING file.
+ */
+#include "parse.h"
+
+int git_parse_ctx_init(git_parse_ctx *ctx, const char *content, size_t content_len)
+{
+	if (content_len)
+		ctx->content = content;
+	else
+		ctx->content = NULL;
+
+	ctx->content_len = content_len;
+	ctx->remain = ctx->content;
+	ctx->remain_len = ctx->content_len;
+	ctx->line = ctx->remain;
+	ctx->line_len = git__linenlen(ctx->line, ctx->remain_len);
+	ctx->line_num = 1;
+
+	return 0;
+}
+
+void git_parse_ctx_clear(git_parse_ctx *ctx)
+{
+	memset(ctx, 0, sizeof(*ctx));
+}
+
+void git_parse_advance_line(git_parse_ctx *ctx)
+{
+	ctx->line += ctx->line_len;
+	ctx->remain_len -= ctx->line_len;
+	ctx->line_len = git__linenlen(ctx->line, ctx->remain_len);
+	ctx->line_num++;
+}
+
+void git_parse_advance_chars(git_parse_ctx *ctx, size_t char_cnt)
+{
+	ctx->line += char_cnt;
+	ctx->remain_len -= char_cnt;
+	ctx->line_len -= char_cnt;
+}
+
+int git_parse_advance_expected(
+	git_parse_ctx *ctx,
+	const char *expected,
+	size_t expected_len)
+{
+	if (ctx->line_len < expected_len)
+		return -1;
+
+	if (memcmp(ctx->line, expected, expected_len) != 0)
+		return -1;
+
+	git_parse_advance_chars(ctx, expected_len);
+	return 0;
+}
+
+int git_parse_advance_ws(git_parse_ctx *ctx)
+{
+	int ret = -1;
+
+	while (ctx->line_len > 0 &&
+		ctx->line[0] != '\n' &&
+		git__isspace(ctx->line[0])) {
+		ctx->line++;
+		ctx->line_len--;
+		ctx->remain_len--;
+		ret = 0;
+	}
+
+	return ret;
+}
+
+int git_parse_advance_nl(git_parse_ctx *ctx)
+{
+	if (ctx->line_len != 1 || ctx->line[0] != '\n')
+		return -1;
+
+	git_parse_advance_line(ctx);
+	return 0;
+}
+
+int git_parse_advance_digit(int64_t *out, git_parse_ctx *ctx, int base)
+{
+	const char *end;
+	int ret;
+
+	if (ctx->line_len < 1 || !git__isdigit(ctx->line[0]))
+		return -1;
+
+	if ((ret = git__strntol64(out, ctx->line, ctx->line_len, &end, base)) < 0)
+		return -1;
+
+	git_parse_advance_chars(ctx, (end - ctx->line));
+	return 0;
+}
+
+int git_parse_peek(char *out, git_parse_ctx *ctx, int flags)
+{
+	size_t remain = ctx->line_len;
+	const char *ptr = ctx->line;
+
+	while (remain) {
+		char c = *ptr;
+
+		if ((flags & GIT_PARSE_PEEK_SKIP_WHITESPACE) &&
+		    git__isspace(c)) {
+			remain--;
+			ptr++;
+			continue;
+		}
+
+		*out = c;
+		return 0;
+	}
+
+	return -1;
+}
diff --git a/src/parse.h b/src/parse.h
new file mode 100644
index 0000000..419014e
--- /dev/null
+++ b/src/parse.h
@@ -0,0 +1,56 @@
+/*
+ * Copyright (C) the libgit2 contributors. All rights reserved.
+ *
+ * This file is part of libgit2, distributed under the GNU GPL v2 with
+ * a Linking Exception. For full terms see the included COPYING file.
+ */
+#include "common.h"
+
+typedef struct {
+	/* Original content buffer */
+	const char *content;
+	size_t content_len;
+
+	/* The remaining (unparsed) buffer */
+	const char *remain;
+	size_t remain_len;
+
+	const char *line;
+	size_t line_len;
+	size_t line_num;
+} git_parse_ctx;
+
+int git_parse_ctx_init(git_parse_ctx *ctx, const char *content, size_t content_len);
+void git_parse_ctx_clear(git_parse_ctx *ctx);
+
+#define git_parse_err(...) \
+	( giterr_set(GITERR_PATCH, __VA_ARGS__), -1 )
+
+#define git_parse_ctx_contains_s(ctx, str) \
+	git_parse_ctx_contains(ctx, str, sizeof(str) - 1)
+
+GIT_INLINE(bool) git_parse_ctx_contains(
+	git_parse_ctx *ctx, const char *str, size_t len)
+{
+	return (ctx->line_len >= len && memcmp(ctx->line, str, len) == 0);
+}
+
+void git_parse_advance_line(git_parse_ctx *ctx);
+void git_parse_advance_chars(git_parse_ctx *ctx, size_t char_cnt);
+int git_parse_advance_expected(
+	git_parse_ctx *ctx,
+	const char *expected,
+	size_t expected_len);
+
+#define git_parse_advance_expected_str(ctx, str) \
+	git_parse_advance_expected(ctx, str, strlen(str))
+
+int git_parse_advance_ws(git_parse_ctx *ctx);
+int git_parse_advance_nl(git_parse_ctx *ctx);
+int git_parse_advance_digit(int64_t *out, git_parse_ctx *ctx, int base);
+
+enum GIT_PARSE_PEEK_FLAGS {
+	GIT_PARSE_PEEK_SKIP_WHITESPACE = (1 << 0)
+};
+
+int git_parse_peek(char *out, git_parse_ctx *ctx, int flags);
diff --git a/src/patch_parse.c b/src/patch_parse.c
index fad892d..fee6afa 100644
--- a/src/patch_parse.c
+++ b/src/patch_parse.c
@@ -12,9 +12,6 @@
 #include "diff_parse.h"
 #include "path.h"
 
-#define parse_err(...) \
-	( giterr_set(GITERR_PATCH, __VA_ARGS__), -1 )
-
 typedef struct {
 	git_patch base;
 
@@ -36,89 +33,21 @@ typedef struct {
 	char *old_prefix, *new_prefix;
 } git_patch_parsed;
 
-
-GIT_INLINE(bool) parse_ctx_contains(
-	git_patch_parse_ctx *ctx, const char *str, size_t len)
-{
-	return (ctx->line_len >= len && memcmp(ctx->line, str, len) == 0);
-}
-
-#define parse_ctx_contains_s(ctx, str) \
-	parse_ctx_contains(ctx, str, sizeof(str) - 1)
-
-static void parse_advance_line(git_patch_parse_ctx *ctx)
-{
-	ctx->line += ctx->line_len;
-	ctx->remain_len -= ctx->line_len;
-	ctx->line_len = git__linenlen(ctx->line, ctx->remain_len);
-	ctx->line_num++;
-}
-
-static void parse_advance_chars(git_patch_parse_ctx *ctx, size_t char_cnt)
-{
-	ctx->line += char_cnt;
-	ctx->remain_len -= char_cnt;
-	ctx->line_len -= char_cnt;
-}
-
-static int parse_advance_expected(
-	git_patch_parse_ctx *ctx,
-	const char *expected,
-	size_t expected_len)
-{
-	if (ctx->line_len < expected_len)
-		return -1;
-
-	if (memcmp(ctx->line, expected, expected_len) != 0)
-		return -1;
-
-	parse_advance_chars(ctx, expected_len);
-	return 0;
-}
-
-#define parse_advance_expected_str(ctx, str) \
-	parse_advance_expected(ctx, str, strlen(str))
-
-static int parse_advance_ws(git_patch_parse_ctx *ctx)
-{
-	int ret = -1;
-
-	while (ctx->line_len > 0 &&
-		ctx->line[0] != '\n' &&
-		git__isspace(ctx->line[0])) {
-		ctx->line++;
-		ctx->line_len--;
-		ctx->remain_len--;
-		ret = 0;
-	}
-
-	return ret;
-}
-
-static int parse_advance_nl(git_patch_parse_ctx *ctx)
-{
-	if (ctx->line_len != 1 || ctx->line[0] != '\n')
-		return -1;
-
-	parse_advance_line(ctx);
-	return 0;
-}
-
 static int header_path_len(git_patch_parse_ctx *ctx)
 {
 	bool inquote = 0;
-	bool quoted = (ctx->line_len > 0 && ctx->line[0] == '"');
+	bool quoted = git_parse_ctx_contains_s(&ctx->parse_ctx, "\"");
 	size_t len;
 
-	for (len = quoted; len < ctx->line_len; len++) {
-		if (!quoted && git__isspace(ctx->line[len]))
+	for (len = quoted; len < ctx->parse_ctx.line_len; len++) {
+		if (!quoted && git__isspace(ctx->parse_ctx.line[len]))
 			break;
-		else if (quoted && !inquote && ctx->line[len] == '"') {
+		else if (quoted && !inquote && ctx->parse_ctx.line[len] == '"') {
 			len++;
 			break;
 		}
 
-		inquote = (!inquote && ctx->line[len] == '\\');
+		inquote = (!inquote && ctx->parse_ctx.line[len] == '\\');
 	}
 
 	return len;
@@ -130,10 +59,10 @@ static int parse_header_path_buf(git_buf *path, git_patch_parse_ctx *ctx)
 
 	path_len = header_path_len(ctx);
 
-	if ((error = git_buf_put(path, ctx->line, path_len)) < 0)
+	if ((error = git_buf_put(path, ctx->parse_ctx.line, path_len)) < 0)
 		goto done;
 
-	parse_advance_chars(ctx, path_len);
+	git_parse_advance_chars(&ctx->parse_ctx, path_len);
 
 	git_buf_rtrim(path);
 
@@ -173,24 +102,17 @@ static int parse_header_git_newpath(
 
 static int parse_header_mode(uint16_t *mode, git_patch_parse_ctx *ctx)
 {
-	const char *end;
-	int32_t m;
-	int ret;
-
-	if (ctx->line_len < 1 || !git__isdigit(ctx->line[0]))
-		return parse_err("invalid file mode at line %"PRIuZ, ctx->line_num);
+	int64_t m;
 
-	if ((ret = git__strntol32(&m, ctx->line, ctx->line_len, &end, 8)) < 0)
-		return ret;
+	if ((git_parse_advance_digit(&m, &ctx->parse_ctx, 8)) < 0)
+		return git_parse_err("invalid file mode at line %"PRIuZ, ctx->parse_ctx.line_num);
 
 	if (m > UINT16_MAX)
 		return -1;
 
 	*mode = (uint16_t)m;
 
-	parse_advance_chars(ctx, (end - ctx->line));
-
-	return ret;
+	return 0;
 }
 
 static int parse_header_oid(
@@ -200,17 +122,17 @@ static int parse_header_oid(
 {
 	size_t len;
 
-	for (len = 0; len < ctx->line_len && len < GIT_OID_HEXSZ; len++) {
-		if (!git__isxdigit(ctx->line[len]))
+	for (len = 0; len < ctx->parse_ctx.line_len && len < GIT_OID_HEXSZ; len++) {
+		if (!git__isxdigit(ctx->parse_ctx.line[len]))
 			break;
 	}
 
 	if (len < GIT_OID_MINPREFIXLEN || len > GIT_OID_HEXSZ ||
-		git_oid_fromstrn(oid, ctx->line, len) < 0)
-		return parse_err("invalid hex formatted object id at line %"PRIuZ,
-			ctx->line_num);
+		git_oid_fromstrn(oid, ctx->parse_ctx.line, len) < 0)
+		return git_parse_err("invalid hex formatted object id at line %"PRIuZ,
+			ctx->parse_ctx.line_num);
 
-	parse_advance_chars(ctx, len);
+	git_parse_advance_chars(&ctx->parse_ctx, len);
 
 	*oid_len = (uint16_t)len;
 
@@ -220,17 +142,19 @@ static int parse_header_oid(
 static int parse_header_git_index(
 	git_patch_parsed *patch, git_patch_parse_ctx *ctx)
 {
+	char c;
+
 	if (parse_header_oid(&patch->base.delta->old_file.id,
 			&patch->base.delta->old_file.id_abbrev, ctx) < 0 ||
-		parse_advance_expected_str(ctx, "..") < 0 ||
+		git_parse_advance_expected_str(&ctx->parse_ctx, "..") < 0 ||
 		parse_header_oid(&patch->base.delta->new_file.id,
 			&patch->base.delta->new_file.id_abbrev, ctx) < 0)
 		return -1;
 
-	if (ctx->line_len > 0 && ctx->line[0] == ' ') {
+	if (git_parse_peek(&c, &ctx->parse_ctx, 0) == 0 && c == ' ') {
 		uint16_t mode;
 
-		parse_advance_chars(ctx, 1);
+		git_parse_advance_chars(&ctx->parse_ctx, 1);
 
 		if (parse_header_mode(&mode, ctx) < 0)
 			return -1;
@@ -329,19 +253,15 @@ static int parse_header_copyto(
 
 static int parse_header_percent(uint16_t *out, git_patch_parse_ctx *ctx)
 {
-	int32_t val;
-	const char *end;
+	int64_t val;
 
-	if (ctx->line_len < 1 || !git__isdigit(ctx->line[0]) ||
-		git__strntol32(&val, ctx->line, ctx->line_len, &end, 10) < 0)
+	if (git_parse_advance_digit(&val, &ctx->parse_ctx, 10) < 0)
 		return -1;
 
-	parse_advance_chars(ctx, (end - ctx->line));
-
-	if (parse_advance_expected_str(ctx, "%") < 0)
+	if (git_parse_advance_expected_str(&ctx->parse_ctx, "%") < 0)
 		return -1;
 
-	if (val > 100)
+	if (val < 0 || val > 100)
 		return -1;
 
 	*out = val;
@@ -352,8 +272,8 @@ static int parse_header_similarity(
 	git_patch_parsed *patch, git_patch_parse_ctx *ctx)
 {
 	if (parse_header_percent(&patch->base.delta->similarity, ctx) < 0)
-		return parse_err("invalid similarity percentage at line %"PRIuZ,
-			ctx->line_num);
+		return git_parse_err("invalid similarity percentage at line %"PRIuZ,
+			ctx->parse_ctx.line_num);
 
 	return 0;
 }
@@ -364,8 +284,8 @@ static int parse_header_dissimilarity(
 	uint16_t dissimilarity;
 
 	if (parse_header_percent(&dissimilarity, ctx) < 0)
-		return parse_err("invalid similarity percentage at line %"PRIuZ,
-			ctx->line_num);
+		return git_parse_err("invalid similarity percentage at line %"PRIuZ,
+			ctx->parse_ctx.line_num);
 
 	patch->base.delta->similarity = 100 - dissimilarity;
 
@@ -375,13 +295,13 @@ static int parse_header_dissimilarity(
 static int parse_header_start(git_patch_parsed *patch, git_patch_parse_ctx *ctx)
 {
 	if (parse_header_path(&patch->header_old_path, ctx) < 0)
-		return parse_err("corrupt old path in git diff header at line %"PRIuZ,
-			ctx->line_num);
+		return git_parse_err("corrupt old path in git diff header at line %"PRIuZ,
+			ctx->parse_ctx.line_num);
 
-	if (parse_advance_ws(ctx) < 0 ||
+	if (git_parse_advance_ws(&ctx->parse_ctx) < 0 ||
 		parse_header_path(&patch->header_new_path, ctx) < 0)
-		return parse_err("corrupt new path in git diff header at line %"PRIuZ,
-			ctx->line_num);
+		return git_parse_err("corrupt new path in git diff header at line %"PRIuZ,
+			ctx->parse_ctx.line_num);
 
 	return 0;
 }
@@ -451,10 +371,10 @@ static int parse_header_git(
 	parse_header_state state = STATE_START;
 
 	/* Parse remaining header lines */
-	for (; ctx->remain_len > 0; parse_advance_line(ctx)) {
+	for (; ctx->parse_ctx.remain_len > 0; git_parse_advance_line(&ctx->parse_ctx)) {
 		bool found = false;
 
-		if (ctx->line_len == 0 || ctx->line[ctx->line_len - 1] != '\n')
+		if (ctx->parse_ctx.line_len == 0 || ctx->parse_ctx.line[ctx->parse_ctx.line_len - 1] != '\n')
 			break;
 
 		for (i = 0; i < ARRAY_SIZE(transitions); i++) {
@@ -462,7 +382,7 @@ static int parse_header_git(
 			size_t op_len = strlen(transition->str);
 
 			if (transition->expected_state != state ||
-			    memcmp(ctx->line, transition->str, min(op_len, ctx->line_len)) != 0)
+			    git__prefixcmp(ctx->parse_ctx.line, transition->str) != 0)
 				continue;
 
 			state = transition->next_state;
@@ -471,16 +391,16 @@ static int parse_header_git(
 			if (transition->fn == NULL)
 				goto done;
 
-			parse_advance_chars(ctx, op_len);
+			git_parse_advance_chars(&ctx->parse_ctx, op_len);
 
 			if ((error = transition->fn(patch, ctx)) < 0)
 				goto done;
 
-			parse_advance_ws(ctx);
+			git_parse_advance_ws(&ctx->parse_ctx);
 
-			if (parse_advance_expected_str(ctx, "\n") < 0 ||
-			    ctx->line_len > 0) {
-				error = parse_err("trailing data at line %"PRIuZ, ctx->line_num);
+			if (git_parse_advance_expected_str(&ctx->parse_ctx, "\n") < 0 ||
+			    ctx->parse_ctx.line_len > 0) {
+				error = git_parse_err("trailing data at line %"PRIuZ, ctx->parse_ctx.line_num);
 				goto done;
 			}
 
@@ -489,14 +409,14 @@ static int parse_header_git(
 		}
 
 		if (!found) {
-			error = parse_err("invalid patch header at line %"PRIuZ,
-				ctx->line_num);
+			error = git_parse_err("invalid patch header at line %"PRIuZ,
+				ctx->parse_ctx.line_num);
 			goto done;
 		}
 	}
 
 	if (state != STATE_END) {
-		error = parse_err("unexpected header line %"PRIuZ, ctx->line_num);
+		error = git_parse_err("unexpected header line %"PRIuZ, ctx->parse_ctx.line_num);
 		goto done;
 	}
 
@@ -509,17 +429,17 @@ static int parse_number(git_off_t *out, git_patch_parse_ctx *ctx)
 	const char *end;
 	int64_t num;
 
-	if (!git__isdigit(ctx->line[0]))
+	if (!git__isdigit(ctx->parse_ctx.line[0]))
 		return -1;
 
-	if (git__strntol64(&num, ctx->line, ctx->line_len, &end, 10) < 0)
+	if (git__strntol64(&num, ctx->parse_ctx.line, ctx->parse_ctx.line_len, &end, 10) < 0)
 		return -1;
 
 	if (num < 0)
 		return -1;
 
 	*out = num;
-	parse_advance_chars(ctx, (end - ctx->line));
+	git_parse_advance_chars(&ctx->parse_ctx, (end - ctx->parse_ctx.line));
 
 	return 0;
 }
@@ -528,7 +448,7 @@ static int parse_int(int *out, git_patch_parse_ctx *ctx)
 {
 	git_off_t num;
 
-	if (parse_number(&num, ctx) < 0 || !git__is_int(num))
+	if (git_parse_advance_digit(&num, &ctx->parse_ctx, 10) < 0 || !git__is_int(num))
 		return -1;
 
 	*out = (int)num;
@@ -539,43 +459,44 @@ static int parse_hunk_header(
 	git_patch_hunk *hunk,
 	git_patch_parse_ctx *ctx)
 {
-	const char *header_start = ctx->line;
+	const char *header_start = ctx->parse_ctx.line;
+	char c;
 
 	hunk->hunk.old_lines = 1;
 	hunk->hunk.new_lines = 1;
 
-	if (parse_advance_expected_str(ctx, "@@ -") < 0 ||
+	if (git_parse_advance_expected_str(&ctx->parse_ctx, "@@ -") < 0 ||
 		parse_int(&hunk->hunk.old_start, ctx) < 0)
 		goto fail;
 
-	if (ctx->line_len > 0 && ctx->line[0] == ',') {
-		if (parse_advance_expected_str(ctx, ",") < 0 ||
+	if (git_parse_peek(&c, &ctx->parse_ctx, 0) == 0 && c == ',') {
+		if (git_parse_advance_expected_str(&ctx->parse_ctx, ",") < 0 ||
 			parse_int(&hunk->hunk.old_lines, ctx) < 0)
 			goto fail;
 	}
 
-	if (parse_advance_expected_str(ctx, " +") < 0 ||
+	if (git_parse_advance_expected_str(&ctx->parse_ctx, " +") < 0 ||
 		parse_int(&hunk->hunk.new_start, ctx) < 0)
 		goto fail;
 
-	if (ctx->line_len > 0 && ctx->line[0] == ',') {
-		if (parse_advance_expected_str(ctx, ",") < 0 ||
+	if (git_parse_peek(&c, &ctx->parse_ctx, 0) == 0 && c == ',') {
+		if (git_parse_advance_expected_str(&ctx->parse_ctx, ",") < 0 ||
 			parse_int(&hunk->hunk.new_lines, ctx) < 0)
 			goto fail;
 	}
 
-	if (parse_advance_expected_str(ctx, " @@") < 0)
+	if (git_parse_advance_expected_str(&ctx->parse_ctx, " @@") < 0)
 		goto fail;
 
-	parse_advance_line(ctx);
+	git_parse_advance_line(&ctx->parse_ctx);
 
 	if (!hunk->hunk.old_lines && !hunk->hunk.new_lines)
 		goto fail;
 
-	hunk->hunk.header_len = ctx->line - header_start;
+	hunk->hunk.header_len = ctx->parse_ctx.line - header_start;
 	if (hunk->hunk.header_len > (GIT_DIFF_HUNK_HEADER_SIZE - 1))
-		return parse_err("oversized patch hunk header at line %"PRIuZ,
-			ctx->line_num);
+		return git_parse_err("oversized patch hunk header at line %"PRIuZ,
+			ctx->parse_ctx.line_num);
 
 	memcpy(hunk->hunk.header, header_start, hunk->hunk.header_len);
 	hunk->hunk.header[hunk->hunk.header_len] = '\0';
@@ -584,7 +505,7 @@ static int parse_hunk_header(
 
 fail:
 	giterr_set(GITERR_PATCH, "invalid patch hunk header at line %"PRIuZ,
-		ctx->line_num);
+		ctx->parse_ctx.line_num);
 	return -1;
 }
 
@@ -600,21 +521,24 @@ static int parse_hunk_body(
 	int newlines = hunk->hunk.new_lines;
 
 	for (;
-		ctx->remain_len > 1 &&
+		ctx->parse_ctx.remain_len > 1 &&
 		(oldlines || newlines) &&
-		(ctx->remain_len <= 4 || memcmp(ctx->line, "@@ -", 4) != 0);
-		parse_advance_line(ctx)) {
+		!git_parse_ctx_contains_s(&ctx->parse_ctx, "@@ -");
+		git_parse_advance_line(&ctx->parse_ctx)) {
 
+		char c;
 		int origin;
 		int prefix = 1;
 
-		if (ctx->line_len == 0 || ctx->line[ctx->line_len - 1] != '\n') {
-			error = parse_err("invalid patch instruction at line %"PRIuZ,
-				ctx->line_num);
+		if (ctx->parse_ctx.line_len == 0 || ctx->parse_ctx.line[ctx->parse_ctx.line_len - 1] != '\n') {
+			error = git_parse_err("invalid patch instruction at line %"PRIuZ,
+				ctx->parse_ctx.line_num);
 			goto done;
 		}
 
-		switch (ctx->line[0]) {
+		git_parse_peek(&c, &ctx->parse_ctx, 0);
+
+		switch (c) {
 		case '\n':
 			prefix = 0;
 
@@ -635,7 +559,7 @@ static int parse_hunk_body(
 			break;
 
 		default:
-			error = parse_err("invalid patch hunk at line %"PRIuZ, ctx->line_num);
+			error = git_parse_err("invalid patch hunk at line %"PRIuZ, ctx->parse_ctx.line_num);
 			goto done;
 		}
 
@@ -644,16 +568,16 @@ static int parse_hunk_body(
 
 		memset(line, 0x0, sizeof(git_diff_line));
 
-		line->content = ctx->line + prefix;
-		line->content_len = ctx->line_len - prefix;
-		line->content_offset = ctx->content_len - ctx->remain_len;
+		line->content = ctx->parse_ctx.line + prefix;
+		line->content_len = ctx->parse_ctx.line_len - prefix;
+		line->content_offset = ctx->parse_ctx.content_len - ctx->parse_ctx.remain_len;
 		line->origin = origin;
 
 		hunk->line_count++;
 	}
 
 	if (oldlines || newlines) {
-		error = parse_err(
+		error = git_parse_err(
 			"invalid patch hunk, expected %d old lines and %d new lines",
 			hunk->hunk.old_lines, hunk->hunk.new_lines);
 		goto done;
@@ -664,19 +588,19 @@ static int parse_hunk_body(
 	 * localized.  Because `diff` optimizes for the case where you
 	 * want to apply the patch by hand.
 	 */
-	if (parse_ctx_contains_s(ctx, "\\ ") &&
+	if (git_parse_ctx_contains_s(&ctx->parse_ctx, "\\ ") &&
 		git_array_size(patch->base.lines) > 0) {
 
 		line = git_array_get(patch->base.lines, git_array_size(patch->base.lines) - 1);
 
 		if (line->content_len < 1) {
-			error = parse_err("cannot trim trailing newline of empty line");
+			error = git_parse_err("cannot trim trailing newline of empty line");
 			goto done;
 		}
 
 		line->content_len--;
 
-		parse_advance_line(ctx);
+		git_parse_advance_line(&ctx->parse_ctx);
 	}
 
 done:
@@ -689,18 +613,15 @@ static int parse_patch_header(
 {
 	int error = 0;
 
-	for (ctx->line = ctx->remain;
-		ctx->remain_len > 0;
-		parse_advance_line(ctx)) {
-
+	for (; ctx->parse_ctx.remain_len > 0; git_parse_advance_line(&ctx->parse_ctx)) {
 		/* This line is too short to be a patch header. */
-		if (ctx->line_len < 6)
+		if (ctx->parse_ctx.line_len < 6)
 			continue;
 
 		/* This might be a hunk header without a patch header, provide a
 		 * sensible error message. */
-		if (parse_ctx_contains_s(ctx, "@@ -")) {
-			size_t line_num = ctx->line_num;
+		if (git_parse_ctx_contains_s(&ctx->parse_ctx, "@@ -")) {
+			size_t line_num = ctx->parse_ctx.line_num;
 			git_patch_hunk hunk;
 
 			/* If this cannot be parsed as a hunk header, it's just leading
@@ -711,17 +632,17 @@ static int parse_patch_header(
 				continue;
 			}
 
-			error = parse_err("invalid hunk header outside patch at line %"PRIuZ,
+			error = git_parse_err("invalid hunk header outside patch at line %"PRIuZ,
 				line_num);
 			goto done;
 		}
 
 		/* This buffer is too short to contain a patch. */
-		if (ctx->remain_len < ctx->line_len + 6)
+		if (ctx->parse_ctx.remain_len < ctx->parse_ctx.line_len + 6)
 			break;
 
 		/* A proper git patch */
-		if (parse_ctx_contains_s(ctx, "diff --git ")) {
+		if (git_parse_ctx_contains_s(&ctx->parse_ctx, "diff --git ")) {
 			error = parse_header_git(patch, ctx);
 			goto done;
 		}
@@ -746,27 +667,30 @@ static int parse_patch_binary_side(
 	git_off_t len;
 	int error = 0;
 
-	if (parse_ctx_contains_s(ctx, "literal ")) {
+	if (git_parse_ctx_contains_s(&ctx->parse_ctx, "literal ")) {
 		type = GIT_DIFF_BINARY_LITERAL;
-		parse_advance_chars(ctx, 8);
-	} else if (parse_ctx_contains_s(ctx, "delta ")) {
+		git_parse_advance_chars(&ctx->parse_ctx, 8);
+	} else if (git_parse_ctx_contains_s(&ctx->parse_ctx, "delta ")) {
 		type = GIT_DIFF_BINARY_DELTA;
-		parse_advance_chars(ctx, 6);
+		git_parse_advance_chars(&ctx->parse_ctx, 6);
 	} else {
-		error = parse_err(
-			"unknown binary delta type at line %"PRIuZ, ctx->line_num);
+		error = git_parse_err(
+			"unknown binary delta type at line %"PRIuZ, ctx->parse_ctx.line_num);
 		goto done;
 	}
 
-	if (parse_number(&len, ctx) < 0 || parse_advance_nl(ctx) < 0 || len < 0) {
-		error = parse_err("invalid binary size at line %"PRIuZ, ctx->line_num);
+	if (git_parse_advance_digit(&len, &ctx->parse_ctx, 10) < 0 ||
+	    git_parse_advance_nl(&ctx->parse_ctx) < 0 || len < 0) {
+		error = git_parse_err("invalid binary size at line %"PRIuZ, ctx->parse_ctx.line_num);
 		goto done;
 	}
 
-	while (ctx->line_len) {
-		char c = ctx->line[0];
+	while (ctx->parse_ctx.line_len) {
+		char c;
 		size_t encoded_len, decoded_len = 0, decoded_orig = decoded.size;
 
+		git_parse_peek(&c, &ctx->parse_ctx, 0);
+
 		if (c == '\n')
 			break;
 		else if (c >= 'A' && c <= 'Z')
@@ -775,32 +699,32 @@ static int parse_patch_binary_side(
 			decoded_len = c - 'a' + (('z' - 'a') + 1) + 1;
 
 		if (!decoded_len) {
-			error = parse_err("invalid binary length at line %"PRIuZ, ctx->line_num);
+			error = git_parse_err("invalid binary length at line %"PRIuZ, ctx->parse_ctx.line_num);
 			goto done;
 		}
 
-		parse_advance_chars(ctx, 1);
+		git_parse_advance_chars(&ctx->parse_ctx, 1);
 
 		encoded_len = ((decoded_len / 4) + !!(decoded_len % 4)) * 5;
 
-		if (encoded_len > ctx->line_len - 1) {
-			error = parse_err("truncated binary data at line %"PRIuZ, ctx->line_num);
+		if (encoded_len > ctx->parse_ctx.line_len - 1) {
+			error = git_parse_err("truncated binary data at line %"PRIuZ, ctx->parse_ctx.line_num);
 			goto done;
 		}
 
 		if ((error = git_buf_decode_base85(
-			&decoded, ctx->line, encoded_len, decoded_len)) < 0)
+			&decoded, ctx->parse_ctx.line, encoded_len, decoded_len)) < 0)
 			goto done;
 
 		if (decoded.size - decoded_orig != decoded_len) {
-			error = parse_err("truncated binary data at line %"PRIuZ, ctx->line_num);
+			error = git_parse_err("truncated binary data at line %"PRIuZ, ctx->parse_ctx.line_num);
 			goto done;
 		}
 
-		parse_advance_chars(ctx, encoded_len);
+		git_parse_advance_chars(&ctx->parse_ctx, encoded_len);
 
-		if (parse_advance_nl(ctx) < 0) {
-			error = parse_err("trailing data at line %"PRIuZ, ctx->line_num);
+		if (git_parse_advance_nl(&ctx->parse_ctx) < 0) {
+			error = git_parse_err("trailing data at line %"PRIuZ, ctx->parse_ctx.line_num);
 			goto done;
 		}
 	}
@@ -822,27 +746,27 @@ static int parse_patch_binary(
 {
 	int error;
 
-	if (parse_advance_expected_str(ctx, "GIT binary patch") < 0 ||
-		parse_advance_nl(ctx) < 0)
-		return parse_err("corrupt git binary header at line %"PRIuZ, ctx->line_num);
+	if (git_parse_advance_expected_str(&ctx->parse_ctx, "GIT binary patch") < 0 ||
+		git_parse_advance_nl(&ctx->parse_ctx) < 0)
+		return git_parse_err("corrupt git binary header at line %"PRIuZ, ctx->parse_ctx.line_num);
 
 	/* parse old->new binary diff */
 	if ((error = parse_patch_binary_side(
 			&patch->base.binary.new_file, ctx)) < 0)
 		return error;
 
-	if (parse_advance_nl(ctx) < 0)
-		return parse_err("corrupt git binary separator at line %"PRIuZ,
-			ctx->line_num);
+	if (git_parse_advance_nl(&ctx->parse_ctx) < 0)
+		return git_parse_err("corrupt git binary separator at line %"PRIuZ,
+			ctx->parse_ctx.line_num);
 
 	/* parse new->old binary diff */
 	if ((error = parse_patch_binary_side(
 			&patch->base.binary.old_file, ctx)) < 0)
 		return error;
 
-	if (parse_advance_nl(ctx) < 0)
-		return parse_err("corrupt git binary patch separator at line %"PRIuZ,
-			ctx->line_num);
+	if (git_parse_advance_nl(&ctx->parse_ctx) < 0)
+		return git_parse_err("corrupt git binary patch separator at line %"PRIuZ,
+			ctx->parse_ctx.line_num);
 
 	patch->base.binary.contains_data = 1;
 	patch->base.delta->flags |= GIT_DIFF_FLAG_BINARY;
@@ -853,13 +777,13 @@ static int parse_patch_binary_nodata(
 	git_patch_parsed *patch,
 	git_patch_parse_ctx *ctx)
 {
-	if (parse_advance_expected_str(ctx, "Binary files ") < 0 ||
-		parse_advance_expected_str(ctx, patch->header_old_path) < 0 ||
-		parse_advance_expected_str(ctx, " and ") < 0 ||
-		parse_advance_expected_str(ctx, patch->header_new_path) < 0 ||
-		parse_advance_expected_str(ctx, " differ") < 0 ||
-		parse_advance_nl(ctx) < 0)
-		return parse_err("corrupt git binary header at line %"PRIuZ, ctx->line_num);
+	if (git_parse_advance_expected_str(&ctx->parse_ctx, "Binary files ") < 0 ||
+		git_parse_advance_expected_str(&ctx->parse_ctx, patch->header_old_path) < 0 ||
+		git_parse_advance_expected_str(&ctx->parse_ctx, " and ") < 0 ||
+		git_parse_advance_expected_str(&ctx->parse_ctx, patch->header_new_path) < 0 ||
+		git_parse_advance_expected_str(&ctx->parse_ctx, " differ") < 0 ||
+		git_parse_advance_nl(&ctx->parse_ctx) < 0)
+		return git_parse_err("corrupt git binary header at line %"PRIuZ, ctx->parse_ctx.line_num);
 
 	patch->base.binary.contains_data = 0;
 	patch->base.delta->flags |= GIT_DIFF_FLAG_BINARY;
@@ -873,7 +797,7 @@ static int parse_patch_hunks(
 	git_patch_hunk *hunk;
 	int error = 0;
 
-	while (parse_ctx_contains_s(ctx, "@@ -")) {
+	while (git_parse_ctx_contains_s(&ctx->parse_ctx, "@@ -")) {
 		hunk = git_array_alloc(patch->base.hunks);
 		GITERR_CHECK_ALLOC(hunk);
 
@@ -896,9 +820,9 @@ done:
 static int parse_patch_body(
 	git_patch_parsed *patch, git_patch_parse_ctx *ctx)
 {
-	if (parse_ctx_contains_s(ctx, "GIT binary patch"))
+	if (git_parse_ctx_contains_s(&ctx->parse_ctx, "GIT binary patch"))
 		return parse_patch_binary(patch, ctx);
-	else if (parse_ctx_contains_s(ctx, "Binary files "))
+	else if (git_parse_ctx_contains_s(&ctx->parse_ctx, "Binary files "))
 		return parse_patch_binary_nodata(patch, ctx);
 	else
 		return parse_patch_hunks(patch, ctx);
@@ -914,10 +838,10 @@ int check_header_names(
 		return 0;
 
 	if (two_null && strcmp(two, "/dev/null") != 0)
-		return parse_err("expected %s path of '/dev/null'", old_or_new);
+		return git_parse_err("expected %s path of '/dev/null'", old_or_new);
 
 	else if (!two_null && strcmp(one, two) != 0)
-		return parse_err("mismatched %s path names", old_or_new);
+		return git_parse_err("mismatched %s path names", old_or_new);
 
 	return 0;
 }
@@ -950,7 +874,7 @@ static int check_prefix(
 	}
 
 	if (remain_len || !*path)
-		return parse_err(
+		return git_parse_err(
 			"header filename does not contain %"PRIuZ" path components",
 			prefix_len);
 
@@ -969,10 +893,10 @@ static int check_filenames(git_patch_parsed *patch)
 	bool deleted = (patch->base.delta->status == GIT_DELTA_DELETED);
 
 	if (patch->old_path && !patch->new_path)
-		return parse_err("missing new path");
+		return git_parse_err("missing new path");
 
 	if (!patch->old_path && patch->new_path)
-		return parse_err("missing old path");
+		return git_parse_err("missing old path");
 
 	/* Ensure (non-renamed) paths match */
 	if (check_header_names(
@@ -1005,7 +929,7 @@ static int check_filenames(git_patch_parsed *patch)
 
 	if (!patch->base.delta->old_file.path &&
 		!patch->base.delta->new_file.path)
-		return parse_err("git diff header lacks old / new paths");
+		return git_parse_err("git diff header lacks old / new paths");
 
 	return 0;
 }
@@ -1026,7 +950,7 @@ static int check_patch(git_patch_parsed *patch)
 			!(delta->flags & GIT_DIFF_FLAG_BINARY) &&
 			delta->new_file.mode == delta->old_file.mode &&
 			git_array_size(patch->base.hunks) == 0)
-		return parse_err("patch with no hunks");
+		return git_parse_err("patch with no hunks");
 
 	if (delta->status == GIT_DELTA_ADDED) {
 		memset(&delta->old_file.id, 0x0, sizeof(git_oid));
@@ -1052,19 +976,11 @@ git_patch_parse_ctx *git_patch_parse_ctx_init(
 	if ((ctx = git__calloc(1, sizeof(git_patch_parse_ctx))) == NULL)
 		return NULL;
 
-	if (content_len) {
-		if ((ctx->content = git__malloc(content_len)) == NULL) {
-			git__free(ctx);
-			return NULL;
-		}
-
-		memcpy((char *)ctx->content, content, content_len);
+	if ((git_parse_ctx_init(&ctx->parse_ctx, content, content_len)) < 0) {
+		git__free(ctx);
+		return NULL;
 	}
 
-	ctx->content_len = content_len;
-	ctx->remain = ctx->content;
-	ctx->remain_len = ctx->content_len;
-
 	if (opts)
 		memcpy(&ctx->opts, opts, sizeof(git_patch_options));
 	else
@@ -1079,7 +995,7 @@ static void patch_parse_ctx_free(git_patch_parse_ctx *ctx)
 	if (!ctx)
 		return;
 
-	git__free((char *)ctx->content);
+	git_parse_ctx_clear(&ctx->parse_ctx);
 	git__free(ctx);
 }
 
@@ -1154,15 +1070,15 @@ int git_patch_parse(
 	patch->base.delta->status = GIT_DELTA_MODIFIED;
 	patch->base.delta->nfiles = 2;
 
-	start = ctx->remain_len;
+	start = ctx->parse_ctx.remain_len;
 
 	if ((error = parse_patch_header(patch, ctx)) < 0 ||
 		(error = parse_patch_body(patch, ctx)) < 0 ||
 		(error = check_patch(patch)) < 0)
 		goto done;
 
-	used = start - ctx->remain_len;
-	ctx->remain += used;
+	used = start - ctx->parse_ctx.remain_len;
+	ctx->parse_ctx.remain += used;
 
 	patch->base.diff_opts.old_prefix = patch->old_prefix;
 	patch->base.diff_opts.new_prefix = patch->new_prefix;
diff --git a/src/patch_parse.h b/src/patch_parse.h
index f27651e..140629d 100644
--- a/src/patch_parse.h
+++ b/src/patch_parse.h
@@ -9,24 +9,15 @@
 
 #include "common.h"
 
+#include "parse.h"
 #include "patch.h"
 
 typedef struct {
 	git_refcount rc;
 
-	/* Original content buffer */
-	const char *content;
-	size_t content_len;
-
 	git_patch_options opts;
 
-	/* The remaining (unparsed) buffer */
-	const char *remain;
-	size_t remain_len;
-
-	const char *line;
-	size_t line_len;
-	size_t line_num;
+	git_parse_ctx parse_ctx;
 } git_patch_parse_ctx;
 
 extern git_patch_parse_ctx *git_patch_parse_ctx_init(