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

Last change on this file since 24 was 23, checked in by diver, 15 years ago

reworked some stuff

File size: 1.7 KB
Line 
1#include <os2.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
Note: See TracBrowser for help on using the repository browser.