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 */
31 #include <ctype.h>
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <string.h>
35 #include <unistd.h>
37 #include "got_compat.h"
39 #include "buf.h"
40 #include "rcsutil.h"
42 /*
43 * Split the contents of a file into a list of lines.
44 */
45 struct rcs_lines *
46 rcs_splitlines(u_char *data, size_t len)
47 {
48 u_char *c, *p;
49 struct rcs_lines *lines;
50 struct rcs_line *lp;
51 size_t i, tlen;
53 lines = calloc(1, sizeof(*lines));
54 if (lines == NULL)
55 return NULL;
56 TAILQ_INIT(&(lines->l_lines));
58 lp = calloc(1, sizeof(*lp));
59 if (lp == NULL) {
60 free(lines);
61 return NULL;
62 }
63 TAILQ_INSERT_TAIL(&(lines->l_lines), lp, l_list);
65 p = c = data;
66 for (i = 0; i < len; i++) {
67 if (*p == '\n' || (i == len - 1)) {
68 tlen = p - c + 1;
69 lp = malloc(sizeof(*lp));
70 if (lp == NULL) {
71 rcs_freelines(lines);
72 return NULL;
73 }
74 lp->l_line = c;
75 lp->l_len = tlen;
76 lp->l_lineno = ++(lines->l_nblines);
77 TAILQ_INSERT_TAIL(&(lines->l_lines), lp, l_list);
78 c = p + 1;
79 }
80 p++;
81 }
83 return (lines);
84 }
86 void
87 rcs_freelines(struct rcs_lines *lines)
88 {
89 struct rcs_line *lp;
91 while ((lp = TAILQ_FIRST(&(lines->l_lines))) != NULL) {
92 TAILQ_REMOVE(&(lines->l_lines), lp, l_list);
93 free(lp);
94 }
96 free(lines);
97 }
99 BUF *
100 rcs_patchfile(u_char *data, size_t dlen, u_char *patch, size_t plen,
101 int (*p)(struct rcs_lines *, struct rcs_lines *))
103 const struct got_error *err = NULL;
104 struct rcs_lines *dlines, *plines;
105 struct rcs_line *lp;
106 BUF *res;
107 size_t newlen;
109 dlines = rcs_splitlines(data, dlen);
110 plines = rcs_splitlines(patch, plen);
112 if (p(dlines, plines) < 0) {
113 rcs_freelines(dlines);
114 rcs_freelines(plines);
115 return (NULL);
118 err = buf_alloc(&res, 1024);
119 if (err)
120 return NULL;
121 TAILQ_FOREACH(lp, &dlines->l_lines, l_list) {
122 if (lp->l_line == NULL)
123 continue;
124 buf_append(&newlen, res, lp->l_line, lp->l_len);
127 rcs_freelines(dlines);
128 rcs_freelines(plines);
129 return (res);