summaryrefslogtreecommitdiff
path: root/firmware/common
diff options
context:
space:
mode:
Diffstat (limited to 'firmware/common')
-rw-r--r--firmware/common/dir_uncached.c11
-rw-r--r--firmware/common/dircache.c28
-rw-r--r--firmware/common/file.c3
-rw-r--r--firmware/common/strlcpy.c52
-rw-r--r--firmware/common/strncpy.c125
5 files changed, 71 insertions, 148 deletions
diff --git a/firmware/common/dir_uncached.c b/firmware/common/dir_uncached.c
index c6af145930..25677a0903 100644
--- a/firmware/common/dir_uncached.c
+++ b/firmware/common/dir_uncached.c
@@ -58,8 +58,7 @@ int strip_volume(const char* name, char* namecopy)
58 name = "/"; /* else this must be the root dir */ 58 name = "/"; /* else this must be the root dir */
59 } 59 }
60 60
61 strncpy(namecopy, name, MAX_PATH); 61 strlcpy(namecopy, name, MAX_PATH);
62 namecopy[MAX_PATH-1] = '\0';
63 62
64 return volume; 63 return volume;
65} 64}
@@ -120,8 +119,7 @@ DIR_UNCACHED* opendir_uncached(const char* name)
120 volume = strip_volume(name, namecopy); 119 volume = strip_volume(name, namecopy);
121 pdir->volumecounter = 0; 120 pdir->volumecounter = 0;
122#else 121#else
123 strncpy(namecopy,name,sizeof(namecopy)); /* just copy */ 122 strlcpy(namecopy, name, sizeof(namecopy)); /* just copy */
124 namecopy[sizeof(namecopy)-1] = '\0';
125#endif 123#endif
126 124
127 if ( fat_opendir(IF_MV2(volume,) &pdir->fatdir, 0, NULL) < 0 ) { 125 if ( fat_opendir(IF_MV2(volume,) &pdir->fatdir, 0, NULL) < 0 ) {
@@ -204,7 +202,7 @@ struct dirent_uncached* readdir_uncached(DIR_UNCACHED* dir)
204 if ( !entry.name[0] ) 202 if ( !entry.name[0] )
205 return NULL; 203 return NULL;
206 204
207 strncpy(theent->d_name, entry.name, sizeof( theent->d_name ) ); 205 strlcpy(theent->d_name, entry.name, sizeof(theent->d_name));
208 theent->attribute = entry.attr; 206 theent->attribute = entry.attr;
209 theent->size = entry.filesize; 207 theent->size = entry.filesize;
210 theent->startcluster = entry.firstcluster; 208 theent->startcluster = entry.firstcluster;
@@ -230,8 +228,7 @@ int mkdir_uncached(const char *name)
230 return -1; 228 return -1;
231 } 229 }
232 230
233 strncpy(namecopy,name,sizeof(namecopy)); 231 strlcpy(namecopy, name, sizeof(namecopy));
234 namecopy[sizeof(namecopy)-1] = 0;
235 232
236 /* Split the base name and the path */ 233 /* Split the base name and the path */
237 end = strrchr(namecopy, '/'); 234 end = strrchr(namecopy, '/');
diff --git a/firmware/common/dircache.c b/firmware/common/dircache.c
index 01ed72adc1..e846d55452 100644
--- a/firmware/common/dircache.c
+++ b/firmware/common/dircache.c
@@ -232,11 +232,11 @@ static int dircache_scan(IF_MV2(int volume,) struct travel_data *td)
232 return -2; 232 return -2;
233 233
234 td->pathpos = strlen(dircache_cur_path); 234 td->pathpos = strlen(dircache_cur_path);
235 strncpy(&dircache_cur_path[td->pathpos], "/", 235 strlcpy(&dircache_cur_path[td->pathpos], "/",
236 sizeof(dircache_cur_path) - td->pathpos - 1); 236 sizeof(dircache_cur_path) - td->pathpos);
237#ifdef SIMULATOR 237#ifdef SIMULATOR
238 strncpy(&dircache_cur_path[td->pathpos+1], td->entry->d_name, 238 strlcpy(&dircache_cur_path[td->pathpos+1], td->entry->d_name,
239 sizeof(dircache_cur_path) - td->pathpos - 2); 239 sizeof(dircache_cur_path) - td->pathpos - 1);
240 240
241 td->newdir = opendir_uncached(dircache_cur_path); 241 td->newdir = opendir_uncached(dircache_cur_path);
242 if (td->newdir == NULL) 242 if (td->newdir == NULL)
@@ -245,8 +245,8 @@ static int dircache_scan(IF_MV2(int volume,) struct travel_data *td)
245 return -3; 245 return -3;
246 } 246 }
247#else 247#else
248 strncpy(&dircache_cur_path[td->pathpos+1], td->entry.name, 248 strlcpy(&dircache_cur_path[td->pathpos+1], td->entry.name,
249 sizeof(dircache_cur_path) - td->pathpos - 2); 249 sizeof(dircache_cur_path) - td->pathpos - 1);
250 250
251 td->newdir = *td->dir; 251 td->newdir = *td->dir;
252 if (fat_opendir(IF_MV2(volume,) &td->newdir, 252 if (fat_opendir(IF_MV2(volume,) &td->newdir,
@@ -399,7 +399,7 @@ static struct dircache_entry* dircache_get_entry(const char *path,
399 char* part; 399 char* part;
400 char* end; 400 char* end;
401 401
402 strncpy(namecopy, path, sizeof(namecopy) - 1); 402 strlcpy(namecopy, path, sizeof(namecopy));
403 cache_entry = dircache_root; 403 cache_entry = dircache_root;
404 before = NULL; 404 before = NULL;
405 405
@@ -926,7 +926,7 @@ static struct dircache_entry* dircache_new_entry(const char *path, int attribute
926 char *new; 926 char *new;
927 long last_cache_size = dircache_size; 927 long last_cache_size = dircache_size;
928 928
929 strncpy(basedir, path, sizeof(basedir)-1); 929 strlcpy(basedir, path, sizeof(basedir));
930 new = strrchr(basedir, '/'); 930 new = strrchr(basedir, '/');
931 if (new == NULL) 931 if (new == NULL)
932 { 932 {
@@ -997,8 +997,8 @@ void dircache_bind(int fd, const char *path)
997 { 997 {
998 if (fdbind_idx >= MAX_PENDING_BINDINGS) 998 if (fdbind_idx >= MAX_PENDING_BINDINGS)
999 return ; 999 return ;
1000 strncpy(fdbind_cache[fdbind_idx].path, path, 1000 strlcpy(fdbind_cache[fdbind_idx].path, path,
1001 sizeof(fdbind_cache[fdbind_idx].path)-1); 1001 sizeof(fdbind_cache[fdbind_idx].path));
1002 fdbind_cache[fdbind_idx].fd = fd; 1002 fdbind_cache[fdbind_idx].fd = fd;
1003 fdbind_idx++; 1003 fdbind_idx++;
1004 return ; 1004 return ;
@@ -1141,7 +1141,7 @@ void dircache_rename(const char *oldpath, const char *newpath)
1141 /* Generate the absolute path for destination if necessary. */ 1141 /* Generate the absolute path for destination if necessary. */
1142 if (newpath[0] != '/') 1142 if (newpath[0] != '/')
1143 { 1143 {
1144 strncpy(absolute_path, oldpath, sizeof(absolute_path)-1); 1144 strlcpy(absolute_path, oldpath, sizeof(absolute_path));
1145 p = strrchr(absolute_path, '/'); 1145 p = strrchr(absolute_path, '/');
1146 if (!p) 1146 if (!p)
1147 { 1147 {
@@ -1151,7 +1151,7 @@ void dircache_rename(const char *oldpath, const char *newpath)
1151 } 1151 }
1152 1152
1153 *p = '\0'; 1153 *p = '\0';
1154 strncpy(p, absolute_path, sizeof(absolute_path)-1-strlen(p)); 1154 strlcpy(p, absolute_path, sizeof(absolute_path)-strlen(p));
1155 newpath = absolute_path; 1155 newpath = absolute_path;
1156 } 1156 }
1157 1157
@@ -1246,7 +1246,7 @@ struct dircache_entry* readdir_cached(DIR_CACHED* dir)
1246 if (regentry == NULL) 1246 if (regentry == NULL)
1247 return NULL; 1247 return NULL;
1248 1248
1249 strncpy(dir->secondary_entry.d_name, regentry->d_name, MAX_PATH-1); 1249 strlcpy(dir->secondary_entry.d_name, regentry->d_name, MAX_PATH);
1250 dir->secondary_entry.size = regentry->size; 1250 dir->secondary_entry.size = regentry->size;
1251 dir->secondary_entry.startcluster = regentry->startcluster; 1251 dir->secondary_entry.startcluster = regentry->startcluster;
1252 dir->secondary_entry.attribute = regentry->attribute; 1252 dir->secondary_entry.attribute = regentry->attribute;
@@ -1268,7 +1268,7 @@ struct dircache_entry* readdir_cached(DIR_CACHED* dir)
1268 1268
1269 dir->entry = ce->next; 1269 dir->entry = ce->next;
1270 1270
1271 strncpy(dir->secondary_entry.d_name, ce->d_name, MAX_PATH-1); 1271 strlcpy(dir->secondary_entry.d_name, ce->d_name, MAX_PATH);
1272 /* Can't do `dir->secondary_entry = *ce` 1272 /* Can't do `dir->secondary_entry = *ce`
1273 because that modifies the d_name pointer. */ 1273 because that modifies the d_name pointer. */
1274 dir->secondary_entry.size = ce->size; 1274 dir->secondary_entry.size = ce->size;
diff --git a/firmware/common/file.c b/firmware/common/file.c
index 7c01f03817..3d7722f687 100644
--- a/firmware/common/file.c
+++ b/firmware/common/file.c
@@ -132,8 +132,7 @@ static int open_internal(const char* pathname, int flags, bool use_cache)
132 } 132 }
133#endif 133#endif
134 134
135 strncpy(pathnamecopy,pathname,sizeof(pathnamecopy)); 135 strlcpy(pathnamecopy, pathname, sizeof(pathnamecopy));
136 pathnamecopy[sizeof(pathnamecopy)-1] = 0;
137 136
138 /* locate filename */ 137 /* locate filename */
139 name=strrchr(pathnamecopy+1,'/'); 138 name=strrchr(pathnamecopy+1,'/');
diff --git a/firmware/common/strlcpy.c b/firmware/common/strlcpy.c
new file mode 100644
index 0000000000..ac30ef01fe
--- /dev/null
+++ b/firmware/common/strlcpy.c
@@ -0,0 +1,52 @@
1/* $OpenBSD: strlcpy.c,v 1.11 2006/05/05 15:27:38 millert Exp $ */
2
3/*
4 * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com>
5 *
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18
19#include <sys/types.h>
20#include <string.h>
21
22/*
23 * Copy src to string dst of size siz. At most siz-1 characters
24 * will be copied. Always NUL terminates (unless siz == 0).
25 * Returns strlen(src); if retval >= siz, truncation occurred.
26 */
27size_t
28strlcpy(char *dst, const char *src, size_t siz)
29{
30 char *d = dst;
31 const char *s = src;
32 size_t n = siz;
33
34 /* Copy as many bytes as will fit */
35 if (n != 0) {
36 while (--n != 0) {
37 if ((*d++ = *s++) == '\0')
38 break;
39 }
40 }
41
42 /* Not enough room in dst, add NUL and traverse rest of src */
43 if (n == 0) {
44 if (siz != 0)
45 *d = '\0'; /* NUL-terminate dst */
46 while (*s++)
47 ;
48 }
49
50 return(s - src - 1); /* count does not include NUL */
51}
52
diff --git a/firmware/common/strncpy.c b/firmware/common/strncpy.c
deleted file mode 100644
index 7c1973ba66..0000000000
--- a/firmware/common/strncpy.c
+++ /dev/null
@@ -1,125 +0,0 @@
1/*
2FUNCTION
3 <<strncpy>>---counted copy string
4
5INDEX
6 strncpy
7
8ANSI_SYNOPSIS
9 #include <string.h>
10 char *strncpy(char *<[dst]>, const char *<[src]>, size_t <[length]>);
11
12TRAD_SYNOPSIS
13 #include <string.h>
14 char *strncpy(<[dst]>, <[src]>, <[length]>)
15 char *<[dst]>;
16 char *<[src]>;
17 size_t <[length]>;
18
19DESCRIPTION
20 <<strncpy>> copies not more than <[length]> characters from the
21 the string pointed to by <[src]> (including the terminating
22 null character) to the array pointed to by <[dst]>. If the
23 string pointed to by <[src]> is shorter than <[length]>
24 characters, null characters are appended to the destination
25 array until a total of <[length]> characters have been
26 written.
27
28RETURNS
29 This function returns the initial value of <[dst]>.
30
31PORTABILITY
32<<strncpy>> is ANSI C.
33
34<<strncpy>> requires no supporting OS subroutines.
35
36QUICKREF
37 strncpy ansi pure
38*/
39
40#include <string.h>
41#include <limits.h>
42
43/*SUPPRESS 560*/
44/*SUPPRESS 530*/
45
46/* Nonzero if either X or Y is not aligned on a "long" boundary. */
47#define UNALIGNED(X, Y) \
48 (((long)X & (sizeof (long) - 1)) | ((long)Y & (sizeof (long) - 1)))
49
50#if LONG_MAX == 2147483647L
51#define DETECTNULL(X) (((X) - 0x01010101) & ~(X) & 0x80808080)
52#else
53#if LONG_MAX == 9223372036854775807L
54/* Nonzero if X (a long int) contains a NULL byte. */
55#define DETECTNULL(X) (((X) - 0x0101010101010101) & ~(X) & 0x8080808080808080)
56#else
57#error long int is not a 32bit or 64bit type.
58#endif
59#endif
60
61#ifndef DETECTNULL
62#error long int is not a 32bit or 64bit byte
63#endif
64
65#define TOO_SMALL(LEN) ((LEN) < sizeof (long))
66
67char *
68_DEFUN (strncpy, (dst0, src0),
69 char *dst0 _AND
70 _CONST char *src0 _AND
71 size_t count)
72{
73#if defined(PREFER_SIZE_OVER_SPEED) || defined(__OPTIMIZE_SIZE__)
74 char *dscan;
75 _CONST char *sscan;
76
77 dscan = dst0;
78 sscan = src0;
79 while (count > 0)
80 {
81 --count;
82 if ((*dscan++ = *sscan++) == '\0')
83 break;
84 }
85 while (count-- > 0)
86 *dscan++ = '\0';
87
88 return dst0;
89#else
90 char *dst = dst0;
91 _CONST char *src = src0;
92 long *aligned_dst;
93 _CONST long *aligned_src;
94
95 /* If SRC and DEST is aligned and count large enough, then copy words. */
96 if (!UNALIGNED (src, dst) && !TOO_SMALL (count))
97 {
98 aligned_dst = (long*)dst;
99 aligned_src = (long*)src;
100
101 /* SRC and DEST are both "long int" aligned, try to do "long int"
102 sized copies. */
103 while (count >= sizeof (long int) && !DETECTNULL(*aligned_src))
104 {
105 count -= sizeof (long int);
106 *aligned_dst++ = *aligned_src++;
107 }
108
109 dst = (char*)aligned_dst;
110 src = (char*)aligned_src;
111 }
112
113 while (count > 0)
114 {
115 --count;
116 if ((*dst++ = *src++) == '\0')
117 break;
118 }
119
120 while (count-- > 0)
121 *dst++ = '\0';
122
123 return dst0;
124#endif /* not PREFER_SIZE_OVER_SPEED */
125}