strings.c (1194c3cb5727aeec402eb7421f31869d897e2fcd) strings.c (55d3f888de48acdad3979651a73825ac11513010)
1/*-
2 * Copyright (c) 2007 S.Sam Arun Raj
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright

--- 70 unchanged lines hidden (view full) ---

79 { "encoding", required_argument, NULL, 'e'},
80 { "help", no_argument, NULL, 'h'},
81 { "print-file-name", no_argument, NULL, 'f'},
82 { "radix", required_argument, NULL, 't'},
83 { "version", no_argument, NULL, 'v'},
84 { NULL, 0, NULL, 0 }
85};
86
1/*-
2 * Copyright (c) 2007 S.Sam Arun Raj
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright

--- 70 unchanged lines hidden (view full) ---

79 { "encoding", required_argument, NULL, 'e'},
80 { "help", no_argument, NULL, 'h'},
81 { "print-file-name", no_argument, NULL, 'f'},
82 { "radix", required_argument, NULL, 't'},
83 { "version", no_argument, NULL, 'v'},
84 { NULL, 0, NULL, 0 }
85};
86
87long getcharacter(FILE *);
87int getcharacter(FILE *, long *);
88int handle_file(const char *);
89int handle_elf(const char *, FILE *);
90int handle_binary(const char *, FILE *, size_t);
91int find_strings(const char *, FILE *, off_t, off_t);
92void show_version(void);
93void usage(void);
94
95/*

--- 190 unchanged lines hidden (view full) ---

286 (void)elf_end(elf);
287 return (rc);
288}
289
290/*
291 * Retrieves a character from input stream based on the encoding
292 * type requested.
293 */
88int handle_file(const char *);
89int handle_elf(const char *, FILE *);
90int handle_binary(const char *, FILE *, size_t);
91int find_strings(const char *, FILE *, off_t, off_t);
92void show_version(void);
93void usage(void);
94
95/*

--- 190 unchanged lines hidden (view full) ---

286 (void)elf_end(elf);
287 return (rc);
288}
289
290/*
291 * Retrieves a character from input stream based on the encoding
292 * type requested.
293 */
294long
295getcharacter(FILE *pfile)
294int
295getcharacter(FILE *pfile, long *rt)
296{
296{
297 long rt;
298 int i, c;
299 char buf[4];
300
301 for(i = 0; i < encoding_size; i++) {
302 c = getc(pfile);
303 if (c == EOF)
297 int i, c;
298 char buf[4];
299
300 for(i = 0; i < encoding_size; i++) {
301 c = getc(pfile);
302 if (c == EOF)
304 return (EOF);
303 return (-1);
305 buf[i] = c;
306 }
307
304 buf[i] = c;
305 }
306
308 rt = EOF;
309 switch (encoding) {
310 case ENCODING_7BIT:
311 case ENCODING_8BIT:
307 switch (encoding) {
308 case ENCODING_7BIT:
309 case ENCODING_8BIT:
312 rt = buf[0];
310 *rt = buf[0];
313 break;
314 case ENCODING_16BIT_BIG:
311 break;
312 case ENCODING_16BIT_BIG:
315 rt = (buf[0] << 8) | buf[1];
313 *rt = (buf[0] << 8) | buf[1];
316 break;
317 case ENCODING_16BIT_LITTLE:
314 break;
315 case ENCODING_16BIT_LITTLE:
318 rt = buf[0] | (buf[1] << 8);
319 break;
316 *rt = buf[0] | (buf[1] << 8);
317 break;
320 case ENCODING_32BIT_BIG:
318 case ENCODING_32BIT_BIG:
321 rt = ((long) buf[0] << 24) | ((long) buf[1] << 16) |
319 *rt = ((long) buf[0] << 24) | ((long) buf[1] << 16) |
322 ((long) buf[2] << 8) | buf[3];
323 break;
324 case ENCODING_32BIT_LITTLE:
320 ((long) buf[2] << 8) | buf[3];
321 break;
322 case ENCODING_32BIT_LITTLE:
325 rt = buf[0] | ((long) buf[1] << 8) | ((long) buf[2] << 16) |
323 *rt = buf[0] | ((long) buf[1] << 8) | ((long) buf[2] << 16) |
326 ((long) buf[3] << 24);
327 break;
324 ((long) buf[3] << 24);
325 break;
326 default:
327 return (-1);
328 }
328 }
329 return (rt);
329
330 return (0);
330}
331
332/*
333 * Input stream is read until the end of file is reached or until
334 * the section size is reached in case of ELF files. Contiguous
335 * characters of >= min_size(default 4) will be displayed.
336 */
337int

--- 14 unchanged lines hidden (view full) ---

352 cur_off = offset;
353 start_off = 0;
354 for (;;) {
355 if ((offset + size) && (cur_off >= offset + size))
356 break;
357 start_off = cur_off;
358 memset(obuf, 0, min_len + 1);
359 for(i = 0; i < min_len; i++) {
331}
332
333/*
334 * Input stream is read until the end of file is reached or until
335 * the section size is reached in case of ELF files. Contiguous
336 * characters of >= min_size(default 4) will be displayed.
337 */
338int

--- 14 unchanged lines hidden (view full) ---

353 cur_off = offset;
354 start_off = 0;
355 for (;;) {
356 if ((offset + size) && (cur_off >= offset + size))
357 break;
358 start_off = cur_off;
359 memset(obuf, 0, min_len + 1);
360 for(i = 0; i < min_len; i++) {
360 c = getcharacter(pfile);
361 if (c == EOF)
361 if (getcharacter(pfile, &c) < 0)
362 goto _exit1;
363 if (PRINTABLE(c)) {
364 obuf[i] = c;
365 obuf[i + 1] = 0;
366 cur_off += encoding_size;
367 } else {
368 if (encoding == ENCODING_8BIT &&
369 (uint8_t)c > 127) {

--- 25 unchanged lines hidden (view full) ---

395 }
396 }
397 printf("%s", obuf);
398
399 for (;;) {
400 if ((offset + size) &&
401 (cur_off >= offset + size))
402 break;
362 goto _exit1;
363 if (PRINTABLE(c)) {
364 obuf[i] = c;
365 obuf[i + 1] = 0;
366 cur_off += encoding_size;
367 } else {
368 if (encoding == ENCODING_8BIT &&
369 (uint8_t)c > 127) {

--- 25 unchanged lines hidden (view full) ---

395 }
396 }
397 printf("%s", obuf);
398
399 for (;;) {
400 if ((offset + size) &&
401 (cur_off >= offset + size))
402 break;
403 c = getcharacter(pfile);
404 cur_off += encoding_size;
405 if (!PRINTABLE(c) || c == EOF)
403 if (getcharacter(pfile, &c) < 0)
406 break;
404 break;
405 cur_off += encoding_size;
407 if (encoding == ENCODING_8BIT &&
408 (uint8_t)c > 127) {
409 putchar(c);
410 continue;
411 }
406 if (encoding == ENCODING_8BIT &&
407 (uint8_t)c > 127) {
408 putchar(c);
409 continue;
410 }
411 if (!PRINTABLE(c))
412 break;
412 putchar(c);
413 }
414 putchar('\n');
415 }
416 }
417_exit1:
418 free(obuf);
419 return (0);

--- 30 unchanged lines hidden ---
413 putchar(c);
414 }
415 putchar('\n');
416 }
417 }
418_exit1:
419 free(obuf);
420 return (0);

--- 30 unchanged lines hidden ---