Tizen 2.1 base
[platform/upstream/hplip.git] / scan / sane / common.c
1 /********************************************************************************************\
2
3   common.c - common code for scl, pml, and soap backends
4
5   (c) 2001-2006 Copyright Hewlett-Packard Development Company, LP
6
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:
13
14   The above copyright notice and this permission notice shall be included in all
15   copies or substantial portions of the Software.
16
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.
23
24   Contributing Authors: David Paschal, Don Welch, David Suffield, Naga Samrat Chowdary Narla
25                                                 Sarbeswar Meher
26
27 \********************************************************************************************/
28
29 #include <stdio.h>
30 #include <stdarg.h>
31 #include <syslog.h>
32 #include <string.h>
33 #include <ctype.h>
34 #include "common.h"
35
36 #define DEBUG_NOT_STATIC
37 #include "sanei_debug.h"
38
39 int __attribute__ ((visibility ("hidden"))) bug(const char *fmt, ...)
40 {
41    char buf[256];
42    va_list args;
43    int n;
44
45    va_start(args, fmt);
46    if ((n = vsnprintf(buf, 256, fmt, args)) == -1)
47       buf[255] = 0;     /* output was truncated */
48    syslog(LOG_WARNING, "%s", buf);
49    DBG(2, "%s", buf);
50    va_end(args);
51    return n;
52 }
53
54 void __attribute__ ((visibility ("hidden"))) sysdump(const void *data, int size)
55 {
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.
58      */
59
60     unsigned char *p = (unsigned char *)data;
61     unsigned char c;
62     int n;
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++) {
68         if (n%16 == 1) {
69             /* store address for this line */
70             snprintf(addrstr, sizeof(addrstr), "%.4d", (int)((p-(unsigned char *)data) & 0xffff));
71         }
72             
73         c = *p;
74         if (isprint(c) == 0) {
75             c = '.';
76         }
77
78         /* store hex str (for left side) */
79         snprintf(bytestr, sizeof(bytestr), "%02X ", *p);
80         strncat(hexstr, bytestr, sizeof(hexstr)-strlen(hexstr)-1);
81
82         /* store char str (for right side) */
83         snprintf(bytestr, sizeof(bytestr), "%c", c);
84         strncat(charstr, bytestr, sizeof(charstr)-strlen(charstr)-1);
85
86         if(n%16 == 0) { 
87             /* line completed */
88             DBG_SZ("[%4.4s]   %-50.50s  %s\n", addrstr, hexstr, charstr);
89             hexstr[0] = 0;
90             charstr[0] = 0;
91         }
92         p++; /* next byte */
93     }
94
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);
98     }
99 }
100
101 void __attribute__ ((visibility ("hidden"))) bugdump(const void *data, int size)
102 {
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.
105      */
106
107     unsigned char *p = (unsigned char *)data;
108     unsigned char c;
109     int n;
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++) {
115         if (n%16 == 1) {
116             /* store address for this line */
117             snprintf(addrstr, sizeof(addrstr), "%.4d", (int)((p-(unsigned char *)data) & 0xffff));
118         }
119             
120         c = *p;
121         if (isprint(c) == 0) {
122             c = '.';
123         }
124
125         /* store hex str (for left side) */
126         snprintf(bytestr, sizeof(bytestr), "%02X ", *p);
127         strncat(hexstr, bytestr, sizeof(hexstr)-strlen(hexstr)-1);
128
129         /* store char str (for right side) */
130         snprintf(bytestr, sizeof(bytestr), "%c", c);
131         strncat(charstr, bytestr, sizeof(charstr)-strlen(charstr)-1);
132
133         if(n%16 == 0) { 
134             /* line completed */
135             BUG_SZ("[%4.4s]   %-50.50s  %s\n", addrstr, hexstr, charstr);
136             hexstr[0] = 0;
137             charstr[0] = 0;
138         }
139         p++; /* next byte */
140     }
141
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);
145     }
146 }
147
148 char __attribute__ ((visibility ("hidden"))) *psnprintf(char *buf, int bufSize, const char *fmt, ...)
149 {
150    va_list args;
151    int n;
152
153    buf[0] = 0;
154
155    va_start(args, fmt);
156    if ((n = vsnprintf(buf, bufSize, fmt, args)) == -1)
157       buf[bufSize] = 0;     /* output was truncated */
158    va_end(args);
159
160    return buf;
161 }
162
163 unsigned long __attribute__ ((visibility ("hidden"))) DivideAndShift( int line,
164                               unsigned long numerator1,
165                               unsigned long numerator2,
166                               unsigned long denominator,
167                               int shift )
168 {
169     unsigned long remainder, shiftLoss = 0;
170     unsigned long long result = numerator1;
171     result *= numerator2;
172     if( shift > 0 )
173     {
174         result <<= shift;
175     }
176     remainder = result % denominator;
177     result /= denominator;
178     if( shift < 0 )
179     {
180         shiftLoss = result & ( ( 1 << ( -shift ) ) - 1 );
181         result >>= ( -shift );
182     }
183     return result;
184 }
185
186 void __attribute__ ((visibility ("hidden"))) NumListClear( int * list )
187 {
188     memset( list, 0, sizeof( int ) * MAX_LIST_SIZE );
189 }
190
191 int __attribute__ ((visibility ("hidden"))) NumListIsInList( int * list, int n )
192 {
193     int i;
194     for( i = 1; i < MAX_LIST_SIZE; i++ )
195     {
196         if( list[i] == n )
197         {
198             return 1;
199         }
200     }
201     return 0;
202 }
203
204 int __attribute__ ((visibility ("hidden"))) NumListAdd( int * list, int n )
205 {
206     if( NumListIsInList( list, n ) )
207     {
208         return 1;
209     }
210     if( list[0] >= ( MAX_LIST_SIZE - 1 ) )
211     {
212         return 0;
213     }
214     list[0]++;
215     list[list[0]] = n;
216     return 1;
217 }
218
219 int __attribute__ ((visibility ("hidden"))) NumListGetCount( int * list )
220 {
221     return list[0];
222 }
223
224 int __attribute__ ((visibility ("hidden"))) NumListGetFirst( int * list )
225 {
226     int n = list[0];
227     if( n > 0 )
228     {
229         n = list[1];
230     }
231     return n;
232 }
233
234 void __attribute__ ((visibility ("hidden"))) StrListClear( const char ** list )
235 {
236     memset( list, 0, sizeof( char * ) * MAX_LIST_SIZE );
237 }
238
239 int __attribute__ ((visibility ("hidden"))) StrListIsInList( const char ** list, char * s )
240 {
241     while( *list )
242     {
243         if( !strcasecmp( *list, s ) )
244         {
245             return 1;
246         }
247         list++;
248     }
249     return 0;
250 }
251
252 int __attribute__ ((visibility ("hidden"))) StrListAdd( const char ** list, char * s )
253 {
254     int i;
255     for( i = 0; i < MAX_LIST_SIZE - 1; i++ )
256     {
257         if( !list[i] )
258         {
259             list[i] = s;
260             return 1;
261         }
262         if( !strcasecmp( list[i], s ) )
263         {
264             return 1;
265         }
266     }
267     return 0;
268 }
269
270 char* __attribute__ ((visibility ("hidden"))) itoa(int value, char* str, int radix)
271 {
272   static char dig[] = "0123456789""abcdefghijklmnopqrstuvwxyz";
273   int n = 0, neg = 0;
274   unsigned int v;
275   char* p, *q;
276   char c;
277  
278   if (radix == 10 && value < 0)
279   {
280     value = -value;
281     neg = 1;
282    }
283   v = value;
284   do {
285     str[n++] = dig[v%radix];
286     v /= radix;
287   } while (v);
288   if (neg)
289     str[n++] = '-';
290     str[n] = '\0';
291  
292   for (p = str, q = p + (n-1); p < q; ++p, --q)
293     c = *p, *p = *q, *q = c;
294   return str;
295 }
296
297
298
299