irods  4.2.8
About: iRODS (the integrated Rule Oriented Data System) is a distributed data-management system for creating data grids, digital libraries, persistent archives, and real-time data systems.
  Fossies Dox: irods-4.2.8.tar.gz  ("unofficial" and yet experimental doxygen-generated source code documentation)  

stringOpr.cpp
Go to the documentation of this file.
1 
3 /* stringOpr - a number of string operations designed for secure string
4  * copying.
5  */
6 
7 #include "stringOpr.h"
8 #include "rodsErrorTable.h"
9 #include "rodsLog.h"
10 #include <string>
11 #include "boost/regex.hpp"
12 
13 #include "irods_stacktrace.hpp"
14 
15 char *rmemmove( void *dest, const void *src, int strLen, int maxLen ) {
16 
17  if ( dest == NULL || src == NULL ) {
18  return NULL;
19  }
20 
21  if ( strLen > maxLen ) {
22  return NULL;
23  }
24 
25  if ( memmove( dest, src, strLen ) != NULL ) {
26  return ( char* )dest;
27  }
28  else {
29  return NULL;
30  }
31 }
32 
33 char *rmemcpy( void *dest, const void *src, int strLen, int maxLen ) {
34 
35  if ( dest == NULL || src == NULL ) {
36  return NULL;
37  }
38 
39  if ( strLen > maxLen ) {
40  return NULL;
41  }
42 
43  if ( memcpy( dest, src, strLen ) != NULL ) {
44  return ( char* )dest;
45  }
46  else {
47  return NULL;
48  }
49 }
50 
51 char *rstrcpy( char *dest, const char *src, int maxLen ) {
52  // snprintf with logging on fail
53 
54  if ( dest == NULL || src == NULL ) {
55  return NULL;
56  }
57  int status = snprintf( dest, maxLen, "%s", src );
58 
59  if ( status >= 0 && status < maxLen ) {
60  return dest;
61  }
62  else if ( status >= 0 ) {
64  "rstrcpy not enough space in dest, slen:%d, maxLen:%d, stacktrace:\n%s",
65  status, maxLen, irods::stacktrace().dump().c_str());
66  rodsLog( LOG_DEBUG, "rstrcpy arguments dest [%s] src [%s]", dest, src );
67  return NULL;
68  }
69  else {
70  rodsLog( LOG_ERROR, "rstrcpy encountered an encoding error." );
71  return NULL;
72  }
73 }
74 
75 char *rstrcat( char *dest, const char *src, int maxLen ) {
76  /* rods strcat: like strncat but make sure the dest doesn't overflow.
77  maxLen is actually max length that can be stored in dest, not
78  just how much of src needs to be copied. Hence the
79  semantics is different from strncat.
80  */
81 
82  int dlen, slen;
83 
84  if ( dest == NULL || src == NULL ) {
85  return NULL;
86  }
87 
88  dlen = strlen( dest );
89  slen = strlen( src );
90 
91  if ( slen + dlen >= maxLen ) {
93  "rstrcat not enough space in dest, slen:%d, dlen:%d, maxLen:%d, stacktrace:\n%s",
94  slen, dlen, maxLen, irods::stacktrace().dump().c_str() );
95  rodsLog( LOG_DEBUG, "rstrcat arguments dest [%s] src [%s]", dest, src );
96  return NULL;
97  }
98 
99  return strncat( dest, src, slen );
100 }
101 
102 /* rods strncat: like strncat but make sure the dest doesn't overflow.
103  maxLen is the max length that can be stored in dest,
104  srcLen is the length to copy.
105 */
106 char *rstrncat( char *dest, const char *src, int srcLen, int maxLen ) {
107 
108  int dlen, slen;
109 
110  if ( dest == NULL || src == NULL ) {
111  return NULL;
112  }
113 
114  dlen = strlen( dest );
115  slen = srcLen;
116 
117  if ( slen + dlen >= maxLen ) {
119  "rstrncat not enough space in dest, slen:%d, dlen:%d, maxLen:%d, stacktrace:\n%s",
120  slen, dlen, maxLen, irods::stacktrace().dump().c_str() );
121  rodsLog( LOG_DEBUG, "rstrncat arguments: dest [%s] src [%s]", dest, src );
122  return NULL;
123  }
124 
125  return strncat( dest, src, slen );
126 }
127 
128 int
129 rSplitStr( const char *inStr, char* outStr1, size_t maxOutLen1,
130  char* outStr2, size_t maxOutLen2, char key ) {
131  std::string base_string( inStr );
132  size_t index_of_first_key = base_string.find( key );
133  if ( std::string::npos == index_of_first_key ) {
134  index_of_first_key = base_string.size();
135  }
136  strncpy( outStr1, base_string.substr( 0, index_of_first_key ).c_str(), maxOutLen1 );
137  if ( maxOutLen1 > 0 ) {
138  outStr1[ maxOutLen1 - 1 ] = '\0';
139  }
140  if ( index_of_first_key >= maxOutLen1 ) {
141  return USER_STRLEN_TOOLONG;
142  }
143 
144  /* copy the second str */
145  size_t copy_start = base_string.size() == index_of_first_key ? base_string.size() : index_of_first_key + 1;
146  if ( rstrcpy( outStr2, base_string.substr( copy_start ).c_str(), maxOutLen2 ) == NULL ) {
147  return USER_STRLEN_TOOLONG;
148  }
149  return 0;
150 }
151 /* copyStrFromBuf - copy a string from buf to outStr, skipping white space
152  * and comment. also advance buf pointer
153  * returns the len of string copied
154  */
155 
156 int
157 copyStrFromBuf( char **buf, char *outStr, int maxOutLen ) {
158  char *bufPtr, *outPtr;
159  int len;
160  int gotSpace;
161 
162  bufPtr = *buf;
163  gotSpace = 0;
164 
165  /* skip over any space */
166 
167  while ( 1 ) {
168  if ( *bufPtr == '\0' || *bufPtr == '\n' ) {
169  return 0;
170  }
171  /* '#' must be preceded by a space to be a valid comment.
172  * the calling routine must check if the line starts with a # */
173 
174  if ( *bufPtr == '#' && gotSpace > 0 ) {
175  *outStr = '\0';
176  return 0;
177  }
178 
179  if ( isspace( *bufPtr ) ) {
180  bufPtr ++;
181  gotSpace ++;
182  continue;
183  }
184  else {
185  break;
186  }
187  }
188 
189  len = 0;
190  outPtr = outStr;
191  while ( !isspace( *bufPtr ) && *bufPtr != '\0' ) {
192  len++;
193  if ( len >= maxOutLen ) {
194  *outStr = '\0';
195  return USER_STRLEN_TOOLONG;
196  }
197  *outPtr = *bufPtr;
198  outPtr++;
199  bufPtr++;
200  }
201 
202  *outPtr = '\0';
203  *buf = bufPtr;
204 
205  return len;
206 }
207 
208 int
209 isAllDigit( const char * myStr ) {
210  char c;
211 
212  while ( ( c = *myStr ) != '\0' ) {
213  if ( isdigit( c ) == 0 ) {
214  return 0;
215  }
216  myStr++;
217  }
218  return 1;
219 }
220 
221 int
222 splitPathByKey( const char * srcPath, char * dir, size_t maxDirLen,
223  char * file, size_t maxFileLen, char key ) {
224  if ( maxDirLen == 0 || maxFileLen == 0 ) {
225  rodsLog( LOG_ERROR, "splitPathByKey called with buffers of size 0" );
227  }
228 
229  const std::string srcPathString( srcPath );
230  if ( srcPathString.size() == 0 ) {
231  *dir = '\0';
232  *file = '\0';
233  return 0;
234  }
235 
236  const size_t index_of_last_key = srcPathString.rfind( key );
237  if ( std::string::npos == index_of_last_key ) {
238  *dir = '\0';
239  rstrcpy( file, srcPathString.c_str(), maxFileLen );
240  return SYS_INVALID_FILE_PATH;
241  }
242 
243  // If dir is the root directory, we want to return the single-character
244  // string consisting of the key, NOT the empty string.
245  const std::string dirPathString = srcPathString.substr( 0, std::max< size_t >( index_of_last_key, 1 ) );
246  const std::string filePathString = srcPathString.substr( index_of_last_key + 1 ) ;
247 
248  if ( dirPathString.size() >= maxDirLen || filePathString.size() >= maxFileLen ) {
249  rodsLog( LOG_ERROR, "splitPathByKey called with buffers of insufficient size" );
250  return USER_STRLEN_TOOLONG;
251  }
252 
253  rstrcpy( dir, dirPathString.c_str(), maxDirLen );
254  rstrcpy( file, filePathString.c_str(), maxFileLen );
255 
256  return 0;
257 
258 }
259 int
260 trimWS( char * s ) {
261  char *t;
262 
263  t = s;
264  while ( isspace( *t ) ) {
265  t++;
266  }
267  if ( s != t ) {
268  memmove( s, t, strlen( t ) + 1 );
269  }
270  t = s + strlen( s ) - 1;
271  while ( isspace( *t ) ) {
272  t--;
273  }
274  *( t + 1 ) = '\0';
275 
276  /*TODO Please return appropriate value*/
277  return 0;
278 }
279 int
280 trimQuotes( char * s ) {
281  char *t;
282 
283  if ( *s == '\'' || *s == '"' ) {
284  memmove( s, s + 1, strlen( s + 1 ) + 1 );
285  t = s + strlen( s ) - 1;
286  if ( *t == '\'' || *t == '"' ) {
287  *t = '\0';
288  }
289  }
290  /* made it so that end quotes are removed only if quoted initially */
291  /*TODO Please return appropriate value*/
292  return 0;
293 }
294 
295 int
296 checkStringForSystem( const char * inString ) {
297  if ( inString == NULL ) {
298  return 0;
299  }
300  if ( boost::regex_match( inString, boost::regex( "[a-zA-Z0-9,./ ]*" ) ) ) {
301  return 0;
302  }
303  return USER_INPUT_STRING_ERR;
304 }
305 
306 /*
307  * Check if inString is a valid email address.
308  * This function only do a simple check that inString contains only a predefined set of characters.
309  * It does not check the structure.
310  * And this set of characters is a subset of that allowed in the RFCs.
311  */
312 int
313 checkStringForEmailAddress( const char * inString ) {
314  if ( inString == NULL ) {
315  return 0;
316  }
317  if ( boost::regex_match( inString, boost::regex( "[-a-zA-Z0-9,./+*_@]*" ) ) ) {
318  return 0;
319  }
320  return USER_INPUT_STRING_ERR;
321 }
rodsLog
void rodsLog(int level, const char *formatStr,...)
Definition: rodsLog.cpp:86
NULL
#define NULL
Definition: rodsDef.h:70
rstrcat
char * rstrcat(char *dest, const char *src, int maxLen)
Definition: stringOpr.cpp:75
checkStringForSystem
int checkStringForSystem(const char *inString)
Definition: stringOpr.cpp:296
irods_stacktrace.hpp
trimQuotes
int trimQuotes(char *s)
Definition: stringOpr.cpp:280
isAllDigit
int isAllDigit(const char *myStr)
Definition: stringOpr.cpp:209
irods::stacktrace
Definition: irods_stacktrace.hpp:11
splitPathByKey
int splitPathByKey(const char *srcPath, char *dir, size_t maxDirLen, char *file, size_t maxFileLen, char key)
Definition: stringOpr.cpp:222
LOG_ERROR
#define LOG_ERROR
Definition: rodsLog.h:43
SYS_INVALID_INPUT_PARAM
@ SYS_INVALID_INPUT_PARAM
Definition: rodsErrorTable.h:195
checkStringForEmailAddress
int checkStringForEmailAddress(const char *inString)
Definition: stringOpr.cpp:313
LOG_DEBUG
#define LOG_DEBUG
Definition: rodsLog.h:23
rmemmove
char * rmemmove(void *dest, const void *src, int strLen, int maxLen)
Definition: stringOpr.cpp:15
run_tests.dest
dest
Definition: run_tests.py:123
irods.pypyodbc.status
status
Definition: pypyodbc.py:467
trimWS
int trimWS(char *s)
Definition: stringOpr.cpp:260
rodsLog.h
rstrcpy
char * rstrcpy(char *dest, const char *src, int maxLen)
Definition: stringOpr.cpp:51
SYS_INVALID_FILE_PATH
@ SYS_INVALID_FILE_PATH
Definition: rodsErrorTable.h:99
rSplitStr
int rSplitStr(const char *inStr, char *outStr1, size_t maxOutLen1, char *outStr2, size_t maxOutLen2, char key)
Definition: stringOpr.cpp:129
stringOpr.h
update_schema_ids_for_cmake.file
file
Definition: update_schema_ids_for_cmake.py:21
USER_STRLEN_TOOLONG
@ USER_STRLEN_TOOLONG
Definition: rodsErrorTable.h:237
rodsErrorTable.h
USER_INPUT_STRING_ERR
@ USER_INPUT_STRING_ERR
Definition: rodsErrorTable.h:281
rmemcpy
char * rmemcpy(void *dest, const void *src, int strLen, int maxLen)
Definition: stringOpr.cpp:33
rstrncat
char * rstrncat(char *dest, const char *src, int srcLen, int maxLen)
Definition: stringOpr.cpp:106
buf
static char buf[64+50+1]
Definition: rsAuthRequest.cpp:21
copyStrFromBuf
int copyStrFromBuf(char **buf, char *outStr, int maxOutLen)
Definition: stringOpr.cpp:157