Detect types of ScriptSig and parse each part As explained in 

https://en.bitcoin.it/wiki/Script

there are many types of scriptPubKey and scriptSig like:

&lt;sig&gt; &lt;pubKey&gt;
&lt;sig&gt; &lt;pubKey&gt; &lt;pubKey&gt; 
&lt;sig&gt; &lt;pubKey&gt; &lt;pubHashA&gt; 
&lt;sig&gt; &lt;pubKey&gt; &lt;pubHashA&gt; &lt;pubKeyHash&gt;
...


how can we detect type of ScriptSig and separate each part?
In other words is there a length variable or something like that to parse each part separately?
Really, the only way to detect the type is to parse it as a program.

All scripts Bitcoin are written in the Bitcoin Script language. Much like any other programming language, certain symbols have special meaning, and correspond to specific op codes, which appear in the raw hex form. You can find a list of op codes on the Bitcoin Wiki: https://en.bitcoin.it/wiki/Script#Opcodes

The standardized scripts, which are usually what we see presented as addresses, are really just specific snippets of Bitcoin Script programs. For example, a P2PKH "address" is really just half a program in the form of:

OP_DUP OP_HASH160 &lt;pubKeyHash&gt; OP_EQUALVERIFY OP_CHECKSIG


When combined with the remaining half of the program, which is provided as the ScriptSig when spending the coins, you get a complete program which can be evaluated.

To determine the script type, you really just need to parse the program (or partial program, in the event of an output) and see if it matches a known, standardized type.