#AutoIt3Wrapper_UseX64=y #include "x64_Ops.au3" Local $sData = "CgNQQ0cVH4UvQRiQvr/a4lsqA05ZUTAIOAFFKBlXQUj61YQeZWhmpj/YAQQ=" __Protobuf_ToJson($sData) ; #FUNCTION# ============================================================================= ; Name...........: __Protobuf_ToJson ; ========================================================================================= Func __Protobuf_ToJson($o_bData) ;Local $1_Timer = TimerInit() Local $o_Base64 = _Base64Decode($o_bData) Local $o_PotoRaw = StringSplit(__Protobuf_Raw($o_Base64), @CR, 2) Local $o_Json = '{' For $i = 0 To UBound($o_PotoRaw) - 1 $o_Json &= __Proto_File($o_PotoRaw[$i]) & ',' Next $o_Json = StringTrimRight($o_Json, 1) $o_Json &= '}' ConsoleWrite($o_Json & @CRLF) ;ConsoleWrite('ProtobufToJson Timer: ' & TimerDiff($1_Timer) & @CRLF) EndFunc ;==>__Protobuf_Decode ; #FUNCTION# ============================================================================= ; Name...........: __Protobuf_Raw ; https://www.autoitscript.com/forum/topic/206536-blob-object-protocol-buffers/?do=findComment&comment=1488443 ; @Danp2 ; ========================================================================================= Func __Protobuf_Raw($HexString) Local $i_Pos = 1 Local $i_Len = BinaryLen($HexString) Local $b_Tag, $b_Field, $b_Len, $b_Compare Local $i_Wire, $i_End Local $s_Result Local $x_Return = '' ;~--- While $i_Pos <= $i_Len $b_Tag = BinaryMid($HexString, $i_Pos, 1) $b_Field = BitShift($b_Tag, 3) $i_Wire = BitAND($b_Tag, 7) If BitAND($b_Field, 16) Then $b_Tag = BinaryMid($HexString, $i_Pos + 1, 1) & BitAND($b_Field, 16) $i_Pos += 1 EndIf Switch $i_Wire Case 0 ;~ Varint ==> used for( int32, int64, uint32, uint64, sint32, sint64, bool, enum) $i_End = $i_Pos + 1 ;~--- While $i_End <= $i_Len $b_Compare = BinaryMid($HexString, $i_End, 1) If Not BitAND($b_Compare, 128) Then ExitLoop $i_End += 1 WEnd ;~--- $s_Result = (BinaryMid($HexString, $i_Pos + 1, $i_End - $i_Pos)) $x_Return &= $b_Field & ' ' & ($s_Result) & @CRLF $i_Pos = $i_End Case 1 ;~ 64-bit ==> used for(fixed64, sfixed64, double) Case 2 ;~ Length-delimited ==> used for(string, bytes, embedded messages, packed repeated fields) $b_Len = BinaryMid($HexString, $i_Pos + 1, 1) $s_Result = BinaryToString(BinaryMid($HexString, $i_Pos + 2, $b_Len)) $x_Return &= $b_Field & ' ' & $s_Result & @CRLF $i_Pos += Number($b_Len) + 1 Case 3 ;~ Start group ==> groups (deprecated) Case 4 ;~ End group ==> groups (deprecated) Case 5 ;~ 32-bit ==> used for(fixed32, sfixed32, float) $s_Result = (BinaryMid($HexString, $i_Pos + 1, 4)) $x_Return &= $b_Field & ' ' & ($s_Result) & @CRLF $i_Pos += 4 EndSwitch $i_Pos += 1 WEnd ;~--- $x_Return = StringTrimRight($x_Return, 2) Return $x_Return EndFunc ;==>__Protobuf_Raw ; #FUNCTION# ============================================================================= ; Name...........: __Proto_File ; ========================================================================================= Func __Proto_File($X_proto) #Region ;~ proto schema Local $proto3_Initialize = _ 'string id = 1' & @CRLF & _ 'float price = 2' & @CRLF & _ 'sint64 time = 3' & @CRLF & _ 'string currency = 4' & @CRLF & _ 'string exchange = 5' & @CRLF & _ 'QuoteType quoteType = 6' & @CRLF & _ 'MarketHoursType marketHours = 7' & @CRLF & _ 'float changePercent = 8' & @CRLF & _ 'sint64 dayVolume = 9' & @CRLF & _ 'float dayHigh = 10' & @CRLF & _ 'float dayLow = 11' & @CRLF & _ 'float change = 12' & @CRLF & _ 'string shortName = 13' & @CRLF & _ 'sint64 expireDate = 14' & @CRLF & _ 'float openPrice = 15' & @CRLF & _ 'float previousClose = 16' & @CRLF & _ 'float strikePrice = 17' & @CRLF & _ 'string underlyingSymbol = 18' & @CRLF & _ 'sint64 openInterest = 19' & @CRLF & _ 'OptionType optionsType = 20' & @CRLF & _ 'sint64 miniOption = 21' & @CRLF & _ 'sint64 lastSize = 22' & @CRLF & _ 'float bid = 23' & @CRLF & _ 'sint64 bidSize = 24' & @CRLF & _ 'float ask = 25' & @CRLF & _ 'sint64 askSize = 26' & @CRLF & _ 'sint64 priceHint = 27' & @CRLF & _ 'sint64 vol_24hr = 28' & @CRLF & _ 'sint64 volAllCurrencies = 29' & @CRLF & _ 'string fromcurrency = 30' & @CRLF & _ 'string lastMarket = 31' & @CRLF & _ 'double circulatingSupply = 32' & @CRLF & _ 'double marketcap = 33' Local $QuoteType = _ 'NONE = 0' & @CRLF & _ 'ALTSYMBOL = 5' & @CRLF & _ 'HEARTBEAT = 7' & @CRLF & _ 'EQUITY = 8' & @CRLF & _ 'INDEX = 9' & @CRLF & _ 'MUTUALFUND = 11' & @CRLF & _ 'MONEYMARKET = 12' & @CRLF & _ 'OPTION = 13' & @CRLF & _ 'CURRENCY = 14' & @CRLF & _ 'WARRANT = 15' & @CRLF & _ 'BOND = 17' & @CRLF & _ 'FUTURE = 18' & @CRLF & _ 'ETF = 20' & @CRLF & _ 'COMMODITY = 23' & @CRLF & _ 'ECNQUOTE = 28' & @CRLF & _ 'CRYPTOCURRENCY = 41' & @CRLF & _ 'INDICATOR = 42' & @CRLF & _ 'INDUSTRY = 1000' Local $OptionType = _ 'CALL = 0' & @CRLF & _ 'PUT = 1' Local $MarketHoursType = _ 'PRE_MARKET = 0' & @CRLF & _ 'REGULAR_MARKET = 1' & @CRLF & _ 'POST_MARKET = 2' & @CRLF & _ 'EXTENDED_HOURS_MARKET = 3' #EndRegion ;~ proto schema #Region ;~ Local $o_Return = '' Local $o_String = StringSplit($X_proto, ' ', 2) Local $o_Regex = '.*\b' & StringStripWS($o_String[0], 8) & '\b' Local $o_Value = StringRegExp($proto3_Initialize, $o_Regex, 3) If Not IsArray($o_Value) Then Return $o_Return Local $o_Position = StringInStr($o_Value[0], '=') Local $o_proto = StringLeft($o_Value[0], $o_Position - 2) $o_proto = StringSplit($o_proto, ' ', 2) #EndRegion ;~ #Region ;~ Local $o_Temp Switch $o_proto[0] Case 'string' $o_Return = '"' & $o_proto[1] & '":"' & $o_String[1] & '"' Case 'float' $o_Temp = __IntToFloat(__Fixed32($o_String[1])) $o_Return = '"' & $o_proto[1] & '":' & $o_Temp Case 'sint64' $o_Temp = __VarInt($o_String[1]) $o_Return = '"' & $o_proto[1] & '":' & $o_Temp Case 'double' $o_Return = '' Case Else $o_Temp = __VarInt($o_String[1]) $o_Temp = StringRegExp(Execute('$' & $o_proto[0]), '.*\b' & $o_Temp & '\b', 3)[0] $o_Temp = StringLeft($o_Temp, StringInStr($o_Temp, '=') - 2) $o_Return = '"' & $o_proto[1] & '":"' & $o_Temp & '"' EndSwitch #EndRegion ;~ Return $o_Return EndFunc ;==>__Proto_File ; #FUNCTION# ============================================================================= ; Name...........: _Base64Decode ; https://www.autoitscript.com/forum/topic/81332-_base64encode-_base64decode/ ; ========================================================================================= Func _Base64Decode($input_string) Local $struct = DllStructCreate("int") Local $a_Call = DllCall("Crypt32.dll", "int", "CryptStringToBinary", _ "str", $input_string, _ "int", 0, _ "int", 1, _ "ptr", 0, _ "ptr", DllStructGetPtr($struct, 1), _ "ptr", 0, _ "ptr", 0) If @error Or Not $a_Call[0] Then Return SetError(1, 0, "") ; error calculating the length of the buffer needed EndIf Local $a = DllStructCreate("byte[" & DllStructGetData($struct, 1) & "]") $a_Call = DllCall("Crypt32.dll", "int", "CryptStringToBinary", _ "str", $input_string, _ "int", 0, _ "int", 1, _ "ptr", DllStructGetPtr($a), _ "ptr", DllStructGetPtr($struct, 1), _ "ptr", 0, _ "ptr", 0) If @error Or Not $a_Call[0] Then Return SetError(2, 0, ""); error decoding EndIf Return DllStructGetData($a, 1) EndFunc ;==>_Base64Decode ; #FUNCTION# ============================================================================= ; Name...........: __Fixed32 ; https://www.autoitscript.com/forum/topic/206536-blob-object-protocol-buffers/?do=findComment&comment=1488589 ; @Nine ; ========================================================================================= Func __Fixed32($bVal) Local $tStruct = DllStructCreate("int data") $tStruct.data = Binary($bVal) Return $tStruct.data EndFunc ;==>__Fixed32 ; #FUNCTION# ============================================================================= ; Name...........: __VarInt ; @Nine ; ========================================================================================= Func __VarInt($b_Data) Local $bData = Binary($b_Data) Local $iResult = 0 Local $iShift = 0 For $i = 1 To BinaryLen($bData) $iResult += _BitShift64(BitAND(Dec(Hex(BinaryMid($bData, $i, 1), 2)), 0x7F), -$iShift) $iShift += 7 Next Return $iResult EndFunc ;==>__VarInt ; #FUNCTION# ============================================================================= ; Name...........: __IntToFloat ; https://www.autoitscript.com/forum/topic/53842-integer-to-float-conversion/?do=findComment&comment=407905 ; @martin ; ========================================================================================= Func __IntToFloat($x_Val) Local $o_Result Local $o_Float = DllStructCreate("float") Local $o_Int = DllStructCreate("int64", DllStructGetPtr($o_Float)) DllStructSetData($o_Int, 1, $x_Val) $o_Result = DllStructGetData($o_Float, 1) $o_Float = 0 $o_Int = 0 Return $o_Result EndFunc ;==>__IntToFloat