aboutsummaryrefslogtreecommitdiffstats
path: root/libpatch_parse_normal_patch.c
diff options
context:
space:
mode:
authorMattias Andrée <maandree@kth.se>2024-01-09 22:04:24 +0100
committerMattias Andrée <maandree@kth.se>2024-01-09 22:04:24 +0100
commitae850b1ac755f471beac4dbfef4654fe3fbaaae9 (patch)
tree319e7f7f1b99cd1ee75e100f0a29b0f7c827ccea /libpatch_parse_normal_patch.c
downloadlibpatch-ae850b1ac755f471beac4dbfef4654fe3fbaaae9.tar.gz
libpatch-ae850b1ac755f471beac4dbfef4654fe3fbaaae9.tar.bz2
libpatch-ae850b1ac755f471beac4dbfef4654fe3fbaaae9.tar.xz
First commitHEADmaster
Signed-off-by: Mattias Andrée <maandree@kth.se>
Diffstat (limited to 'libpatch_parse_normal_patch.c')
-rw-r--r--libpatch_parse_normal_patch.c140
1 files changed, 140 insertions, 0 deletions
diff --git a/libpatch_parse_normal_patch.c b/libpatch_parse_normal_patch.c
new file mode 100644
index 0000000..2d84a39
--- /dev/null
+++ b/libpatch_parse_normal_patch.c
@@ -0,0 +1,140 @@
+/* See LICENSE file for copyright and license details. */
+#include "common.h"
+
+
+static size_t
+hunk_head_part(const char *text, size_t len, size_t *first, size_t *last)
+{
+ size_t off, r;
+ off = libpatch_get_zu__(&text[off], len - off, first);
+ if (!off)
+ return 0;
+ if (off < len && text[off] == ',') {
+ off += 1;
+ off += r = libpatch_get_zu__(&text[off], len - off, last);
+ if (!r)
+ return 0;
+ } else {
+ *last = *first;
+ }
+ return off;
+}
+
+
+int
+libpatch_parse_normal_patch(const char *text, size_t textlen, size_t *textend,
+ struct libpatch_patch **patch, size_t *patchlen)
+{
+ const char *patchtext = text;
+ size_t len = 0, size = 0, off, lineno1, lineno2, count1, count2, last;
+ int in_hunk = 0, need2, type, has_patch = 0;
+ char op;
+
+ *patch = NULL;
+ *patchlen = 0;
+
+ while ((len = libpatch_next_line__(&text, &textlen, len))) {
+ if (len >= 5 && !strncmp(text, "diff ", 5)) {
+ if (has_patch && textend)
+ break;
+ APPEND_STRING(5, LIBPATCH_PATCH_DIFF_LINE);
+ in_hunk = 0;
+
+ } else if (len >= 3 && in_hunk == 1 && need2 && !strncmp(text, "---", 3)) {
+ APPEND_OPTSTRING(3, LIBPATCH_PATCH_HUNK_FILE2);
+ in_hunk = 2;
+
+ } else if (len >= 3 && in_hunk == 0 && text[1] != ' ') {
+ off = hunk_head_part(&text[off], len - off, &lineno1, &last);
+ if (!off || 2 > len - off)
+ goto garbage;
+ if (!lineno1 && last)
+ goto garbage;
+ else if (!lineno1)
+ count1 = 0;
+ else if (last < lineno1)
+ goto garbage;
+ else
+ count1 = last - lineno1 + 1;
+
+ op = text[off++];
+
+ off = hunk_head_part(&text[off], len - off, &lineno2, &last);
+ if (!off || 2 > len - off)
+ goto garbage;
+ if (!lineno2 && last)
+ goto garbage;
+ else if (!lineno2)
+ count2 = 0;
+ else if (last < lineno2)
+ goto garbage;
+ else
+ count2 = last - lineno2 + 1;
+
+ if (op == 'a') {
+ in_hunk = 2;
+ } else if (op == 'c') {
+ in_hunk = 1;
+ need2 = 1;
+ } else if (op == 'd') {
+ in_hunk = 1;
+ need2 = 0;
+ } else {
+ goto garbage;
+ }
+
+ if (in_hunk == 1)
+ APPEND_OPTSTRING(off, LIBPATCH_PATCH_HUNK_FILE1);
+ else
+ APPEND_OPTSTRING(off, LIBPATCH_PATCH_HUNK);
+
+ has_patch = 1;
+
+ } else if (len >= 2 && in_hunk == 1 && !strncmp(text, "< ", 2)) {
+ type = LIBPATCH_PATCH_FILE1_ONLY;
+ file1_line:
+ APPEND_OPTSTRING(2, type);
+ if (!--count2)
+ in_hunk = 0;
+ (*patch)[*patchlen - 1].file1_lineno = lineno1++;
+ } else if (len >= 2 && in_hunk == 1 && !strncmp(text, " ", 2)) {
+ type = LIBPATCH_PATCH_FILE1_CONTEXT;
+ goto file1_line;
+
+ } else if (len >= 2 && in_hunk == 2 && !strncmp(text, "> ", 2)) {
+ type = LIBPATCH_PATCH_FILE2_ONLY;
+ file2_line:
+ APPEND_OPTSTRING(2, type);
+ if (!--count2)
+ in_hunk = 0;
+ (*patch)[*patchlen - 1].file2_lineno = lineno2++;
+ } else if (len >= 2 && in_hunk == 2 && !strncmp(text, " ", 2)) {
+ type = LIBPATCH_PATCH_FILE2_CONTEXT;
+ goto file2_line;
+
+ } else if (len >= 1 && !strncmp(text, "\\", 1) && *patchlen) {
+ if (IS_LINE((*patch)[*patchlen - 1].type) ||
+ !(*patch)[*patchlen - 1].lf_terminated)
+ goto garbage;
+ (*patch)[*patchlen - 1].lf_terminated = 0;
+ APPEND_NO_TEXT(LIBPATCH_PATCH_SYNTACTICAL);
+
+ } else {
+ garbage:
+ APPEND_STRING(0, LIBPATCH_PATCH_GARBAGE);
+ if (len == textlen)
+ (*patch)[*patchlen - 1].lf_terminated = 0;
+ }
+ }
+
+ if (textend)
+ *textend = (size_t)(text - patchtext);
+
+ return 0;
+
+fail:
+ free(*patch);
+ *patch = NULL;
+ *patchlen = 0;
+ return -1;
+}