1 /*------------------------------------------------------------------
2  * strremovews_s.c
3  *
4  * November 2008, Bo Berry
5  *
6  * Copyright (c) 2008-2011 by Cisco Systems, Inc
7  * All rights resevered.
8  *
9  * Permission is hereby granted, free of charge, to any person
10  * obtaining a copy of this software and associated documentation
11  * files (the "Software"), to deal in the Software without
12  * restriction, including without limitation the rights to use,
13  * copy, modify, merge, publish, distribute, sublicense, and/or
14  * sell copies of the Software, and to permit persons to whom the
15  * Software is furnished to do so, subject to the following
16  * conditions:
17  *
18  * The above copyright notice and this permission notice shall be
19  * included in all copies or substantial portions of the Software.
20  *
21  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
23  * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24  * NONINFRINGEMENT.  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
25  * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
26  * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
27  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
28  * OTHER DEALINGS IN THE SOFTWARE.
29  *------------------------------------------------------------------
30  */
31 
32 #include "safeclib_private.h"
33 #include "safe_str_constraint.h"
34 #include "safe_str_lib.h"
35 
36 
37 /**
38  * NAME
39  *    strremovews_s
40  *
41  * SYNOPSIS
42  *    #include "safe_str_lib.h"
43  *    errno_t
44  *    strremovews_s(char *dest, rsize_t dmax)
45  *
46  * DESCRIPTION
47  *    Removes beginning and trailing whitespace from the string pointed to by
48  *    dest by shifting the text left over writting the beginning whitespace.
49  *    The shifted-trimmed text is null terminated.
50  *
51  *    The text is shifted so the original pointer can continue to be used. This
52  *    is useful when the memory was malloc'ed and will need to be freed.
53  *
54  * EXTENSION TO
55  *    ISO/IEC TR 24731, Programming languages, environments
56  *    and system software interfaces, Extensions to the C Library,
57  *    Part I: Bounds-checking interfaces
58  *
59  * INPUT PARAMETERS
60  *    dest    pointer to string to remove whitespace
61  *
62  *    dmax    restricted maximum length of string
63  *
64  * RUNTIME CONSTRAINTS
65  *    dest shall not be a null pointer.
66  *    dmax shall not be 0
67  *    dmax shall not be greater than RSIZE_MAX_STR
68  *    dest shall be null terminated
69  *
70  * RETURN VALUE
71  *    EOK
72  *    ESNULLP     NULL pointer
73  *    ESZEROL     zero length
74  *    ESLEMAX     length exceeds max limit
75  *    ESUNTERM    dest was not null terminated
76  *
77  * SEE ALSO
78  *    strljustify_s(),
79  *
80  */
81 errno_t
strremovews_s(char * dest,rsize_t dmax)82 strremovews_s (char *dest, rsize_t dmax)
83 {
84     char *orig_dest;
85     char *orig_end;
86     rsize_t orig_dmax;
87 
88     if (dest == NULL) {
89         invoke_safe_str_constraint_handler("strremovews_s: dest is null",
90                    NULL, ESNULLP);
91         return (ESNULLP);
92     }
93 
94     if (dmax == 0 ) {
95         invoke_safe_str_constraint_handler("strremovews_s: dmax is 0",
96                    NULL, ESZEROL);
97         return (ESZEROL);
98     }
99 
100     if (dmax > RSIZE_MAX_STR) {
101         invoke_safe_str_constraint_handler("strremovews_s: dmax exceeds max",
102                    NULL, ESLEMAX);
103         return (ESLEMAX);
104     }
105 
106     /*
107      * corner case, a dmax of one requires a null
108      */
109     if (*dest == '\0' || dmax <= RSIZE_MIN_STR) {
110         *dest = '\0';
111         return (EOK);
112     }
113 
114     orig_dest = dest;
115     orig_dmax = dmax;
116 
117      /*
118       * scan the string to be sure it is properly terminated
119       */
120      while (*dest) {
121         if (dmax == 0) {
122             while (orig_dmax) { *orig_dest++ = '\0';  orig_dmax--; }
123 
124             invoke_safe_str_constraint_handler(
125                       "strremovews_s: dest is unterminated",
126                        NULL, ESUNTERM);
127             return (ESUNTERM);
128         }
129         dmax--;
130         dest++;
131     }
132 
133     /*
134      * find first non-white space char
135      */
136     orig_end = dest-1;
137     dest = orig_dest;
138     while ((*dest == ' ') || (*dest == '\t')) {
139         dest++;
140     }
141 
142     /*
143      * shift the text over the leading spaces
144      */
145     if (orig_dest != dest && *dest) {
146         while (*dest) {
147             *orig_dest++ = *dest;
148             *dest++ = ' ';
149         }
150         *dest = '\0';
151      }
152 
153     /*
154      * strip trailing whitespace
155      */
156     dest = orig_end;
157     while ((*dest == ' ') || (*dest == '\t')) {
158         *dest = '\0';
159         dest--;
160     }
161 
162     return (EOK);
163 }
164 EXPORT_SYMBOL(strremovews_s)
165