Blob


1 /* $OpenBSD: rcsutil.c,v 1.46 2017/08/29 16:47:33 otto Exp $ */
2 /*
3 * Copyright (c) 2005, 2006 Joris Vink <joris@openbsd.org>
4 * Copyright (c) 2006 Xavier Santolaria <xsa@openbsd.org>
5 * Copyright (c) 2006 Niall O'Higgins <niallo@openbsd.org>
6 * Copyright (c) 2006 Ray Lai <ray@openbsd.org>
7 * All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 *
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
19 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
20 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
21 * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
24 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
27 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
30 #include <sys/queue.h>
32 #include <ctype.h>
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <string.h>
36 #include <unistd.h>
38 #include "buf.h"
39 #include "rcsutil.h"
41 /*
42 * Split the contents of a file into a list of lines.
43 */
44 struct rcs_lines *
45 rcs_splitlines(u_char *data, size_t len)
46 {
47 u_char *c, *p;
48 struct rcs_lines *lines;
49 struct rcs_line *lp;
50 size_t i, tlen;
52 lines = calloc(1, sizeof(*lines));
53 if (lines == NULL)
54 return NULL;
55 TAILQ_INIT(&(lines->l_lines));
57 lp = calloc(1, sizeof(*lp));
58 if (lp == NULL) {
59 free(lines);
60 return NULL;
61 }
62 TAILQ_INSERT_TAIL(&(lines->l_lines), lp, l_list);
64 p = c = data;
65 for (i = 0; i < len; i++) {
66 if (*p == '\n' || (i == len - 1)) {
67 tlen = p - c + 1;
68 lp = malloc(sizeof(*lp));
69 if (lp == NULL) {
70 rcs_freelines(lines);
71 return NULL;
72 }
73 lp->l_line = c;
74 lp->l_len = tlen;
75 lp->l_lineno = ++(lines->l_nblines);
76 TAILQ_INSERT_TAIL(&(lines->l_lines), lp, l_list);
77 c = p + 1;
78 }
79 p++;
80 }
82 return (lines);
83 }
85 void
86 rcs_freelines(struct rcs_lines *lines)
87 {
88 struct rcs_line *lp;
90 while ((lp = TAILQ_FIRST(&(lines->l_lines))) != NULL) {
91 TAILQ_REMOVE(&(lines->l_lines), lp, l_list);
92 free(lp);
93 }
95 free(lines);
96 }
98 BUF *
99 rcs_patchfile(u_char *data, size_t dlen, u_char *patch, size_t plen,
100 int (*p)(struct rcs_lines *, struct rcs_lines *))
102 const struct got_error *err = NULL;
103 struct rcs_lines *dlines, *plines;
104 struct rcs_line *lp;
105 BUF *res;
106 size_t newlen;
108 dlines = rcs_splitlines(data, dlen);
109 plines = rcs_splitlines(patch, plen);
111 if (p(dlines, plines) < 0) {
112 rcs_freelines(dlines);
113 rcs_freelines(plines);
114 return (NULL);
117 err = buf_alloc(&res, 1024);
118 if (err)
119 return NULL;
120 TAILQ_FOREACH(lp, &dlines->l_lines, l_list) {
121 if (lp->l_line == NULL)
122 continue;
123 buf_append(&newlen, res, lp->l_line, lp->l_len);
126 rcs_freelines(dlines);
127 rcs_freelines(plines);
128 return (res);