Home > other >  output hex data from xattr to create icns file
output hex data from xattr to create icns file

Time:08-18

I'm trying to extract the icon from an xattr of a file. Using xattr to get the "com.apple.ResourceFork" in hex format i use:

xattr -px com.apple.ResourceFork file

and save the output to a variable

var="$(xattr -px com.apple.ResourceFork file)"

then using variable expansion i remove the first bytes until i reach 69 (icns magic number is 69 63 6E 73)

var=${var#*69 63 6E 73}

next i output the variable and append "69 63 6E 73" to the beginning to restore the magic number.

echo "69 63 6E 73$var" > output.txt

if i take the hex data from the output.txt and insert it into a hexeditor to save it then it works and the .icns is created.

i want to do this programmatically in bash/zsh without having to save it manually.

tried using

touch icon.icns

to create an empty file then

 echo "69 63 6E 73$var" > icon.icns

just transforms the output file into an ASCII file.

i'm not stuck to my method, any working method is acceptable to me.

CodePudding user response:

I have access to my Mac again... strangely (to me) it seems xxd works differently when given parameters all together rather than individually, so rather than what I suggested in the comments:

xxd -rp ...

you would need:

xxd -r -p ...

As I don't have an icon.icns file to hand, I'll take a JPEG (which is just as binary), convert it to readable hex and reconstruct it from the hex with xxd.

Here's a JPEG, converted to hex:

xxd x.jpg | more

00000000: ffd8 ffe0 0010 4a46 4946 0001 0100 0001  ......JFIF......
00000010: 0001 0000 ffdb 0043 0003 0202 0202 0203  .......C........
...
...

Then take the hex and give reconstruct the first few bytes of the JPEG:

printf "ff d8 ff e0" | xxd -r -p > recreated.jpg

And look at the recreated file:

xxd recreated.jpg 

00000000: ffd8 ffe0

So the process for a while file would be:

hex=$(xxd -p x.jpg)
printf "$hex" | xxd -r -p > recreated.jpg  
  • Related