How do you write the hexdump -C function in C? - c

The first block of code below is an example of how I want my output to look like when I run my hexdump command in C. Atm, I don't know how to get the 3 columns of double spaces and I don't know how to add the ASCII representation and "." for non-ASCII of the bytes on the right.
hexdump -C /etc/passwd
00000000 72 6f 6f 74 3a 78 3a 30 3a 30 3a 72 6f 6f 74 3a |root:x:0:0:root:|
00000010 2f 72 6f 6f 74 3a 2f 62 69 6e 2f 62 61 73 68 0a |/root:/bin/bash.|
00000020 64 61 65 6d 6f 6e 3a 78 3a 31 3a 31 3a 64 61 65 |daemon:x:1:1:dae|
00000030 6d 6f 6e 3a 2f 75 73 72 2f 73 62 69 6e 3a 2f 62 |mon:/usr/sbin:/b|
00000040 69 6e 2f 73 68 0a 62 69 6e 3a 78 3a 32 3a 32 3a |in/sh.bin:x:2:2:|
00000050 62 69 6e 3a 2f 62 69 6e 3a 2f 62 69 6e 2f 73 68 |bin:/bin:/bin/sh|
This is my actual output:
00000000 2f 2a 20 68 65 6c 6c 6f 2e 63 20 2a 2f a. a. 23
00000010 69 6e 63 6c 75 64 65 20 3c 73 74 64 69 6f 2e 68
00000020 3e a. a. 69 6e 74 20 6d 61 69 6e 28 69 6e 74 20
00000030 61 72 67 63 2c 20 63 68 61 72 20 2a 61 72 67 76
00000040 5b 5d 29 a. 7b a. 20 20 70 75 74 73 28 22 48 65
00000050 6c 6c 6f 20 77 6f 72 6c 64 21 5c 6e 22 29 3b a.
00000060 20 20 72 65 74 75 72 6e 20 30 3b a. 7d a.
0000006e
thanks

I don't know how to get the 3 columns of double spaces
Just emit an extra space in the appropriate place.
The first one (between the offset and the first octet) is always there.
The second one is emitted before the 8th octet if there is one.
The third one is also always there.
You can either build them into a format string or bake them into your octet-formatting loop.
I don't know how to add the ASCII representation and "." for non-ASCII of the bytes on the right
Roughly, by using isprint() and then printing toprint instead of the raw character
char toprint = isprint(raw) ? raw : '.';

Related

Why SMBIOS of the same machine reported by Linux differs from reported by Windows?

Recently I was porting dmidecode to Windows, and was looking into SMBIOS table. I thought that will be straightforward task, just remove Linux dependent stuff from dmidecode to make it compile on windows, and instead of getting SMBIOS from memory get it with GetSystemFirmwareTable('RSMB', 0, smBiosData, size1); and feed it to other functions.
But there is a problem, GetSystemFirmwareTable reports not full SMBIOS, it is truncated at the beginning and lack some values in the header, like number of structures(I have ver 2.8 other can differ). the rest of SMBIOS is the same and can be decoded by dmidecode.
Can someone explain why is that? Or am I wrong and doing it wrong(reading SMBIOS in Windows)?
The difference:
SMBIOS from Linux(small part):
5F 53 4D 5F DF 1F 02 08 9A 00 00 00 00 00 00 00 5F 44 4D 49 5F 35 A7 09 20 00 00 00 3B 00 28 00 00 1A 00 00 01 02 00 F0 03 FF 80 98 FB 7F 01 00 00 00 03 0D 05 11 FF FF 20 00 41 6D 65 72 69 63 61 6E 20 4D 65 67 61 74 72 65 6E 64 73 20 49 6E 74 65 72 6E 61 74 69 6F 6E 61 6C 2C 20 4C 4C 43 2E 00 31 2E 44 36 00 30 31 2F 31 32 2F 32 30 32 31 00 00 01 1B 01 00 01 02 03 04 57 87 C5 95 08 81 18 D9 A7 58 00 D8 61 A1 F2 74 06 05 06 4D 69 63 72 6F 2D 53 74 61 72 20 49 6E 74 65 72 6E 61 74 69 6F 6E 61 6C 20 43 6F 2E 2C 20 4C 74 64 2E
SMBIOS from Windows(small part):
00 02 08 00 A7 09 00 00 00 1A 00 00 01 02 00 F0 03 FF 80 98 FB 7F 01 00 00 00 03 0D 05 11 FF FF 20 00 41 6D 65 72 69 63 61 6E 20 4D 65 67 61 74 72 65 6E 64 73 20 49 6E 74 65 72 6E 61 74 69 6F 6E 61 6C 2C 20 4C 4C 43 2E 00 31 2E 44 36 00 30 31 2F 31 32 2F 32 30 32 31 00 00 01 1B 01 00 01 02 03 04 57 87 C5 95 08 81 18 D9 A7 58 00 D8 61 A1 F2 74 06 05 06 4D 69 63 72 6F 2D 53 74 61 72 20 49 6E 74 65 72 6E 61 74 69 6F 6E 61 6C 20 43 6F 2E 2C 20 4C 74 64 2E

How do I add a plaintext file in a second column in a makeshift hexdump program?

I'm attempting to recreate a hexdump program in C. This is what I have so far
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
typedef unsigned char byte;
void openfile(FILE **fptr, char *fname)
{
*fptr = fopen(fname, "r");
if(fptr == NULL)
{
fprintf(stderr, "How dare you... \n");
exit(-1);
}
}
int readContents(FILE *fptr, int *filesize, byte *array)
{
return (fread(array, 1, *filesize, fptr)); // read all bytes from the file
}
int printit(FILE **fptr, int *filesize, byte *array)
{
byte sixteen[17];
readContents(*fptr, filesize, array);
int idx;
for(idx = 0; idx <= *filesize; idx++)
{
if((idx%16)==0)
{
printf("%08x ", idx);
}
if((idx%8)==0)
{
printf(" ");
}
printf("%02x ", array[idx]);
if((idx+1)%16==0)
{
printf("\n");
}
}
for(idx = 0; idx <= *filesize; idx++)
{
if((idx%16)==0)
printf("%c ", array[idx]);
}
}
int main(int argc, char *argv[])
{
char filename[30];
if(argc > 1)
{
strcpy(filename, argv[1]);
}
else
{
printf("Please enter a filename: ");
scanf(" %[^\n]s", filename);
}
FILE *ptr;
openfile(&ptr, filename);
fseek(ptr, 0, SEEK_END);
int filelen = ftell(ptr);
printf("Length of file is: %d\n", filelen);
fseek(ptr, 0, SEEK_SET);
byte *bytes = malloc(filelen);
printit(&ptr, &filelen, bytes);
printf("\n");
free(bytes);
fclose(ptr);
}
The offset placement and hexes appear successfully in the output, but I just can't seem to figure out how I should add a column on the right hand side with the plaintext of the contents of a file
This is what the output looks like without it
00000010 03 00 3e 00 01 00 00 00 70 09 00 00 00 00 00 00
00000020 40 00 00 00 00 00 00 00 98 2b 00 00 00 00 00 00
00000030 00 00 00 00 40 00 38 00 09 00 40 00 1d 00 1c 00
00000040 06 00 00 00 04 00 00 00 40 00 00 00 00 00 00 00
00000050 40 00 00 00 00 00 00 00 40 00 00 00 00 00 00 00
00000060 f8 01 00 00 00 00 00 00 f8 01 00 00 00 00 00 00
00000070 08 00 00 00 00 00 00 00 03 00 00 00 04 00 00 00
00000080 38 02 00 00 00 00 00 00 38 02 00 00 00 00 00 00
00000090 38 02 00 00 00 00 00 00 1c 00 00 00 00 00 00 00
000000a0 1c 00 00 00 00 00 00 00 01 00 00 00 00 00 00 00
000000b0 01 00 00 00 05 00 00 00 00 00 00 00 00 00 00 00
000000c0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
000000d0 38 0f 00 00 00 00 00 00 38 0f 00 00 00 00 00 00
000000e0 00 00 20 00 00 00 00 00 01 00 00 00 06 00 00 00
000000f0 50 1d 00 00 00 00 00 00 50 1d 20 00 00 00 00 00
00000100 50 1d 20 00 00 00 00 00 c0 02 00 00 00 00 00 00
00000110 e0 02 00 00 00 00 00 00 00 00 20 00 00 00 00 00
00000120 02 00 00 00 06 00 00 00 60 1d 00 00 00 00 00 00
00000130 60 1d 20 00 00 00 00 00 60 1d 20 00 00 00 00 00
00000140 f0 01 00 00 00 00 00 00 f0 01 00 00 00 00 00 00
00000150 08 00 00 00 00 00 00 00 04 00 00 00 04 00 00 00
00000160 54 02 00 00 00 00 00 00 54 02 00 00 00 00 00 00
00000170 54 02 00 00 00 00 00 00 44 00 00 00 00 00 00 00
00000180 44 00 00 00 00 00 00 00 04 00 00 00 00 00 00 00
00000190 50 e5 74 64 04 00 00 00 f0 0d 00 00 00 00 00 00
000001a0 f0 0d 00 00 00 00 00 00 f0 0d 00 00 00 00 00 00
000001b0 3c 00 00 00 00 00 00 00 3c 00 00 00 00 00 00 00
000001c0 04 00 00 00 00 00 00 00 51 e5 74 64 06 00 00 00
000001d0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
And this is where the plaintext appears at the end
# # � 8 8 8 P P � ` � T T D P � < R P � d o G G ` J o + D u �
� h . e n a c o s i i i L . I C n e b u P p � � p � � � � � � � � � � � � � � � � � � � � � � 1 � � H 9 � ] H ) H � ] � � � U � � � � 5 � � H � U � � � � � � H , � � � � H � � � � � d � A � � L A � n : D . V x f F d f � X � �
� � � A p
�
� � � � � ` � � � � 6 V G . 4 8 t � � 0 X ` � P ` � 0 7 X m P � � � ` � � � K g � � � � ' C e 0 � + f z w � � � X f m o o d a n n e A _ M _ F L _ p . a . e r 5 # t L G b L a # l _ _ s c # b o 5 # # T e b I f 2 I a r t . n . s n e l n r m f m c 8 T t � � � � � 0 B X p ` p � 0 P X ` P 0 # x �
I also happen to be reading an ELF file, FYI
EDIT: I forgot to paste #include stdio.h
the following proposed code:
cleanly compiles
checks for and handles any errors
produces the desired output
avoids the use of malloc() and free()
does not try to consume the whole input file in one gulp
and now, the proposed code:
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#define MAX_BYTES 16
#define MAX_FILENAME_LEN 30
typedef unsigned char byte;
static unsigned int idx = 0;
void printit( size_t bytesRead, byte *array)
{
// display index into file
printf("\n%08x ", idx);
for( size_t i = 0; i < bytesRead; i++ )
{
if( i == 8 )
{
printf(" ");
}
printf("%02x ", array[i]);
}
printf( "%s", " | " );
for( size_t i = 0; i< bytesRead; i++ )
{
if( isalpha( array[i] ) )
{
printf( "%c", array[i] );
}
else
{
printf( "%c", '.' );
}
}
printf( "%s", " | " );
idx += (unsigned int)bytesRead;
}
int main(int argc, char *argv[])
{
char filename[ MAX_FILENAME_LEN ];
if(argc > 1)
{
strncpy(filename, argv[1], MAX_FILENAME_LEN );
}
else
{
printf("Please enter a filename: ");
if( scanf("%29s", filename) != 1 )
{
fprintf( stderr, "scanf to get filename from user failed\n" );
exit( EXIT_FAILURE );
}
}
FILE *fp = fopen(filename, "rb");
if( ! fp )
{
perror( "fopen for input file failed");
exit( EXIT_FAILURE );
}
fseek( fp, 0, SEEK_END );
long filelen = ftell( fp );
printf("Length of file is: %ld\n", filelen);
fseek( fp, 0, SEEK_SET );
byte filedata[ 16 ];
size_t bytesRead;
while( ( bytesRead = fread( filedata, 1, sizeof filedata, fp )) > 0 )
{
//printf( "bytesRead: %zu\n", bytesRead );
printit( bytesRead, filedata );
}
fclose( fp );
}
here is a typical run of the proposed code, against the source file:
Please enter a filename: untitled2.c
Length of file is: 1533
00000000 09 23 69 6e 63 6c 75 64 65 20 3c 73 74 72 69 6e | ..include..strin |
00000010 67 2e 68 3e 0a 09 23 69 6e 63 6c 75 64 65 20 3c | g.h....include.. |
00000020 73 74 64 6c 69 62 2e 68 3e 0a 09 23 69 6e 63 6c | stdlib.h....incl |
00000030 75 64 65 20 3c 73 74 64 69 6f 2e 68 3e 0a 09 23 | ude..stdio.h.... |
00000040 69 6e 63 6c 75 64 65 20 3c 63 74 79 70 65 2e 68 | include..ctype.h |
00000050 3e 0a 0a 0a 09 23 64 65 66 69 6e 65 20 4d 41 58 | ......define.MAX |
00000060 5f 42 59 54 45 53 20 31 36 0a 09 23 64 65 66 69 | .BYTES......defi |
00000070 6e 65 20 4d 41 58 5f 46 49 4c 45 4e 41 4d 45 5f | ne.MAX.FILENAME. |
00000080 4c 45 4e 20 33 30 0a 0a 09 74 79 70 65 64 65 66 | LEN......typedef |
00000090 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 62 | .unsigned.char.b |
000000a0 79 74 65 3b 0a 0a 09 73 74 61 74 69 63 20 75 6e | yte....static.un |
000000b0 73 69 67 6e 65 64 20 69 6e 74 20 69 64 78 20 3d | signed.int.idx.. |
000000c0 20 30 3b 0a 0a 09 76 6f 69 64 20 70 72 69 6e 74 | ......void.print |
000000d0 69 74 28 20 73 69 7a 65 5f 74 20 62 79 74 65 73 | it..size.t.bytes |
000000e0 52 65 61 64 2c 20 62 79 74 65 20 2a 61 72 72 61 | Read..byte..arra |
000000f0 79 29 0a 09 7b 0a 09 09 2f 2f 20 64 69 73 70 6c | y..........displ |
00000100 61 79 20 69 6e 64 65 78 20 69 6e 74 6f 20 66 69 | ay.index.into.fi |
00000110 6c 65 0a 09 09 70 72 69 6e 74 66 28 22 5c 6e 25 | le...printf...n. |
00000120 30 38 78 20 22 2c 20 69 64 78 29 3b 0a 09 09 0a | ..x....idx...... |
00000130 09 09 66 6f 72 28 20 73 69 7a 65 5f 74 20 69 20 | ..for..size.t.i. |
00000140 3d 20 30 3b 20 69 20 3c 20 62 79 74 65 73 52 65 | .....i...bytesRe |
00000150 61 64 3b 20 69 2b 2b 20 20 29 0a 09 09 7b 0a 09 | ad..i........... |
00000160 09 09 69 66 28 20 69 20 3d 3d 20 38 20 29 0a 09 | ..if..i......... |
00000170 09 09 7b 0a 09 09 09 09 70 72 69 6e 74 66 28 22 | ........printf.. |
00000180 20 22 29 3b 0a 09 09 09 7d 0a 0a 09 09 09 70 72 | ..............pr |
00000190 69 6e 74 66 28 22 25 30 32 78 20 22 2c 20 61 72 | intf.....x....ar |
000001a0 72 61 79 5b 69 5d 29 3b 0a 09 09 7d 0a 09 09 0a | ray.i........... |
000001b0 09 09 70 72 69 6e 74 66 28 20 22 25 73 22 2c 20 | ..printf....s... |
000001c0 22 20 7c 20 22 20 29 3b 0a 09 09 0a 09 09 66 6f | ..............fo |
000001d0 72 28 20 73 69 7a 65 5f 74 20 69 20 3d 20 30 3b | r..size.t.i..... |
000001e0 20 69 3c 20 62 79 74 65 73 52 65 61 64 3b 20 69 | .i..bytesRead..i |
000001f0 2b 2b 20 29 0a 09 09 7b 0a 09 09 09 69 66 28 20 | ............if.. |
00000200 69 73 61 6c 70 68 61 28 20 61 72 72 61 79 5b 69 | isalpha..array.i |
00000210 5d 20 29 20 29 0a 09 09 09 7b 0a 09 09 09 09 20 | ................ |
00000220 70 72 69 6e 74 66 28 20 22 25 63 22 2c 20 61 72 | printf....c...ar |
00000230 72 61 79 5b 69 5d 20 29 3b 0a 09 09 09 7d 0a 09 | ray.i........... |
00000240 09 09 20 0a 09 09 09 65 6c 73 65 0a 09 09 09 7b | .......else..... |
00000250 0a 09 09 09 09 20 70 72 69 6e 74 66 28 20 22 25 | ......printf.... |
00000260 63 22 2c 20 27 2e 27 20 29 3b 0a 09 09 09 7d 0a | c............... |
00000270 09 09 7d 0a 09 09 70 72 69 6e 74 66 28 20 22 25 | ......printf.... |
00000280 73 22 2c 20 22 20 7c 20 22 20 29 3b 0a 09 09 0a | s............... |
00000290 09 09 69 64 78 20 2b 3d 20 28 75 6e 73 69 67 6e | ..idx.....unsign |
000002a0 65 64 20 69 6e 74 29 62 79 74 65 73 52 65 61 64 | ed.int.bytesRead |
000002b0 3b 20 20 0a 09 7d 0a 0a 0a 09 69 6e 74 20 6d 61 | ..........int.ma |
000002c0 69 6e 28 69 6e 74 20 61 72 67 63 2c 20 63 68 61 | in.int.argc..cha |
000002d0 72 20 2a 61 72 67 76 5b 5d 29 0a 09 7b 0a 09 09 | r..argv......... |
000002e0 63 68 61 72 20 66 69 6c 65 6e 61 6d 65 5b 20 4d | char.filename..M |
000002f0 41 58 5f 46 49 4c 45 4e 41 4d 45 5f 4c 45 4e 20 | AX.FILENAME.LEN. |
00000300 5d 3b 0a 0a 09 09 69 66 28 61 72 67 63 20 3e 20 | ......if.argc... |
00000310 31 29 0a 09 09 7b 0a 09 09 09 73 74 72 6e 63 70 | ..........strncp |
00000320 79 28 66 69 6c 65 6e 61 6d 65 2c 20 61 72 67 76 | y.filename..argv |
00000330 5b 31 5d 2c 20 4d 41 58 5f 46 49 4c 45 4e 41 4d | .....MAX.FILENAM |
00000340 45 5f 4c 45 4e 20 29 3b 0a 09 09 7d 0a 09 09 0a | E.LEN........... |
00000350 09 09 65 6c 73 65 0a 09 09 7b 0a 09 09 09 70 72 | ..else........pr |
00000360 69 6e 74 66 28 22 50 6c 65 61 73 65 20 65 6e 74 | intf..Please.ent |
00000370 65 72 20 61 20 66 69 6c 65 6e 61 6d 65 3a 20 22 | er.a.filename... |
00000380 29 3b 0a 09 09 09 69 66 28 20 73 63 61 6e 66 28 | ......if..scanf. |
00000390 22 25 32 39 73 22 2c 20 66 69 6c 65 6e 61 6d 65 | ....s...filename |
000003a0 29 20 21 3d 20 31 20 29 0a 09 09 09 7b 0a 09 09 | ................ |
000003b0 09 09 66 70 72 69 6e 74 66 28 20 73 74 64 65 72 | ..fprintf..stder |
000003c0 72 2c 20 22 73 63 61 6e 66 20 74 6f 20 67 65 74 | r...scanf.to.get |
000003d0 20 66 69 6c 65 6e 61 6d 65 20 66 72 6f 6d 20 75 | .filename.from.u |
000003e0 73 65 72 20 66 61 69 6c 65 64 5c 6e 22 20 29 3b | ser.failed.n.... |
000003f0 0a 09 09 09 09 65 78 69 74 28 20 45 58 49 54 5f | .....exit..EXIT. |
00000400 46 41 49 4c 55 52 45 20 29 3b 0a 09 09 09 7d 0a | FAILURE......... |
00000410 09 09 7d 0a 0a 09 09 46 49 4c 45 20 2a 66 70 20 | .......FILE..fp. |
00000420 3d 20 66 6f 70 65 6e 28 66 69 6c 65 6e 61 6d 65 | ..fopen.filename |
00000430 2c 20 22 72 62 22 29 3b 0a 09 09 69 66 28 20 21 | ...rb......if... |
00000440 20 66 70 20 29 0a 09 09 7b 0a 09 09 09 70 65 72 | .fp..........per |
00000450 72 6f 72 28 20 22 66 6f 70 65 6e 20 66 6f 72 20 | ror...fopen.for. |
00000460 69 6e 70 75 74 20 66 69 6c 65 20 66 61 69 6c 65 | input.file.faile |
00000470 64 22 29 3b 0a 09 09 09 65 78 69 74 28 20 45 58 | d.......exit..EX |
00000480 49 54 5f 46 41 49 4c 55 52 45 20 29 3b 0a 09 09 | IT.FAILURE...... |
00000490 7d 0a 0a 09 09 66 73 65 65 6b 28 20 66 70 2c 20 | .....fseek..fp.. |
000004a0 30 2c 20 53 45 45 4b 5f 45 4e 44 20 29 3b 0a 09 | ...SEEK.END..... |
000004b0 09 6c 6f 6e 67 20 66 69 6c 65 6c 65 6e 20 3d 20 | .long.filelen... |
000004c0 66 74 65 6c 6c 28 20 66 70 20 29 3b 0a 09 09 70 | ftell..fp......p |
000004d0 72 69 6e 74 66 28 22 4c 65 6e 67 74 68 20 6f 66 | rintf..Length.of |
000004e0 20 66 69 6c 65 20 69 73 3a 20 25 6c 64 5c 6e 22 | .file.is...ld.n. |
000004f0 2c 20 66 69 6c 65 6c 65 6e 29 3b 0a 09 09 66 73 | ..filelen.....fs |
00000500 65 65 6b 28 20 66 70 2c 20 30 2c 20 53 45 45 4b | eek..fp.....SEEK |
00000510 5f 53 45 54 20 29 3b 0a 09 09 0a 09 09 62 79 74 | .SET.........byt |
00000520 65 20 66 69 6c 65 64 61 74 61 5b 20 31 36 20 5d | e.filedata...... |
00000530 3b 0a 09 09 73 69 7a 65 5f 74 20 62 79 74 65 73 | ....size.t.bytes |
00000540 52 65 61 64 3b 0a 09 09 77 68 69 6c 65 28 20 28 | Read....while... |
00000550 20 62 79 74 65 73 52 65 61 64 20 3d 20 66 72 65 | .bytesRead...fre |
00000560 61 64 28 20 66 69 6c 65 64 61 74 61 2c 20 31 2c | ad..filedata.... |
00000570 20 73 69 7a 65 6f 66 20 66 69 6c 65 64 61 74 61 | .sizeof.filedata |
00000580 2c 20 66 70 20 29 29 20 3e 20 30 20 29 0a 09 09 | ..fp............ |
00000590 7b 0a 09 09 09 2f 2f 70 72 69 6e 74 66 28 20 22 | .......printf... |
000005a0 62 79 74 65 73 52 65 61 64 3a 20 25 7a 75 5c 6e | bytesRead...zu.n |
000005b0 22 2c 20 62 79 74 65 73 52 65 61 64 20 29 3b 0a | ...bytesRead.... |
000005c0 09 09 09 70 72 69 6e 74 69 74 28 20 62 79 74 65 | ...printit..byte |
000005d0 73 52 65 61 64 2c 20 66 69 6c 65 64 61 74 61 20 | sRead..filedata. |
000005e0 29 3b 0a 09 09 7d 0a 09 09 0a 09 09 66 63 6c 6f | ............fclo |
000005f0 73 65 28 20 66 70 20 29 3b 0a 09 7d 0a | se..fp....... |
Note: when the final read is less than 16 bytes, you might want to fill out the last line of the input with spaces
Note you might want to modify the if( isalpha() ) to if( isprint() ) so other than the letters a..z and A..Z are printed
I'd to the job somewhat differently. In fact, (many years ago) I did the job somewhat differently:
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char **argv) {
unsigned long offset = 0;
FILE *input;
int bytes, i, j;
unsigned char buffer[16];
char outbuffer[60];
if ( argc < 2 ) {
fprintf(stderr, "\nUsage: dump filename [filename...]");
return EXIT_FAILURE;
}
for (j=1;j<argc; ++j) {
if ( NULL ==(input=fopen(argv[j], "rb")))
continue;
printf("\n%s:\n", argv[j]);
while (0 < (bytes=fread(buffer, 1, 16, input))) {
sprintf(outbuffer, "%8.8lx: ", offset+=16);
for (i=0;i<bytes;i++) {
sprintf(outbuffer+10+3*i, "%2.2X ",buffer[i]);
if (!isprint(buffer[i]))
buffer[i] = '.';
}
printf("%-60s %*.*s\n", outbuffer, bytes, bytes, buffer);
}
fclose(input);
}
return 0;
}
(edited to match hexdump -Cv output)
Along the same lines and continuing from my comment, if you read from your input file in 16-byte chunks, you can output the address and your hex-bytes and then add a separator and then loop separately again over the bytes to provide the ASCII values as the right column. Below the program takes the filename to read as the first argument (or it reads from stdin by default if no argument is given).
If the filename is provided, you can also provide a second option that provides the character to use in place of non-printing characters in the ASCII output (you should actually implement getopt to handle the arguments - but that if left to you should you desire).
A quick implementation could be:
#include <stdio.h>
#define OFFSET 16 /* bytes to display per-line of output */
int main (int argc, char const *argv[])
{
char buff[OFFSET] = "",
sep = argc > 2 ? *argv[2] : '.'; /* default non-print char ',' */
int bytes, /* (add getopt to handle opts) */
address = 0,
i;
/* read from filename provided as 1st argument (stdin by default) */
FILE *fp = argc > 1 ? fopen (argv[1], "rb") : stdin;
if (!fp) { /* validate file open for reading */
perror ("fopen");
return 1;
}
/* read OFFSET number of bytes at a time */
while ((bytes = fread(buff, 1, sizeof buff, fp)) > 0) {
/* print address */
printf("%0*x ", OFFSET / 2, address);
address += OFFSET;
/* print hex values */
for (i = 0; i < OFFSET; i++) {
if (i && i % (OFFSET / 2) == 0) /* additional space between bytes */
putchar (' ');
if (i >= bytes) /* final line where i >= bytes */
fputs (" ", stdout);
else /* output zero-padded hex-byte */
printf("%02hhx ", (unsigned char)buff[i]);
}
fputs (" |", stdout); /* add separator */
/* print ascii values - putchar() outputs a single char */
for (i = 0; i < bytes; i++)
putchar (buff[i] >= ' ' && buff[i] <= '~' ? buff[i] : sep);
puts ("|"); /* add closing '|' and \n */
}
printf("%0*x\n", OFFSET / 2, address); /* output final address */
if (fp != stdin) /* close file if not stdin */
fclose (fp);
}
Example Use/Output
$ ./bin/hexdump dat/animals.bin.dat
00000000 01 00 41 62 69 67 61 69 6c 00 00 00 04 00 00 00 |..Abigail.......|
00000010 74 01 00 00 74 01 43 61 70 72 69 63 6f 72 6e 69 |t...t.Capricorni|
00000020 73 20 73 75 6d 61 74 72 61 65 6e 73 69 73 00 00 |s sumatraensis..|
00000030 5c 3f 1b 00 5c 4f 1b 00 5c 53 08 00 02 00 75 6e |\?..\O..\S....un|
00000040 6b 6e 6f 77 6e 00 00 00 04 00 00 00 74 01 00 00 |known.......t...|
...
00000370 73 00 73 69 73 00 00 00 5c 3f 1b 00 5c 4f 1b 00 |s.sis...\?..\O..|
00000380 5c 58 01 00 |\X..|
00000384
With a space provided as the second argument as the non-printing char for the ASCII output:
$ ./bin/hexdump dat/animals.bin.dat ' '
00000000 01 00 41 62 69 67 61 69 6c 00 00 00 04 00 00 00 | Abigail |
00000010 74 01 00 00 74 01 43 61 70 72 69 63 6f 72 6e 69 |t t Capricorni|
00000020 73 20 73 75 6d 61 74 72 61 65 6e 73 69 73 00 00 |s sumatraensis |
00000030 5c 3f 1b 00 5c 4f 1b 00 5c 53 08 00 02 00 75 6e |\? \O \S un|
00000040 6b 6e 6f 77 6e 00 00 00 04 00 00 00 74 01 00 00 |known t |
...
00000370 73 00 73 69 73 00 00 00 5c 3f 1b 00 5c 4f 1b 00 |s sis \? \O |
00000380 5c 58 01 00 |\X |
00000384
Comparison with hexdump -Cv:
$ hexdump -Cv dat/animals.bin.dat
00000000 01 00 41 62 69 67 61 69 6c 00 00 00 04 00 00 00 |..Abigail.......|
00000010 74 01 00 00 74 01 43 61 70 72 69 63 6f 72 6e 69 |t...t.Capricorni|
00000020 73 20 73 75 6d 61 74 72 61 65 6e 73 69 73 00 00 |s sumatraensis..|
00000030 5c 3f 1b 00 5c 4f 1b 00 5c 53 08 00 02 00 75 6e |\?..\O..\S....un|
00000040 6b 6e 6f 77 6e 00 00 00 04 00 00 00 74 01 00 00 |known.......t...|
...
00000370 73 00 73 69 73 00 00 00 5c 3f 1b 00 5c 4f 1b 00 |s.sis...\?..\O..|
00000380 5c 58 01 00 |\X..|
00000384
Let me know if you have further questions.

The first letter is being repeated of the first word

I am reading text from a file and outputting it as binary. I have modified the binary conversion as per follows:
Each capital letter shall start with 01 and will be followed by 5 bits.
The 5 bits shall hold the value of the letter.
The letters will have the value as A-2,B-3,C-4,D-5...
For example: HI-> (0101001)(0101010)
My code snippet is as follows:
void printinbits(int n)
{
for (int c = 4; c >= 0; c--)
{
long int k = n >> c;
if (k & 1)
printf("1");
else
printf("0");
}
}
int main()
{
//first letter is being repeated
char check[200];
FILE*fin= fopen("/Users/priya/Desktop/test.txt.rtf","r");
while((fscanf(fin,"%199s",check))==1)
{
for(int i=0;i<strlen(check);++i)
{
if(check[i]>=65&&check[i]<=90)
{
printf("01");
int n=check[i];
n-=63;
printinbits(n);
}
}
}
return 0;
}
My input->
HELLO
My output->
(0101001)(0101001)(0100110)(0101101)(0101101)(0110000)
(As you can see, the first letter H is being repeated)(Various letters are separated by brackets)
Here's a hex dump of a file hello.rtf containing the word HELLO in upper case. It was generated by TextEdit on a Mac.
0x0000: 7B 5C 72 74 66 31 5C 61 6E 73 69 5C 61 6E 73 69 {\rtf1\ansi\ansi
0x0010: 63 70 67 31 32 35 32 5C 63 6F 63 6F 61 72 74 66 cpg1252\cocoartf
0x0020: 31 34 30 34 5C 63 6F 63 6F 61 73 75 62 72 74 66 1404\cocoasubrtf
0x0030: 34 36 30 0A 7B 5C 66 6F 6E 74 74 62 6C 5C 66 30 460.{\fonttbl\f0
0x0040: 5C 66 73 77 69 73 73 5C 66 63 68 61 72 73 65 74 \fswiss\fcharset
0x0050: 30 20 48 65 6C 76 65 74 69 63 61 3B 7D 0A 7B 5C 0 Helvetica;}.{\
0x0060: 63 6F 6C 6F 72 74 62 6C 3B 5C 72 65 64 32 35 35 colortbl;\red255
0x0070: 5C 67 72 65 65 6E 32 35 35 5C 62 6C 75 65 32 35 \green255\blue25
0x0080: 35 3B 7D 0A 5C 6D 61 72 67 6C 31 34 34 30 5C 6D 5;}.\margl1440\m
0x0090: 61 72 67 72 31 34 34 30 5C 76 69 65 77 77 31 30 argr1440\vieww10
0x00A0: 38 30 30 5C 76 69 65 77 68 38 34 30 30 5C 76 69 800\viewh8400\vi
0x00B0: 65 77 6B 69 6E 64 30 0A 5C 70 61 72 64 5C 74 78 ewkind0.\pard\tx
0x00C0: 37 32 30 5C 74 78 31 34 34 30 5C 74 78 32 31 36 720\tx1440\tx216
0x00D0: 30 5C 74 78 32 38 38 30 5C 74 78 33 36 30 30 5C 0\tx2880\tx3600\
0x00E0: 74 78 34 33 32 30 5C 74 78 35 30 34 30 5C 74 78 tx4320\tx5040\tx
0x00F0: 35 37 36 30 5C 74 78 36 34 38 30 5C 74 78 37 32 5760\tx6480\tx72
0x0100: 30 30 5C 74 78 37 39 32 30 5C 74 78 38 36 34 30 00\tx7920\tx8640
0x0110: 5C 70 61 72 64 69 72 6E 61 74 75 72 61 6C 5C 70 \pardirnatural\p
0x0120: 61 72 74 69 67 68 74 65 6E 66 61 63 74 6F 72 30 artightenfactor0
0x0130: 0A 0A 5C 66 30 5C 66 73 32 34 20 5C 63 66 30 20 ..\f0\fs24 \cf0
0x0140: 48 45 4C 4C 4F 7D HELLO}
0x0146:
You may or may not be able to see the H of 'Helvetica' as the only other capital letter in the file — that would account for producing the output for HHELLO. It looks like you might be on a Mac too, so maybe you'd see the same result — or, at least, an equivalent one. (I used a homebrew hex dump program; you'd probably use xxd -g 1 test.txt.rtf, which would produce the hex with lower-case letters, and wouldn't include the final byte count line.)
You could, and should, print the data that your program reads in the loop, at least while debugging it, so that you can see what the program is processing. This is a very basic debugging technique.
In TextEdit, you can switch between rich text and plain text with the 'Make Plain Text' or 'Make Rich Text' option under the Format menu, or using ⇧⌘T (shift command T) to toggle between the two modes. Note how the file name changes as you do that.
Community Wiki since M Oehm pointed out the likely problem.

How to print the name of the files inside an archive file?

I'm new to C and system programming. I want to open an archive file and print out the name of the files inside the archive file (e.g., my archive file is weds.a; inside weds.a, I have thurs.txt and fri.txt". I want to create an output that shows
thurs.txt
fri.txt
EDITED: It should work like the ar -t command.
Can someone give me some tips on how to do it? I've been reading the man page and looking for examples online, but I'm getting no where. I believe I'm missing something. The code I have below only prints the link count. Can someone help? Thanks in advance for your help!!
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/utsname.h>
#include <ctype.h>
#include <string.h>
int main (int argc, char **argv)
{
int in_fd;
struct stat sb;
if (argc != 2) {
printf("Error", argv[0]);
exit(EXIT_FAILURE);
}
if (stat(argv[1], &sb) == -1) {
perror("stat");
exit(EXIT_FAILURE); //change from EXIT_SUCCESS to EXIT_FAILURE
}
//open the archive file (e.g., hw.a)
in_fd = open(argv[1], O_RDONLY);
if (in_fd == -1)
{
perror("Can't open input file\n");
exit(-1);
}
printf("Link Count: %ld\n", (long)sb.st_nlink);
return 0;
}
The easiest way is to use the ar program to list the names:
ar -tv weds.a
The - is optional; the v means you'll get size and time information.
If you want to get into reading the archive file itself, you'll have to be aware of the differences in the formats on different systems. The relevant header is (normally) <ar.h>. I have information for a number of systems, many of them obsolete, and there are a variety of different tricks used for handling long file names (and other even more basic file format issues) but you may have a more limited scope in mind. Any such work based on <ar.h> will be non-trivial; you're best off reusing what already exists (the ar program) if at all possible.
This is an archive from a Mac OS X 10.8.4 machine.
$ cat thurs.txt
0123456789:;<=>?#ABCDEFGHIJKLMNO
$ cat fri.txt
PQRSTUVWXYZ[\]^_`abcdefghijklmno
$ odx weds.a
0x0000: 21 3C 61 72 63 68 3E 0A 74 68 75 72 73 2E 74 78 !<arch>.thurs.tx
0x0010: 74 20 20 20 20 20 20 20 31 33 37 34 30 39 36 30 t 13740960
0x0020: 31 32 20 20 32 38 37 36 20 20 35 30 30 30 20 20 12 2876 5000
0x0030: 31 30 30 36 34 34 20 20 33 33 20 20 20 20 20 20 100644 33
0x0040: 20 20 60 0A 30 31 32 33 34 35 36 37 38 39 3A 3B `.0123456789:;
0x0050: 3C 3D 3E 3F 40 41 42 43 44 45 46 47 48 49 4A 4B <=>?#ABCDEFGHIJK
0x0060: 4C 4D 4E 4F 0A 0A 66 72 69 2E 74 78 74 20 20 20 LMNO..fri.txt
0x0070: 20 20 20 20 20 20 31 33 37 34 30 39 36 30 30 35 1374096005
0x0080: 20 20 32 38 37 36 20 20 35 30 30 30 20 20 31 30 2876 5000 10
0x0090: 30 36 34 34 20 20 33 33 20 20 20 20 20 20 20 20 0644 33
0x00A0: 60 0A 50 51 52 53 54 55 56 57 58 59 5A 5B 5C 5D `.PQRSTUVWXYZ[\]
0x00B0: 5E 5F 60 61 62 63 64 65 66 67 68 69 6A 6B 6C 6D ^_`abcdefghijklm
0x00C0: 6E 6F 0A 0A no..
0x00C4:
$
Fortunately for you, the same files produce essentially the same archive on Linux too. In the Linux header <ar.h> you find:
/* Archive files start with the ARMAG identifying string. Then follows a
`struct ar_hdr', and as many bytes of member file data as its `ar_size'
member indicates, for each member file. */
#define ARMAG "!<arch>\n" /* String that begins an archive file. */
#define SARMAG 8 /* Size of that string. */
#define ARFMAG "`\n" /* String in ar_fmag at end of each header. */
struct ar_hdr
{
char ar_name[16]; /* Member file name, sometimes / terminated. */
char ar_date[12]; /* File date, decimal seconds since Epoch. */
char ar_uid[6], ar_gid[6]; /* User and group IDs, in ASCII decimal. */
char ar_mode[8]; /* File mode, in ASCII octal. */
char ar_size[10]; /* File size, in ASCII decimal. */
char ar_fmag[2]; /* Always contains ARFMAG. */
};
The Mac OS X header has the same structure and ARMAG and ARFMAG values, but one extra macro:
#define AR_EFMT1 "#1/" /* extended format #1 */
You can see the ARMAG string at the start of the file. Each file is then preceded by a struct ar_hdr. Note that the example names here are blank terminated, not slash terminated.
After that, you find the data for the file. You can read the header in its entirety. Note that if any of the names reaches above 15 characters, or if a name contains spaces, then you get an extra entry at the start of the archive file that contains the file name strings, and you also get a modified name entry in the per-file header that identifies the relevant string in the string table.
Linux archive with long names etc
0x0000: 21 3C 61 72 63 68 3E 0A 2F 2F 20 20 20 20 20 20 !<arch>.//
0x0010: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
* (1)
0x0030: 20 20 20 20 20 20 20 20 34 36 20 20 20 20 20 20 46
0x0040: 20 20 60 0A 66 69 6C 74 65 72 2E 73 74 64 65 72 `.filter.stder
0x0050: 72 2E 73 68 2F 0A 6C 6F 6E 67 20 6E 61 6D 65 20 r.sh/.long name
0x0060: 77 69 74 68 20 73 70 61 63 65 73 2E 74 78 74 2F with spaces.txt/
0x0070: 0A 0A 74 68 75 72 73 2E 74 78 74 2F 20 20 20 20 ..thurs.txt/
0x0080: 20 20 31 33 37 34 30 39 36 32 31 31 20 20 31 39 1374096211 19
0x0090: 39 34 38 34 35 30 30 30 20 20 31 30 30 36 34 30 94845000 100640
0x00A0: 20 20 33 33 20 20 20 20 20 20 20 20 60 0A 30 31 33 `.01
0x00B0: 32 33 34 35 36 37 38 39 3A 3B 3C 3D 3E 3F 40 41 23456789:;<=>?#A
0x00C0: 42 43 44 45 46 47 48 49 4A 4B 4C 4D 4E 4F 0A 0A BCDEFGHIJKLMNO..
0x00D0: 66 72 69 2E 74 78 74 2F 20 20 20 20 20 20 20 20 fri.txt/
0x00E0: 31 33 37 34 30 39 36 31 39 37 20 20 31 39 39 34 1374096197 1994
0x00F0: 38 34 35 30 30 30 20 20 31 30 30 36 34 30 20 20 845000 100640
0x0100: 33 33 20 20 20 20 20 20 20 20 60 0A 50 51 52 53 33 `.PQRS
0x0110: 54 55 56 57 58 59 5A 5B 5C 5D 5E 5F 60 61 62 63 TUVWXYZ[\]^_`abc
0x0120: 64 65 66 67 68 69 6A 6B 6C 6D 6E 6F 0A 0A 2F 30 defghijklmno../0
0x0130: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 33 13
0x0140: 37 31 31 34 35 35 38 34 20 20 31 39 39 34 38 34 71145584 199484
0x0150: 35 30 30 30 20 20 31 30 30 36 34 30 20 20 32 33 5000 100640 23
0x0160: 30 20 20 20 20 20 20 20 60 0A 23 21 2F 62 69 6E 0 `.#!/bin
0x0170: 2F 62 61 73 68 0A 73 65 74 20 2D 78 0A 72 6D 20 /bash.set -x.rm
0x0180: 2D 66 20 6F 75 74 2E 5B 31 32 33 5D 0A 2E 2F 67 -f out.[123]../g
0x0190: 65 6E 6F 75 74 65 72 72 2E 73 68 20 31 3E 2F 64 enouterr.sh 1>/d
0x01A0: 65 76 2F 6E 75 6C 6C 0A 2E 2F 67 65 6E 6F 75 74 ev/null../genout
0x01B0: 65 72 72 2E 73 68 20 32 3E 2F 64 65 76 2F 6E 75 err.sh 2>/dev/nu
0x01C0: 6C 6C 0A 28 20 2E 2F 67 65 6E 6F 75 74 65 72 72 ll.( ./genouterr
0x01D0: 2E 73 68 20 32 3E 26 31 20 31 3E 26 33 20 7C 20 .sh 2>&1 1>&3 |
0x01E0: 67 72 65 70 20 27 5B 30 2D 39 5D 30 27 20 3E 26 grep '[0-9]0' >&
0x01F0: 32 29 20 33 3E 6F 75 74 2E 33 20 32 3E 6F 75 74 2) 3>out.3 2>out
0x0200: 2E 32 20 31 3E 6F 75 74 2E 31 0A 6C 73 20 2D 6C .2 1>out.1.ls -l
0x0210: 20 6F 75 74 2E 5B 31 32 33 5D 0A 28 20 2E 2F 67 out.[123].( ./g
0x0220: 65 6E 6F 75 74 65 72 72 2E 73 68 20 32 3E 26 31 enouterr.sh 2>&1
0x0230: 20 31 3E 26 33 20 7C 20 67 72 65 70 20 27 5B 30 1>&3 | grep '[0
0x0240: 2D 39 5D 30 27 20 3E 26 32 29 20 33 3E 26 31 0A -9]0' >&2) 3>&1.
0x0250: 2F 31 38 20 20 20 20 20 20 20 20 20 20 20 20 20 /18
0x0260: 31 33 37 34 30 39 36 35 37 37 20 20 31 39 39 34 1374096577 1994
0x0270: 38 34 35 30 30 30 20 20 31 30 30 36 34 30 20 20 845000 100640
0x0280: 33 33 20 20 20 20 20 20 20 20 60 0A 30 31 32 33 33 `.0123
0x0290: 34 35 36 37 38 39 3A 3B 3C 3D 3E 3F 40 41 42 43 456789:;<=>?#ABC
0x02A0: 44 45 46 47 48 49 4A 4B 4C 4D 4E 4F 0A 0A DEFGHIJKLMNO..
0x02AE:
Mac OS X archive with long names etc
0x0000: 21 3C 61 72 63 68 3E 0A 74 68 75 72 73 2E 74 78 !<arch>.thurs.tx
0x0010: 74 20 20 20 20 20 20 20 31 33 37 34 30 39 36 30 t 13740960
0x0020: 31 32 20 20 32 38 37 36 20 20 35 30 30 30 20 20 12 2876 5000
0x0030: 31 30 30 36 34 34 20 20 33 33 20 20 20 20 20 20 100644 33
0x0040: 20 20 60 0A 30 31 32 33 34 35 36 37 38 39 3A 3B `.0123456789:;
0x0050: 3C 3D 3E 3F 40 41 42 43 44 45 46 47 48 49 4A 4B <=>?#ABCDEFGHIJK
0x0060: 4C 4D 4E 4F 0A 0A 66 72 69 2E 74 78 74 20 20 20 LMNO..fri.txt
0x0070: 20 20 20 20 20 20 31 33 37 34 30 39 36 30 30 35 1374096005
0x0080: 20 20 32 38 37 36 20 20 35 30 30 30 20 20 31 30 2876 5000 10
0x0090: 30 36 34 34 20 20 33 33 20 20 20 20 20 20 20 20 0644 33
0x00A0: 60 0A 50 51 52 53 54 55 56 57 58 59 5A 5B 5C 5D `.PQRSTUVWXYZ[\]
0x00B0: 5E 5F 60 61 62 63 64 65 66 67 68 69 6A 6B 6C 6D ^_`abcdefghijklm
0x00C0: 6E 6F 0A 0A 66 69 6C 74 65 72 2E 73 74 64 65 72 no..filter.stder
0x00D0: 72 2E 73 68 31 33 37 34 30 39 37 37 39 34 20 20 r.sh1374097794
0x00E0: 32 38 37 36 20 20 35 30 30 30 20 20 31 30 30 36 2876 5000 1006
0x00F0: 34 34 20 20 32 33 30 20 20 20 20 20 20 20 60 0A 44 230 `.
0x0100: 23 21 2F 62 69 6E 2F 62 61 73 68 0A 73 65 74 20 #!/bin/bash.set
0x0110: 2D 78 0A 72 6D 20 2D 66 20 6F 75 74 2E 5B 31 32 -x.rm -f out.[12
0x0120: 33 5D 0A 2E 2F 67 65 6E 6F 75 74 65 72 72 2E 73 3]../genouterr.s
0x0130: 68 20 31 3E 2F 64 65 76 2F 6E 75 6C 6C 0A 2E 2F h 1>/dev/null../
0x0140: 67 65 6E 6F 75 74 65 72 72 2E 73 68 20 32 3E 2F genouterr.sh 2>/
0x0150: 64 65 76 2F 6E 75 6C 6C 0A 28 20 2E 2F 67 65 6E dev/null.( ./gen
0x0160: 6F 75 74 65 72 72 2E 73 68 20 32 3E 26 31 20 31 outerr.sh 2>&1 1
0x0170: 3E 26 33 20 7C 20 67 72 65 70 20 27 5B 30 2D 39 >&3 | grep '[0-9
0x0180: 5D 30 27 20 3E 26 32 29 20 33 3E 6F 75 74 2E 33 ]0' >&2) 3>out.3
0x0190: 20 32 3E 6F 75 74 2E 32 20 31 3E 6F 75 74 2E 31 2>out.2 1>out.1
0x01A0: 0A 6C 73 20 2D 6C 20 6F 75 74 2E 5B 31 32 33 5D .ls -l out.[123]
0x01B0: 0A 28 20 2E 2F 67 65 6E 6F 75 74 65 72 72 2E 73 .( ./genouterr.s
0x01C0: 68 20 32 3E 26 31 20 31 3E 26 33 20 7C 20 67 72 h 2>&1 1>&3 | gr
0x01D0: 65 70 20 27 5B 30 2D 39 5D 30 27 20 3E 26 32 29 ep '[0-9]0' >&2)
0x01E0: 20 33 3E 26 31 0A 23 31 2F 32 38 20 20 20 20 20 3>&1.#1/28
0x01F0: 20 20 20 20 20 20 31 33 37 34 30 39 37 38 32 32 1374097822
0x0200: 20 20 32 38 37 36 20 20 35 30 30 30 20 20 31 30 2876 5000 10
0x0210: 30 36 34 34 20 20 36 31 20 20 20 20 20 20 20 20 0644 61
0x0220: 60 0A 6C 6F 6E 67 20 6E 61 6D 65 20 77 69 74 68 `.long name with
0x0230: 20 73 70 61 63 65 73 2E 74 78 74 00 00 00 30 31 spaces.txt...01
0x0240: 32 33 34 35 36 37 38 39 3A 3B 3C 3D 3E 3F 40 41 23456789:;<=>?#A
0x0250: 42 43 44 45 46 47 48 49 4A 4B 4C 4D 4E 4F 0A 0A BCDEFGHIJKLMNO..
0x0260:
Differences
The Linux archive has a list of strings at the top of the file that have to be remembered. The Mac OS X archive has the special entry #1/28 which identifies the header as being followed by a 28-byte entry containing the file name (null padded to a multiple of 4 bytes; the length given includes the null padding). The Mac archive has no space after the name when it is exactly 16 characters long; the Linux archive puts the 16-character name into the string table.

I recovered a file from chrome://cache, how do I read it?

This is the data it displays:
http://krill.larvit.se/resihop_dev/style.css
HTTP/1.1 200 OK
Date: Tue, 17 Jul 2012 20:36:17 GMT
Server: Apache/2.2.16 (Debian)
Last-Modified: Mon, 16 Jul 2012 23:33:42 GMT
ETag: "3003d8-92f-4c4fadc326180"
Accept-Ranges: bytes
Vary: Accept-Encoding
Content-Encoding: gzip
Content-Length: 871
Content-Type: text/css
00000000: 4a 01 00 00 03 08 00 00 68 fc 1b c8 a2 23 2e 00 J.......h....#..
00000010: c4 c4 1c c8 a2 23 2e 00 0f 01 00 00 48 54 54 50 .....#......HTTP
00000020: 2f 31 2e 31 20 32 30 30 20 4f 4b 00 44 61 74 65 /1.1 200 OK.Date
00000030: 3a 20 54 75 65 2c 20 31 37 20 4a 75 6c 20 32 30 : Tue, 17 Jul 20
00000040: 31 32 20 32 30 3a 33 36 3a 31 37 20 47 4d 54 00 12 20:36:17 GMT.
00000050: 53 65 72 76 65 72 3a 20 41 70 61 63 68 65 2f 32 Server: Apache/2
00000060: 2e 32 2e 31 36 20 28 44 65 62 69 61 6e 29 00 4c .2.16 (Debian).L
00000070: 61 73 74 2d 4d 6f 64 69 66 69 65 64 3a 20 4d 6f ast-Modified: Mo
00000080: 6e 2c 20 31 36 20 4a 75 6c 20 32 30 31 32 20 32 n, 16 Jul 2012 2
00000090: 33 3a 33 33 3a 34 32 20 47 4d 54 00 45 54 61 67 3:33:42 GMT.ETag
000000a0: 3a 20 22 33 30 30 33 64 38 2d 39 32 66 2d 34 63 : "3003d8-92f-4c
000000b0: 34 66 61 64 63 33 32 36 31 38 30 22 00 41 63 63 4fadc326180".Acc
000000c0: 65 70 74 2d 52 61 6e 67 65 73 3a 20 62 79 74 65 ept-Ranges: byte
000000d0: 73 00 56 61 72 79 3a 20 41 63 63 65 70 74 2d 45 s.Vary: Accept-E
000000e0: 6e 63 6f 64 69 6e 67 00 43 6f 6e 74 65 6e 74 2d ncoding.Content-
000000f0: 45 6e 63 6f 64 69 6e 67 3a 20 67 7a 69 70 00 43 Encoding: gzip.C
00000100: 6f 6e 74 65 6e 74 2d 4c 65 6e 67 74 68 3a 20 38 ontent-Length: 8
00000110: 37 31 00 43 6f 6e 74 65 6e 74 2d 54 79 70 65 3a 71.Content-Type:
00000120: 20 74 65 78 74 2f 63 73 73 00 00 00 1c 75 7f de text/css....u..
00000130: 67 1f 5e 3e ea 80 b1 88 00 f0 37 0c 0b 00 00 00 g.^>......7.....
00000140: 33 31 2e 32 34 2e 36 37 2e 36 37 00 50 00 31.24.67.67.P.
00000000: 1f 8b 08 00 00 00 00 00 00 03 8d 55 db 4e e3 30 ...........U.N.0
00000010: 10 7d 6e be c2 6a b5 62 41 04 28 2d 2c 4d df 10 .}n..j.bA.(-,M..
00000020: ec 6f ac a6 89 93 78 71 ec c8 76 81 6e c5 77 ed .o....xq..v.n.w.
00000030: fb 7e d9 8e 6f 49 1a 0a 42 55 a5 d8 99 cb 99 33 .~..oI..BU.....3
00000040: 67 26 97 67 1b d0 2c 27 1c 76 72 6b ce 2e 93 59 g&.g..,'.vrk...Y
00000050: 2e 85 01 26 a8 da 27 93 17 56 98 3a 23 f3 ab ab ...&..'..V.:#...
00000060: 6f eb 64 d2 4a cd 0c 93 22 23 b0 d1 92 6f 0d c5 o.d.J..."#...o..
00000070: cb 9a b2 aa 36 9d 8d 7c a6 aa e4 f2 25 23 35 2b ....6..|....%#5+
00000080: 0a 2a d6 c9 5b 92 cc 38 2d 0d f9 5a 3c 6b 9a 91 .*..[..8-..Z<k..
00000090: 2b 7c ba 3c c3 10 4c 54 c4 d4 94 6c 2b be 23 da +|.<..LT...l+.#.
000000a0: 6c cb 92 80 71 37 1b 69 8c 6c 88 2c dd a9 81 16 l...q7.i.l.,....
000000b0: c1 f7 68 ae 5d 06 6f 93 91 d4 1d 3b 24 2d 42 69 ..h.].o....;$-Bi
000000c0: 40 55 4c b8 4c f6 1e fd 7f e5 20 9e 41 5b 9c 2d #UL.L..... .A[.-
000000d0: 14 36 b3 c7 31 aa f0 a0 06 e7 ab ec fb de 2b 55 .6..1.........+U
000000e0: d1 be 7d 45 fb 82 e9 16 d9 cd c8 86 cb fc c9 82 ..}E............
000000f0: 82 fc a9 52 72 2b 8a 34 97 5c aa 8c bc d4 ec 73 ...Rr+.4.\.....s
00000100: 2e 75 ae 24 e7 7d f2 e5 62 e1 62 1f 65 30 a4 f7 .u.$.}..b.b.e0..
00000110: d9 8d 6c 63 8d 97 67 0f 54 b3 4a 20 4f 0d 15 db ..lc..g.T.J O...
00000120: fd 07 ee 91 33 ef ef 83 4d 42 2d 1d 2f f6 ec c8 ....3...MB-./...
00000130: 19 d0 38 28 ac 2f e9 10 30 a2 b0 a9 09 67 98 9d ..8(./..0....g..
00000140: 33 6d 52 6d 76 9c a6 66 d7 d2 8c 08 29 e8 80 30 3mRmv..f....)..0
00000150: 26 38 b3 17 6f d1 27 db d0 52 2a 6a 1b 64 45 4a &8..o.'..R*j.dEJ
00000160: 05 56 39 fd f7 97 4c 87 81 b3 92 29 8c 9b d7 8c .V9...L....)....
00000170: 17 47 1d bc f5 46 16 3b 7b 5f e2 7d 5a 42 c3 38 .G...F.;{_.}ZB.8
00000180: 76 68 ca b7 39 2b 80 54 0a 44 41 a7 e7 06 6a d9 vh..9+.T.DA...j.
00000190: c0 39 b6 a1 00 01 e7 a0 18 f0 73 0d 42 a7 9a 2a .9........s.B..*
000001a0: 56 ae 83 bb 66 7f 10 fd dc b7 c4 62 4e bb 46 fe V...f......bN.F.
000001b0: 70 77 87 5a 03 07 c7 36 7e 32 5b dc df ac 56 77 pw.Z...6~2[...Vw
000001c0: b6 4f f4 d5 a4 05 cd a5 02 df 11 4f 06 9a 27 90 .O.........O..'.
000001d0: d5 56 09 fb 23 46 48 35 55 91 a5 f7 42 f4 b3 34 .V..#FH5U...B..4
000001e0: bf 09 d4 fb f7 a4 9e ef 0f 80 33 51 63 35 66 1d ..........3Qc5f.
000001f0: 51 d9 f2 77 4e 08 0a a3 a7 51 0f 73 ec 38 8a 84 Q..wN....Q.s.8..
00000200: 15 64 f6 b8 b2 bf 58 fe 4b 28 56 48 d5 00 1f aa .d....X.K(VH....
00000210: e4 ce 8a 84 74 04 a4 03 31 a2 1a b9 04 8c 8f 6a ....t...1......j
00000220: bc f0 4f 07 92 c0 da 14 ff 7e 02 bf e1 35 f5 af ..O......~...5..
00000230: 2f 2a 56 9e 9c 3a e7 e0 e0 65 14 8b 8d 38 63 b5 /*V..:...e...8c.
00000240: bd 11 81 03 ad 2b ca 91 be 67 1a a7 23 5d 06 0f .....+...g..#]..
00000250: c4 84 56 26 b5 28 2c 2c 7b d0 e3 6d 70 d8 cb 24 ..V&.(,,{..mp..$
00000260: 58 8d 15 dd 89 f9 0b 24 1e 45 d6 4f 1a 6d 42 ae X......$.E.O.mB.
00000270: 90 89 35 55 b7 be 46 ab 06 97 05 e0 c9 b6 dd 7b ..5U..F........{
00000280: 58 c1 0c f7 79 f0 f2 ba b8 f5 4e 61 42 17 37 73 X...y.....NaB.7s
00000290: 7b 7c ef e6 d9 1b 91 ec b5 9e 73 0a a8 17 07 e2 {|........s.....
000002a0: 48 c2 c1 9e 0d ad 8f ad 11 d0 d0 fd 48 3e 1b c9 H...........H>..
000002b0: 0b ff b6 a1 5a 43 45 7b e7 77 ad 35 ac a1 a4 9f ....ZCE{.w.5....
000002c0: 23 32 bb 7d b8 5b de 2f 3f 9d a3 e0 f4 d5 61 72 #2.}.[./?.....ar
000002d0: e6 83 0c ab 55 27 f8 30 ef f3 c1 6c 77 10 e3 aa ....U'.0...lw...
000002e0: cd 65 83 0b c9 68 ab a3 f8 fc 91 94 6e dc 98 5c .e...h......n..\
000002f0: 8d 76 68 fc 38 cc 1e 1f 1e 7f fe 5c 1e 4b 6e 61 .vh.8......\.Kna
00000300: c6 e0 5e 81 0d 42 89 cb 67 e1 74 fd b1 02 e3 8a ..^..B..g.t.....
00000310: ee 30 a1 bd c3 12 fe eb c9 58 ed 7d b2 d1 17 b4 .0.......X.}....
00000320: ff c8 f5 4b bb 37 b6 8d 7a 67 f0 51 f3 07 f5 5c ...K.7..zg.Q...\
00000330: d0 d7 16 17 f1 a8 ac f1 97 c6 a2 cd d1 03 f5 66 ...............f
00000340: 57 46 f0 bf 68 45 75 8a cd 4f 15 6d 29 98 f5 78 WF..hEu..O.m)..x
00000350: 29 5e 5f 7b 02 8f e4 73 78 8f 6e b6 b7 e4 3f 49 )^_{...sx.n...?I
00000360: 7b 1f b2 2f 09 00 00 {../...

Resources