forked from twaugh/patchutils
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpatch_scanner.c
More file actions
2106 lines (1836 loc) · 78.9 KB
/
patch_scanner.c
File metadata and controls
2106 lines (1836 loc) · 78.9 KB
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
/*
* patch_scanner.c - patch parsing implementation
* Copyright (C) 2025 Tim Waugh <twaugh@redhat.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <ctype.h>
#include <limits.h>
#include <stdint.h>
#include "patch_scanner.h"
#include "util.h"
/* Maximum context buffer size (lines) to prevent excessive memory usage */
#define MAX_CONTEXT_BUFFER_SIZE 65536
/* Maximum number of temporary strings to prevent excessive memory usage */
#define MAX_TEMP_STRINGS 16384
/* Maximum line length to prevent integer overflow */
#define MAX_LINE_LENGTH (1024 * 1024)
/* Forward declarations for header parsing functions */
static void scanner_parse_git_diff_line(patch_scanner_t *scanner, const char *line);
static void scanner_parse_old_file_line(patch_scanner_t *scanner, const char *line);
static void scanner_parse_new_file_line(patch_scanner_t *scanner, const char *line);
static void scanner_parse_index_line(patch_scanner_t *scanner, const char *line);
static void scanner_parse_mode_line(patch_scanner_t *scanner, const char *line, int *mode_field);
static void scanner_parse_similarity_line(patch_scanner_t *scanner, const char *line);
static void scanner_parse_dissimilarity_line(patch_scanner_t *scanner, const char *line);
static void scanner_determine_git_diff_type(patch_scanner_t *scanner);
/* Helper functions for common parsing patterns */
static char *scanner_extract_filename(const char *line, int prefix_len);
static const char *scanner_find_timestamp_start(const char *filename);
static void scanner_parse_index_percentage(const char *line, const char *prefix, int *target_field);
static void scanner_parse_filename_field(const char *line, int prefix_len, char **target_field);
/* Forward declarations for header order validation functions */
static int scanner_validate_git_header_order(patch_scanner_t *scanner);
static int scanner_validate_context_header_order(patch_scanner_t *scanner);
static int scanner_validate_unified_header_order(patch_scanner_t *scanner);
static int scanner_is_git_extended_header(const char *line);
/* Scanner internal state */
enum scanner_state {
STATE_SEEKING_PATCH, /* Looking for start of patch */
STATE_ACCUMULATING_HEADERS, /* Collecting potential headers */
STATE_IN_PATCH, /* Processing patch content */
STATE_IN_HUNK, /* Processing hunk lines */
STATE_BINARY_READY, /* Ready to emit binary content */
STATE_ERROR /* Error state */
};
/* Internal scanner structure */
struct patch_scanner {
FILE *file; /* Input stream */
/* Line reading state */
char *line_buffer; /* Reusable line buffer */
size_t line_buffer_size; /* Buffer size */
unsigned long line_number; /* Current line number (1-based) */
long current_position; /* Current file position */
/* Parser state */
enum scanner_state state; /* Current parsing state */
/* Header accumulation */
struct patch_headers *pending_headers; /* Headers being accumulated */
char **header_lines; /* Raw header lines */
unsigned int num_header_lines; /* Number of accumulated headers */
unsigned int header_lines_allocated; /* Allocated header slots */
unsigned long header_start_line; /* Line number where current headers started */
/* Current content being emitted */
struct patch_content current_content; /* Content structure for emission */
struct patch_headers current_headers; /* Current patch headers */
struct patch_hunk current_hunk; /* Current hunk */
struct patch_hunk_line current_line; /* Current hunk line */
/* Temporary storage for content strings (to avoid buffer reuse issues) */
char **temp_strings; /* Array of allocated strings */
unsigned int temp_strings_count; /* Number of allocated strings */
unsigned int temp_strings_allocated; /* Allocated slots */
/* Hunk processing state */
unsigned long hunk_orig_remaining; /* Remaining original lines in hunk */
unsigned long hunk_new_remaining; /* Remaining new lines in hunk */
int in_hunk; /* Are we currently in a hunk? */
/* Context diff buffering (bounded by hunk size) */
struct patch_hunk_line *context_buffer; /* Buffered old section lines */
unsigned int context_buffer_count; /* Number of buffered lines */
unsigned int context_buffer_allocated; /* Allocated buffer slots */
unsigned int context_buffer_emit_index; /* Next buffered line to emit */
int context_buffering; /* Are we buffering old section? */
int context_emitting_buffer; /* Are we emitting buffered lines? */
unsigned long context_hunk_start_line; /* Line number where hunk started (*** line) */
/* Simple one-line buffer for stdin-compatible peek-ahead */
char *next_line; /* Next line buffered for peek-ahead */
unsigned long next_line_number; /* Line number of buffered line */
int has_next_line; /* Flag: next_line contains valid data */
/* Pending line for reprocessing after emitting accumulated headers */
char *pending_line; /* Line to reprocess on next call */
};
/* Forward declarations */
static int scanner_read_line(patch_scanner_t *scanner);
static int scanner_is_potential_patch_start(const char *line);
static int scanner_context_buffer_init(patch_scanner_t *scanner);
static void scanner_context_buffer_clear(patch_scanner_t *scanner);
static int scanner_context_buffer_add(patch_scanner_t *scanner, const struct patch_hunk_line *line);
static int scanner_context_buffer_emit_next(patch_scanner_t *scanner, const patch_content_t **content);
static int scanner_is_header_continuation(patch_scanner_t *scanner, const char *line);
static int scanner_validate_headers(patch_scanner_t *scanner);
static int scanner_parse_headers(patch_scanner_t *scanner);
static void scanner_init_content(patch_scanner_t *scanner, enum patch_content_type type);
static char *scanner_store_temp_string(patch_scanner_t *scanner, const char *str, size_t length);
static int scanner_emit_non_patch(patch_scanner_t *scanner, const char *line, size_t length);
static int scanner_emit_headers(patch_scanner_t *scanner);
static int scanner_emit_hunk_header(patch_scanner_t *scanner, const char *line);
static int scanner_emit_context_hunk_header(patch_scanner_t *scanner, const char *line);
static int scanner_emit_context_new_hunk_header(patch_scanner_t *scanner, const char *line);
static int scanner_emit_hunk_line(patch_scanner_t *scanner, const char *line);
static int scanner_emit_no_newline(patch_scanner_t *scanner, const char *line);
static int scanner_emit_binary(patch_scanner_t *scanner, const char *line);
static void scanner_free_headers(patch_scanner_t *scanner);
static void scanner_reset_for_next_patch(patch_scanner_t *scanner);
/* Stdin-compatible header completion logic */
static int scanner_should_wait_for_unified_headers(patch_scanner_t *scanner);
/* Context diff buffering functions */
static int scanner_context_buffer_init(patch_scanner_t *scanner)
{
if (scanner->context_buffer_allocated == 0) {
scanner->context_buffer_allocated = 16; /* Initial size */
scanner->context_buffer = malloc(scanner->context_buffer_allocated * sizeof(struct patch_hunk_line));
if (!scanner->context_buffer) {
return PATCH_SCAN_MEMORY_ERROR;
}
}
scanner->context_buffer_count = 0;
scanner->context_buffer_emit_index = 0;
scanner->context_buffering = 1;
scanner->context_emitting_buffer = 0;
return PATCH_SCAN_OK;
}
static void scanner_context_buffer_clear(patch_scanner_t *scanner)
{
/* Free the line strings we allocated */
for (unsigned int i = 0; i < scanner->context_buffer_count; i++) {
free((void*)scanner->context_buffer[i].line);
}
scanner->context_buffer_count = 0;
scanner->context_buffer_emit_index = 0;
scanner->context_buffering = 0;
scanner->context_emitting_buffer = 0;
}
static int scanner_context_buffer_add(patch_scanner_t *scanner, const struct patch_hunk_line *line)
{
/* Ensure we have space */
if (scanner->context_buffer_count >= scanner->context_buffer_allocated) {
/* Cap buffer size at reasonable maximum */
if (scanner->context_buffer_allocated >= MAX_CONTEXT_BUFFER_SIZE) {
return PATCH_SCAN_MEMORY_ERROR;
}
unsigned int new_size = scanner->context_buffer_allocated * 2;
if (new_size > MAX_CONTEXT_BUFFER_SIZE) {
new_size = MAX_CONTEXT_BUFFER_SIZE;
}
struct patch_hunk_line *new_buffer = realloc(scanner->context_buffer,
new_size * sizeof(struct patch_hunk_line));
if (!new_buffer) {
return PATCH_SCAN_MEMORY_ERROR;
}
scanner->context_buffer = new_buffer;
scanner->context_buffer_allocated = new_size;
}
/* Copy the line data (we need to own the line string) */
scanner->context_buffer[scanner->context_buffer_count] = *line;
scanner->context_buffer[scanner->context_buffer_count].line = strndup(line->line, line->length);
if (!scanner->context_buffer[scanner->context_buffer_count].line) {
return PATCH_SCAN_MEMORY_ERROR;
}
/* Update content pointer to point into the copied buffer */
if (line->content && line->content >= line->line && line->content < line->line + line->length) {
/* Calculate offset of content within original line */
size_t content_offset = line->content - line->line;
/* Update content to point into copied buffer */
scanner->context_buffer[scanner->context_buffer_count].content =
scanner->context_buffer[scanner->context_buffer_count].line + content_offset;
}
scanner->context_buffer_count++;
return PATCH_SCAN_OK;
}
static int scanner_context_buffer_emit_next(patch_scanner_t *scanner, const patch_content_t **content)
{
if (scanner->context_buffer_emit_index < scanner->context_buffer_count) {
/* Emit the next buffered line */
scanner_init_content(scanner, PATCH_CONTENT_HUNK_LINE);
/* Get the buffered line - context was set correctly when buffered */
struct patch_hunk_line *buffered_line = &scanner->context_buffer[scanner->context_buffer_emit_index];
scanner->current_content.data.line = buffered_line;
*content = &scanner->current_content;
scanner->context_buffer_emit_index++;
return PATCH_SCAN_OK;
} else {
/* All buffered lines emitted */
scanner->context_emitting_buffer = 0;
scanner_context_buffer_clear(scanner);
return PATCH_SCAN_EOF; /* Signal that buffered content is exhausted */
}
}
/* Public API implementation */
patch_scanner_t* patch_scanner_create(FILE *file)
{
patch_scanner_t *scanner;
if (!file) {
return NULL;
}
scanner = xmalloc(sizeof(patch_scanner_t));
memset(scanner, 0, sizeof(patch_scanner_t));
scanner->file = file;
scanner->line_buffer_size = 1024;
scanner->line_buffer = xmalloc(scanner->line_buffer_size);
scanner->line_number = 0;
scanner->current_position = ftell(file);
scanner->state = STATE_SEEKING_PATCH;
/* Initialize header accumulation */
scanner->header_lines_allocated = 8;
scanner->header_lines = xmalloc(sizeof(char*) * scanner->header_lines_allocated);
/* Initialize temporary string storage */
scanner->temp_strings_allocated = 16;
scanner->temp_strings = xmalloc(sizeof(char*) * scanner->temp_strings_allocated);
scanner->temp_strings_count = 0;
/* Initialize simple peek-ahead buffer */
scanner->next_line = NULL;
scanner->next_line_number = 0;
scanner->has_next_line = 0;
return scanner;
}
int patch_scanner_next(patch_scanner_t *scanner, const patch_content_t **content)
{
char *line;
size_t line_length;
int result;
if (!scanner || !content) {
return PATCH_SCAN_ERROR;
}
if (scanner->state == STATE_ERROR) {
return PATCH_SCAN_ERROR;
}
/* Check if we need to emit buffered context diff lines */
if (scanner->context_emitting_buffer) {
int result = scanner_context_buffer_emit_next(scanner, content);
if (result == PATCH_SCAN_OK) {
return PATCH_SCAN_OK;
}
/* If result is PATCH_SCAN_EOF, continue with normal processing */
}
/* Main parsing loop */
for (;;) {
/* Handle states that don't require reading a new line */
if (scanner->state == STATE_BINARY_READY) {
/* Emit binary content for binary-only patches */
scanner_emit_binary(scanner, "Binary patch");
scanner->state = STATE_SEEKING_PATCH; /* Reset for next patch */
*content = &scanner->current_content;
return PATCH_SCAN_OK;
}
/* Check for pending line first */
if (scanner->pending_line) {
/* Use pending line instead of reading new one */
strncpy(scanner->line_buffer, scanner->pending_line, scanner->line_buffer_size - 1);
scanner->line_buffer[scanner->line_buffer_size - 1] = '\0';
free(scanner->pending_line);
scanner->pending_line = NULL;
result = PATCH_SCAN_OK;
} else {
/* Read next line */
result = scanner_read_line(scanner);
}
if (result == PATCH_SCAN_EOF) {
/* Handle EOF - if we were accumulating headers, emit them as non-patch */
if (scanner->state == STATE_ACCUMULATING_HEADERS && scanner->num_header_lines > 0) {
/* Create a single string with all accumulated headers */
size_t total_len = 0;
for (unsigned int i = 0; i < scanner->num_header_lines; i++) {
size_t header_len = strlen(scanner->header_lines[i]) + 1; /* +1 for newline */
/* Check for integer overflow */
if (total_len > SIZE_MAX - header_len) {
scanner->state = STATE_ERROR;
return PATCH_SCAN_ERROR;
}
total_len += header_len;
}
char *combined = xmalloc(total_len + 1);
combined[0] = '\0';
for (unsigned int i = 0; i < scanner->num_header_lines; i++) {
strcat(combined, scanner->header_lines[i]);
if (i < scanner->num_header_lines - 1) {
strcat(combined, "\n");
}
}
scanner_emit_non_patch(scanner, combined, strlen(combined));
free(combined);
scanner_free_headers(scanner);
scanner->state = STATE_SEEKING_PATCH;
*content = &scanner->current_content;
return PATCH_SCAN_OK;
}
return PATCH_SCAN_EOF;
} else if (result != PATCH_SCAN_OK) {
scanner->state = STATE_ERROR;
return result;
}
line = scanner->line_buffer;
line_length = strlen(line);
/* State machine for parsing */
switch (scanner->state) {
case STATE_SEEKING_PATCH:
if (scanner_is_potential_patch_start(line)) {
/* Start accumulating headers */
scanner->state = STATE_ACCUMULATING_HEADERS;
scanner->num_header_lines = 0;
scanner->header_start_line = scanner->line_number;
/* Store first header line */
if (scanner->num_header_lines >= scanner->header_lines_allocated) {
/* Prevent integer overflow and limit maximum headers */
if (scanner->header_lines_allocated > 1024) {
scanner->state = STATE_ERROR;
return PATCH_SCAN_ERROR;
}
unsigned int new_size = scanner->header_lines_allocated * 2;
if (new_size < scanner->header_lines_allocated) {
/* Overflow detected */
scanner->state = STATE_ERROR;
return PATCH_SCAN_ERROR;
}
scanner->header_lines_allocated = new_size;
scanner->header_lines = xrealloc(scanner->header_lines,
sizeof(char*) * scanner->header_lines_allocated);
}
scanner->header_lines[scanner->num_header_lines++] = xstrdup(line);
/* Don't emit yet, continue accumulating */
continue;
} else {
/* Emit as non-patch content */
scanner_emit_non_patch(scanner, line, line_length);
*content = &scanner->current_content;
return PATCH_SCAN_OK;
}
case STATE_ACCUMULATING_HEADERS:
if (scanner_is_header_continuation(scanner, line)) {
/* Add to accumulated headers */
if (scanner->num_header_lines >= scanner->header_lines_allocated) {
/* Prevent integer overflow and limit maximum headers */
if (scanner->header_lines_allocated > 1024) {
scanner->state = STATE_ERROR;
return PATCH_SCAN_ERROR;
}
unsigned int new_size = scanner->header_lines_allocated * 2;
if (new_size < scanner->header_lines_allocated) {
/* Overflow detected */
scanner->state = STATE_ERROR;
return PATCH_SCAN_ERROR;
}
scanner->header_lines_allocated = new_size;
scanner->header_lines = xrealloc(scanner->header_lines,
sizeof(char*) * scanner->header_lines_allocated);
}
scanner->header_lines[scanner->num_header_lines++] = xstrdup(line);
/* Check if we have complete headers */
if (scanner_validate_headers(scanner)) {
/* We have valid headers - parse and emit them */
scanner_parse_headers(scanner);
scanner->state = STATE_IN_PATCH;
/* Check if this is a binary-only patch (no hunks expected) */
if (scanner->current_headers.is_binary &&
(scanner->current_headers.git_type == GIT_DIFF_NEW_FILE ||
scanner->current_headers.git_type == GIT_DIFF_DELETED_FILE ||
scanner->current_headers.git_type == GIT_DIFF_BINARY)) {
/* For binary patches, we need to emit both headers and binary content */
scanner->state = STATE_BINARY_READY;
}
scanner_emit_headers(scanner);
*content = &scanner->current_content;
return PATCH_SCAN_OK;
}
/* Continue accumulating */
continue;
} else {
/* This line doesn't continue headers - accumulated lines weren't a patch */
/* Create a single string with all accumulated headers */
size_t total_len = 0;
for (unsigned int i = 0; i < scanner->num_header_lines; i++) {
size_t header_len = strlen(scanner->header_lines[i]) + 1; /* +1 for newline */
/* Check for integer overflow */
if (total_len > SIZE_MAX - header_len) {
scanner->state = STATE_ERROR;
return PATCH_SCAN_ERROR;
}
total_len += header_len;
}
char *combined = xmalloc(total_len + 1);
combined[0] = '\0';
for (unsigned int i = 0; i < scanner->num_header_lines; i++) {
strcat(combined, scanner->header_lines[i]);
if (i < scanner->num_header_lines - 1) {
strcat(combined, "\n");
}
}
scanner_emit_non_patch(scanner, combined, strlen(combined));
free(combined);
scanner_free_headers(scanner);
scanner->state = STATE_SEEKING_PATCH;
/* Store current line for next call */
if (scanner->pending_line) {
free(scanner->pending_line);
}
scanner->pending_line = xstrdup(line);
*content = &scanner->current_content;
return PATCH_SCAN_OK;
}
case STATE_IN_PATCH:
if (!strncmp(line, "@@ ", sizeof("@@ ") - 1)) {
/* Unified diff hunk header */
scanner->state = STATE_IN_HUNK;
scanner_emit_hunk_header(scanner, line);
*content = &scanner->current_content;
return PATCH_SCAN_OK;
} else if (!strncmp(line, "*** ", sizeof("*** ") - 1) && strstr(line, " ****")) {
/* Context diff old hunk header: *** 1,3 **** */
scanner->state = STATE_IN_HUNK;
int result = scanner_emit_context_hunk_header(scanner, line);
if (result != PATCH_SCAN_OK) {
scanner->state = STATE_ERROR;
return result;
}
/* Don't return content yet - wait for complete hunk header from --- line */
continue;
} else if (!strncmp(line, "***************", sizeof("***************") - 1)) {
/* Context diff separator - skip it */
continue;
} else if (!strncmp(line, "Binary files ", sizeof("Binary files ") - 1) ||
!strncmp(line, "GIT binary patch", sizeof("GIT binary patch") - 1)) {
/* Binary content */
scanner_emit_binary(scanner, line);
*content = &scanner->current_content;
return PATCH_SCAN_OK;
} else if (scanner_is_potential_patch_start(line)) {
/* Start of next patch */
scanner_reset_for_next_patch(scanner);
scanner->state = STATE_ACCUMULATING_HEADERS;
scanner->num_header_lines = 0;
scanner->header_start_line = scanner->line_number;
scanner->header_lines[scanner->num_header_lines++] = xstrdup(line);
continue;
} else {
/* Non-patch content between patches */
scanner_emit_non_patch(scanner, line, line_length);
*content = &scanner->current_content;
return PATCH_SCAN_OK;
}
case STATE_IN_HUNK:
if (line[0] == ' ' || line[0] == '+' || line[0] == '!' ||
(line[0] == '-' && !(strncmp(line, "--- ", 4) == 0 && strstr(line, " ----")))) {
/* Hunk line - but exclude context diff "--- N ----" headers */
int result = scanner_emit_hunk_line(scanner, line);
if (result != PATCH_SCAN_OK) {
scanner->state = STATE_ERROR;
return result;
}
/* For context diffs, check if we should buffer this line */
if (scanner->context_buffering) {
/* Buffer this line for later emission */
result = scanner_context_buffer_add(scanner, &scanner->current_line);
if (result != PATCH_SCAN_OK) {
scanner->state = STATE_ERROR;
return result;
}
/* All lines in old section are buffered for later emission - no immediate emission */
/* For other lines, continue to next line without emitting */
continue;
}
/* Check if hunk is complete */
if (scanner->hunk_orig_remaining == 0 && scanner->hunk_new_remaining == 0) {
/* For context diffs, make sure we've actually processed the new section */
/* If new_count is 0 but new_remaining was never set (still 0 from init), */
/* it means we haven't seen the "--- N ----" line yet */
if (scanner->current_headers.type == PATCH_TYPE_CONTEXT &&
scanner->current_hunk.new_count == 0 && scanner->hunk_new_remaining == 0) {
/* Context diff: old section complete, but new section not started yet */
/* Don't transition out of hunk state yet */
} else {
scanner->state = STATE_IN_PATCH;
scanner->in_hunk = 0;
}
}
*content = &scanner->current_content;
return PATCH_SCAN_OK;
} else if (line[0] == '\\') {
/* No newline marker */
scanner_emit_no_newline(scanner, line);
*content = &scanner->current_content;
return PATCH_SCAN_OK;
} else if (!strncmp(line, "@@ ", sizeof("@@ ") - 1)) {
/* Next unified diff hunk */
int result = scanner_emit_hunk_header(scanner, line);
if (result != PATCH_SCAN_OK) {
scanner->state = STATE_ERROR;
return result;
}
*content = &scanner->current_content;
return PATCH_SCAN_OK;
} else if (!strncmp(line, "--- ", sizeof("--- ") - 1) && strstr(line, " ----")) {
/* Context diff new hunk header: --- 1,3 ---- */
int result = scanner_emit_context_new_hunk_header(scanner, line);
if (result != PATCH_SCAN_OK) {
scanner->state = STATE_ERROR;
return result;
}
/* Now we have complete hunk info - return the hunk header */
*content = &scanner->current_content;
return PATCH_SCAN_OK;
} else if (!strncmp(line, "*** ", sizeof("*** ") - 1) && strstr(line, " ****")) {
/* Context diff old hunk header: *** 1,3 **** */
int result = scanner_emit_context_hunk_header(scanner, line);
if (result != PATCH_SCAN_OK) {
scanner->state = STATE_ERROR;
return result;
}
/* Continue to next line - wait for --- line to complete hunk header */
continue;
} else if (!strncmp(line, "***************", sizeof("***************") - 1)) {
/* Context diff hunk separator - complete current hunk and continue */
scanner->state = STATE_IN_PATCH;
scanner->in_hunk = 0;
continue;
} else {
/* End of patch */
scanner->state = STATE_SEEKING_PATCH;
scanner->in_hunk = 0;
/* Process current line in seeking state */
if (scanner_is_potential_patch_start(line)) {
scanner->state = STATE_ACCUMULATING_HEADERS;
scanner->num_header_lines = 0;
scanner->header_start_line = scanner->line_number;
scanner->header_lines[scanner->num_header_lines++] = xstrdup(line);
continue;
} else {
scanner_emit_non_patch(scanner, line, line_length);
*content = &scanner->current_content;
return PATCH_SCAN_OK;
}
}
case STATE_ERROR:
return PATCH_SCAN_ERROR;
default:
scanner->state = STATE_ERROR;
return PATCH_SCAN_ERROR;
}
/* Should never reach here due to loop structure */
} /* end of for(;;) loop */
}
long patch_scanner_position(patch_scanner_t *scanner)
{
if (!scanner) {
return -1;
}
return scanner->current_position;
}
unsigned long patch_scanner_line_number(patch_scanner_t *scanner)
{
if (!scanner) {
return 0;
}
return scanner->line_number;
}
void patch_scanner_destroy(patch_scanner_t *scanner)
{
if (!scanner) {
return;
}
scanner_free_headers(scanner);
if (scanner->header_lines) {
free(scanner->header_lines);
}
if (scanner->line_buffer) {
free(scanner->line_buffer);
}
/* Free simple peek-ahead buffer */
if (scanner->next_line) {
free(scanner->next_line);
}
/* Free pending line buffer */
if (scanner->pending_line) {
free(scanner->pending_line);
}
/* Free context diff buffer */
if (scanner->context_buffer) {
scanner_context_buffer_clear(scanner);
free(scanner->context_buffer);
}
/* Free any allocated strings in current content structures */
if (scanner->current_headers.old_name) {
free(scanner->current_headers.old_name);
}
if (scanner->current_headers.new_name) {
free(scanner->current_headers.new_name);
}
if (scanner->current_headers.git_old_name) {
free(scanner->current_headers.git_old_name);
}
if (scanner->current_headers.git_new_name) {
free(scanner->current_headers.git_new_name);
}
if (scanner->current_headers.old_hash) {
free(scanner->current_headers.old_hash);
}
if (scanner->current_headers.new_hash) {
free(scanner->current_headers.new_hash);
}
if (scanner->current_hunk.context) {
free(scanner->current_hunk.context);
}
/* Free temporary string storage */
if (scanner->temp_strings) {
for (unsigned int i = 0; i < scanner->temp_strings_count; i++) {
if (scanner->temp_strings[i]) {
free(scanner->temp_strings[i]);
}
}
free(scanner->temp_strings);
}
free(scanner);
}
int patch_scanner_skip_current_patch(patch_scanner_t *scanner)
{
const patch_content_t *content;
int result;
if (!scanner) {
return PATCH_SCAN_ERROR;
}
/* Skip until we're no longer in a patch */
while (scanner->state == STATE_IN_PATCH || scanner->state == STATE_IN_HUNK) {
result = patch_scanner_next(scanner, &content);
if (result != PATCH_SCAN_OK) {
return result;
}
}
return PATCH_SCAN_OK;
}
int patch_scanner_at_patch_start(patch_scanner_t *scanner)
{
if (!scanner) {
return 0;
}
return (scanner->state == STATE_ACCUMULATING_HEADERS ||
scanner->state == STATE_IN_PATCH);
}
/* Internal helper functions */
static int scanner_read_line(patch_scanner_t *scanner)
{
ssize_t result;
/* Check if we have a buffered line from peek-ahead */
if (scanner->has_next_line) {
/* Use the buffered line */
size_t len = strlen(scanner->next_line) + 1; /* +1 for null terminator */
/* Ensure line_buffer is large enough */
if (scanner->line_buffer_size < len) {
scanner->line_buffer = xrealloc(scanner->line_buffer, len);
scanner->line_buffer_size = len;
}
/* Copy buffered line to line_buffer */
strcpy(scanner->line_buffer, scanner->next_line);
/* Update line number */
scanner->line_number = scanner->next_line_number;
/* Clear the buffer */
free(scanner->next_line);
scanner->next_line = NULL;
scanner->has_next_line = 0;
/* Set current position (approximate) */
scanner->current_position = ftell(scanner->file);
return PATCH_SCAN_OK;
}
/* Normal line reading */
scanner->current_position = ftell(scanner->file);
result = getline(&scanner->line_buffer, &scanner->line_buffer_size, scanner->file);
if (result == -1) {
if (feof(scanner->file)) {
return PATCH_SCAN_EOF;
}
return PATCH_SCAN_IO_ERROR;
}
scanner->line_number++;
return PATCH_SCAN_OK;
}
static int scanner_is_potential_patch_start(const char *line)
{
/* Check for diff command */
if (!strncmp(line, "diff ", sizeof("diff ") - 1)) {
return 1;
}
/* Check for unified diff old file line */
if (!strncmp(line, "--- ", sizeof("--- ") - 1)) {
/* Exclude context diff hunk headers like "--- 1,3 ----" */
if (strstr(line, " ----")) {
return 0;
}
return 1;
}
/* Check for context diff old file line */
if (!strncmp(line, "*** ", sizeof("*** ") - 1)) {
/* Exclude context diff hunk headers like "*** 1,3 ****" */
if (strstr(line, " ****")) {
return 0;
}
return 1;
}
return 0;
}
static int scanner_is_header_continuation(patch_scanner_t *scanner, const char *line)
{
/* Check if line is a valid patch header line */
(void)scanner; /* unused parameter */
/* Handle context diff file headers vs hunk headers */
if (!strncmp(line, "*** ", sizeof("*** ") - 1)) {
/* Context diff: *** filename is a header, but *** N **** is a hunk header */
if (strstr(line, " ****")) {
return 0; /* This is a hunk header like "*** 1,3 ****" */
}
return 1; /* This is a file header like "*** filename" */
}
/* Handle context diff new file headers vs hunk headers */
if (!strncmp(line, "--- ", sizeof("--- ") - 1)) {
/* Context diff: --- filename is a header, but --- N ---- is a hunk header */
if (strstr(line, " ----")) {
return 0; /* This is a hunk header like "--- 1,3 ----" */
}
return 1; /* This is a file header like "--- filename" */
}
/* Context diff hunk separator is not a header */
if (!strncmp(line, "***************", sizeof("***************") - 1)) {
return 0;
}
return (!strncmp(line, "diff --git ", sizeof("diff --git ") - 1) ||
!strncmp(line, "+++ ", sizeof("+++ ") - 1) ||
!strncmp(line, "index ", sizeof("index ") - 1) ||
!strncmp(line, "new file mode ", sizeof("new file mode ") - 1) ||
!strncmp(line, "deleted file mode ", sizeof("deleted file mode ") - 1) ||
!strncmp(line, "old mode ", sizeof("old mode ") - 1) ||
!strncmp(line, "new mode ", sizeof("new mode ") - 1) ||
!strncmp(line, "similarity index ", sizeof("similarity index ") - 1) ||
!strncmp(line, "dissimilarity index ", sizeof("dissimilarity index ") - 1) ||
!strncmp(line, "rename from ", sizeof("rename from ") - 1) ||
!strncmp(line, "rename to ", sizeof("rename to ") - 1) ||
!strncmp(line, "copy from ", sizeof("copy from ") - 1) ||
!strncmp(line, "copy to ", sizeof("copy to ") - 1) ||
strstr(line, "Binary files ") ||
!strncmp(line, "GIT binary patch", sizeof("GIT binary patch") - 1));
}
static int scanner_validate_headers(patch_scanner_t *scanner)
{
/* Validate header presence, order, and structure */
unsigned int i;
int has_old_file = 0;
int has_new_file = 0;
int has_git_diff = 0;
int has_context_old = 0;
int has_context_new = 0;
(void)has_git_diff; /* used in validation logic */
/* Reset header info */
memset(&scanner->current_headers, 0, sizeof(scanner->current_headers));
scanner->current_headers.type = PATCH_TYPE_UNIFIED;
scanner->current_headers.git_type = GIT_DIFF_NORMAL;
/* First pass: identify patch type and basic structure */
for (i = 0; i < scanner->num_header_lines; i++) {
const char *line = scanner->header_lines[i];
if (!strncmp(line, "diff --git ", sizeof("diff --git ") - 1)) {
has_git_diff = 1;
scanner->current_headers.type = PATCH_TYPE_GIT_EXTENDED;
}
else if (!strncmp(line, "--- ", sizeof("--- ") - 1)) {
if (has_context_old) {
/* This is the new file line in context diff */
has_context_new = 1;
} else {
has_old_file = 1;
}
}
else if (!strncmp(line, "+++ ", sizeof("+++ ") - 1)) {
has_new_file = 1;
}
else if (!strncmp(line, "*** ", sizeof("*** ") - 1)) {
has_context_old = 1;
scanner->current_headers.type = PATCH_TYPE_CONTEXT;
}
}
/* Validate header order based on patch type */
if (scanner->current_headers.type == PATCH_TYPE_GIT_EXTENDED) {
if (!scanner_validate_git_header_order(scanner)) {
return 0;
}
} else if (scanner->current_headers.type == PATCH_TYPE_CONTEXT) {
if (!scanner_validate_context_header_order(scanner)) {
return 0;
}
} else {
if (!scanner_validate_unified_header_order(scanner)) {
return 0;
}
}
/* Determine if we have a valid patch header structure */
if (scanner->current_headers.type == PATCH_TYPE_CONTEXT) {
return has_context_old && has_context_new;
} else if (scanner->current_headers.type == PATCH_TYPE_GIT_EXTENDED) {
/* Git extended headers are complete if:
* 1. Git validation passed (already done above), AND
* 2. Either no unified diff headers present, OR both --- and +++ are present
*/
if (has_old_file || has_new_file) {
/* If we have any unified diff headers, we need both */
return has_old_file && has_new_file;
}
/* Pure Git metadata diff (no hunks) - complete */
return 1;
}
return has_old_file && has_new_file;
}
static int scanner_parse_headers(patch_scanner_t *scanner)
{
/* Parse headers and extract file information */
memset(&scanner->current_headers, 0, sizeof(scanner->current_headers));
scanner->current_headers.type = PATCH_TYPE_UNIFIED;
scanner->current_headers.git_type = GIT_DIFF_NORMAL;
scanner->current_headers.old_mode = -1;
scanner->current_headers.new_mode = -1;
scanner->current_headers.similarity_index = -1;
scanner->current_headers.dissimilarity_index = -1;
scanner->current_headers.start_position = scanner->current_position;
scanner->current_headers.start_line = scanner->header_start_line;
/* Copy header lines */
scanner->current_headers.header_lines = scanner->header_lines;
scanner->current_headers.num_headers = scanner->num_header_lines;
/* Parse specific header types */
for (unsigned int i = 0; i < scanner->num_header_lines; i++) {
const char *line = scanner->header_lines[i];
if (!strncmp(line, "diff --git ", sizeof("diff --git ") - 1)) {
scanner->current_headers.type = PATCH_TYPE_GIT_EXTENDED;
scanner_parse_git_diff_line(scanner, line);
}
else if (!strncmp(line, "--- ", sizeof("--- ") - 1)) {
/* Check if this is a context diff by looking for a previous *** line */
int is_context_diff = 0;
for (unsigned int j = 0; j < scanner->num_header_lines; j++) {
if (!strncmp(scanner->header_lines[j], "*** ", sizeof("*** ") - 1)) {
is_context_diff = 1;
break;
}
}
if (is_context_diff) {
/* In context diff, --- line is the new file */
scanner_parse_new_file_line(scanner, line);
} else {
/* In unified diff, --- line is the old file */
scanner_parse_old_file_line(scanner, line);
}
}
else if (!strncmp(line, "+++ ", sizeof("+++ ") - 1)) {
scanner_parse_new_file_line(scanner, line);
}
else if (!strncmp(line, "*** ", sizeof("*** ") - 1)) {