comparison dcclib/ck2str.c @ 0:c7f6b056b673

First import of vendor version
author Peter Gervai <grin@grin.hu>
date Tue, 10 Mar 2009 13:49:58 +0100
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:c7f6b056b673
1 /* Distributed Checksum Clearinghouse
2 *
3 * Copyright (c) 2008 by Rhyolite Software, LLC
4 *
5 * This agreement is not applicable to any entity which sells anti-spam
6 * solutions to others or provides an anti-spam solution as part of a
7 * security solution sold to other entities, or to a private network
8 * which employs the DCC or uses data provided by operation of the DCC
9 * but does not provide corresponding data to other users.
10 *
11 * Permission to use, copy, modify, and distribute this software without
12 * changes for any purpose with or without fee is hereby granted, provided
13 * that the above copyright notice and this permission notice appear in all
14 * copies and any distributed versions or copies are either unchanged
15 * or not called anything similar to "DCC" or "Distributed Checksum
16 * Clearinghouse".
17 *
18 * Parties not eligible to receive a license under this agreement can
19 * obtain a commercial license to use DCC by contacting Rhyolite Software
20 * at sales@rhyolite.com.
21 *
22 * A commercial license would be for Distributed Checksum and Reputation
23 * Clearinghouse software. That software includes additional features. This
24 * free license for Distributed ChecksumClearinghouse Software does not in any
25 * way grant permision to use Distributed Checksum and Reputation Clearinghouse
26 * software
27 *
28 * THE SOFTWARE IS PROVIDED "AS IS" AND RHYOLITE SOFTWARE, LLC DISCLAIMS ALL
29 * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
30 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL RHYOLITE SOFTWARE, LLC
31 * BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES
32 * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
33 * WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
34 * ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
35 * SOFTWARE.
36 *
37 * Rhyolite Software DCC 1.3.103-1.25 $Revision$
38 */
39
40 #include "dcc_defs.h"
41
42 char *
43 dcc_ck2str(char *buf, u_int buf_len,
44 DCC_CK_TYPES type, const DCC_SUM sum, u_int32_t extra)
45 {
46 u_int32_t n[sizeof(DCC_SUM)+3/4];
47 u_char c;
48 char *p;
49 const char *type_str;
50 char type_buf[32];
51 int i;
52
53 /* checksums other than server-ID declarations are easy */
54 if (type != DCC_CK_SRVR_ID) {
55 memcpy(n, sum, sizeof(n));
56 snprintf(buf, buf_len, "%08x %08x %08x %08x",
57 (int)ntohl(n[0]), (int)ntohl(n[1]),
58 (int)ntohl(n[2]), (int)ntohl(n[3]));
59 return buf;
60 }
61
62 /* decode server sanity declarations */
63 if (sum[0] == DCC_CK_SRVR_ID) {
64 if (extra == 0)
65 type_str = "server";
66 else
67 type_str = id2str(type_buf, sizeof(type_buf), extra);
68 snprintf(buf, buf_len, "%s at %d",
69 type_str, (sum[1]<<8)+sum[2]);
70 return buf;
71 }
72
73 /* decode the string from server-ID declaration */
74 p = buf;
75 if (buf_len == 0)
76 return buf;
77 *p++ = '"';
78 --buf_len;
79 for (i = 0; i < ISZ(DCC_SUM); ++i) {
80 if (buf_len == 0)
81 return buf;
82 c = sum[i];
83 if (c == '\0')
84 break;
85 if (c >= ' ' && c < 0x7f) {
86 /* simple ASCII is easy */
87 *p++ = c;
88 --buf_len;
89 continue;
90 }
91
92 /* convert non-ASCII to an escape sequence */
93 *p++ = '\\';
94 if (--buf_len == 0)
95 return buf;
96 *p++ = '0'+(c>>6);
97 if (--buf_len == 0)
98 return buf;
99 *p++ = '0'+((c>>3) & 7);
100 if (--buf_len == 0)
101 return buf;
102 *p++ = '0'+(c & 7);
103 --buf_len;
104 }
105 *p++ = '"';
106 --buf_len;
107 if (--buf_len != 0)
108 *p = '\0';
109 return buf;
110 }
111
112
113
114 /* this is not thread safe */
115 const char *
116 dcc_ck2str_err(DCC_CK_TYPES type, const DCC_SUM sum, u_int32_t extra)
117 {
118 static char ck_buf[DCC_CK2STR_LEN];
119
120 return dcc_ck2str(ck_buf, sizeof(ck_buf), type, sum, extra);
121 }