Blob


1 /* Split source by line breaks, and calculate a simplistic checksum. */
2 /*
3 * Copyright (c) 2020 Neels Hofmeyr <neels@hofmeyr.de>
4 *
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 */
18 #include <errno.h>
19 #include <inttypes.h>
20 #include <stdbool.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <unistd.h>
25 #include <arraylist.h>
26 #include <diff_main.h>
28 #include "diff_internal.h"
29 #include "diff_debug.h"
31 static int
32 diff_data_atomize_text_lines_fd(struct diff_data *d)
33 {
34 off_t pos = 0;
35 const off_t end = pos + d->len;
36 unsigned int array_size_estimate = d->len / 50;
37 unsigned int pow2 = 1;
39 while (array_size_estimate >>= 1)
40 pow2++;
42 ARRAYLIST_INIT(d->atoms, 1 << pow2);
44 if (fseek(d->root->f, 0L, SEEK_SET) == -1)
45 return errno;
47 while (pos < end) {
48 off_t line_end = pos;
49 unsigned int hash = 0;
50 unsigned char buf[512];
51 size_t r, i;
52 struct diff_atom *atom;
53 int eol = 0;
55 while (eol == 0 && line_end < end) {
56 r = fread(buf, sizeof(char), sizeof(buf), d->root->f);
57 if (r == 0 && ferror(d->root->f))
58 return errno;
59 i = 0;
60 while (eol == 0 && i < r) {
61 if (buf[i] != '\r' && buf[i] != '\n') {
62 hash = hash * 23 + buf[i];
63 line_end++;
64 } else
65 eol = buf[i];
66 i++;
67 }
68 }
70 /* When not at the end of data, the line ending char ('\r' or
71 * '\n') must follow */
72 if (line_end < end)
73 line_end++;
74 /* If that was an '\r', also pull in any following '\n' */
75 if (line_end < end && eol == '\r') {
76 if (fseeko(d->root->f, line_end, SEEK_SET) == -1)
77 return errno;
78 r = fread(buf, sizeof(char), sizeof(buf), d->root->f);
79 if (r == 0 && ferror(d->root->f))
80 return errno;
81 if (r == 1 && buf[0] == '\n' )
82 line_end++;
83 }
85 /* Record the found line as diff atom */
86 ARRAYLIST_ADD(atom, d->atoms);
87 if (!atom)
88 return ENOMEM;
90 *atom = (struct diff_atom){
91 .d = d,
92 .pos = pos,
93 .at = NULL, /* atom data is not memory-mapped */
94 .len = line_end - pos,
95 .hash = hash,
96 };
98 /* Starting point for next line: */
99 pos = line_end;
100 if (fseeko(d->root->f, pos, SEEK_SET) == -1)
101 return errno;
104 return DIFF_RC_OK;
107 static int
108 diff_data_atomize_text_lines_mmap(struct diff_data *d)
110 const uint8_t *pos = d->data;
111 const uint8_t *end = pos + d->len;
113 unsigned int array_size_estimate = d->len / 50;
114 unsigned int pow2 = 1;
115 while (array_size_estimate >>= 1)
116 pow2++;
118 ARRAYLIST_INIT(d->atoms, 1 << pow2);
120 while (pos < end) {
121 const uint8_t *line_end = pos;
122 unsigned int hash = 0;
124 while (line_end < end && *line_end != '\r' && *line_end != '\n') {
125 hash = hash * 23 + *line_end;
126 line_end++;
129 /* When not at the end of data, the line ending char ('\r' or
130 * '\n') must follow */
131 if (line_end < end)
132 line_end++;
133 /* If that was an '\r', also pull in any following '\n' */
134 if (line_end[0] == '\r'
135 && line_end < end && line_end[1] == '\n')
136 line_end++;
138 /* Record the found line as diff atom */
139 struct diff_atom *atom;
140 ARRAYLIST_ADD(atom, d->atoms);
141 if (!atom)
142 return ENOMEM;
144 *atom = (struct diff_atom){
145 .d = d,
146 .pos = (off_t)(pos - d->data),
147 .at = pos,
148 .len = line_end - pos,
149 .hash = hash,
150 };
152 /* Starting point for next line: */
153 pos = line_end;
156 return DIFF_RC_OK;
159 static int
160 diff_data_atomize_text_lines(struct diff_data *d)
162 if (d->data == NULL)
163 return diff_data_atomize_text_lines_fd(d);
164 else
165 return diff_data_atomize_text_lines_mmap(d);
168 int
169 diff_atomize_text_by_line(void *func_data, struct diff_data *left,
170 struct diff_data *right)
172 int rc;
173 rc = diff_data_atomize_text_lines(left);
174 if (rc != DIFF_RC_OK)
175 return rc;
176 return diff_data_atomize_text_lines(right);