imagemagick gm node buffer file conversion - node.js

I'm currently using ImageMagick and gm to process images from a buffer. My problem is that I cannot control what file types are put in the buffer but wish to have everything turned to jpg.
Not sure how to do that using the buffer since I'm not giving it an output file path with extension.
gm(buf).command('convert').in('-auto-orient','-resize','500x','-quality','92','-strip','-quality','100','jpg').toBuffer((err, buffer) => err ? reject(err) : resolve(buffer));

I don't know anything about the node.js bindings of ImageMagick or GraphicsMagick, but I do know that if you do this in Terminal, you will force a JPEG output, so maybe you can adapt that...
# Make a 1x1 black image and write to stdout as JPEG. Dump result with 'xxd'
gm convert xc:black jpg:- | xxd
00000000: ffd8 ffe0 0010 4a46 4946 0001 0101 0048 ......JFIF.....H
00000010: 0048 0000 ffdb 0043 0008 0606 0706 0508 .H.....C........
00000020: 0707 0709 0908 0a0c 140d 0c0b 0b0c 1912 ................
00000030: 130f 141d 1a1f 1e1d 1a1c 1c20 242e 2720 ........... $.'
00000040: 222c 231c 1c28 3729 2c30 3134 3434 1f27 ",#..(7),01444.'
00000050: 393d 3832 3c2e 3334 32ff c000 0b08 0001 9=82<.342.......
00000060: 0001 0101 1100 ffc4 0014 0001 0000 0000 ................
00000070: 0000 0000 0000 0000 0000 0008 ffc4 0014 ................
00000080: 1001 0000 0000 0000 0000 0000 0000 0000 ................
00000090: 0000 ffda 0008 0101 0000 3f00 3fbf ffd9 ..........?.?...
It is just the same with ImageMagick and, say, PNG output:
magick xc:black png:-

Related

Unix -Delete even bytes from a file

I want to find and delete all even bytes from a file given in command line. Is there any command for this situation?
I think this does what you want. It dumps the file as continuous plain hex, then reads two bytes, saving them for later and then two more bytes. Then it outputs the bytes it saved and pipes the whole lot back into xxd to "reconstruct":
xxd -p INPUT | sed -E 's/(..)../\1/g' | xxd -r -p > OUTPUT
So, if I dump a PNG image like this:
xxd image.png
00000000: 8950 4e47 0d0a 1a0a 0000 000d 4948 4452 .PNG........IHDR
00000010: 0000 0064 0000 0064 0203 0000 000d 8c7d ...d...d.......}
00000020: c700 0000 0467 414d 4100 00b1 8f0b fc61 .....gAMA......a
00000030: 0500 0000 2063 4852 4d00 007a 2600 0080 .... cHRM..z&...
00000040: 8400 00fa 0000 0080 e800 0075 3000 00ea ...........u0...
00000050: 6000 003a 9800 0017 709c ba51 3c00 0000 `..:....p..Q<...
00000060: 0950 4c54 4500 0000 ff00 00ff ffff 6719 .PLTE.........g.
00000070: 641e 0000 0001 624b 4744 0266 0b7c 6400 d.....bKGD.f.|d.
00000080: 0000 0774 494d 4507 e404 1c0a 0820 506e ...tIME...... Pn
00000090: 92c5 0000 0025 4944 4154 48c7 6360 1805 .....%IDATH.c`..
000000a0: a300 0c58 4361 2064 5466 5466 5466 5466 ...XCa dTfTfTfTf
000000b0: 5466 5406 8fcc 2818 3100 00e8 0fcb 7f57 TfT...(.1......W
000000c0: afef dd00 0000 2574 4558 7464 6174 653a ......%tEXtdate:
000000d0: 6372 6561 7465 0032 3032 302d 3034 2d32 create.2020-04-2
000000e0: 3854 3130 3a30 383a 3332 2b30 303a 3030 8T10:08:32+00:00
000000f0: d513 d3d0 0000 0025 7445 5874 6461 7465 .......%tEXtdate
00000100: 3a6d 6f64 6966 7900 3230 3230 2d30 342d :modify.2020-04-
00000110: 3238 5431 303a 3038 3a33 322b 3030 3a30 28T10:08:32+00:0
00000120: 30a4 4e6b 6c00 0000 0049 454e 44ae 4260 0.Nkl....IEND.B`
00000130: 82
and then put that through my filter and display it again:
xxd -p image.png | sed -E 's/(..)../\1/g' | xxd -r -pp | xxd
00000000: 894e 0d1a 0000 4944 0000 0000 0200 008c .N....ID........
00000010: c700 0441 4100 8ffc 0500 2048 4d00 2600 ...AA..... HM.&.
00000020: 8400 0000 e800 3000 6000 9800 70ba 3c00 ......0.`...p.<.
00000030: 094c 4500 ff00 ff67 6400 0062 4702 0b64 .LE....gd..bG..d
00000040: 0007 4945 e41c 0850 9200 0049 4148 6318 ..IE...P...IAHc.
00000050: a30c 4320 5454 5454 5454 8f28 3100 0f7f ..C TTTTTT.(1...
00000060: afdd 0025 4574 6165 6365 7400 3030 302d ...%Etaecet.000-
00000070: 3831 3a38 332b 3030 d5d3 0000 7458 6474 81:83+00....tXdt
00000080: 3a6f 6979 3232 2d34 3254 3030 3a32 303a :oiy22-42T00:20:
00000090: 304e 6c00 0045 4442 82 0Nl..EDB.
Or in Perl
#!/usr/bin/perl
use strict;
use warnings;
open my $ifh, '<', $ARGV[0] or die "Need an input filename";
binmode $ifh;
open my $ofh, '>', $ARGV[1] or die "Need an output filename";
binmode $ofh;
my $x;
while (read $ifh,$x,2) {
my $num = pack 'C',ord($x); # or ord(substr $x,1) for odd bytes
print $ofh $num;
}
I am not sure if there such shell/bash built in command. seems like you need to write you own bash script for doing so.

Creating new file with Square root of specific columns using awk

I have to find the square root of values in some specific columns in the file test.text and write the resulting column into another file. The file test.txt is shown below.
0004 0015 0018 0007 0015 0009 0003 0018 0012 0007 0013 0010 0018 0015 0002 0020 0007 0005
0010 0018 0015 0018 0007 0011 0010 0004 0013 0017 0016 0010 0019 0003 0002 0016 0005 0009
0002 0011 0015 0014 0008 0017 0002 0001 0015 0004 0008 0008 0002 0011 0015 0015 0009 0011
0013 0020 0005 0007 0007 0004 0019 0011 0001 0008 0013 0015 0015 0012 0007 0019 0009 0016
0003 0003 0020 0015 0007 0017 0005 0010 0012 0012 0009 0004 0016 0006 0014 0019 0001 0001
0014 0006 0016 0003 0008 0017 0002 0001 0011 0015 0014 0008 0016 0006 0014 0019 0012 0007
I have to find the square root of values from 2nd, 5th, 8th, 11th.. etc columns and write them into another file.
After searching, I have obtained the below code to print the square root of only one column.
awk '{r=$2; print "root=" sqrt(r)}' test.txt
This code prints only square root of column 2 values.
So I tried the below code using for loop.
awk 'BEGIN{FS=" "}{for(r=2;r<=NF;r+=3) printf sqrt($r)}' test.txt >newtest.txt
But still I am not getting the desired result as separate columns. I need newtest.txt file containing columns with square root values of 2nd , 5th, 8th ..etc columns of test.txt.
Any help regarding this will be appreciated.
Edit
The sample output I expect is newtest.txt which is shown below:
3.87298334621 3.87298334621 4.24264068712 3.60555127546 3.87298334621 2.64575131106
4.24264068712 2.64575131106 2.00000000000 4.00000000000 1.73205080757 2.23606797750
3.31662479036 2.82842712475 1.00000000000 2.82842712475 3.31662479036 3.00000000000
4.47213595500 2.64575131106 3.31662479036 3.60555127546 3.46410161514 3.00000000000
1.73205080757 2.64575131106 3.16227766017 3.00000000000 2.44948974278 1.00000000000
2.44948974278 2.82842712475 1.00000000000 3.74165738677 2.44948974278 3.46410161514
awk '{for(r=2;r<=NF;r+=3) printf("%.11f ",sqrt($r)); print ""}' test.txt >newtest.txt
Output to newtest.txt:
3.87298334621 3.87298334621 4.24264068712 3.60555127546 3.87298334621 2.64575131106
4.24264068712 2.64575131106 2.00000000000 4.00000000000 1.73205080757 2.23606797750
3.31662479036 2.82842712475 1.00000000000 2.82842712475 3.31662479036 3.00000000000
4.47213595500 2.64575131106 3.31662479036 3.60555127546 3.46410161514 3.00000000000
1.73205080757 2.64575131106 3.16227766017 3.00000000000 2.44948974278 1.00000000000
2.44948974278 2.82842712475 1.00000000000 3.74165738677 2.44948974278 3.46410161514

Why TCP FIN/ACK packets have payload data but length is 0?

I use tcpdump to capture some data, but found that the FIN and ACK packets have some payload while the length is 0. Can anyone explain that? When I use Wireshark to see the pcap file, all is right. Why is that?
20:56:05.174314 IP 10.0.2.15.20281 > 192.168.4.80.21224: Flags [.], ack 1721, win 33232, length 0
0x0000: 0004 0001 0006 0800 2793 4e00 0000 0800 ........'.N.....
0x0010: 4500 0028 417a 4000 4006 284f 0a00 020f E..(Az#.#.(O....
0x0020: c0a8 0450 4f39 52e8 b35c bf82 f4d8 b0ba ...PO9R..\......
0x0030: 5010 81d0 d121 0000 4745 5420 2f75 7365 P....!..GET./use
0x0040: 7273 2f31 3f75 7365 rs/1?use
20:56:15.179096 IP 10.0.2.15.20281 > 192.168.4.80.21224: Flags [F.], seq 649, ack 1721, win 33232, length 0
0x0000: 0004 0001 0006 0800 2793 4e00 0000 0800 ........'.N.....
0x0010: 4500 0028 417b 4000 4006 284e 0a00 020f E..(A{#.#.(N....
0x0020: c0a8 0450 4f39 52e8 b35c bf82 f4d8 b0ba ...PO9R..\......
0x0030: 5011 81d0 d121 0000 0000 0000 0000 2e31 P....!.........1
0x0040: 2035 3030 2049 6e74 .500.Int
20:56:15.179528 IP 192.168.4.80.21224 > 10.0.2.15.20281: Flags [.], ack 650, win 65535, length 0
0x0000: 0000 0001 0006 5254 0012 3502 0000 0800 ......RT..5.....
0x0010: 4500 0028 73f3 0000 4006 35d6 c0a8 0450 E..(s...#.5....P
0x0020: 0a00 020f 52e8 4f39 f4d8 b0ba b35c bf83 ....R.O9.....\..
0x0030: 5010 ffff 2438 0000 0000 0000 0000 2e31 P...$8.........1
0x0040: 2035 3030 2049 6e74 6572 6e61 6c20 .500.Internal.
20:56:15.181826 IP 192.168.4.80.21224 > 10.0.2.15.20281: Flags [F.], seq 1721, ack 650, win 65535, length 0
0x0000: 0000 0001 0006 5254 0012 3502 0000 0800 ......RT..5.....
0x0010: 4500 0028 73f5 0000 4006 35d4 c0a8 0450 E..(s...#.5....P
0x0020: 0a00 020f 52e8 4f39 f4d8 b0ba b35c bf83 ....R.O9.....\..
0x0030: 5011 ffff 2437 0000 0000 0000 0000 7365 P...$7........se
0x0040: 7273 2f31 3f75 7365 725f 6964 3d35 rs/1?user_id=5
20:56:15.181884 IP 10.0.2.15.20281 > 192.168.4.80.21224: Flags [.], ack 1722, win 33232, length 0
0x0000: 0004 0001 0006 0800 2793 4e00 0000 0800 ........'.N.....
0x0010: 4500 0028 eaf7 4000 4006 7ed1 0a00 020f E..(..#.#.~.....
0x0020: c0a8 0450 4f39 52e8 b35c bf83 f4d8 b0bb ...PO9R..\......
0x0030: 5010 81d0 a266 0000 4745 5420 2f75 7365 P....f..GET./use
0x0040: 7273 2f31 3f75 7365 rs/1?use

`tcpdump` shows different length for the same ARP packet

I'm running tcpdump in two identical Linux machines with this command:
tcpdump -i enp0s8 -nn -XX -vvv
During an ARP request in the sender machine I see:
20:03:29.113813 ARP, Ethernet (len 6), IPv4 (len 4), Request who-has 10.0.0.1 tell 10.0.0.2, length 28
0x0000: 0800 27bb f251 0800 27cf ce8e 0806 0001 ..'..Q..'.......
0x0010: 0800 0604 0001 0800 27cf ce8e 0a00 0002 ........'.......
0x0020: 0000 0000 0000 0a00 0001 ..........
but in the destination machine:
20:03:29.114928 ARP, Ethernet (len 6), IPv4 (len 4), Request who-has 10.0.0.1 tell 10.0.0.2, length 46
0x0000: 0800 27bb f251 0800 27cf ce8e 0806 0001 ..'..Q..'.......
0x0010: 0800 0604 0001 0800 27cf ce8e 0a00 0002 ........'.......
0x0020: 0000 0000 0000 0a00 0001 0000 0000 0000 ................
0x0030: 0000 0000 0000 0000 0000 0000 ............
Why the packet is zerofilled reaching a length of 46?
The ARP message itself is 28 bytes long, exactly as you indicated. Now, with correct Ethernet implementations, the outgoing frame has to be padded to be at least 64 bytes long. There are some quirks about this, however - the device that originated this ARP message may itself be capable of sending it in an untagged frame or in 802.1Q-tagged frame. The tag size is always accounted towards the total frame size, resulting in different paddings:

Node request output differing from curl output

I'm able to make a request using curl, get the response and parse it into protobuf. However, on trying to do the same with node's request, I'm unable to parse the body argument into protobuf, even when I've converted it into a buffer.
Here's the hex dump of what the request should have output
00000000: 1088 8080 80f0 fca4 f02c a206 00a2 0600 .........,......
00000010: a206 5408 a7b5 a9a4 e22a 10ff 8ac8 b9e3 ..T......*......
00000020: 2a1a 121a 100a 0e09 2130 0000 0000 0000 *.......!0......
00000030: 107b 18d4 011a 121a 100a 0e09 2130 0000 .{..........!0..
00000040: 0000 0000 107b 18d4 011a 121a 100a 0e09 .....{..........
00000050: 2130 0000 0000 0000 107b 18d4 011a 081a !0.......{......
00000060: 0612 0408 0110 15a2 0600 a206 00 .............
This is what dumping the created buffer onto disk and using xxd to dump it into hex
00000000: 10ef bfbd efbf bdef bfbd efbf bdef bfbd ................
00000010: efbf bdef bfbd efbf bd2c efbf bd06 00ef .........,......
00000020: bfbd 0600 efbf bd06 5408 efbf bdef bfbd ........T.......
00000030: efbf bdef bfbd efbf bd2a 10ef bfbd efbf .........*......
00000040: bdef bfbd efbf bdef bfbd 2a1a 121a 100a ..........*.....
00000050: 0e09 2130 0000 0000 0000 107b 18ef bfbd ..!0.......{....
00000060: 011a 121a 100a 0e09 2130 0000 0000 0000 ........!0......
00000070: 107b 18ef bfbd 011a 121a 100a 0e09 2130 .{............!0
00000080: 0000 0000 0000 107b 18ef bfbd 011a 081a .......{........
00000090: 0612 0408 0110 15ef bfbd 0600 efbf bd06 ................
There might be a small difference due to change in timestamps (my server sends that back within the protobuf), but both of the files should have been parsable.
Okay, so request assumes your response to be a unicode string, strips non-unicode characters and mutilates it unless you add encoding: null
to requestSettings.
Adding the said param to the settings fixes it.

Resources