1 | /* $Id: vfsiosmisc.cpp 56290 2015-06-09 14:01:31Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - Virtual File System, Misc I/O Stream Operations.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2010-2015 Oracle Corporation
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | *
|
---|
17 | * The contents of this file may alternatively be used under the terms
|
---|
18 | * of the Common Development and Distribution License Version 1.0
|
---|
19 | * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
|
---|
20 | * VirtualBox OSE distribution, in which case the provisions of the
|
---|
21 | * CDDL are applicable instead of those of the GPL.
|
---|
22 | *
|
---|
23 | * You may elect to license modified versions of this file under the
|
---|
24 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
25 | */
|
---|
26 |
|
---|
27 |
|
---|
28 | /*******************************************************************************
|
---|
29 | * Header Files *
|
---|
30 | *******************************************************************************/
|
---|
31 | #include <iprt/vfs.h>
|
---|
32 | #include <iprt/vfslowlevel.h>
|
---|
33 |
|
---|
34 | #include <iprt/err.h>
|
---|
35 | #include <iprt/string.h>
|
---|
36 |
|
---|
37 |
|
---|
38 |
|
---|
39 | RTDECL(int) RTVfsIoStrmValidateUtf8Encoding(RTVFSIOSTREAM hVfsIos, uint32_t fFlags, PRTFOFF poffError)
|
---|
40 | {
|
---|
41 | /*
|
---|
42 | * Validate input.
|
---|
43 | */
|
---|
44 | if (poffError)
|
---|
45 | {
|
---|
46 | AssertPtrReturn(poffError, VINF_SUCCESS);
|
---|
47 | *poffError = 0;
|
---|
48 | }
|
---|
49 | AssertReturn(!(fFlags & ~RTVFS_VALIDATE_UTF8_VALID_MASK), VERR_INVALID_PARAMETER);
|
---|
50 |
|
---|
51 | /*
|
---|
52 | * The loop.
|
---|
53 | */
|
---|
54 | char achBuf[1024 + 1];
|
---|
55 | size_t cbUsed = 0;
|
---|
56 | int rc;
|
---|
57 | for (;;)
|
---|
58 | {
|
---|
59 | /*
|
---|
60 | * Fill the buffer
|
---|
61 | */
|
---|
62 | size_t cbRead = 0;
|
---|
63 | rc = RTVfsIoStrmRead(hVfsIos, &achBuf[cbUsed], sizeof(achBuf) - cbUsed - 1, true /*fBlocking*/, &cbRead);
|
---|
64 | if (RT_FAILURE(rc))
|
---|
65 | break;
|
---|
66 | cbUsed += cbRead;
|
---|
67 | if (!cbUsed)
|
---|
68 | {
|
---|
69 | Assert(rc == VINF_EOF);
|
---|
70 | break;
|
---|
71 | }
|
---|
72 | achBuf[sizeof(achBuf) - 1] = '\0';
|
---|
73 |
|
---|
74 | /*
|
---|
75 | * Process the data in the buffer, maybe leaving the final chars till
|
---|
76 | * the next round.
|
---|
77 | */
|
---|
78 | const char *pszCur = achBuf;
|
---|
79 | size_t offEnd = rc == VINF_EOF
|
---|
80 | ? cbUsed
|
---|
81 | : cbUsed >= 7
|
---|
82 | ? cbUsed - 7
|
---|
83 | : 0;
|
---|
84 | size_t off;
|
---|
85 | while ((off = (pszCur - &achBuf[0])) < offEnd)
|
---|
86 | {
|
---|
87 | RTUNICP uc;
|
---|
88 | rc = RTStrGetCpEx(&pszCur, &uc);
|
---|
89 | if (RT_FAILURE(rc))
|
---|
90 | break;
|
---|
91 | if (!uc)
|
---|
92 | {
|
---|
93 | if (fFlags & RTVFS_VALIDATE_UTF8_NO_NULL)
|
---|
94 | {
|
---|
95 | rc = VERR_INVALID_UTF8_ENCODING;
|
---|
96 | break;
|
---|
97 | }
|
---|
98 | }
|
---|
99 | else if (uc > 0x10ffff)
|
---|
100 | {
|
---|
101 | if (fFlags & RTVFS_VALIDATE_UTF8_BY_RTC_3629)
|
---|
102 | {
|
---|
103 | rc = VERR_INVALID_UTF8_ENCODING;
|
---|
104 | break;
|
---|
105 | }
|
---|
106 | }
|
---|
107 | }
|
---|
108 |
|
---|
109 | if (off < cbUsed)
|
---|
110 | {
|
---|
111 | cbUsed -= off;
|
---|
112 | memmove(achBuf, pszCur, cbUsed);
|
---|
113 | }
|
---|
114 | }
|
---|
115 |
|
---|
116 | /*
|
---|
117 | * Set the offset on failure.
|
---|
118 | */
|
---|
119 | if (poffError && RT_FAILURE(rc))
|
---|
120 | {
|
---|
121 | }
|
---|
122 |
|
---|
123 | return rc == VINF_EOF ? VINF_SUCCESS : rc;
|
---|
124 | }
|
---|
125 |
|
---|