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