searching files for hex or oct values


 
Thread Tools Search this Thread
Top Forums Programming searching files for hex or oct values
# 1  
Old 07-29-2010
searching files for hex or oct values

I have a set of files without extensions. How can I programatically tell if a file is in gzip format? The gzip file format spec

RFC 1952 GZIP File Format Specification version 4.3

states that gzip files have certain hex/oct values at the beginning of the file.

1st byte = 0x1f in hex, \037 in octal
2nd byte = 0x8b in hex, \213 in octal

How can I search for these values to determine if a file is truly gzip format?

Thanks.
# 2  
Old 07-29-2010
Code:
od -c infile.gz| head
0000000 037 213  \b  \b   Ã 201   Q   L  \0 003   i   n   f   i   l   e
0000020  \0  \v   N   -   Ì   Ë   ×   q   Í   -   ð   t 001 221   ~ 211

# 3  
Old 07-29-2010
Here is one way of doing it using C
Code:
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

#define GZIP_MAGIC "\037\213"     /* Magic header for gzip files, 1F 8B */

int
main(int argc, char *argv[])
{
    int fd;
    char magic[2];
    ssize_t ret;

    if (argc < 2) {
        fprintf (stderr, "ERROR: A filename must be specified.\n");
        exit(1);
    }

    fd = open(argv[1], O_RDONLY);
    if (fd < 0) {
        fprintf (stderr, "ERROR: cannot open %s\n", argv[1]);
        exit(1);
    }

    if ((ret = read(fd, &magic, 2)) == -1) {
        fprintf (stderr, "ERROR: cannot read %s\n", argv[1]);
        exit(1);
    }

    if (memcmp(magic, GZIP_MAGIC, 2) == 0) {
        fprintf (stdout, "%s is a gzipped file\n", argv[1]);
    } else {
        fprintf (stdout, "%s is not a gzipped file\n", argv[1]);
    }

    close(fd);
    exit(0);
}

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Replace hex values using sed command

File lalo.txt contains: Á I need to replace Á by A using sed command. od -x lalo.txt 0000000 c10a 0000002 sed -e 's/\xc1\x0a/A/g' lalo.txt > lalo2.txt Also tried: sed -e 's/\xc3\x81/A/g' lalo.txt > lalo2.txt Output file lalo2.txt still has Á Unix version: SunOS 5.11 ... (9 Replies)
Discussion started by: mrreds
9 Replies

2. Shell Programming and Scripting

How to replace with "sed" some hex values by other hex values?

Assume I have a file \usr\home\\somedir\myfile123.txt and I want to replace all occurencies of the two (concatenated) hex values x'AD' x'A0' bytwo other (concatenated) hex values x'20' x'6E' How can I achieve this with the gnu sed tool? Additional question: Is there a way to let sed show... (1 Reply)
Discussion started by: pstein
1 Replies

3. Shell Programming and Scripting

Swapping a string of numbers between higher and lower order values(HEX)

I have this below string in a variable cutString=21222222222222222122222222222222 this string is nothing but hex values depicted as below 21:22:22:22:22:22:22:22:21:22:22:22:22:22:22:22 so what i want to achieve is swap the lower order with higher order values in the... (3 Replies)
Discussion started by: vivek d r
3 Replies

4. Shell Programming and Scripting

Searching for values in a file

Hi guys. I'm trying to do a search on the fruit & brand inside Fruit.txt, and printing the result out in the following format: , , $, I am able to do this via the following code: awk -F: -vOFS=", " -vt="$Fruit:$Brand" '$0~t{$3="$"$3;print}' Fruit.txt However, I want to be able to... (5 Replies)
Discussion started by: todaealas
5 Replies

5. Shell Programming and Scripting

Searching columns and subtracting values in awk

Hi everyone, I had a similar question a couple days ago but my problem has gotten significantly (to me anyway) more complex. I have two files: File 1: 0808 166 166 62 9 0 1000fights 1 1 2 1 0 100places2visit 2 2 2 2 0 10veronica91 167 167 3 1 0 11thgorgeous 346 346 3806 1461 122... (2 Replies)
Discussion started by: collards
2 Replies

6. Shell Programming and Scripting

Help search and replace hex values only in specific files

perl -pi -e 's/\x00/\x0d\x0a/g' `grep -l $'GS' filelist` This isn't working :confused:, it's not pulling the files that contain the regex. Please help me rewrite this :wall:. Ideally for this to work on 9K of 20K files in the directory, I've tried this but I don't know enough about awk... (7 Replies)
Discussion started by: verge
7 Replies

7. Shell Programming and Scripting

Convert hex values to displayable characters

Hi, I am a bit stuck with displaying characters. I am having values like below in the proper displayable characters. which I would want to print the actual value on the right hand side. I dont want to create an array because I would have to create 255 different values. isnt there another way of... (17 Replies)
Discussion started by: ahmedwaseem2000
17 Replies

8. Shell Programming and Scripting

Searching data files for another file of values

I've used awk for some simple scripting, but having trouble figuring out how to search a couple of data files that have Name/Address/Zip Codes from another file that has list of only Zip Codes, and write out the lines that matched. Zip code field in the data file is 27 I was thinking... (5 Replies)
Discussion started by: matkins99
5 Replies

9. Programming

After converting the hexstr to Hex and storing the Hex in a char*

Hi All, My main intension of is to convert the Hexstring stored in a char* into hex and then prefixing it with "0x" and suffix it with ',' This has to be done for all the hexstring char* is NULL. Store the result prefixed with "0x" and suffixed with ',' in another char* and pass it to... (1 Reply)
Discussion started by: rvan
1 Replies

10. UNIX for Advanced & Expert Users

Modifying binary file by editing Hex values ?

Hi , i'm using special binary file (lotus notes) and modifying an hexadecimal address range with windows hex editor and it works fine ! The file is an AIX one and i'm forced to transfert (ftp) it before modifying it and re-transfert ! NOW i would do this directly under AIX ! I can... (4 Replies)
Discussion started by: Nicol
4 Replies
Login or Register to Ask a Question