1 /********************************************************************************************\
3 common.c - common code for scl, pml, and soap backends
5 (c) 2001-2006 Copyright Hewlett-Packard Development Company, LP
7 Permission is hereby granted, free of charge, to any person obtaining a copy
8 of this software and associated documentation files (the "Software"), to deal
9 in the Software without restriction, including without limitation the rights
10 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
11 of the Software, and to permit persons to whom the Software is furnished to do
12 so, subject to the following conditions:
14 The above copyright notice and this permission notice shall be included in all
15 copies or substantial portions of the Software.
17 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
19 FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
20 COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
21 IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 Contributing Authors: David Paschal, Don Welch, David Suffield, Naga Samrat Chowdary Narla
27 \********************************************************************************************/
36 #define DEBUG_NOT_STATIC
37 #include "sanei_debug.h"
39 int __attribute__ ((visibility ("hidden"))) bug(const char *fmt, ...)
46 if ((n = vsnprintf(buf, 256, fmt, args)) == -1)
47 buf[255] = 0; /* output was truncated */
48 syslog(LOG_WARNING, "%s", buf);
54 void __attribute__ ((visibility ("hidden"))) sysdump(const void *data, int size)
56 /* Dump size bytes of *data. Output looks like:
57 * [0000] 75 6E 6B 6E 6F 77 6E 20 30 FF 00 00 00 00 39 00 unknown 0.....9.
60 unsigned char *p = (unsigned char *)data;
63 char bytestr[4] = {0};
64 char addrstr[10] = {0};
65 char hexstr[16*3 + 5] = {0};
66 char charstr[16*1 + 5] = {0};
67 for(n=1;n<=size;n++) {
69 /* store address for this line */
70 snprintf(addrstr, sizeof(addrstr), "%.4d", (int)((p-(unsigned char *)data) & 0xffff));
74 if (isprint(c) == 0) {
78 /* store hex str (for left side) */
79 snprintf(bytestr, sizeof(bytestr), "%02X ", *p);
80 strncat(hexstr, bytestr, sizeof(hexstr)-strlen(hexstr)-1);
82 /* store char str (for right side) */
83 snprintf(bytestr, sizeof(bytestr), "%c", c);
84 strncat(charstr, bytestr, sizeof(charstr)-strlen(charstr)-1);
88 DBG_SZ("[%4.4s] %-50.50s %s\n", addrstr, hexstr, charstr);
95 if (strlen(hexstr) > 0) {
96 /* print rest of buffer if not empty */
97 DBG_SZ("[%4.4s] %-50.50s %s\n", addrstr, hexstr, charstr);
101 void __attribute__ ((visibility ("hidden"))) bugdump(const void *data, int size)
103 /* Dump size bytes of *data. Output looks like:
104 * [0000] 75 6E 6B 6E 6F 77 6E 20 30 FF 00 00 00 00 39 00 unknown 0.....9.
107 unsigned char *p = (unsigned char *)data;
110 char bytestr[4] = {0};
111 char addrstr[10] = {0};
112 char hexstr[16*3 + 5] = {0};
113 char charstr[16*1 + 5] = {0};
114 for(n=1;n<=size;n++) {
116 /* store address for this line */
117 snprintf(addrstr, sizeof(addrstr), "%.4d", (int)((p-(unsigned char *)data) & 0xffff));
121 if (isprint(c) == 0) {
125 /* store hex str (for left side) */
126 snprintf(bytestr, sizeof(bytestr), "%02X ", *p);
127 strncat(hexstr, bytestr, sizeof(hexstr)-strlen(hexstr)-1);
129 /* store char str (for right side) */
130 snprintf(bytestr, sizeof(bytestr), "%c", c);
131 strncat(charstr, bytestr, sizeof(charstr)-strlen(charstr)-1);
135 BUG_SZ("[%4.4s] %-50.50s %s\n", addrstr, hexstr, charstr);
142 if (strlen(hexstr) > 0) {
143 /* print rest of buffer if not empty */
144 BUG_SZ("[%4.4s] %-50.50s %s\n", addrstr, hexstr, charstr);
148 char __attribute__ ((visibility ("hidden"))) *psnprintf(char *buf, int bufSize, const char *fmt, ...)
156 if ((n = vsnprintf(buf, bufSize, fmt, args)) == -1)
157 buf[bufSize] = 0; /* output was truncated */
163 unsigned long __attribute__ ((visibility ("hidden"))) DivideAndShift( int line,
164 unsigned long numerator1,
165 unsigned long numerator2,
166 unsigned long denominator,
169 unsigned long remainder, shiftLoss = 0;
170 unsigned long long result = numerator1;
171 result *= numerator2;
176 remainder = result % denominator;
177 result /= denominator;
180 shiftLoss = result & ( ( 1 << ( -shift ) ) - 1 );
181 result >>= ( -shift );
186 void __attribute__ ((visibility ("hidden"))) NumListClear( int * list )
188 memset( list, 0, sizeof( int ) * MAX_LIST_SIZE );
191 int __attribute__ ((visibility ("hidden"))) NumListIsInList( int * list, int n )
194 for( i = 1; i < MAX_LIST_SIZE; i++ )
204 int __attribute__ ((visibility ("hidden"))) NumListAdd( int * list, int n )
206 if( NumListIsInList( list, n ) )
210 if( list[0] >= ( MAX_LIST_SIZE - 1 ) )
219 int __attribute__ ((visibility ("hidden"))) NumListGetCount( int * list )
224 int __attribute__ ((visibility ("hidden"))) NumListGetFirst( int * list )
234 void __attribute__ ((visibility ("hidden"))) StrListClear( const char ** list )
236 memset( list, 0, sizeof( char * ) * MAX_LIST_SIZE );
239 int __attribute__ ((visibility ("hidden"))) StrListIsInList( const char ** list, char * s )
243 if( !strcasecmp( *list, s ) )
252 int __attribute__ ((visibility ("hidden"))) StrListAdd( const char ** list, char * s )
255 for( i = 0; i < MAX_LIST_SIZE - 1; i++ )
262 if( !strcasecmp( list[i], s ) )
270 char* __attribute__ ((visibility ("hidden"))) itoa(int value, char* str, int radix)
272 static char dig[] = "0123456789""abcdefghijklmnopqrstuvwxyz";
278 if (radix == 10 && value < 0)
285 str[n++] = dig[v%radix];
292 for (p = str, q = p + (n-1); p < q; ++p, --q)
293 c = *p, *p = *q, *q = c;