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

Last change on this file since 33 was 30, checked in by herwigb, 15 years ago

Split parameter string into separate parameters

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