]> git.ipfire.org Git - thirdparty/squid.git/blob - lib/ntlmauth/support_bits.cci
SourceFormat Enforcement
[thirdparty/squid.git] / lib / ntlmauth / support_bits.cci
1 /*
2 * Copyright (C) 1996-2015 The Squid Software Foundation and contributors
3 *
4 * Squid software is distributed under GPLv2+ license and includes
5 * contributions from numerous individuals and organizations.
6 * Please see the COPYING and CONTRIBUTORS files for details.
7 */
8
9 #ifndef SQUID_LIBNTLMAUTH_SUPPORT_BITS_CCI
10 #define SQUID_LIBNTLMAUTH_SUPPORT_BITS_CCI
11
12 #if HAVE_STRING_H
13 #include <string.h>
14 #endif
15
16 /*
17 * Defines several functions which are used and mutually shared by the NTLM helpers
18 * These do not (yet) have a defined stable home to go to.
19 * For now include this file into helper main .cc where needed.
20 */
21
22 /* makes a null-terminated string upper-case. Changes CONTENTS! */
23 inline void
24 uc(char *string)
25 {
26 char *p = string, c;
27 while ((c = *p)) {
28 *p = xtoupper(c);
29 ++p;
30 }
31 }
32
33 /* makes a null-terminated string lower-case. Changes CONTENTS! */
34 inline void
35 lc(char *string)
36 {
37 char *p = string, c;
38 while ((c = *p)) {
39 *p = xtolower(c);
40 ++p;
41 }
42 }
43
44 inline void
45 hex_dump(unsigned char *data, int size)
46 {
47 /* dumps size bytes of *data to stdout. Looks like:
48 * [0000] 75 6E 6B 6E 6F 77 6E 20
49 * 30 FF 00 00 00 00 39 00 unknown 0.....9.
50 * (in a single line of course)
51 */
52
53 if (!data)
54 return;
55
56 if (debug_enabled) {
57 unsigned char *p = data;
58 unsigned char c;
59 int n;
60 char bytestr[4] = {0};
61 char addrstr[10] = {0};
62 char hexstr[16 * 3 + 5] = {0};
63 char charstr[16 * 1 + 5] = {0};
64 for (n = 1; n <= size; ++n) {
65 if (n % 16 == 1) {
66 /* store address for this line */
67 snprintf(addrstr, sizeof(addrstr), "%.4x", (int) (p - data));
68 }
69 c = *p;
70 if (xisalnum(c) == 0) {
71 c = '.';
72 }
73 /* store hex str (for left side) */
74 snprintf(bytestr, sizeof(bytestr), "%02X ", *p);
75 strncat(hexstr, bytestr, sizeof(hexstr) - strlen(hexstr) - 1);
76
77 /* store char str (for right side) */
78 snprintf(bytestr, sizeof(bytestr), "%c", c);
79 strncat(charstr, bytestr, sizeof(charstr) - strlen(charstr) - 1);
80
81 if (n % 16 == 0) {
82 /* line completed */
83 fprintf(stderr, "[%4.4s] %-50.50s %s\n", addrstr, hexstr, charstr);
84 hexstr[0] = 0;
85 charstr[0] = 0;
86 } else if (n % 8 == 0) {
87 /* half line: add whitespaces */
88 strncat(hexstr, " ", sizeof(hexstr) - strlen(hexstr) - 1);
89 strncat(charstr, " ", sizeof(charstr) - strlen(charstr) - 1);
90 }
91 ++p; /* next byte */
92 }
93
94 if (strlen(hexstr) > 0) {
95 /* print rest of buffer if not empty */
96 fprintf(stderr, "[%4.4s] %-50.50s %s\n", addrstr, hexstr, charstr);
97 }
98 }
99
100 if (0) { //temporary hack to keep the linker happy
101 uc(NULL);
102 lc(NULL);
103 hex_dump(NULL,0);
104 }
105
106 }
107
108 #endif /* SQUID_LIBNTLMAUTH_SUPPORT_BITS_CCI */
109