kaitai-io / kaitai_struct

Kaitai Struct: declarative language to generate binary data parsers in C++ / C# / Go / Java / JavaScript / Lua / Nim / Perl / PHP / Python / Ruby
https://kaitai.io
3.99k stars 194 forks source link

Windows DIB format in bytearray to image? #398

Closed paulopires16 closed 6 years ago

paulopires16 commented 6 years ago

I have OLE Object in bytearray that I intent to write to disc: https://ufile.io/5mnfn How to convert this info (Windows DIB) to image with kaitaistruct? Thanks

KOLANICH commented 6 years ago

dib is bmp if I remember right.

paulopires16 commented 6 years ago

Yes, I now. I have that image, see the link. I write it but can't open: https://ufile.io/5mnfn

That image was copied with keyboard to ole objects data source. I read that ole object stored in byte array into a variable. Then I just try to write to disc...

Something is missing...

my variable[:100] after read images, see image link please: https://imgur.com/q2Z4nEo eatch row is one image… this is a dib image, right?

Can I convert them to BMP with kaitaistruct ?

ams-tschoening commented 6 years ago

Can I convert them to BMP with kaitaistruct ?

I'm not sure I understand your problem, but KS is not a converter of different file formats in the first place. It's a generator for a parser which allows you to access individual parts of your data by some meaning in the end. So yes, you can write such a parser using KS for your file format to access individual data fields and use that in some kind of format conversion you build on your own. But no, KS will not provide you with some DIB-input method and create some BMP-output for you automatically.

But from what I've read and how I understand @KOLANICH, implementing such a conversion shouldn't be necessary, because a DIB file seems to be compatible with a BMP file.

KOLANICH commented 6 years ago

@paulopires16, Do you know what abstract syntax tree is? It is an object representation of source code. For example

from b import a
b=a(1+2)

will give the following AST

ImportFrom(module='b', names=[alias(name='a', asname=None)], level=0), Assign(targets=[Name(id='b', ctx=Store())], value=Call(func=Name(id='a', ctx=Load()), args=[BinOp(left=Num(n=1), op=Add(), right=Num(n=2))], keywords=[], starargs=None, kwargs=None))

A parser is a program building an AST from source. A parser is generated from a grammar written in a special programming language, usually EBNF.

Now imagine that binary string is the source. Then all the KSYs used is the grammar, and the code compiled from them is parser. You apply this parser to binary and get an AST of that binary. If you wanna get pixels values you will have to parse a file with the parser and some part of AST should have something which can be converted into pixels values.

KOLANICH commented 6 years ago

Here is the example for NT MDT format https://github.com/KOLANICH/NTMDTRead/blob/master/examples/show.py

paulopires16 commented 6 years ago

Thanks I already found a solution yesterday. My problem was the header: http://cybersphinx.com/2017/03/01/simple-bitmap-file-fuzzer/

Offset Size Hex Value Value Description

BMP Header 0h | 2 | 42 4D | “BM” | ID field (42h, 4Dh) 2h | 4 | 46 00 00 00 | 70 bytes (54+16) | Size of the BMP file 6h | 2 | 00 00 | Unused | Application specific 8h | 2 | 00 00 | Unused | Application specific Ah | 4 | 36 00 00 00 | 54 bytes (14+40) | Offset where the pixel array (bitmap data) can be found

data = row[i] data[:57] = str('\x42\x4D\xDA\x82\x15\x00\x00\x00\x00\x00\x36\x00\x00\x00') # bmp header data = data[:len(data)-4]