Blame


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