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.
30 537a2366 2019-02-08 stsp #include <sys/queue.h>
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>
38 537a2366 2019-02-08 stsp #include "buf.h"
39 537a2366 2019-02-08 stsp #include "rcsutil.h"
42 d947271f 2019-02-08 stsp * Split the contents of a file into a list of lines.
44 d947271f 2019-02-08 stsp struct rcs_lines *
45 d947271f 2019-02-08 stsp rcs_splitlines(u_char *data, size_t len)
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;
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));
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;
62 d947271f 2019-02-08 stsp TAILQ_INSERT_TAIL(&(lines->l_lines), lp, l_list);
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;
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);
82 d947271f 2019-02-08 stsp return (lines);
86 d947271f 2019-02-08 stsp rcs_freelines(struct rcs_lines *lines)
88 d947271f 2019-02-08 stsp struct rcs_line *lp;
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);
95 d947271f 2019-02-08 stsp free(lines);
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 *))
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;
106 537a2366 2019-02-08 stsp size_t newlen;
108 d947271f 2019-02-08 stsp dlines = rcs_splitlines(data, dlen);
109 d947271f 2019-02-08 stsp plines = rcs_splitlines(patch, plen);
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);
117 575e8218 2019-10-07 stsp err = buf_alloc(&res, 1024);
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)
123 537a2366 2019-02-08 stsp buf_append(&newlen, res, lp->l_line, lp->l_len);
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);