summaryrefslogtreecommitdiffstats
path: root/private/mvdm/vdmredir/vrputil.c
blob: 756a108e9fb5ebf985dba03f84b085bd79f7c2ff (plain) (blame)
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
/*++

Copyright (c) 1991  Microsoft Corporation

Module Name:

    vrputil.c

Abstract:

    Contains 'private' Vdm Redir (Vrp) 32-bit side utility routines:

        VrpMapLastError
        VrpMapDosError
        VrpTranslateDosNetPath

Author:

    Richard L Firth (rfirth) 13-Sep-1991

Environment:

    32-bit flat address space

Revision History:

    13-Sep-1991 RFirth
        Created

--*/

#include <stdio.h>
#include <stdlib.h>     // toupper
#include <nt.h>
#include <ntrtl.h>      // ASSERT, DbgPrint
#include <nturtl.h>
#include <windows.h>
#include <softpc.h>     // x86 virtual machine definitions
#include <vrdlctab.h>
#include <vdmredir.h>   // common Vr stuff
#include <vrinit.h>
#include "vrputil.h"    // prototypes
#include <lmcons.h>
#include <lmerr.h>


WORD
VrpMapLastError(
    VOID
    )

/*++

Routine Description:

    Gets last error code returned by Win32 function and maps it to corresponding
    Dos error

Arguments:

    None.

Return Value:

    WORD        - Dos equivalent error code

--*/

{
    DWORD   LastError;

    LastError = GetLastError();
#ifdef VR_DIAGNOSE
    DbgPrint("VrpMapLastError: last error was %d\n", LastError);
#endif
    return VrpMapDosError(LastError);
}


WORD
VrpMapDosError(
    IN  DWORD   ErrorCode
    )

/*++

Routine Description:

    Maps (DWORD) errors returned from Win32 routines to (WORD) Dos errors

Arguments:

    ErrorCode   - Error code returned from Win32 routine

Return Value:

    WORD        - Dos equivalent error code

--*/

{
    switch (ErrorCode) {
    case NERR_UseNotFound:
        ErrorCode = ERROR_PATH_NOT_FOUND;
        break;
    }
    return (WORD)ErrorCode;
}


WORD
VrpTranslateDosNetPath(
    IN OUT LPSTR* InputString,
    OUT LPSTR* OutputString
    )

/*++

Routine Description:

    Converts a DOS net string: use UPPER CASE, convert / to \. Called with
    net strings, so validates them too - expects \\computername\share.
    computername is 1 <= name <= 15. sharename is 1 <= name <= 8. There must
    be 2 leading back-slashes

    BUGBUG: code page? Kanji? DBCS?

Arguments:

    InputString     - pointer to pointer to string in DOS memory <= LM20_PATHLEN
    OutputString    - pointer to pointer to string in 32-bit memory

Return Value:

    WORD
        Success = 0
            InputString points to one character past the end of the input string
            OutputString points to one character past the end of the output string
        Failure = ERROR_INVALID_PARAMETER

--*/

{
    char ch;
    char lastCh = 0;
    int state = 0;  // 0 = leading slashes; 1 = computer name; 2 = share name
    int slashesToGo = 2;
    int charsToGo = 0;
    int maxLen = LM20_PATHLEN;

    while (ch = *((*InputString)++)) {
        --maxLen;
        if (maxLen < 0) {
            break;
        }
        if (ch == '/') {
            ch = '\\';
        } else {
            ch = toupper(ch);

        }
        if (ch == '\\') {
            --slashesToGo;
            if (slashesToGo < 0) {
                break;
            }
        } else {
            if (lastCh == '\\') {
                if (slashesToGo) {
                    break;
                } else {
                    if (state == 0) {
                        state = 1;
                        charsToGo = LM20_CNLEN;
                        slashesToGo = 1;
                    } else if (state == 1) {
                        state = 2;
                        charsToGo = LM20_NNLEN;
                        slashesToGo = 0;
                    }
                }
            }
            --charsToGo;
            if (charsToGo < 0) {
                break;
            }
        }
        *((*OutputString)++) = ch;
        lastCh = ch;
    }
    *((*OutputString)++) = 0;
    return ch ? (WORD)ERROR_INVALID_PARAMETER : 0;
}