summaryrefslogtreecommitdiff
path: root/open-vm-tools/lib/hgfs/cpName.c
blob: 4cd0d3746c116073b1bc0beb50dd462597eece9b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
/*********************************************************
 * Copyright (C) 1998-2015 VMware, Inc. All rights reserved.
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published
 * by the Free Software Foundation version 2.1 and no later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the Lesser GNU General Public
 * License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA.
 *
 *********************************************************/

/*********************************************************
 * The contents of this file are subject to the terms of the Common
 * Development and Distribution License (the "License") version 1.0
 * and no later version.  You may not use this file except in
 * compliance with the License.
 *
 * You can obtain a copy of the License at
 *         http://www.opensource.org/licenses/cddl1.php
 *
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 *********************************************************/

/*
 * cpName.c --
 *
 *    Shared portions of cross-platform name conversion routines used
 *    by hgfs. [bac]
 *
 */

#ifdef sun
#include <string.h>
#endif

#include "cpName.h"
#include "cpNameInt.h"
#include "vm_assert.h"
#include "hgfsEscape.h"

/*
 *----------------------------------------------------------------------
 *
 * CPName_GetComponent --
 *
 *    Get the next component of the CP name.
 *
 *    Returns the length of the component starting with the begin
 *    pointer, and a pointer to the next component in the buffer, if
 *    any. The "next" pointer is set to "end" if there is no next
 *    component.
 *
 * Results:
 *    length (not including NUL termination) >= 0 of next
 *    component on success.
 *    error < 0 on failure (invalid component).
 *
 * Side effects:
 *    None
 *
 *----------------------------------------------------------------------
 */

int
CPName_GetComponent(char const *begin,   // IN: Beginning of buffer
                    char const *end,     // IN: End of buffer
                    char const **next)   // OUT: Start of next component
{
   char const *walk;
   char const *myNext;
   size_t len;

   ASSERT(begin);
   ASSERT(end);
   ASSERT(next);
   ASSERT(begin <= end);

   for (walk = begin; ; walk++) {
      if (walk == end) {
         /* End of buffer. No NUL was found */

         myNext = end;
         break;
      }

      if (*walk == '\0') {
         /* Found a NUL */

         if (walk == begin) {
            Log("%s: error: first char can't be NUL\n", __FUNCTION__);
            return -1;
         }

         myNext = walk + 1;
         /* Skip consecutive path delimiters. */
         while ((*myNext == '\0') && (myNext != end)) {
            myNext++;
         }
         if (myNext == end) {
            /* Last character in the buffer is not allowed to be NUL */
            Log("%s: error: last char can't be NUL\n", __FUNCTION__);
            return -1;
         }

         break;
      }
   }

   len = walk - begin;

   *next = myNext;
   return (int) len;
}


/*
 *----------------------------------------------------------------------
 *
 * CPNameEscapeAndConvertFrom --
 *
 *    Converts a cross-platform name representation into a string for
 *    use in the local filesystem.
 *    Escapes illegal characters as a part of convertion.
 *    This is a cross-platform implementation and takes the path separator
 *    argument as an argument. The path separator is prepended before each
 *    additional path component, so this function never adds a trailing path
 *    separator.
 *
 * Results:
 *    0 on success.
 *    error < 0 on failure (the converted string did not fit in
 *    the buffer provided or the input was invalid).
 *
 * Side effects:
 *    None
 *
 *----------------------------------------------------------------------
 */

int
CPNameEscapeAndConvertFrom(char const **bufIn, // IN/OUT: Input to convert
                           size_t *inSize,     // IN/OUT: Size of input
                           size_t *outSize,    // IN/OUT: Size of output buffer
                           char **bufOut,      // IN/OUT: Output buffer
                           char pathSep)       // IN: Path separator character
{
   int result;
   int inputSize;
   inputSize = HgfsEscape_GetSize(*bufIn, *inSize);
   if (inputSize < 0) {
      result = -1;
   } else if (inputSize != 0) {
      char *savedBufOut = *bufOut;
      char const *savedOutConst = savedBufOut;
      size_t savedOutSize = *outSize;
      if (inputSize > *outSize) {
         Log("%s: error: not enough room for escaping\n", __FUNCTION__);
         return -1;
      }

      /* Leaving space for the leading path separator, thus output to savedBufOut + 1. */
      *inSize = HgfsEscape_Do(*bufIn, *inSize, savedOutSize, savedBufOut + 1);
      result = CPNameConvertFrom(&savedOutConst, inSize, outSize, bufOut, pathSep);
      *bufIn += *inSize;
      *inSize = 0;
   } else {
      result = CPNameConvertFrom(bufIn, inSize, outSize, bufOut, pathSep);
   }
   return result;
}


/*
 *----------------------------------------------------------------------
 *
 * CPNameConvertFrom --
 *
 *    Converts a cross-platform name representation into a string for
 *    use in the local filesystem. This is a cross-platform
 *    implementation and takes the path separator argument as an
 *    argument. The path separator is prepended before each additional
 *    path component, so this function never adds a trailing path
 *    separator.
 *
 * Results:
 *    0 on success.
 *    error < 0 on failure (the converted string did not fit in
 *    the buffer provided or the input was invalid).
 *
 * Side effects:
 *    None
 *
 *----------------------------------------------------------------------
 */

int
CPNameConvertFrom(char const **bufIn, // IN/OUT: Input to convert
                  size_t *inSize,     // IN/OUT: Size of input
                  size_t *outSize,    // IN/OUT: Size of output buffer
                  char **bufOut,      // IN/OUT: Output buffer
                  char pathSep)       // IN: Path separator character
{
   char const *in;
   char const *inEnd;
   size_t myOutSize;
   char *out;
   Bool inPlaceConvertion = (*bufIn == *bufOut);

   ASSERT(bufIn);
   ASSERT(inSize);
   ASSERT(outSize);
   ASSERT(bufOut);

   in = *bufIn;
   if (inPlaceConvertion) {
      in++; // Skip place for the leading path separator.
   }
   inEnd = in + *inSize;
   myOutSize = *outSize;
   out = *bufOut;

   for (;;) {
      char const *next;
      int len;
      int newLen;

      len = CPName_GetComponent(in, inEnd, &next);
      if (len < 0) {
         Log("%s: error: get next component failed\n", __FUNCTION__);
         return len;
      }

      /* Bug 27926 - preventing escaping from shared folder. */
      if ((len == 1 && *in == '.') ||
          (len == 2 && in[0] == '.' && in[1] == '.')) {
         Log("%s: error: found dot/dotdot\n", __FUNCTION__);
         return -1;
      }

      if (len == 0) {
         /* No more component */
         break;
      }

      newLen = ((int) myOutSize) - len - 1;
      if (newLen < 0) {
         Log("%s: error: not enough room\n", __FUNCTION__);
         return -1;
      }
      myOutSize = (size_t) newLen;

      *out++ = pathSep;
      if (!inPlaceConvertion) {
         memcpy(out, in, len);
      }
      out += len;

      in = next;
   }

   /* NUL terminate */
   if (myOutSize < 1) {
      Log("%s: error: not enough room\n", __FUNCTION__);
      return -1;
   }
   *out = '\0';

   /* Path name size should not require more than 4 bytes. */
   ASSERT((in - *bufIn) <= 0xFFFFFFFF);

   /* Update pointers. */
   *inSize -= (in - *bufIn);
   *outSize = myOutSize;
   *bufIn = in;
   *bufOut = out;

   return 0;
}


/*
 *----------------------------------------------------------------------------
 *
 * CPName_Print --
 *
 *    Converts a CPName formatted string to a valid, NUL-terminated string by
 *    replacing all embedded NUL characters with '|'.
 *
 * Results:
 *    Pointer to a static buffer containing the converted string.
 *
 * Side effects:
 *    None.
 *
 *----------------------------------------------------------------------------
 */

char const *
CPName_Print(char const *in, // IN: Name to print
             size_t size)    // IN: Size of name
{
   /* Static so it does not go on a kernel stack --hpreg */
   static char out[128];
   size_t i;

   ASSERT(in);

   ASSERT(sizeof out >= 4);
   if (size > sizeof out - 1) {
      size = sizeof out - 4;
      out[size] = '.';
      out[size + 1] = '.';
      out[size + 2] = '.';
      out[size + 3] = '\0';
   } else {
      out[size] = '\0';
   }

   for (i = 0; i < size; i++) {
      out[i] = in[i] != '\0' ? in[i] : '|';
   }

   return out;
}


/*
 *----------------------------------------------------------------------------
 *
 * CPName_LinuxConvertTo --
 *
 *    Wrapper function that calls CPNameConvertTo() with the correct arguments
 *    for Linux path conversions.
 *
 *    Makes a cross-platform name representation from the Linux path input
 *    string and writes it into the output buffer.
 *
 * Results:
 *    On success, returns the number of bytes used in the cross-platform name,
 *    NOT including the final terminating NUL character.  On failure, returns
 *    a negative error.
 *
 * Side effects:
 *    None.
 *
 *----------------------------------------------------------------------------
 */

int
CPName_LinuxConvertTo(char const *nameIn, // IN:  Buf to convert
                      size_t bufOutSize,  // IN:  Size of the output buffer
                      char *bufOut)       // OUT: Output buffer
{
   return CPNameConvertTo(nameIn, bufOutSize, bufOut, '/');
}


/*
 *----------------------------------------------------------------------------
 *
 * CPName_WindowsConvertTo --
 *
 *    Wrapper function that calls CPNameConvertTo() with the correct arguments
 *    for Windows path conversions.
 *
 *    Makes a cross-platform name representation from the Linux path input
 *    string and writes it into the output buffer.
 *
 * Results:
 *    On success, returns the number of bytes used in the cross-platform name,
 *    NOT including the final terminating NUL character.  On failure, returns
 *    a negative error.
 *
 * Side effects:
 *    None.
 *
 *----------------------------------------------------------------------------
 */

int
CPName_WindowsConvertTo(char const *nameIn, // IN:  Buf to convert
                        size_t bufOutSize,  // IN:  Size of the output buffer
                        char *bufOut)       // OUT: Output buffer
{
   return CPNameConvertTo(nameIn, bufOutSize, bufOut, '\\');
}


/*
 *----------------------------------------------------------------------
 *
 * CPNameConvertTo --
 *
 *    Makes a cross-platform name representation from the input string
 *    and writes it into the output buffer.
 *    HGFS convention is to echange names between guest and host in uescaped form.
 *    Both ends perform necessary name escaping according to its own rules
 *    to avoid presenitng invalid file names to OS. Thus the name needs to be unescaped
 *    as a part of conversion to host-independent format.
 *
 * Results:
 *    On success, returns the number of bytes used in the
 *    cross-platform name, NOT including the final terminating NUL
 *    character. On failure, returns a negative error.
 *
 * Side effects:
 *    None
 *
 *----------------------------------------------------------------------
 */

int
CPNameConvertTo(char const *nameIn, // IN:  Buf to convert
                size_t bufOutSize,  // IN:  Size of the output buffer
                char *bufOut,       // OUT: Output buffer
                char pathSep)       // IN:  path separator to use
{
   char *origOut = bufOut;
   char const *endOut = bufOut + bufOutSize;
   size_t cpNameLength = 0;

   ASSERT(nameIn);
   ASSERT(bufOut);

   /* Skip any path separators at the beginning of the input string */
   while (*nameIn == pathSep) {
      nameIn++;
   }

    /*
     * Copy the string to the output buf, converting all path separators into '\0'.
     * Collapse multiple consecutive path separators into a single one since
     * CPName_GetComponent can't handle consecutive path separators.
     */
   while (*nameIn != '\0' && bufOut < endOut) {
      if (*nameIn == pathSep) {
         *bufOut = '\0';
         do {
            nameIn++;
         } while (*nameIn == pathSep);
      } else {
         *bufOut = *nameIn;
         nameIn++;
      }
      bufOut++;
   }

   /*
    * NUL terminate. XXX This should go away.
    *
    * When we get rid of NUL termination here, this test should
    * also change to "if (*nameIn != '\0')".
    */
   if (bufOut == endOut) {
      return -1;
   }
   *bufOut = '\0';

   /* Path name size should not require more than 4 bytes. */
   ASSERT((bufOut - origOut) <= 0xFFFFFFFF);

   /* If there were any trailing path separators, dont count them [krishnan] */
   cpNameLength = bufOut - origOut;
   while ((cpNameLength >= 1) && (origOut[cpNameLength - 1] == 0)) {
      cpNameLength--;
   }
   cpNameLength = HgfsEscape_Undo(origOut, cpNameLength);

   /* Return number of bytes used */
   return (int) cpNameLength;
}