source: branches/1.0/src/utils.c@ 29

Last change on this file since 29 was 26, checked in by diver, 15 years ago

major rework

File size: 5.5 KB
Line 
1#include "uni.h"
2
3#include <stdlib.h>
4#include <string.h>
5#include <stdio.h>
6#include <stdlib.h>
7#include "utils.h"
8
9/* replace one string by another */
10BOOL searchReplace(const char *search, const char *replace, const char *string, char *replaced)
11{
12 /* create init some variables */
13 char *searchStart;
14 int len = 0;
15
16 /* do we find the searched string at all */
17 searchStart = strstr(string, search);
18 if (searchStart == NULL)
19 {
20 strncpy(replaced, string, strlen(replaced));
21 return FALSE;
22 }
23
24 /* copy first part */
25 len = searchStart - string;
26 strncpy(replaced, string, len);
27
28 /* add the replaced string */
29 strcat(replaced, replace);
30
31 /* add the last part */
32 len += strlen(search);
33 strcat(replaced, string+len);
34
35 return TRUE;
36}
37
38/* Password encryption/decryption routines from ndpsmb.c */
39
40unsigned char fromhex(char c)
41{
42 if ('0' <= c && c <= '9')
43 {
44 return c - '0';
45 }
46
47 if ('A' <= c && c <= 'F')
48 {
49 return c - 'A' + 0xA;
50 }
51
52 if ('a' <= c && c <= 'f')
53 {
54 return c - 'a' + 0xA;
55 }
56
57 return 0;
58}
59
60char tohex(unsigned char b)
61{
62 b &= 0xF;
63
64 if (b <= 9)
65 {
66 return b + '0';
67 }
68
69 return 'A' + (b - 0xA);
70}
71
72void decryptPassword(const char *pszCrypt, char *pszPlain)
73{
74 /* A simple "decryption", character from the hex value. */
75 const char *s = pszCrypt;
76 char *d = pszPlain;
77
78 while (*s)
79 {
80 *d++ = (char)((fromhex (*s++) << 4) + fromhex (*s++));
81 }
82
83 *d++ = 0;
84}
85
86void encryptPassword(const char *pszPlain, char *pszCrypt)
87{
88 /* A simple "encryption" encode each character as hex value. */
89 const char *s = pszPlain;
90 char *d = pszCrypt;
91
92 while (*s)
93 {
94 *d++ = tohex ((*s) >> 4);
95 *d++ = tohex (*s);
96 s++;
97 }
98
99 *d++ = 0;
100}
101
102/* Alex Taylor's new version of lprtok() that can handle missing fields */
103char * lprtok (char *string,char *control)
104{
105 char *c;
106 static char *next;
107
108 if ( control == NULL ) return string;
109 if ( string == NULL ) string = next;
110 if ( string == NULL ) return NULL;
111
112 if (( c = strpbrk( string, control )) == NULL ) {
113 next = NULL;
114 }
115 else {
116 next = c+1;
117 *c = '\0';
118 }
119
120 return ( string );
121}
122
123ULONG CalcStructLength ( HAB hab, HMODULE hModule, USHORT usID )
124{
125 ULONG cbRequired;
126 CHAR chString[STR_LEN_PORTDESC];
127
128 cbRequired = 0;
129
130 WinLoadString(hab, hModule, usID, STR_LEN_PORTDESC, chString);
131 cbRequired += strlen (chString) + 1;
132 WinLoadString(hab, hModule, (USHORT)(usID + 1), STR_LEN_PORTDESC, chString);
133 cbRequired += strlen (chString) + 1;
134 cbRequired += sizeof (PORTNAMES);
135 return(cbRequired);
136}
137
138ULONG CalcBufLength ( HAB hab, HMODULE hModule )
139{
140 ULONG cbRequired;
141 USHORT usID;
142
143 cbRequired = 0;
144
145 /*
146 ** calculate length required to fit all the port info.
147 */
148 for (usID = PORT_ID_FIRST; usID <= PORT_ID_LAST; usID += 2)
149 {
150 cbRequired += CalcStructLength (hab, hModule, usID);
151 }
152
153 return(cbRequired);
154}
155
156ULONG NumPortsCanFit ( HAB hab, HMODULE hModule, ULONG cbBuf )
157{
158 ULONG cbRequired;
159 USHORT usID;
160 ULONG ulNumPort;
161
162 cbRequired = 0;
163 ulNumPort = 0;
164
165 /*
166 ** calculate how many ports we can fit in buf.
167 */
168 for (usID = PORT_ID_FIRST; usID <= PORT_ID_LAST; usID += 2)
169 {
170 cbRequired += CalcStructLength (hab, hModule, usID);
171 if (cbRequired > cbBuf)
172 {
173 return(ulNumPort);
174 }
175 ulNumPort++;
176 }
177
178 return(ulNumPort);
179}
180VOID CopyStruct ( HAB hab, HMODULE hModule, USHORT usID, PCH pBuf, PULONG pulBeginStruct, PULONG pulBeginText )
181{
182 PPORTNAMES pPortNames;
183
184 pPortNames = (PPORTNAMES)(pBuf + *pulBeginStruct);
185 *pulBeginStruct += sizeof (PORTNAMES);
186
187 /*
188 ** copy port name in the structure
189 */
190 pPortNames->pszPortName = pBuf + *pulBeginText;
191 WinLoadString(hab, hModule, usID, STR_LEN_PORTDESC, pPortNames->pszPortName);
192 *pulBeginText += strlen (pPortNames->pszPortName) + 1;
193
194 /*
195 ** copy port description to the structure
196 */
197 pPortNames->pszPortDesc = pBuf + *pulBeginText;
198 WinLoadString(hab, hModule, usID, STR_LEN_PORTDESC, pPortNames->pszPortDesc);
199 *pulBeginText += strlen (pPortNames->pszPortDesc) + 1;
200}
201VOID CopyNPorts ( HAB hab, HMODULE hModule, PCH pBuf, ULONG ulReturned )
202{
203 USHORT usID;
204 ULONG ulBeginText;
205 ULONG ulBeginStruct;
206
207 ulBeginText = ulReturned * sizeof (PORTNAMES);
208 ulBeginStruct = 0;
209
210 for (usID = PORT_ID_FIRST;
211 usID <= PORT_ID_LAST && ulReturned;
212 usID += 2, --ulReturned)
213 {
214 CopyStruct (hab, hModule, usID, pBuf, &ulBeginStruct, &ulBeginText);
215 }
216}
217
218BOOL GetPortDescription ( HAB hab, HMODULE hModule, PSZ pszPortName, PSZ pszPortDesc )
219{
220 USHORT usID;
221 CHAR chBuf[STR_LEN_PORTDESC] = {0};
222
223 for (usID = PORT_ID_FIRST; usID <= PORT_ID_LAST; usID += 2)
224 {
225 WinLoadString(hab, hModule, usID, STR_LEN_PORTNAME, chBuf);
226 if (!strcmp (pszPortName, chBuf))
227 {
228 if ( WinLoadString(hab, hModule, usID+1, STR_LEN_PORTDESC, chBuf) ) {
229 strcpy (pszPortDesc, chBuf);
230 return(TRUE);
231 }
232 break;
233 }
234 }
235 return(FALSE);
236}
237
238BOOL GenerateUniquePortName( PSZ pszPortName )
239{
240 BOOL fPortExists;
241 PSZ pszEndPortName;
242 USHORT i;
243 CHAR chPortData[STR_LEN_PORTNAME];
244
245 /*
246 ** Generate a unique port name by adding numbers to the
247 ** end of pszPortName
248 */
249 pszEndPortName = pszPortName + strlen( pszPortName );
250 i = 1;
251 fPortExists = TRUE;
252 while ( (i < MAX_PORTS) && fPortExists )
253 {
254 _itoa( i, pszEndPortName, 4);
255 fPortExists = PrfQueryProfileString (HINI_SYSTEMPROFILE,
256 APPNAME_PM_SPOOLER_PORT,
257 pszPortName,
258 NULL,
259 chPortData,
260 sizeof(chPortData) - 1);
261 i++;
262 }
263 return(!fPortExists);
264}
265
Note: See TracBrowser for help on using the repository browser.