- Add some checks to prevent crashes in unexpected situations and add useful error...
[reactos.git] / rostests / rosautotest / tools.c
1 /*
2 * PROJECT: ReactOS Automatic Testing Utility
3 * LICENSE: GNU GPLv2 or any later version as published by the Free Software Foundation
4 * PURPOSE: Various helper functions
5 * COPYRIGHT: Copyright 2008-2009 Colin Finck <colin@reactos.org>
6 */
7
8 #include "precomp.h"
9
10 /**
11 * Escapes a string according to RFC 1738.
12 * Required for passing parameters to the web service.
13 *
14 * @param Output
15 * Pointer to a CHAR array, which will receive the escaped string.
16 * The output buffer must be large enough to hold the full escaped string. You're on the safe side
17 * if you make the output buffer three times as large as the input buffer.
18 *
19 * @param Input
20 * Pointer to a CHAR array, which contains the input buffer to escape.
21 */
22 VOID
23 EscapeString(PCHAR Output, PCHAR Input)
24 {
25 const CHAR HexCharacters[] = "0123456789ABCDEF";
26
27 do
28 {
29 if(strchr("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_.~", *Input) )
30 {
31 /* It's a character we don't need to escape, just add it to the output string */
32 *Output++ = *Input;
33 }
34 else
35 {
36 /* We need to escape this character */
37 *Output++ = '%';
38 *Output++ = HexCharacters[((UCHAR)*Input >> 4) % 16];
39 *Output++ = HexCharacters[(UCHAR)*Input % 16];
40 }
41 }
42 while(*++Input);
43
44 *Output = 0;
45 }
46
47 /**
48 * Outputs a string through the standard output and the debug output.
49 * The string may have LF or CRLF line endings.
50 *
51 * @param String
52 * The string to output
53 */
54 VOID
55 StringOut(PCHAR String)
56 {
57 PCHAR NewString;
58 PCHAR pNewString;
59 size_t Length;
60
61 /* The piped output of the tests may use CRLF line endings, so convert them to LF.
62 As both printf and OutputDebugStringA operate in text mode, the line-endings will be properly converted again later. */
63 Length = strlen(String);
64 NewString = HeapAlloc(hProcessHeap, 0, Length + 1);
65 pNewString = NewString;
66
67 do
68 {
69 /* If this is a CRLF line-ending, only copy a \n to the new string and skip the next character */
70 if(*String == '\r' && *(String + 1) == '\n')
71 {
72 *pNewString = '\n';
73 ++String;
74 }
75 else
76 {
77 /* Otherwise copy the string */
78 *pNewString = *String;
79 }
80
81 ++pNewString;
82 }
83 while(*++String);
84
85 /* Null-terminate it */
86 *pNewString = 0;
87
88 /* Output it */
89 printf(NewString);
90 OutputDebugStringA(NewString);
91
92 /* Cleanup */
93 HeapFree(hProcessHeap, 0, NewString);
94 }