Subversion Repositories Kolibri OS

Rev

Rev 6104 | Rev 6661 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 6104 Rev 6321
Line 157... Line 157...
157
{
157
{
158
    return c | 0x20;
158
    return c | 0x20;
159
}
159
}
Line 160... Line -...
160
 
-
 
161
 
-
 
162
//const char hex_asc[] = "0123456789abcdef";
-
 
163
 
-
 
164
/**
-
 
165
 * hex_to_bin - convert a hex digit to its real value
-
 
166
 * @ch: ascii character represents hex digit
-
 
167
 *
-
 
168
 * hex_to_bin() converts one hex digit to its actual value or -1 in case of bad
-
 
169
 * input.
-
 
170
 */
-
 
171
int hex_to_bin(char ch)
-
 
172
{
-
 
173
    if ((ch >= '0') && (ch <= '9'))
-
 
174
        return ch - '0';
-
 
175
    ch = tolower(ch);
-
 
176
    if ((ch >= 'a') && (ch <= 'f'))
-
 
177
        return ch - 'a' + 10;
-
 
178
    return -1;
-
 
179
}
-
 
180
EXPORT_SYMBOL(hex_to_bin);
-
 
181
 
-
 
182
/**
-
 
183
 * hex2bin - convert an ascii hexadecimal string to its binary representation
-
 
184
 * @dst: binary result
-
 
185
 * @src: ascii hexadecimal string
-
 
186
 * @count: result length
-
 
187
 *
-
 
188
 * Return 0 on success, -1 in case of bad input.
-
 
189
 */
-
 
190
int hex2bin(u8 *dst, const char *src, size_t count)
-
 
191
{
-
 
192
    while (count--) {
-
 
193
        int hi = hex_to_bin(*src++);
-
 
194
        int lo = hex_to_bin(*src++);
-
 
195
 
-
 
196
        if ((hi < 0) || (lo < 0))
-
 
197
            return -1;
-
 
198
 
-
 
199
        *dst++ = (hi << 4) | lo;
-
 
200
    }
-
 
201
    return 0;
-
 
202
}
-
 
203
EXPORT_SYMBOL(hex2bin);
-
 
204
 
-
 
205
/**
-
 
206
 * hex_dump_to_buffer - convert a blob of data to "hex ASCII" in memory
-
 
207
 * @buf: data blob to dump
-
 
208
 * @len: number of bytes in the @buf
-
 
209
 * @rowsize: number of bytes to print per line; must be 16 or 32
-
 
210
 * @groupsize: number of bytes to print at a time (1, 2, 4, 8; default = 1)
-
 
211
 * @linebuf: where to put the converted data
-
 
212
 * @linebuflen: total size of @linebuf, including space for terminating NUL
-
 
213
 * @ascii: include ASCII after the hex output
-
 
214
 *
-
 
215
 * hex_dump_to_buffer() works on one "line" of output at a time, i.e.,
-
 
216
 * 16 or 32 bytes of input data converted to hex + ASCII output.
-
 
217
 *
-
 
218
 * Given a buffer of u8 data, hex_dump_to_buffer() converts the input data
-
 
219
 * to a hex + ASCII dump at the supplied memory location.
-
 
220
 * The converted output is always NUL-terminated.
-
 
221
 *
-
 
222
 * E.g.:
-
 
223
 *   hex_dump_to_buffer(frame->data, frame->len, 16, 1,
-
 
224
 *          linebuf, sizeof(linebuf), true);
-
 
225
 *
-
 
226
 * example output buffer:
-
 
227
 * 40 41 42 43 44 45 46 47 48 49 4a 4b 4c 4d 4e 4f  @ABCDEFGHIJKLMNO
-
 
228
 */
-
 
229
int hex_dump_to_buffer(const void *buf, size_t len, int rowsize, int groupsize,
-
 
230
               char *linebuf, size_t linebuflen, bool ascii)
-
 
231
{
-
 
232
    const u8 *ptr = buf;
-
 
233
    int ngroups;
-
 
234
    u8 ch;
-
 
235
    int j, lx = 0;
-
 
236
    int ascii_column;
-
 
237
    int ret;
-
 
238
 
-
 
239
    if (rowsize != 16 && rowsize != 32)
-
 
240
        rowsize = 16;
-
 
241
 
-
 
242
    if (len > rowsize)      /* limit to one line at a time */
-
 
243
        len = rowsize;
-
 
244
    if (!is_power_of_2(groupsize) || groupsize > 8)
-
 
245
        groupsize = 1;
-
 
246
    if ((len % groupsize) != 0) /* no mixed size output */
-
 
247
        groupsize = 1;
-
 
248
 
-
 
249
    ngroups = len / groupsize;
-
 
250
    ascii_column = rowsize * 2 + rowsize / groupsize + 1;
-
 
251
 
-
 
252
    if (!linebuflen)
-
 
253
        goto overflow1;
-
 
254
 
-
 
255
    if (!len)
-
 
256
        goto nil;
-
 
257
 
-
 
258
    if (groupsize == 8) {
-
 
259
        const u64 *ptr8 = buf;
-
 
260
 
-
 
261
        for (j = 0; j < ngroups; j++) {
-
 
262
            ret = snprintf(linebuf + lx, linebuflen - lx,
-
 
263
                       "%s%16.16llx", j ? " " : "",
-
 
264
                       (unsigned long long)*(ptr8 + j));
-
 
265
            if (ret >= linebuflen - lx)
-
 
266
                goto overflow1;
-
 
267
            lx += ret;
-
 
268
        }
-
 
269
    } else if (groupsize == 4) {
-
 
270
        const u32 *ptr4 = buf;
-
 
271
 
-
 
272
        for (j = 0; j < ngroups; j++) {
-
 
273
            ret = snprintf(linebuf + lx, linebuflen - lx,
-
 
274
                       "%s%8.8x", j ? " " : "",
-
 
275
                       *(ptr4 + j));
-
 
276
            if (ret >= linebuflen - lx)
-
 
277
                goto overflow1;
-
 
278
            lx += ret;
-
 
279
        }
-
 
280
    } else if (groupsize == 2) {
-
 
281
        const u16 *ptr2 = buf;
-
 
282
 
-
 
283
        for (j = 0; j < ngroups; j++) {
-
 
284
            ret = snprintf(linebuf + lx, linebuflen - lx,
-
 
285
                       "%s%4.4x", j ? " " : "",
-
 
286
                       *(ptr2 + j));
-
 
287
            if (ret >= linebuflen - lx)
-
 
288
                goto overflow1;
-
 
289
            lx += ret;
-
 
290
        }
-
 
291
    } else {
-
 
292
        for (j = 0; j < len; j++) {
-
 
293
            if (linebuflen < lx + 3)
-
 
294
                goto overflow2;
-
 
295
            ch = ptr[j];
-
 
296
            linebuf[lx++] = hex_asc_hi(ch);
-
 
297
            linebuf[lx++] = hex_asc_lo(ch);
-
 
298
            linebuf[lx++] = ' ';
-
 
299
        }
-
 
300
        if (j)
-
 
301
            lx--;
-
 
302
    }
-
 
303
    if (!ascii)
-
 
304
        goto nil;
-
 
305
 
-
 
306
    while (lx < ascii_column) {
-
 
307
        if (linebuflen < lx + 2)
-
 
308
            goto overflow2;
-
 
309
        linebuf[lx++] = ' ';
-
 
310
    }
-
 
311
    for (j = 0; j < len; j++) {
-
 
312
        if (linebuflen < lx + 2)
-
 
313
            goto overflow2;
-
 
314
        ch = ptr[j];
-
 
315
        linebuf[lx++] = (isascii(ch) && isprint(ch)) ? ch : '.';
-
 
316
    }
-
 
317
nil:
-
 
318
    linebuf[lx] = '\0';
-
 
319
    return lx;
-
 
320
overflow2:
-
 
321
    linebuf[lx++] = '\0';
-
 
322
overflow1:
-
 
323
    return ascii ? ascii_column + len : (groupsize * 2 + 1) * ngroups - 1;
-
 
324
}
-
 
325
/**
-
 
326
 * print_hex_dump - print a text hex dump to syslog for a binary blob of data
-
 
327
 * @level: kernel log level (e.g. KERN_DEBUG)
-
 
328
 * @prefix_str: string to prefix each line with;
-
 
329
 *  caller supplies trailing spaces for alignment if desired
-
 
330
 * @prefix_type: controls whether prefix of an offset, address, or none
-
 
331
 *  is printed (%DUMP_PREFIX_OFFSET, %DUMP_PREFIX_ADDRESS, %DUMP_PREFIX_NONE)
-
 
332
 * @rowsize: number of bytes to print per line; must be 16 or 32
-
 
333
 * @groupsize: number of bytes to print at a time (1, 2, 4, 8; default = 1)
-
 
334
 * @buf: data blob to dump
-
 
335
 * @len: number of bytes in the @buf
-
 
336
 * @ascii: include ASCII after the hex output
-
 
337
 *
-
 
338
 * Given a buffer of u8 data, print_hex_dump() prints a hex + ASCII dump
-
 
339
 * to the kernel log at the specified kernel log level, with an optional
-
 
340
 * leading prefix.
-
 
341
 *
-
 
342
 * print_hex_dump() works on one "line" of output at a time, i.e.,
-
 
343
 * 16 or 32 bytes of input data converted to hex + ASCII output.
-
 
344
 * print_hex_dump() iterates over the entire input @buf, breaking it into
-
 
345
 * "line size" chunks to format and print.
-
 
346
 *
-
 
347
 * E.g.:
-
 
348
 *   print_hex_dump(KERN_DEBUG, "raw data: ", DUMP_PREFIX_ADDRESS,
-
 
349
 *          16, 1, frame->data, frame->len, true);
-
 
350
 *
-
 
351
 * Example output using %DUMP_PREFIX_OFFSET and 1-byte mode:
-
 
352
 * 0009ab42: 40 41 42 43 44 45 46 47 48 49 4a 4b 4c 4d 4e 4f  @ABCDEFGHIJKLMNO
-
 
353
 * Example output using %DUMP_PREFIX_ADDRESS and 4-byte mode:
-
 
354
 * ffffffff88089af0: 73727170 77767574 7b7a7978 7f7e7d7c  pqrstuvwxyz{|}~.
-
 
355
 */
-
 
356
void print_hex_dump(const char *level, const char *prefix_str, int prefix_type,
-
 
357
            int rowsize, int groupsize,
-
 
358
            const void *buf, size_t len, bool ascii)
-
 
359
{
-
 
360
    const u8 *ptr = buf;
-
 
361
    int i, linelen, remaining = len;
-
 
362
    unsigned char linebuf[32 * 3 + 2 + 32 + 1];
-
 
363
 
-
 
364
    if (rowsize != 16 && rowsize != 32)
-
 
365
        rowsize = 16;
-
 
366
 
-
 
367
    for (i = 0; i < len; i += rowsize) {
-
 
368
        linelen = min(remaining, rowsize);
-
 
369
        remaining -= rowsize;
-
 
370
 
-
 
371
        hex_dump_to_buffer(ptr + i, linelen, rowsize, groupsize,
-
 
372
                   linebuf, sizeof(linebuf), ascii);
-
 
373
 
-
 
374
        switch (prefix_type) {
-
 
375
        case DUMP_PREFIX_ADDRESS:
-
 
376
            printk("%s%s%p: %s\n",
-
 
377
                   level, prefix_str, ptr + i, linebuf);
-
 
378
            break;
-
 
379
        case DUMP_PREFIX_OFFSET:
-
 
380
            printk("%s%s%.8x: %s\n", level, prefix_str, i, linebuf);
-
 
381
            break;
-
 
382
        default:
-
 
383
            printk("%s%s%s\n", level, prefix_str, linebuf);
-
 
384
            break;
-
 
385
        }
-
 
386
    }
-
 
387
}
-
 
388
 
-
 
389
void print_hex_dump_bytes(const char *prefix_str, int prefix_type,
-
 
390
                          const void *buf, size_t len)
-
 
391
{
-
 
392
    print_hex_dump(KERN_DEBUG, prefix_str, prefix_type, 16, 1,
-
 
Line 393... Line 160...
393
                       buf, len, true);
160
 
Line 394... Line 161...
394
}
161
 
395
 
162
 
Line 1117... Line 884...
1117
                b = r;
884
                b = r;
1118
        }
885
        }
1119
        return b;
886
        return b;
1120
}
887
}
Line -... Line 888...
-
 
888
 
-
 
889
void vfree(const void *addr)
-
 
890
{
-
 
891
    KernelFree(addr);
-
 
892
}