1 d947271f 2019-02-08 stsp /* $OpenBSD: rcsutil.c,v 1.46 2017/08/29 16:47:33 otto Exp $ */
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.
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
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.
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.
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>
37 dd038bc6 2021-09-21 thomas.ad #include "got_compat.h"
39 537a2366 2019-02-08 stsp #include "buf.h"
40 537a2366 2019-02-08 stsp #include "rcsutil.h"
43 d947271f 2019-02-08 stsp * Split the contents of a file into a list of lines.
45 d947271f 2019-02-08 stsp struct rcs_lines *
46 d947271f 2019-02-08 stsp rcs_splitlines(u_char *data, size_t len)
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;
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));
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;
63 d947271f 2019-02-08 stsp TAILQ_INSERT_TAIL(&(lines->l_lines), lp, l_list);
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;
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);
83 d947271f 2019-02-08 stsp return (lines);
87 d947271f 2019-02-08 stsp rcs_freelines(struct rcs_lines *lines)
89 d947271f 2019-02-08 stsp struct rcs_line *lp;
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);
96 d947271f 2019-02-08 stsp free(lines);
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 *))
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;
107 537a2366 2019-02-08 stsp size_t newlen;
109 d947271f 2019-02-08 stsp dlines = rcs_splitlines(data, dlen);
110 d947271f 2019-02-08 stsp plines = rcs_splitlines(patch, plen);
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);
118 575e8218 2019-10-07 stsp err = buf_alloc(&res, 1024);
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)
124 537a2366 2019-02-08 stsp buf_append(&newlen, res, lp->l_line, lp->l_len);
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);