/src/clamav/libclamav/line.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright (C) 2013-2023 Cisco Systems, Inc. and/or its affiliates. All rights reserved. |
3 | | * Copyright (C) 2007-2013 Sourcefire, Inc. |
4 | | * |
5 | | * Authors: Nigel Horne |
6 | | * |
7 | | * This program is free software; you can redistribute it and/or modify |
8 | | * it under the terms of the GNU General Public License version 2 as |
9 | | * published by the Free Software Foundation. |
10 | | * |
11 | | * This program is distributed in the hope that it will be useful, |
12 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 | | * GNU General Public License for more details. |
15 | | * |
16 | | * You should have received a copy of the GNU General Public License |
17 | | * along with this program; if not, write to the Free Software |
18 | | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, |
19 | | * MA 02110-1301, USA. |
20 | | * |
21 | | * $Log: line.c,v $ |
22 | | * Revision 1.11 2007/02/12 20:46:08 njh |
23 | | * Various tidy |
24 | | * |
25 | | * Revision 1.10 2006/04/09 19:59:27 kojm |
26 | | * update GPL headers with new address for FSF |
27 | | * |
28 | | * Revision 1.9 2005/03/10 08:53:33 nigelhorne |
29 | | * Tidy |
30 | | * |
31 | | * Revision 1.8 2005/03/01 11:38:11 nigelhorne |
32 | | * Fix typo |
33 | | * |
34 | | * Revision 1.7 2004/12/08 20:07:23 nigelhorne |
35 | | * Fix compilation error on Solaris |
36 | | * |
37 | | * Revision 1.6 2004/10/14 17:45:55 nigelhorne |
38 | | * Try to reclaim some memory if it becomes low when decoding |
39 | | * |
40 | | * Revision 1.5 2004/09/30 08:58:56 nigelhorne |
41 | | * Remove empty lines |
42 | | * |
43 | | * Revision 1.4 2004/09/21 14:55:26 nigelhorne |
44 | | * Handle blank lines in text/plain messages |
45 | | * |
46 | | * Revision 1.3 2004/08/25 12:30:36 nigelhorne |
47 | | * Use memcpy rather than strcpy |
48 | | * |
49 | | * Revision 1.2 2004/08/21 11:57:57 nigelhorne |
50 | | * Use line.[ch] |
51 | | * |
52 | | * Revision 1.1 2004/08/20 11:58:20 nigelhorne |
53 | | * First draft |
54 | | * |
55 | | */ |
56 | | |
57 | | #if HAVE_CONFIG_H |
58 | | #include "clamav-config.h" |
59 | | #endif |
60 | | |
61 | | #include <stdio.h> |
62 | | #include <string.h> |
63 | | #include <assert.h> |
64 | | |
65 | | #include "clamav.h" |
66 | | #include "line.h" |
67 | | #include "others.h" |
68 | | |
69 | | line_t * |
70 | | lineCreate(const char *data) |
71 | 80.0M | { |
72 | 80.0M | const size_t size = strlen(data); |
73 | 80.0M | line_t *ret = (line_t *)cli_malloc(size + 2); |
74 | | |
75 | 80.0M | if (ret == NULL) { |
76 | 0 | cli_errmsg("lineCreate: Unable to allocate memory for ret\n"); |
77 | 0 | return (line_t *)NULL; |
78 | 0 | } |
79 | | |
80 | 80.0M | ret[0] = (char)1; |
81 | | /*strcpy(&ret[1], data);*/ |
82 | 80.0M | memcpy(&ret[1], data, size); |
83 | 80.0M | ret[size + 1] = '\0'; |
84 | | |
85 | 80.0M | return ret; |
86 | 80.0M | } |
87 | | |
88 | | line_t * |
89 | | lineLink(line_t *line) |
90 | 106M | { |
91 | 106M | assert(line != NULL); |
92 | 106M | if ((unsigned char)line[0] == (unsigned char)255) { |
93 | 3.28M | cli_dbgmsg("lineLink: linkcount too large (%s)\n", lineGetData(line)); |
94 | 3.28M | return lineCreate(lineGetData(line)); |
95 | 3.28M | } |
96 | 103M | line[0]++; |
97 | | /*printf("%d:\n\t'%s'\n", (int)line[0], &line[1]);*/ |
98 | 103M | return line; |
99 | 106M | } |
100 | | |
101 | | line_t * |
102 | | lineUnlink(line_t *line) |
103 | 183M | { |
104 | | /*printf("%d:\n\t'%s'\n", (int)line[0], &line[1]);*/ |
105 | | |
106 | 183M | if (--line[0] == 0) { |
107 | 80.0M | free(line); |
108 | 80.0M | return NULL; |
109 | 80.0M | } |
110 | 103M | return line; |
111 | 183M | } |
112 | | |
113 | | const char * |
114 | | lineGetData(const line_t *line) |
115 | 497M | { |
116 | 497M | return line ? &line[1] : NULL; |
117 | 497M | } |