Exemplo n.º 1
0
 internal static byte[] Digest(DigestFactory.Digest digest, MutableString key, MutableString data)
 {
     // TODO: does MRI really modify the digest object?
     digest.Algorithm.Key = key.ConvertToBytes();
     byte[] hash = digest.Algorithm.ComputeHash(data.ConvertToBytes());
     return(hash);
 }
Exemplo n.º 2
0
        public static int Send(ConversionStorage <int> /*!*/ fixnumCast,
                               RubyBasicSocket /*!*/ self, [DefaultProtocol, NotNull] MutableString /*!*/ message, object flags)
        {
            Protocols.CheckSafeLevel(fixnumCast.Context, 4, "send");
            SocketFlags socketFlags = ConvertToSocketFlag(fixnumCast, flags);

            return(self.Socket.Send(message.ConvertToBytes(), socketFlags));
        }
Exemplo n.º 3
0
        public static int Send(RubyContext /*!*/ context, RubyBasicSocket /*!*/ self, object message, object flags)
        {
            Protocols.CheckSafeLevel(context, 4, "send");
            SocketFlags   socketFlags = ConvertToSocketFlag(context, flags);
            MutableString strMessage  = Protocols.CastToString(context, message);

            return(self.Socket.Send(strMessage.ConvertToBytes(), socketFlags));
        }
Exemplo n.º 4
0
        public static RubyArray GetHostByAddress(RubyClass /*!*/ self,
                                                 [DefaultProtocol, NotNull] MutableString /*!*/ address, [DefaultParameterValue(null)] object type)
        {
            AddressFamily addressFamily = ConvertToAddressFamily(self.Context, type);
            IPHostEntry   entry         = GetHostEntry(new IPAddress(address.ConvertToBytes()));

            return(CreateHostEntryArray(entry, true));
        }
Exemplo n.º 5
0
        public static RubyArray GetHostByAddress(ConversionStorage <MutableString> /*!*/ stringCast, ConversionStorage <int> /*!*/ fixnumCast,
                                                 RubyClass /*!*/ self, [DefaultProtocol, NotNull] MutableString /*!*/ address, [DefaultParameterValue(null)] object type)
        {
            // TODO: ignore family, the only supported families are InterNetwork and InterNetworkV6
            ConvertToAddressFamily(stringCast, fixnumCast, type);
            IPHostEntry entry = GetHostEntry(new IPAddress(address.ConvertToBytes()), DoNotReverseLookup(self.Context).Value);

            return(CreateHostEntryArray(self.Context, entry, true));
        }
Exemplo n.º 6
0
            public static MutableString /*!*/ HexDigest(RubyClass /*!*/ self, [NotNull] DigestFactory.Digest /*!*/ digest,
                                                        [NotNull] MutableString /*!*/ key, [NotNull] MutableString /*!*/ data)
            {
                // TODO: does MRI really modify the digest object?
                digest.Algorithm.Key = key.ConvertToBytes();
                byte[] hash = digest.Algorithm.ComputeHash(data.ConvertToBytes());

                return(MutableString.Create(BitConverter.ToString(hash).Replace("-", "").ToLower()));
            }
Exemplo n.º 7
0
        public static int Send(RubyContext /*!*/ context, RubyBasicSocket /*!*/ self, object message, object flags, object hostname, object port)
        {
            Protocols.CheckSafeLevel(context, 4, "send");
            // Convert the parameters
            SocketFlags   sFlags     = ConvertToSocketFlag(context, flags);
            MutableString strMessage = Protocols.CastToString(context, message);
            MutableString address    = GetAddressInternal(context, hostname);
            int           iPort      = ConvertToPortNum(context, port);
            EndPoint      toEndPoint = new IPEndPoint(IPAddress.Parse(address.ConvertToString()), iPort);

            return(self.Socket.SendTo(strMessage.ConvertToBytes(), sFlags, toEndPoint));
        }
Exemplo n.º 8
0
        internal static IPEndPoint /*!*/ UnpackSockAddr(MutableString /*!*/ stringAddress)
        {
            byte[]        bytes = stringAddress.ConvertToBytes();
            SocketAddress addr  = new SocketAddress(AddressFamily.InterNetwork, bytes.Length);

            for (int i = 0; i < bytes.Length; ++i)
            {
                addr[i] = bytes[i];
            }
            IPEndPoint ep = new IPEndPoint(0, 0);

            return((IPEndPoint)ep.Create(addr));
        }
Exemplo n.º 9
0
        private int ReadFromObject(byte[] /*!*/ buffer, int offset, int count)
        {
            MutableString result = _readSite.Target(_readSite, _context, _obj, count);

            if (result == null)
            {
                return(0);
            }
            else
            {
                byte[] readdata = result.ConvertToBytes();
                Buffer.BlockCopy(readdata, 0, buffer, offset, readdata.Length);
                return(readdata.Length);
            }
        }
Exemplo n.º 10
0
        public static int Send(ConversionStorage <int> /*!*/ fixnumCast, ConversionStorage <MutableString> /*!*/ stringCast,
                               RubyBasicSocket /*!*/ self, [DefaultProtocol, NotNull] MutableString /*!*/ message,
                               object flags, object hostNameOrAddress, object port)
        {
            Protocols.CheckSafeLevel(fixnumCast.Context, 4, "send");
            int         iPort  = ConvertToPortNum(stringCast, fixnumCast, port);
            SocketFlags sFlags = ConvertToSocketFlag(fixnumCast, flags);

            // Convert the parameters
            IPAddress address = (hostNameOrAddress != null) ?
                                GetHostAddress(ConvertToHostString(stringCast, hostNameOrAddress)) : IPAddress.Loopback;

            EndPoint toEndPoint = new IPEndPoint(address, iPort);

            return(self.Socket.SendTo(message.ConvertToBytes(), sFlags, toEndPoint));
        }
Exemplo n.º 11
0
        public static int Send(ConversionStorage <int> /*!*/ fixnumCast, RubyBasicSocket /*!*/ self,
                               [DefaultProtocol, NotNull] MutableString /*!*/ message, object flags, [DefaultProtocol, NotNull] MutableString /*!*/ to)
        {
            Protocols.CheckSafeLevel(fixnumCast.Context, 4, "send");
            // Convert the parameters
            SocketFlags socketFlags = ConvertToSocketFlag(fixnumCast, flags);
            // Unpack the socket address information from the to parameter
            SocketAddress address = new SocketAddress(AddressFamily.InterNetwork);

            for (int i = 0; i < to.GetByteCount(); i++)
            {
                address[i] = to.GetByte(i);
            }
            EndPoint toEndPoint = self.Socket.LocalEndPoint.Create(address);

            return(self.Socket.SendTo(message.ConvertToBytes(), socketFlags, toEndPoint));
        }
Exemplo n.º 12
0
        private int ReadFromObject(byte[] /*!*/ buffer, int offset, int count)
        {
            // TODO: readpartial is called if available
            // TODO: conversion
            MutableString result = (MutableString)ReadSite.Target(ReadSite, _io, count);

            if (result == null)
            {
                return(0);
            }
            else
            {
                byte[] readdata = result.ConvertToBytes();
                Buffer.BlockCopy(readdata, 0, buffer, offset, readdata.Length);
                return(readdata.Length);
            }
        }
Exemplo n.º 13
0
        public static int Send(RubyContext /*!*/ context, RubyBasicSocket /*!*/ self, object message, object flags, object to)
        {
            Protocols.CheckSafeLevel(context, 4, "send");
            // Convert the parameters
            SocketFlags   socketFlags  = ConvertToSocketFlag(context, flags);
            MutableString strMessage   = Protocols.CastToString(context, message);
            MutableString strToAddress = Protocols.CastToString(context, to);
            // Unpack the socket address information from the to parameter
            SocketAddress address = new SocketAddress(AddressFamily.InterNetwork);

            for (int i = 0; i < strToAddress.GetByteCount(); i++)
            {
                address[i] = strToAddress.GetByte(i);
            }
            EndPoint toEndPoint = self.Socket.LocalEndPoint.Create(address);

            return(self.Socket.SendTo(strMessage.ConvertToBytes(), socketFlags, toEndPoint));
        }
Exemplo n.º 14
0
        public static MutableString /*!*/ iconv(Iconv /*!*/ self, [DefaultProtocol] MutableString /*!*/ str,
                                                [DefaultProtocol, DefaultParameterValue(0)] int startIndex, [DefaultProtocol, DefaultParameterValue(-1)] int endIndex)
        {
            // TODO:
            int  bytesUsed, charsUsed;
            bool completed;

            byte[] source = str.ConvertToBytes();
            char[] buffer = new char[self._fromEncoding.GetCharCount(source, 0, source.Length)];
            self._fromEncoding.Convert(source, 0, source.Length, buffer, 0, buffer.Length, false, out bytesUsed, out charsUsed, out completed);
            Debug.Assert(charsUsed == buffer.Length && bytesUsed == source.Length);

            byte[] result       = new byte[self._toEncoding.GetByteCount(buffer, 0, buffer.Length, false)];
            int    bytesEncoded = self._toEncoding.GetBytes(buffer, 0, buffer.Length, result, 0, false);

            Debug.Assert(bytesEncoded == result.Length);

            return(MutableString.CreateBinary(result));
        }
Exemplo n.º 15
0
        public static Node ToYamlNode(MutableString /*!*/ self, [NotNull] RubyRepresenter /*!*/ rep)
        {
            if (RubyOps.IsTrue(_IsBinaryData.Target(_IsBinaryData, rep.Context, self)))
            {
                return(rep.BaseCreateNode(self.ConvertToBytes()));
            }

            string    str   = self.ConvertToString();
            RubyArray props = RubyRepresenter.ToYamlProperties(rep.Context, self);

            if (props.Count == 0)
            {
                MutableString taguri = RubyRepresenter.TagUri(rep.Context, self);

                char style = (char)0;
                if (str.StartsWith(":"))
                {
                    style = '"';
                }
                else
                {
                    MutableString styleStr = RubyRepresenter.ToYamlStyle(rep.Context, self) as MutableString;
                    if (styleStr != null && styleStr.Length > 0)
                    {
                        style = styleStr.GetChar(0);
                    }
                }

                return(rep.Scalar(taguri != null ? taguri.ConvertToString() : "", str, style));
            }

            Hash map = new Hash(rep.Context);

            map.Add(MutableString.Create("str"), str);
            RubyRepresenter.AddYamlProperties(rep.Context, self, map, props);
            return(rep.Map(self, map));
        }
Exemplo n.º 16
0
            public static MutableString /*!*/ InflateStream(Inflate /*!*/ self, MutableString /*!*/ zstring)
            {
                self._inputBuffer.AddRange(zstring.ConvertToBytes());

                if (self._rawDeflate == false)
                {
                    byte compression_method_and_flags = self._inputBuffer[++(self._inPos)];
                    byte flags = self._inputBuffer[++(self._inPos)];
                    if (((compression_method_and_flags << (byte)0x08) + flags) % (byte)31 != 0)
                    {
                        throw new DataError("incorrect header check");
                    }

                    byte compression_method = (byte)(compression_method_and_flags & (byte)0x0F);
                    if (compression_method != Z_DEFLATED)
                    {
                        throw new DataError("unknown compression method");
                    }

                    byte compression_info = (byte)(compression_method_and_flags >> (byte)0x04);
                    if ((compression_info + 8) > self._wBits)
                    {
                        throw new DataError("invalid window size");
                    }

                    bool preset_dictionary_flag = ((flags & 0x20) >> 0x05 == 1);
                    byte compression_level      = (byte)((flags & 0xC0) >> (byte)0x06);

                    //TODO: Add Preset Dictionary Support
                    if (preset_dictionary_flag)
                    {
                        self._inPos += 4;
                    }
                }

                bool last_block = false;

                while (!last_block)
                {
                    last_block = (self.GetBits(1) == 1);
                    byte block_type = (byte)self.GetBits(2);
                    switch (block_type)
                    {
                    case 0:
                        self.NoCompression();
                        break;

                    case 1:
                        self.FixedCodes();
                        break;

                    case 2:
                        self.DynamicCodes();
                        break;

                    case 3:
                        throw new DataError("invalid block type");
                    }
                }

                return(Inflate.Close(self));
            }
Exemplo n.º 17
0
        public static Node ToYamlNode(MutableString/*!*/ self, [NotNull]RubyRepresenter/*!*/ rep) {
            if (RubyOps.IsTrue(_IsBinaryData.Target(_IsBinaryData, rep.Context, self))) {
                return rep.BaseCreateNode(self.ConvertToBytes());
            }

            string str = self.ConvertToString();
            RubyArray props = RubyRepresenter.ToYamlProperties(rep.Context, self);
            if (props.Count == 0) {
                MutableString taguri = RubyRepresenter.TagUri(rep.Context, self);

                char style = (char)0;
                if (str.StartsWith(":")) {
                    style = '"';
                } else {
                    MutableString styleStr = RubyRepresenter.ToYamlStyle(rep.Context, self) as MutableString;
                    if (styleStr != null && styleStr.Length > 0) {
                        style = styleStr.GetChar(0);
                    }
                }

                return rep.Scalar(taguri != null ? taguri.ConvertToString() : "", str, style);
            }

            Hash map = new Hash(rep.Context);
            map.Add(MutableString.Create("str"), str);
            RubyRepresenter.AddYamlProperties(rep.Context, self, map, props);
            return rep.Map(self, map);
        }
Exemplo n.º 18
0
 internal static MutableString /*!*/ HexEncode(MutableString /*!*/ str)
 {
     return(Bytes2Hex(str.ConvertToBytes()));
 }
Exemplo n.º 19
0
 /*!*/
 internal static MutableString HexEncode(MutableString/*!*/ str)
 {
     return Bytes2Hex(str.ConvertToBytes());
 }
Exemplo n.º 20
0
        public static MutableString /*!*/ iconv(Iconv /*!*/ self,
                                                [DefaultProtocol] MutableString str,
                                                [DefaultProtocol, DefaultParameterValue(0)] int startIndex,
                                                [DefaultProtocol, NotNull, DefaultParameterValue(-1)] int length)
        {
            if (self._isClosed)
            {
                throw RubyExceptions.CreateArgumentError("closed stream");
            }

            if (str == null)
            {
                return(self.Close(true));
            }

            // TODO:
            int  bytesUsed, charsUsed;
            bool completed;

            byte[] source = str.ConvertToBytes();
            if (startIndex < 0)
            {
                startIndex = source.Length + startIndex;
                if (startIndex < 0)
                {
                    //throw new IllegalSequence("start index is too large of a negative number");
                    startIndex = 0;
                    length     = 0;
                }
            }
            else if (startIndex > source.Length)
            {
                startIndex = 0;
                length     = 0;
            }

            if ((length < 0) || (startIndex + length > source.Length))
            {
                length = source.Length - startIndex;
            }

            char[] buffer = new char[self._fromEncoding.GetCharCount(source, startIndex, length)];
            self._fromEncoding.Convert(source, startIndex, length, buffer, 0, buffer.Length, false, out bytesUsed, out charsUsed, out completed);
            Debug.Assert(charsUsed == buffer.Length && bytesUsed == length);

            byte[] result       = new byte[self._toEncoding.GetByteCount(buffer, 0, buffer.Length, false)];
            int    bytesEncoded = self._toEncoding.GetBytes(buffer, 0, buffer.Length, result, 0, false);

            Debug.Assert(bytesEncoded == result.Length);

            if (self._emitBom && result.Length > 0)
            {
                byte[] resultWithBom = new byte[2 + result.Length];
                resultWithBom[0] = 0xff;
                resultWithBom[1] = 0xfe;
                Array.Copy(result, 0, resultWithBom, 2, result.Length);
                result        = resultWithBom;
                self._emitBom = false;
            }

            return(MutableString.CreateBinary(result));
        }
Exemplo n.º 21
0
 public static void SetSocketOption(RubyContext /*!*/ context, RubyBasicSocket /*!*/ self,
                                    [DefaultProtocol] int level, [DefaultProtocol] int optname, [DefaultProtocol, NotNull] MutableString /*!*/ value)
 {
     Protocols.CheckSafeLevel(context, 2, "setsockopt");
     self.Socket.SetSocketOption((SocketOptionLevel)level, (SocketOptionName)optname, value.ConvertToBytes());
 }
Exemplo n.º 22
0
 internal static IPEndPoint/*!*/ UnpackSockAddr(MutableString/*!*/ stringAddress) {
     byte[] bytes = stringAddress.ConvertToBytes();
     SocketAddress addr = new SocketAddress(AddressFamily.InterNetwork, bytes.Length);
     for (int i = 0; i < bytes.Length; ++i) {
         addr[i] = bytes[i];
     }
     IPEndPoint ep = new IPEndPoint(0, 0);
     return (IPEndPoint)ep.Create(addr);
 }
Exemplo n.º 23
0
        public static Node/*!*/ ToYamlNode(UnaryOpStorage/*!*/ isBinaryDataStorage, MutableString/*!*/ self, [NotNull]RubyRepresenter/*!*/ rep) {

            var site = isBinaryDataStorage.GetCallSite("is_binary_data?");
            if (RubyOps.IsTrue(site.Target(site, rep.Context, self))) {
                return rep.BaseCreateNode(self.ConvertToBytes());
            }

            string str = self.ConvertToString();
            RubyArray props = rep.ToYamlProperties(self);
            if (props.Count == 0) {
                MutableString taguri = rep.GetTagUri(self);

                char style = '\0';
                if (str.StartsWith(":")) {
                    style = '"';
                } else {
                    MutableString styleStr = rep.ToYamlStyle(self);
                    if (styleStr != null && styleStr.Length > 0) {
                        style = styleStr.GetChar(0);
                    }
                }

                return rep.Scalar(taguri != null ? taguri.ConvertToString() : "", str, style);
            }

            var map = new Dictionary<MutableString, object>() {
                { MutableString.Create("str"), str }
            };
            rep.AddYamlProperties(self, map, props);
            return rep.Map(self, map);
        }
Exemplo n.º 24
0
 internal static byte[] Digest(DigestFactory.Digest digest, MutableString key, MutableString data) {
     // TODO: does MRI really modify the digest object?
     digest.Algorithm.Key = key.ConvertToBytes();
     byte[] hash = digest.Algorithm.ComputeHash(data.ConvertToBytes());
     return hash;
 }
Exemplo n.º 25
0
        public static void SetSocketOption(RubyContext /*!*/ context, RubyBasicSocket /*!*/ self, object /*Numeric*/ level, object /*Numeric*/ optname, object value)
        {
            Protocols.CheckSafeLevel(context, 2, "setsockopt");
            MutableString strValue = Protocols.CastToString(context, value);
            int           iLevel   = Protocols.CastToFixnum(context, Protocols.ConvertToInteger(context, level));
            int           iOptname = Protocols.CastToFixnum(context, Protocols.ConvertToInteger(context, optname));

            self.Socket.SetSocketOption((SocketOptionLevel)iLevel, (SocketOptionName)iOptname, strValue.ConvertToBytes());
        }