1 /* Part of libhgfs - (c) 2009, D.C. van Moolenbroek */ 2 3 #include "inc.h" 4 5 #include <limits.h> 6 7 /*===========================================================================* 8 * path_put * 9 *===========================================================================*/ 10 void path_put(const char *path) 11 { 12 /* Append the given path name in HGFS format to the RPC buffer. Truncate it 13 * if it is longer than PATH_MAX bytes. 14 */ 15 const char *p; 16 char buf[PATH_MAX]; 17 unsigned int len; 18 19 /* No leading slashes are allowed. */ 20 for (p = path; *p == '/'; p++); 21 22 /* No double or tailing slashes, either. */ 23 for (len = 0; *p && len < sizeof(buf) - 1; len++) { 24 if (*p == '/') { 25 for (p++; *p == '/'; p++); 26 27 if (!*p) break; 28 29 buf[len] = 0; 30 } 31 else buf[len] = *p++; 32 } 33 34 RPC_NEXT32 = len; 35 36 memcpy(RPC_PTR, buf, len); 37 RPC_ADVANCE(len); 38 39 RPC_NEXT8 = 0; 40 } 41 42 /*===========================================================================* 43 * path_get * 44 *===========================================================================*/ 45 int path_get(char *path, int max) 46 { 47 /* Retrieve a HGFS formatted path name from the RPC buffer. Returns EINVAL if 48 * the path name is invalid. Returns ENAMETOOLONG if the path name is too 49 * long. Returns OK on success. 50 */ 51 char *p, *q; 52 int n, len; 53 54 n = len = RPC_NEXT32; 55 56 if (len >= max) return ENAMETOOLONG; 57 58 for (p = path, q = RPC_PTR; n--; p++, q++) { 59 /* We can not deal with a slash in a path component. */ 60 if (*q == '/') return EINVAL; 61 62 if (*q == 0) *p = '/'; 63 else *p = *q; 64 } 65 66 RPC_ADVANCE(len); 67 68 *p = 0; 69 70 return (RPC_NEXT8 != 0) ? EINVAL : OK; 71 } 72