/// <summary>
        /// Performs FFT on a list of values returns a list of Frequencies associated with maximum magnitudes
        /// </summary>
        /// <param name="values">Values to perform FFT on</param>
        /// <param name="amount">Amount of maximum aplitudes to return</param>
        /// <returns></returns>
        public static IEnumerable <Tuple <int, Complex> > CalculateSalientMagnitudesOfOneSecond(double[] values, int amount)
        {
            Complex[] input  = ConvertToComplex(values);
            Complex[] output = new Complex[input.Length];

            using (var pinIn = new PinnedArray <Complex>(input))
                using (var pinOut = new PinnedArray <Complex>(output))
                {
                    DFT.FFT(pinIn, pinOut); //leave extra threads out as we are already multithreading
                }

            //only half the data is within the frequency domain
            //tuples appear to be faster than dictionaries for this particular use case (sorting, returning, etc.)
            Tuple <int, Complex>[] results = new Tuple <int, Complex> [output.Length / 2];
            for (int i = 0; i < output.Length / 2; i++)
            {
                results[i] = new Tuple <int, Complex>(i, output[i] / input[i]);
            }

            IEnumerable <Tuple <int, Complex> > max = (from result in results
                                                       where true
                                                       orderby result.Item2.Magnitude descending
                                                       select result).Take(amount);

            return(max);
        }
Exemplo n.º 2
0
        public void PinnedArrayTest()
        {
            using (PinnedArray <int> array = new PinnedArray <int>(10))
            {
                array.Pinned = true;
                Assert.AreEqual(10, array.Buffer.Length);
                for (int i = 0; i < 10; i++)
                {
                    array.Buffer[i] = i;
                }

                array.Resize(20);
                Assert.AreEqual(20, array.Buffer.Length);
                Assert.IsTrue(array.Buffer.Take(10).SequenceEqual(Enumerable.Range(0, 10)));

                array.Resize(5);
                Assert.AreEqual(5, array.Buffer.Length);
                Assert.IsTrue(array.Buffer.SequenceEqual(Enumerable.Range(0, 5)));

                int[] array2 = new int[5];
                Marshal.Copy(array.GetElementAddress(0), array2, 0, 5);
                Assert.IsTrue(array.Buffer.SequenceEqual(array2));

                int[] array3 = Enumerable.Range(5, 5).ToArray();
                Marshal.Copy(array3, 0, array.GetElementAddress(0), 5);
                Assert.IsTrue(array.Buffer.SequenceEqual(array3));
            }
        }
Exemplo n.º 3
0
        public bool ContentsEqualTo(SecureEdit aSecureEdit)
        {
            Debug.Assert(aSecureEdit != null);
            if (aSecureEdit == null)
            {
                return(false);
            }

            using (var thisString = new PinnedArray <byte>(this.ToUtf8())) {
                using (var otherString = new PinnedArray <byte>(aSecureEdit.ToUtf8())) {
                    if (thisString.Data.Length != otherString.Data.Length)
                    {
                        return(false);
                    }
                    else
                    {
                        for (int i = 0; i < thisString.Data.Length; ++i)
                        {
                            if (thisString.Data[i] != otherString.Data [i])
                            {
                                return(false);
                            }
                        }
                    }
                    return(true);
                }
            }
        }
Exemplo n.º 4
0
        public void Test_BakeSingleCodepoint()
        {
            #region Init
            string assemblyDir  = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
            string solution_dir = Path.GetDirectoryName(Path.GetDirectoryName(Path.GetDirectoryName(assemblyDir)));
            #endregion

            //Read ttf file into byte array
            byte[] ttfFileContent = File.ReadAllBytes(solution_dir + @"\FontSamples\SIMHEI.TTF");
            using (var ttf = new PinnedArray <byte>(ttfFileContent))
            {
                //get pointer of the ttf file content
                var ttf_buffer = ttf.Pointer;
                //Initialize fontinfo
                FontInfo font = new FontInfo();
                STBTrueType.InitFont(ref font, ttf_buffer, STBTrueType.GetFontOffsetForIndex(ttf_buffer, 0));
                //set pixel height of the bitmap
                float pixelHeight = 32.0f;
                //get vertical metrics of the font
                int ascent = 0, descent = 0, lineGap = 0;
                STBTrueType.GetFontVMetrics(font, ref ascent, ref descent, ref lineGap);
                //calculate the vertical scale
                float scaleY = pixelHeight / (ascent - descent);
                //get bitmap of one codepoint '啊' as well as its width and height
                int width = 0, height = 0;
                var bitmap = STBTrueType.GetCodepointBitmap(font, 0f, scaleY, '啊' & 0xFFFF, ref width, ref height, null, null);
                //output the bitmap to a text file
                WriteBitmapToFileAsText("testOuput.txt", height, width, bitmap);
                //Open the text file
                OpenFile("testOuput.txt");
            }
        }
Exemplo n.º 5
0
        public static TorchTensor Stack(this TorchTensor[] tensors, long dimension)
        {
            var    parray     = new PinnedArray <IntPtr>();
            IntPtr tensorsRef = parray.CreateArray(tensors.Select(p => p.Handle).ToArray());

            return(new TorchTensor(THSTensor_stack(tensorsRef, parray.Array.Length, dimension)));
        }
Exemplo n.º 6
0
        private byte[] CreateGlyph(char firstChar, int charCount, ref int width, ref int height)
        {
            byte[] bitmapData = null;

            //Read ttf file into byte array
            byte[] ttfFileContent = File.ReadAllBytes(ttfSampleDir + '\\' + FontSelectorComboBox.SelectedItem as string);
            using (var ttf = new PinnedArray <byte>(ttfFileContent))
            {
                //get pointer of the ttf file content
                var ttf_buffer = ttf.Pointer;
                //Initialize fontinfo
                FontInfo font = new FontInfo();
                STBTrueType.InitFont(ref font, ttf_buffer, STBTrueType.GetFontOffsetForIndex(ttf_buffer, 0));
                //set bitmap size
                const int BITMAP_W = 512;
                const int BITMAP_H = 512;
                //allocate bitmap buffer
                byte[]      bitmapBuffer = new byte[BITMAP_W * BITMAP_W];
                BakedChar[] cdata        = new BakedChar[charCount];
                //bake bitmap for codepoint from firstChar to firstChar + charCount - 1
                STBTrueType.BakeFontBitmap(ttf_buffer, STBTrueType.GetFontOffsetForIndex(ttf_buffer, 0), pixelHeight, bitmapBuffer, BITMAP_W, BITMAP_H, firstChar, charCount, cdata); // no guarantee this fits!
                bitmapData = bitmapBuffer;
                width      = BITMAP_W;
                height     = BITMAP_H;
            }
            return(bitmapData);
        }
        public double[] timeDelaySignal(short[] signalInput, double s)
        {
            double[] input = new double[signalInput.Length];
            for (int j = 0; j < signalInput.Length; j++)
            {
                input[j] = (double)signalInput[j];
            }

            Complex[] output = new Complex[input.GetLength(input.Rank - 1) / 2 + 1];
            double[]  inOut  = new double[input.Length];

            using (var pinIn = new PinnedArray <double>(input))
                using (var pinOut = new PinnedArray <Complex>(output))
                    using (var in1Out = new PinnedArray <double>(inOut))
                    {
                        DFT.FFT(pinIn, pinOut);
                        for (int j = 0; j < pinOut.Length; j++)
                        {
                            double angle = ((2 * Math.PI) / pinOut.Length) * SampleRate * j * s;
                            pinOut[j] = pinOut[j] * Complex.Exp(new Complex(0, -angle));
                        }

                        DFT.IFFT(pinOut, in1Out);
                        for (int j = 0; j < inOut.Length; j++)
                        {
                            inOut[j] = inOut[j] / input.Length;
                        }
                        return(inOut);
                    }
        }
Exemplo n.º 8
0
        public bool SetDataBuffer()
        {
            //buffer = new short[bufferlength];

            //pStat = PS2000ACSConsole.Imports.SetDataBuffer(handle, PS2000ACSConsole.Imports.Channel.ChannelA, buffer,
            //    (int)bufferlength, 0, RatioMode);
            buffers    = new short[2][];
            buffers[0] = new short[50000];
            buffers[1] = new short[50000];


            appBuffers[0] = new short[50000];
            appBuffers[1] = new short[50000];

            appBuffersPinned[0] = new PinnedArray <short>(appBuffers[0]);

            pStat = Imports.SetDataBuffers(handle, Imports.Channel.ChannelA, buffers[0], buffers[1], (int)bufferlength, 0, RatioMode);
            // pStat = Imports.SetDataBuffer(handle, Imports.Channel.ChannelA, buffer, (int)bufferlength, 0, Imports.RatioMode.None);

            if (pStat == 0)
            {
                return(true);
            }
            else
            {
                return(false);
            }
        }
Exemplo n.º 9
0
        public void Test_BakeMultipleCodepoint()
        {
            #region Init
            string assemblyDir  = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
            string solution_dir = Path.GetDirectoryName(Path.GetDirectoryName(Path.GetDirectoryName(assemblyDir)));
            #endregion

            //Read ttf file into byte array
            byte[] ttfFileContent = File.ReadAllBytes(solution_dir + @"\FontSamples\Windsong.ttf");
            using (var ttf = new PinnedArray <byte>(ttfFileContent))
            {
                //get pointer of the ttf file content
                var ttf_buffer = ttf.Pointer;
                //Initialize fontinfo
                FontInfo font = new FontInfo();
                STBTrueType.InitFont(ref font, ttf_buffer, STBTrueType.GetFontOffsetForIndex(ttf_buffer, 0));
                //set bitmap size
                const int BITMAP_W = 512;
                const int BITMAP_H = 512;
                //allocate bitmap buffer
                byte[]      bitmapBuffer = new byte[BITMAP_W * BITMAP_W];
                BakedChar[] cdata        = new BakedChar[256 * 2];                                                                                                // ASCII 32..126 is 95 glyphs
                //bake bitmap for codepoint from 32 to 126
                STBTrueType.BakeFontBitmap(ttf_buffer, STBTrueType.GetFontOffsetForIndex(ttf_buffer, 0), 32.0f, bitmapBuffer, BITMAP_W, BITMAP_H, 32, 96, cdata); // no guarantee this fits!
                //output the bitmap to a text file
                WriteBitmapToFileAsText("testOuput.txt", BITMAP_H, BITMAP_W, bitmapBuffer);
                //Open the text file
                OpenFile("testOuput.txt");
            }
        }
Exemplo n.º 10
0
        private void btnTestFFT_Click(object sender, EventArgs e)
        {
            System.Numerics.Complex[] input  = new System.Numerics.Complex[4096];
            System.Numerics.Complex[] output = new System.Numerics.Complex[input.Length];
            double biggestMag = 0;

            //Generate an array of sine values
            for (int i = 0; i < input.Length; i++)
            {
                input[i] = Math.Sin(i * 2 * Math.PI * 128 / input.Length) + Math.Sin(i * 7 * Math.PI * 128 / input.Length);
            }

            //Run the Transfoamtion
            using (var pinIn = new PinnedArray <System.Numerics.Complex>(input))
                using (var pinOut = new PinnedArray <System.Numerics.Complex>(output)) {
                    DFT.FFT(pinIn, pinOut);
                }



            //Draw the output
            for (int i = 0; i < input.Length; i++)
            {
                using (Graphics g = picGraph.CreateGraphics()) {
                    g.DrawEllipse(Pens.Black, i, Convert.ToSingle(output[i].Magnitude), 4, 4);
                    txtFreqList.Text = txtFreqList.Text + output[i].Magnitude.ToString();
                }
            }
        }
Exemplo n.º 11
0
        /// <summary>
        /// Get the data in this storage
        /// </summary>
        /// <returns>The data in this storage</returns>
        public IEnumerator <T> GetEnumerator()
        {
            if (!_fileInfo.Exists)
            {
                yield break;
            }

            int elementsInTrunk = _trunkSize / _elementSize;

            if (elementsInTrunk <= 0)
            {
                elementsInTrunk = 1;
            }

            Byte[] buffer = new byte[_elementSize * elementsInTrunk];

            using (FileStream stream = _fileInfo.OpenRead())
                using (BufferedStream bufferStream = new BufferedStream(stream, _trunkSize))
                    using (PinnedArray <T> structures = new PinnedArray <T>(elementsInTrunk))
                    {
                        IntPtr structAddr = structures.AddrOfPinnedObject();
                        int    bytesRead;
                        while ((bytesRead = bufferStream.Read(buffer, 0, buffer.Length)) > 0)
                        {
                            Marshal.Copy(buffer, 0, structAddr, bytesRead);
                            int elementsRead = bytesRead / _elementSize;
                            for (int i = 0; i < elementsRead; i++)
                            {
                                yield return(structures.Array[i]);
                            }
                        }
                    }
        }
Exemplo n.º 12
0
        /// <summary>
        /// Get the subsampled data in this storage
        /// </summary>
        /// <param name="subsampleRate">The subsample rate</param>
        /// <returns>The sub-sampled data in this storage</returns>
        public IEnumerable <T> GetSubsamples(int subsampleRate)
        {
            if (!_fileInfo.Exists)
            {
                yield break;
            }

            int bufferSize = _elementSize * subsampleRate;

            using (FileStream stream = _fileInfo.OpenRead())
#if !NETSTANDARD1_4
                using (BufferedStream bufferStream = new BufferedStream(stream, _trunkSize))
#endif
                using (PinnedArray <Byte> buffer = new PinnedArray <byte>(bufferSize))
                    using (PinnedArray <T> structure = new PinnedArray <T>(subsampleRate))
                    {
#if NETSTANDARD1_4
                        var bufferStream = stream;
#endif
                        IntPtr structAddr = structure.AddrOfPinnedObject();
                        IntPtr addr       = buffer.AddrOfPinnedObject();
                        while (bufferStream.Read(buffer.Array, 0, bufferSize) > 0)
                        {
                            CvToolbox.Memcpy(structAddr, addr, bufferSize);
                            yield return(structure.Array[0]);
                        }
                    }
        }
Exemplo n.º 13
0
        public override int Read(byte[] buffer, int offset, int count)
        {
            if (_mode != ZLibMode.Decompress)
            {
                throw new NotSupportedException("Read not supported on compression");
            }

            ValidateReadWriteArgs(buffer, offset, count);

            int readLen = 0;

            if (_internalBufPos != -1)
            {
                using (PinnedArray pinRead = new PinnedArray(_internalBuf)) // [In] Compressed
                    using (PinnedArray pinWrite = new PinnedArray(buffer))  // [Out] Will-be-decompressed
                    {
                        _zstream.NextIn   = pinRead[_internalBufPos];
                        _zstream.NextOut  = pinWrite[offset];
                        _zstream.AvailOut = (uint)count;

                        while (0 < _zstream.AvailOut)
                        {
                            if (_zstream.AvailIn == 0)
                            {
                                // Compressed Data is no longer available in array, so read more from _stream
                                int baseReadSize = BaseStream.Read(_internalBuf, 0, _internalBuf.Length);

                                _internalBufPos  = 0;
                                _zstream.NextIn  = pinRead;
                                _zstream.AvailIn = (uint)baseReadSize;
                                TotalIn         += baseReadSize;
                            }

                            uint inCount  = _zstream.AvailIn;
                            uint outCount = _zstream.AvailOut;

                            // flush method for inflate has no effect
                            ZLibReturnCode ret = ZLibNative.Inflate(_zstream, ZLibFlush.NO_FLUSH);

                            _internalBufPos += (int)(inCount - _zstream.AvailIn);
                            readLen         += (int)(outCount - _zstream.AvailOut);

                            if (ret == ZLibReturnCode.STREAM_END)
                            {
                                _internalBufPos = -1; // magic for StreamEnd
                                break;
                            }

                            if (ret != ZLibReturnCode.OK)
                            {
                                throw new Exception(ret + " " + _zstream.LastErrorMsg);
                            }
                        }

                        TotalOut += readLen;
                    }
            }

            return(readLen);
        }
Exemplo n.º 14
0
        public TorchTensor Forward(params TorchTensor[] tensors)
        {
            var    parray     = new PinnedArray <IntPtr>();
            IntPtr tensorRefs = parray.CreateArray(tensors.Select(p => p.Handle).ToArray());

            return(new TorchTensor(THSJIT_forward(handle, tensorRefs, parray.Array.Length)));
        }
Exemplo n.º 15
0
    static void Main(string[] args)
    {
        var A = new PinnedArray(100);
        var B = new PinnedArray(100);

        B.FillRandom();
        A.AddFrom(B);
    }
Exemplo n.º 16
0
 public override void Write(byte[] buffer, int offset, int count)
 {
     _baseStream.Write(buffer, offset, count);
     using (PinnedArray pinRead = new PinnedArray(buffer))
     {
         _adler32 = NativeMethods.Adler32(_adler32, pinRead[offset], (uint)count);
     }
 }
Exemplo n.º 17
0
        public static double clip_grad_norm(this IList <TorchTensor> tensors, double max_norm, double norm_type = 2.0)
        {
            using (var parray = new PinnedArray <IntPtr>()) {
                IntPtr tensorsRef = parray.CreateArray(tensors.Select(p => p.Handle).ToArray());

                return(THSTensor_clip_grad_norm_(tensorsRef, parray.Array.Length, max_norm, norm_type));
            }
        }
Exemplo n.º 18
0
 /// <summary>
 /// Get a copy of the first element in the storage. If the storage is empty, a default value will be returned
 /// </summary>
 /// <returns>A copy of the first element in the storage. If the storage is empty, a default value will be returned</returns>
 public T Peek()
 {
     using (FileStream stream = _fileInfo.OpenRead())
         using (PinnedArray <Byte> buffer = new PinnedArray <byte>(_elementSize))
         {
             return((stream.Read(buffer.Array, 0, _elementSize) > 0) ?
                    Marshal.PtrToStructure <T>(buffer.AddrOfPinnedObject()) : new T());
         }
 }
Exemplo n.º 19
0
        /// <summary>
        /// Initializes generic image (allocates data).
        /// </summary>
        /// <param name="im">Generic image.</param>
        /// <param name="width">Image width.</param>
        /// <param name="height">Image height.</param>
        /// <param name="strideAllignment">Stride alignment. Usual practice is that every image row ends with address aligned with 4.</param>
        protected static void Initialize(Image im, int width, int height, int strideAllignment = 4)
        {
            int stride = CalculateStride(im.ColorInfo, width, strideAllignment);
            var buffer = new PinnedArray <byte>(stride * height);

            im.IsAllocated = true;
            im.buffer      = buffer;

            initializeProperties(im, buffer.Data, width, height, stride);
        }
Exemplo n.º 20
0
        public void TrimLeadingZeroTest()
        {
            PinnedArray<byte> array1 = new PinnedArray<byte>(new byte[] { 1, 2, 3, 4 });
              Util.TrimLeadingZero(array1);
              Assert.That(array1.Data, Is.EqualTo(new byte[]{ 1, 2, 3, 4 }));

              PinnedArray<byte> array2 = new PinnedArray<byte>(new byte[] { 0, 1, 2, 3, 4 });
              Util.TrimLeadingZero(array2);
              Assert.That(array2.Data, Is.EqualTo(new byte[] { 1, 2, 3, 4 }));
        }
Exemplo n.º 21
0
        private static string ProcessMessage(string s, PinnedArray <double> pin, FftwArrayComplex com, FftwPlanRC fft)
        {
            //Deserialize and then FFTW then return datasample as json string
            WFOTransporter wav    = JsonConvert.DeserializeObject <WFOTransporter>(s);
            WaveFileObject obj    = new WaveFileObject(wav);
            DataSample     sample = new DataSample(FFT(obj, pin, com, fft));
            string         json   = JsonConvert.SerializeObject(sample);

            return(json);
        }
Exemplo n.º 22
0
        public override int Read(byte[] buffer, int offset, int count)
        {
            int readLen = _baseStream.Read(buffer, offset, count);

            using (PinnedArray pinRead = new PinnedArray(buffer))
            {
                _adler32 = NativeMethods.Adler32(_adler32, pinRead[offset], (uint)readLen);
            }
            return(readLen);
        }
Exemplo n.º 23
0
 // This is fast because it uses pointers
 public void AddFrom(PinnedArray other)
 {
     if (values.Length != other.values.Length)
     {
         throw new ArgumentException("other");
     }
     for (int i = 0; i < values.Length; i++)
     {
         pointer[i] += other.pointer[i];
     }
 }
Exemplo n.º 24
0
        public (TorchTensor values, TorchTensor indexes) Max(long dimension, bool keepDim = false)
        {
            IntPtr[] ptrArray;

            using (var pa = new PinnedArray <IntPtr>())
            {
                THSTensor_max(handle, pa.CreateArray, dimension, keepDim);
                ptrArray = pa.Array;
            }
            return(new TorchTensor(ptrArray[0]), new TorchTensor(ptrArray[1]));
        }
Exemplo n.º 25
0
        void Append(IEnumerable <T> samples)
        {
            int elementsInTrunk = _trunkSize / _elementSize;

            if (elementsInTrunk <= 0)
            {
                elementsInTrunk = 1;
            }

            byte[] byteBuffer = new byte[elementsInTrunk * _elementSize];
            int    index      = 0;

#if NETFX_CORE
            StorageFile storageFile = await StorageFile.GetFileFromPathAsync(_fileInfo);

            using (PinnedArray <T> buffer = new PinnedArray <T>(elementsInTrunk))
                using (Stream bufferStream = await storageFile.OpenStreamForWriteAsync())
                {
                    bufferStream.Seek(0, SeekOrigin.End);
#else
            using (PinnedArray <T> buffer = new PinnedArray <T>(elementsInTrunk))
                using (FileStream stream = _fileInfo.Open(FileMode.Append, FileAccess.Write))
#if !NETSTANDARD1_4
                    using (BufferedStream bufferStream = new BufferedStream(stream, _trunkSize))
#endif
                {
#endif
#if NETSTANDARD1_4
                    var bufferStream = stream;
#endif
                    IntPtr ptr = buffer.AddrOfPinnedObject();
                    foreach (T s in samples)
                    {
                        buffer.Array[index++] = s;

                        if (index == buffer.Array.Length)
                        {
                            int writeCount = index * _elementSize;
                            Marshal.Copy(ptr, byteBuffer, 0, writeCount);
                            bufferStream.Write(byteBuffer, 0, writeCount);
                            index = 0;
                        }
                    }
                    if (index != 0)
                    {
                        int writeCount = index * _elementSize;
                        Marshal.Copy(ptr, byteBuffer, 0, writeCount);
                        bufferStream.Write(byteBuffer, 0, writeCount);
                        //index = 0;
                    }
                }
        }
Exemplo n.º 26
0
        static double[] FFT(WaveFileObject obj, PinnedArray <double> pin, FftwArrayComplex com, FftwPlanRC fft)
        {
            Console.WriteLine("FFT");
            double[] magnitudes;
            Console.WriteLine(obj.soundData.Count);
            double[] input = new double[obj.soundData.Count + 20286];
            Array.Clear(input, 0, input.Length);
            obj.soundData.CopyTo(input, 0);

            switch (obj.header.channels)
            {
            case 1:
                for (int i = 0; i < pin.Length; i++)
                {
                    pin[i] = input[i];
                    //Console.Write(pin[i] + " : ");
                }
                break;

            case 2:
                for (int i = 0; i < pin.Length; i++)
                {
                    pin[i] = input[i + i];
                    //Console.WriteLine(pin[i]);
                }
                break;

            default:
                break;
            }

            fft.Execute();

            magnitudes = new double[com.Length];
            for (int i = 0; i < 4000; i++)
            {
                magnitudes[i] = 10 * Math.Log10((com[i].Magnitude / inputSize) * (com[i].Magnitude / inputSize));

                /*
                 * if (10 * Math.Log10((com[i].Magnitude / inputSize) * (com[i].Magnitude / inputSize)) > 10)
                 * {
                 *  Console.WriteLine("Bin: " + i * sampleRate / com.Length + " " + 10 * Math.Log10((com[i].Magnitude / inputSize) * (com[i].Magnitude / inputSize)));
                 * }
                 */
            }

            Console.WriteLine(com.Length);
            Console.WriteLine();

            Console.WriteLine("Returning magnitudes");
            return(magnitudes);
        }
Exemplo n.º 27
0
        private void btnFindPitch_Click(object sender, EventArgs e)
        {
            System.Numerics.Complex[] input  = new System.Numerics.Complex[4096];
            System.Numerics.Complex[] output = new System.Numerics.Complex[input.Length];
            double biggestPitch         = 0;
            int    biggestPitchLocation = 0;

            byte[] bufferCopy = buffer;

            //convert the byte array to the short array
            for (int i = 1; i < bufferCopy.Length - 1; i += 2)
            {
                input[i / 2] = (short)(Convert.ToUInt16((bufferCopy[i]) | bufferCopy[i + 1] << 8));
            }



            //Do the transformation
            using (var pinIn = new PinnedArray <System.Numerics.Complex>(input))
                using (var pinOut = new PinnedArray <System.Numerics.Complex>(output)) {
                    DFT.FFT(pinIn, pinOut);
                }

            //miss half of the values cuz they repeated
            for (int i = 0; i < output.Length / 2; i++)
            {
                if (output[i].Magnitude > biggestPitch)
                {
                    biggestPitch         = output[i].Magnitude;
                    biggestPitchLocation = i;
                }
            }

            lblBiggestPitch.Text = biggestPitchLocation.ToString();

            //fft(bufferCopy);


            double biggestMag = 0;

            foreach (var currentNum in output)
            {
                if (currentNum.Magnitude > biggestMag)
                {
                    biggestMag = currentNum.Magnitude;
                }
            }

            using (Graphics g = picGraph.CreateGraphics()) {
                g.DrawLine(Pens.Blue, biggestPitchLocation / 2 + 2, 500, biggestPitchLocation / 2 + 2, 500 - Convert.ToSingle(biggestPitch * 500 / biggestMag));
            }
        }
Exemplo n.º 28
0
        public static TorchTensor dstack(this IList <TorchTensor> tensors)
        {
            using (var parray = new PinnedArray <IntPtr>()) {
                IntPtr tensorsRef = parray.CreateArray(tensors.Select(p => p.Handle).ToArray());

                var res = THSTensor_dstack(tensorsRef, parray.Array.Length);
                if (res == IntPtr.Zero)
                {
                    Torch.CheckForErrors();
                }
                return(new TorchTensor(res));
            }
        }
Exemplo n.º 29
0
        public void Test_BakePackedCodepoint()
        {
            #region Init
            string assemblyDir  = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
            string solution_dir = Path.GetDirectoryName(Path.GetDirectoryName(Path.GetDirectoryName(assemblyDir)));
            #endregion

            //Read ttf file into byte array
            byte[] ttfFileContent = File.ReadAllBytes(solution_dir + @"\FontSamples\Windsong.ttf");
            using (var ttf = new PinnedArray <byte>(ttfFileContent))
            {
                //get pointer of the ttf file content
                var ttf_buffer = ttf.Pointer;
                //Initialize fontinfo
                FontInfo font = new FontInfo();
                STBTrueType.InitFont(ref font, ttf_buffer, STBTrueType.GetFontOffsetForIndex(ttf_buffer, 0));
                //set bitmap size
                const int BITMAP_W = 512;
                const int BITMAP_H = 512;
                //allocate bitmap buffer
                byte[] bitmapBuffer = new byte[BITMAP_W * BITMAP_H];
                //Initialize a pack context
                PackContext pc = new PackContext();
                STBTrueType.PackBegin(ref pc, bitmapBuffer, BITMAP_W, BITMAP_H, 0, 1, IntPtr.Zero);
                //allocate packed char buffer
                PackedChar[] pdata = new PackedChar[256 * 2];
                using (var pin_pdata = new PinnedArray <PackedChar>(pdata))
                {
                    //get pointer of the pdata
                    var ptr_pdata = pin_pdata.Pointer;
                    //set pack ranges
                    PackRange[] ranges = new PackRange[2];
                    ranges[0].chardata_for_range          = ptr_pdata;
                    ranges[0].first_unicode_char_in_range = 32;
                    ranges[0].num_chars_in_range          = 95;
                    ranges[0].font_size                   = 20.0f;
                    ranges[1].chardata_for_range          = IntPtr.Add(ptr_pdata, 256 * Marshal.SizeOf(pdata[0]));
                    ranges[1].first_unicode_char_in_range = 0xa0;
                    ranges[1].num_chars_in_range          = 0x100 - 0xa0;
                    ranges[1].font_size                   = 20.0f;
                    //Bake bitmap
                    STBTrueType.PackFontRanges(ref pc, ttf_buffer, 0, ranges, 2);
                    //Clean up
                    STBTrueType.PackEnd(ref pc);
                }
                //output the bitmap to a text file
                WriteBitmapToFileAsText("testOuput.txt", BITMAP_H, BITMAP_W, bitmapBuffer);
                //Open the text file
                OpenFile("testOuput.txt");
            }
        }
Exemplo n.º 30
0
        public void ModMinusOneTest()
        {
            RSAParameters p;
              using (RSA rsa = RSA.Create()) {
            p = rsa.ExportParameters(true);
              }
              PinnedArray<byte> a = new PinnedArray<byte>(p.D);
              PinnedArray<byte> b = new PinnedArray<byte>(p.P);
              byte[] expected = p.DP;

              PinnedArray<byte> actual;
              actual = Util.ModMinusOne(a, b);
              Assert.That(actual.Data, Is.EqualTo(expected));
        }
Exemplo n.º 31
0
        /// <summary>
        /// Calculate the frequency domain of one second of data
        /// </summary>
        /// <param name="values"></param>
        /// <returns></returns>
        public static Span <Complex> CalculateFrequencyDomainOfOneSecond(double[] values)
        {
            Complex[] input  = ConvertToComplex(values);
            Complex[] output = new Complex[input.Length];

            using (var pinIn = new PinnedArray <Complex>(input))
                using (var pinOut = new PinnedArray <Complex>(output))
                {
                    DFT.FFT(pinIn, pinOut); //leave extra threads out as we are already multithreading
                }

            //only half the data is within the frequency domain
            return(output.AsSpan().Slice(0, (input.Length / 2) - 1));
        }
Exemplo n.º 32
0
        public void TestMain()
        {
            var arr = new Point[] { new Point(0, 0), new Point(1, 2) }; //point is struct for (x,y) pair

            var pinnedArray = new PinnedArray<Point>(arr); //data is shared

            unsafe
            {
                Point* pt = (Point*)pinnedArray.Data;
                pt->X = 3;
            }

            Assert.IsTrue(arr[0].X == 3);
        }
Exemplo n.º 33
0
 public FftProcessor(int fftLength)
 {
     this.fftLength = fftLength;
     m             = (int)Math.Log(fftLength, 2.0);
     input         = new Complex[fftLength];
     input2        = new Complex[fftLength];
     output        = new Complex[fftLength];
     pinIn         = new PinnedArray <Complex>(input);
     pinIn2        = new PinnedArray <Complex>(input2);
     pinOut        = new PinnedArray <Complex>(output);
     fftPos        = 0;
     fftPos2       = fftLength / 2;
     spreadBuilder = new SpreadBuilder <float>(fftLength / 2);
 }
Exemplo n.º 34
0
        public override void Serialize(Stream aStream, object aObject)
        {
            /* check for required parameters */
              if (aStream == null) {
            throw new ArgumentNullException("aStream");
              }

              if (aObject == null) {
            throw new ArgumentNullException("aObject");
              }

              PasswordFinder pwFinder = null;
              if (GetPassphraseCallbackMethod != null) {
            pwFinder = new PasswordFinder(GetPassphraseCallbackMethod);
              }
              PinnedArray<char> passphrase = null;
              if (pwFinder != null) {
            passphrase = new PinnedArray<char>(0);
            passphrase.Data = pwFinder.GetPassword();
              }

              byte cipherType;
              if (passphrase == null || passphrase.Data.Length == 0) {
            cipherType = SSH_CIPHER_NONE;
              } else {
            cipherType = SSH_CIPHER_3DES;
              }

              BlobBuilder builder = new BlobBuilder();

              ISshKey sshKey = aObject as ISshKey;
              RsaKeyParameters publicKeyParams = sshKey.GetPublicKeyParameters()
            as RsaKeyParameters;
              RsaPrivateCrtKeyParameters privateKeyParams = sshKey.GetPrivateKeyParameters()
            as RsaPrivateCrtKeyParameters;

              /* writing info headers */
              builder.AddBytes(Encoding.ASCII.GetBytes(FILE_HEADER_LINE + "\n"));
              builder.AddByte(0);          //end of string
              builder.AddByte(cipherType); //cipher
              builder.AddInt(0);           //reserved

              /* writing public key */
              builder.AddInt(sshKey.Size);
              builder.AddSsh1BigIntBlob(publicKeyParams.Modulus);
              builder.AddSsh1BigIntBlob(publicKeyParams.Exponent);
              builder.AddStringBlob(sshKey.Comment);

              /* writing private key */
              BlobBuilder privateKeyBuilder = new BlobBuilder();

              /* adding some control values */
              Random random = new Random();
              byte[] resultCheck = new byte[2];
              random.NextBytes(resultCheck);

              privateKeyBuilder.AddByte(resultCheck[0]);
              privateKeyBuilder.AddByte(resultCheck[1]);
              privateKeyBuilder.AddByte(resultCheck[0]);
              privateKeyBuilder.AddByte(resultCheck[1]);
              privateKeyBuilder.AddSsh1BigIntBlob(privateKeyParams.Exponent);
              privateKeyBuilder.AddSsh1BigIntBlob(privateKeyParams.DQ);
              privateKeyBuilder.AddSsh1BigIntBlob(privateKeyParams.P);
              privateKeyBuilder.AddSsh1BigIntBlob(privateKeyParams.Q);

              if (cipherType == SSH_CIPHER_NONE) {
            /* plain-text */
            builder.AddBytes(privateKeyBuilder.GetBlobAsPinnedByteArray().Data);
              } else {
            byte[] keydata;
            using (MD5 md5 = MD5.Create()) {
              keydata = md5.ComputeHash(Encoding.ASCII.GetBytes(passphrase.Data));
            }

            /* encryption */
            DesSsh1Engine desEngine = new DesSsh1Engine();
            desEngine.Init(true, new KeyParameter(keydata));

            BufferedBlockCipher bufferedBlockCipher = new BufferedBlockCipher(desEngine);
            byte[] ouputBuffer = bufferedBlockCipher.ProcessBytes(
              privateKeyBuilder.GetBlobAsPinnedByteArray().Data);

            builder.AddBytes(ouputBuffer);

            passphrase.Dispose();
              }

              /* writing result to file */
              var builderOutput = builder.GetBlobAsPinnedByteArray();
              aStream.Write(builderOutput.Data, 0, builderOutput.Data.Length);
              aStream.Close();
        }
Exemplo n.º 35
0
        /****************************************************************************
        * Stream Data Handler
        * - Used by the two stream data examples - untriggered and triggered
        * Inputs:
        * - preTrigger - the number of samples in the pre-trigger phase
        *					(0 if no trigger has been set)
        ***************************************************************************/
        void StreamDataHandler(uint preTrigger)
        {
            uint sampleCount = BUFFER_SIZE * 10; /*  *10 is to make sure buffer large enough */
            short[][] minBuffers = new short[_channelCount][];
            short[][] maxBuffers = new short[_channelCount][];
            PinnedArray<short>[] minPinned = new PinnedArray<short>[_channelCount];
            PinnedArray<short>[] maxPinned = new PinnedArray<short>[_channelCount];
            uint totalsamples = 0;
            uint triggeredAt = 0;
            short status;

            uint sampleInterval = 1;

            for (int i = 0; i < _channelCount; i++) // create data buffers
            {
                minBuffers[i] = new short[sampleCount];
                maxBuffers[i] = new short[sampleCount];
                minPinned[i] = new PinnedArray<short>(minBuffers[i]);
                maxPinned[i] = new PinnedArray<short>(maxBuffers[i]);
                status = Imports.SetDataBuffers(_handle, (Imports.Channel)i, minBuffers[i], maxBuffers[i], (int)sampleCount);
            }

            Console.WriteLine("Waiting for trigger...Press a key to abort");
            _autoStop = false;
            status = Imports.RunStreaming(_handle, ref sampleInterval, Imports.ReportedTimeUnits.MicroSeconds,
                                        preTrigger, 1000000 - preTrigger, true, 1000, sampleCount);
            Console.WriteLine("Run Streaming : {0} ", status);

            Console.WriteLine("Streaming data...Press a key to abort");

            TextWriter writer = new StreamWriter("stream.txt", false);
            writer.Write("For each of the {0} Channels, results shown are....", _channelCount);
            writer.WriteLine();
            writer.WriteLine("Maximum Aggregated value ADC Count & mV, Minimum Aggregated value ADC Count & mV");
            writer.WriteLine();

            for (int i = 0; i < _channelCount; i++)
                writer.Write("Ch  Max ADC    Max mV   Min ADC    Min mV   ");
            writer.WriteLine();

            while (!_autoStop && !Console.KeyAvailable)
            {
                /* Poll until data is received. Until then, GetStreamingLatestValues wont call the callback */
                Thread.Sleep(100);
                _ready = false;
                Imports.GetStreamingLatestValues(_handle, StreamingCallback, IntPtr.Zero);

                if (_ready && _sampleCount > 0) /* can be ready and have no data, if autoStop has fired */
                {
                    if (_trig > 0)
                        triggeredAt = totalsamples + _trigAt;
                    totalsamples += (uint)_sampleCount;

                    Console.Write("\nCollected {0,4} samples, index = {1,5} Total = {2,5}", _sampleCount, _startIndex, totalsamples);

                    if (_trig > 0)
                        Console.Write("\tTrig at Index {0}", triggeredAt);

                    for (uint i = _startIndex; i < (_startIndex + _sampleCount); i++)
                    {
                        for (int ch = 0; ch < _channelCount; ch++)
                        {
                            if (_channelSettings[ch].enabled)
                            {
                                writer.Write("Ch{0} {1,7}   {2,7}   {3,7}   {4,7}   ",
                                                     (char)('A' + ch),
                                                     minPinned[ch].Target[i],
                                                     adc_to_mv(minPinned[ch].Target[i], (int)_channelSettings[(int)(Imports.Channel.ChannelA + ch)].range),
                                                     maxPinned[ch].Target[i],
                                                     adc_to_mv(maxPinned[ch].Target[i], (int)_channelSettings[(int)(Imports.Channel.ChannelA + ch)].range));
                            }
                        }
                        writer.WriteLine();
                    }
                }
            }
            if (Console.KeyAvailable) Console.ReadKey(true); // clear the key

            Imports.Stop(_handle);
            writer.Close();

            if (!_autoStop)
            {
                Console.WriteLine("\ndata collection aborted");
            }

            foreach (PinnedArray<short> p in minPinned)
            {
                if (p != null)
                    p.Dispose();
            }
            foreach (PinnedArray<short> p in maxPinned)
            {
                if (p != null)
                    p.Dispose();
            }
        }
Exemplo n.º 36
0
        /****************************************************************************
         * RapidBlockDataHandler
         * - Used by all the CollectBlockRapid routine
         * - acquires data (user sets trigger mode before calling), displays 10 items
         *   and saves all to data.txt
         * Input :
         * - nRapidCaptures : the user specified number of blocks to capture
         ****************************************************************************/
        private void RapidBlockDataHandler(ushort nRapidCaptures)
        {
            short status;
            int numChannels = _channelCount;
            uint numSamples = BUFFER_SIZE;

            // Run the rapid block capture
            int timeIndisposed;
            _ready = false;

            _callbackDelegate = BlockCallback;
            Imports.RunBlock(_handle,
                        0,
                        (int)numSamples,
                        _timebase,
                        _oversample,
                        out timeIndisposed,
                        0,
                        _callbackDelegate,
                        IntPtr.Zero);

            Console.WriteLine("Waiting for data...Press a key to abort");

            while (!_ready && !Console.KeyAvailable)
            {
                Thread.Sleep(100);
            }
            if (Console.KeyAvailable) Console.ReadKey(true); // clear the key

            Imports.Stop(_handle);

            // Set up the data arrays and pin them
            short[][][] values = new short[nRapidCaptures][][];
            PinnedArray<short>[,] pinned = new PinnedArray<short>[nRapidCaptures, numChannels];

            for (ushort segment = 0; segment < nRapidCaptures; segment++)
            {
                values[segment] = new short[numChannels][];
                for (short channel = 0; channel < numChannels; channel++)
                {
                    if (_channelSettings[channel].enabled)
                    {
                        values[segment][channel] = new short[numSamples];
                        pinned[segment, channel] = new PinnedArray<short>(values[segment][channel]);

                        status = Imports.SetDataBuffersRapid(_handle,
                                               (Imports.Channel)channel,
                                               values[segment][channel],
                                               (int)numSamples,
                                               segment);
                    }
                    else
                    {
                        status = Imports.SetDataBuffersRapid(_handle,
                                   (Imports.Channel)channel,
                                    null,
                                    0,
                                    segment);

                    }
                }
            }

            // Read the data
            short[] overflows = new short[nRapidCaptures];

            status = Imports.GetValuesRapid(_handle, ref numSamples, 0, (ushort)(nRapidCaptures - 1), overflows);

            /* Print out the first 10 readings, converting the readings to mV if required */
            Console.WriteLine("\nValues in {0}", (_scaleVoltages) ? ("mV") : ("ADC Counts"));

            for (int seg = 0; seg < nRapidCaptures; seg++)
            {
                Console.WriteLine("Capture {0}", seg);
                for (int i = 0; i < 10; i++)
                {
                    for (int chan = 0; chan < _channelCount; chan++)
                    {
                        Console.Write("{0}\t", _scaleVoltages ?
                                                adc_to_mv(pinned[seg, chan].Target[i], (int)_channelSettings[(int)(Imports.Channel.ChannelA + chan)].range) // If _scaleVoltages, show mV values
                                                : pinned[seg, chan].Target[i]);                                                                             // else show ADC counts
                    }
                    Console.WriteLine();
                }
                Console.WriteLine();
            }

            // Un-pin the arrays
            foreach (PinnedArray<short> p in pinned)
            {
                if (p != null)
                    p.Dispose();
            }

            //TODO: Do what ever is required with the data here.
        }
Exemplo n.º 37
0
        /****************************************************************************
         * BlockDataHandler
         * - Used by all block data routines
         * - acquires data (user sets trigger mode before calling), displays 10 items
         *   and saves all to data.txt
         * Input :
         * - unit : the unit to use.
         * - text : the text to display before the display of data slice
         * - offset : the offset into the data buffer to start the display's slice.
         ****************************************************************************/
        void BlockDataHandler(string text, int offset)
        {
            uint sampleCount = BUFFER_SIZE;
            PinnedArray<short>[] minPinned = new PinnedArray<short>[_channelCount];
            PinnedArray<short>[] maxPinned = new PinnedArray<short>[_channelCount];

            int timeIndisposed;

            for (int i = 0; i < _channelCount; i++)
            {
                short[] minBuffers = new short[sampleCount];
                short[] maxBuffers = new short[sampleCount];
                minPinned[i] = new PinnedArray<short>(minBuffers);
                maxPinned[i] = new PinnedArray<short>(maxBuffers);
                int status = Imports.SetDataBuffers(_handle, (Imports.Channel)i, maxBuffers, minBuffers, (int)sampleCount);
            }

            /*  find the maximum number of samples, the time interval (in timeUnits),
             *		 the most suitable time units, and the maximum _oversample at the current _timebase*/
            int timeInterval;
            int maxSamples;
            while (Imports.GetTimebase(_handle, _timebase, (int)sampleCount, out timeInterval, _oversample, out maxSamples, 0) != 0)
            {
                Console.WriteLine("Selected timebase {0} could not be used\n", _timebase);
                _timebase++;

            }
            Console.WriteLine("Timebase: {0}\toversample:{1}\n", _timebase, _oversample);

            /* Start it collecting, then wait for completion*/
            _ready = false;
            _callbackDelegate = BlockCallback;
            Imports.RunBlock(_handle, 0, (int)sampleCount, _timebase, _oversample, out timeIndisposed, 0, _callbackDelegate, IntPtr.Zero);

            /*Console.WriteLine("Run Block : {0}", status);*/
            Console.WriteLine("Waiting for data...Press a key to abort");

            while (!_ready && !Console.KeyAvailable)
            {
                Thread.Sleep(100);
            }
            if (Console.KeyAvailable) Console.ReadKey(true); // clear the key

            Imports.Stop(_handle);

            if (_ready)
            {
                short overflow;
                Imports.GetValues(_handle, 0, ref sampleCount, 1, Imports.DownSamplingMode.None, 0, out overflow);

                /* Print out the first 10 readings, converting the readings to mV if required */
                Console.WriteLine(text);
                Console.WriteLine("Value {0}", (_scaleVoltages) ? ("mV") : ("ADC Counts"));

                for (int ch = 0; ch < _channelCount; ch++)
                {
                    Console.Write("   Ch{0}    ", (char)('A' + ch));
                }
                Console.WriteLine();

                for (int i = offset; i < offset + 10; i++)
                {
                    for (int ch = 0; ch < _channelCount; ch++)
                    {
                        if (_channelSettings[ch].enabled)
                        {
                            Console.Write("{0,6}    ", _scaleVoltages ?
                                adc_to_mv(maxPinned[ch].Target[i], (int)_channelSettings[(int)(Imports.Channel.ChannelA + ch)].range)  // If _scaleVoltages, show mV values
                                : maxPinned[ch].Target[i]);                                                                           // else show ADC counts
                        }
                    }
                    Console.WriteLine();
                }

                sampleCount = Math.Min(sampleCount, BUFFER_SIZE);
                TextWriter writer = new StreamWriter("block.txt", false);
                writer.Write("For each of the {0} Channels, results shown are....", _channelCount);
                writer.WriteLine();
                writer.WriteLine("Time interval Maximum Aggregated value ADC Count & mV, Minimum Aggregated value ADC Count & mV");
                writer.WriteLine();

                for (int i = 0; i < _channelCount; i++)
                    writer.Write("Time  Ch  Max ADC    Max mV   Min ADC    Min mV   ");
                writer.WriteLine();

                for (int i = 0; i < sampleCount; i++)
                {
                    for (int ch = 0; ch < _channelCount; ch++)
                    {
                        writer.Write("{0,4}  ", (i * timeInterval));
                        if (_channelSettings[ch].enabled)
                        {
                            writer.Write("Ch{0} {1,7}   {2,7}   {3,7}   {4,7}   ",
                                           (char)('A' + ch),
                                           maxPinned[ch].Target[i],
                                           adc_to_mv(maxPinned[ch].Target[i],
                                                     (int)_channelSettings[(int)(Imports.Channel.ChannelA + ch)].range),
                                           minPinned[ch].Target[i],
                                           adc_to_mv(minPinned[ch].Target[i],
                                                     (int)_channelSettings[(int)(Imports.Channel.ChannelA + ch)].range));
                        }
                    }
                    writer.WriteLine();
                }
                writer.Close();
            }
            else
            {
                Console.WriteLine("data collection aborted");
                WaitForKey();
            }

            foreach (PinnedArray<short> p in minPinned)
            {
                if (p != null)
                    p.Dispose();
            }
            foreach (PinnedArray<short> p in maxPinned)
            {
                if (p != null)
                    p.Dispose();
            }
        }
Exemplo n.º 38
0
        /****************************************************************************
         * RapidBlockDataHandler
         * - Used by all the CollectBlockRapid routine
         * - acquires data (user sets trigger mode before calling), displays 10 items
         * Input :
         * - nRapidCaptures : the user specified number of blocks to capture
         ****************************************************************************/
        private void RapidBlockDataHandler(uint nRapidCaptures)
        {
            short status;
            int numChannels = _channelCount;
            uint numSamples = BUFFER_SIZE;

            // Run the rapid block capture
            int timeIndisposed;
            _ready = false;

            //  find the maximum number of samples, the time interval (in timeUnits),
              	//	 the most suitable time units, and the maximum _oversample at the current _timebase
            int timeInterval;
            int maxSamples;
            while (Imports.GetTimebase(_handle, _timebase, (int)numSamples, out timeInterval, _oversample, out maxSamples, 0) != 0)
            {
            _timebase++;
            }
            Console.WriteLine("Timebase: {0}\toversample:{1}", _timebase, _oversample);

            _callbackDelegate = BlockCallback;
            Imports.RunBlock(_handle,
                    0,
                    (int)numSamples,
                    _timebase,
                    _oversample,
                    out timeIndisposed,
                    0,
                    _callbackDelegate,
                    IntPtr.Zero);

            Console.WriteLine("Waiting for data...Press a key to abort");

            while (!_ready && !Console.KeyAvailable)
            {
            Thread.Sleep(100);
            }
            if (Console.KeyAvailable) Console.ReadKey(true); // clear the key

            Imports.Stop(_handle);

            // Set up the data arrays and pin them
            short[][][] values = new short[nRapidCaptures][][];
            PinnedArray<short>[,] pinned = new PinnedArray<short>[nRapidCaptures, numChannels];

            for (ushort segment = 0; segment < nRapidCaptures; segment++)
            {
            values[segment] = new short[numChannels][];
            for (short channel = 0; channel < numChannels; channel++)
            {
                if (_channelSettings[channel].enabled)
                {
                    values[segment][channel] = new short[numSamples];
                    pinned[segment, channel] = new PinnedArray<short>(values[segment][channel]);

                    status = Imports.SetDataBuffersRapid(_handle,
                                           (Imports.Channel)channel,
                                           values[segment][channel],
                                           (int)numSamples,
                                           segment,
                                           Imports.PS6000DownSampleRatioMode.PS6000_RATIO_MODE_NONE);
                }
                else
                {
                    status = Imports.SetDataBuffersRapid(_handle,
                               (Imports.Channel)channel,
                                null,
                                0,
                                segment,
                                Imports.PS6000DownSampleRatioMode.PS6000_RATIO_MODE_NONE);

                }
            }
            }

            // Read the data
            short[] overflows = new short[nRapidCaptures];

            status = Imports.GetValuesRapid(_handle, ref numSamples, 0, nRapidCaptures - 1, 1, Imports.PS6000DownSampleRatioMode.PS6000_RATIO_MODE_NONE, overflows);

            /* Print out the first 10 readings, converting the readings to mV if required */
            Console.WriteLine("\nValues in {0}", (_scaleVoltages) ? ("mV") : ("ADC Counts"));

            for (int seg = 0; seg < nRapidCaptures; seg++)
            {
            Console.WriteLine("Capture {0}", seg);

            for (int ch = 0; ch < _channelCount; ch++)
            {
                if (_channelSettings[ch].enabled)
                    Console.Write("  Ch{0}   ", (char)('A' + ch));
            }
            Console.WriteLine();

            for (int i = 0; i < 10; i++)
            {
                for (int ch = 0; ch < _channelCount; ch++)
                {
                    if (_channelSettings[ch].enabled)
                    {
                        Console.Write("{0,6}\t", _scaleVoltages ?
                                            adc_to_mv(pinned[seg, ch].Target[i], (int)_channelSettings[(int)(Imports.Channel.ChannelA + ch)].range) // If _scaleVoltages, show mV values
                                            : pinned[seg, ch].Target[i]);                                                                             // else show ADC counts
                    }
                }
                Console.WriteLine();
            }
            Console.WriteLine();
            }

            // Un-pin the arrays
            foreach (PinnedArray<short> p in pinned)
            {
            if (p != null)
                p.Dispose();
            }
        }
Exemplo n.º 39
0
        public bool ContentsEqualTo(SecureEdit aSecureEdit)
        {
            Debug.Assert(aSecureEdit != null);
              if (aSecureEdit == null) {
            return false;
              }

              using (var thisString = new PinnedArray<byte>(this.ToUtf8())) {
            using (var otherString = new PinnedArray<byte>(aSecureEdit.ToUtf8())) {

              if (thisString.Data.Length != otherString.Data.Length) {
            return false;
              } else {
            for (int i = 0; i < thisString.Data.Length; ++i) {
              if (thisString.Data[i] != otherString.Data [i]) {
                return false;
              }
            }
              }
              return true;
            }
              }
        }
Exemplo n.º 40
0
        public void Run()
        {
            osc = new OSC("127.0.0.1", 5500);

            // setup devices
            _channelSettings = new ChannelSettings[MAX_CHANNELS];
            for (int i = 0; i < MAX_CHANNELS; i++)
            {
                _channelSettings[i].enabled = (i < ENABLED_CHANNELS);
                _channelSettings[i].DCcoupled = Imports.PS6000Coupling.PS6000_DC_1M;
                _channelSettings[i].range = VOLTAGE_RANGE;
            }
            SetDefaults();

            Console.WriteLine("Press a key to start.");
            WaitForKey();

            // Allocate storage for buffers
            uint sampleCount = BUFFER_SIZE * 100; /*  *100 is to make sure buffer large enough */
            for (int i = 0; i < ENABLED_CHANNELS; i++) // create data buffers
            {
                minBuffers[i] = new short[sampleCount];
                maxBuffers[i] = new short[sampleCount];
                minPinned[i] = new PinnedArray<short>(minBuffers[i]);
                maxPinned[i] = new PinnedArray<short>(maxBuffers[i]);
                Imports.SetDataBuffers(
                    _handle,
                    (Imports.Channel)i,
                    minBuffers[i],
                    maxBuffers[i],
                    (int)sampleCount,
                    Imports.PS6000DownSampleRatioMode.PS6000_RATIO_MODE_AGGREGATE
                );
            }

            // main loop - read key and call routine
            char ch = ' ';
            while (ch != 'X')
            {
                BlockDataHandler();
                if (Console.KeyAvailable)
                    ch = char.ToUpper(Console.ReadKey(true).KeyChar);
            }

            // Deallocate storage for buffers
            foreach (PinnedArray<short> p in minPinned)
            {
                if (p != null)
                    p.Dispose();
            }
            foreach (PinnedArray<short> p in maxPinned)
            {
                if (p != null)
                    p.Dispose();
            }
        }
Exemplo n.º 41
0
        private string GetAsString()
        {
            if (mSecureString != null) {
            IntPtr p = Marshal.SecureStringToGlobalAllocUnicode(mSecureString);
            string str = Marshal.PtrToStringUni(p);
            Marshal.ZeroFreeGlobalAllocUnicode(p);

            return str;
              } else {
            using (var bytes = new PinnedArray<byte>(
              Encoding.Unicode.GetBytes(mAltSecureString.Data)))
            {
              return Encoding.Unicode.GetString(bytes.Data);
            }
              }
        }
Exemplo n.º 42
0
        private void RemoveInsert(int aLeftRemainingCount,
                              int aRigthRemainingCount,
                              string aStringToInsert)
        {
            Debug.Assert(aLeftRemainingCount >= 0);

              if (mSecureString != null) {
            while (mSecureString.Length > (aLeftRemainingCount +
                                       aRigthRemainingCount))
            {
              mSecureString.RemoveAt(aLeftRemainingCount);
            }
            for (int i = 0; i < aStringToInsert.Length; ++i) {
              mSecureString.InsertAt(aLeftRemainingCount + i, aStringToInsert[i]);
            }
              } else {
            using (var newString =
              new PinnedArray<char>(aLeftRemainingCount + aRigthRemainingCount +
                                aStringToInsert.Length)) {
              Array.Copy(mAltSecureString.Data, newString.Data, aLeftRemainingCount);
              using (var insertString =
                 new PinnedArray<char>(Encoding.Unicode.GetChars(
                   Encoding.Unicode.GetBytes(aStringToInsert)))) {
            Array.Copy(insertString.Data, 0, newString.Data, aLeftRemainingCount,
                       insertString.Data.Length);
            Array.Copy(mAltSecureString.Data,
                       mAltSecureString.Data.Length - aRigthRemainingCount,
                       newString.Data, aLeftRemainingCount +
                       insertString.Data.Length,
                       aRigthRemainingCount);
              }
              mAltSecureString.Clear();
              mAltSecureString = newString.Clone() as PinnedArray<char>;
            }
              }
        }
Exemplo n.º 43
0
        /// <summary>
        /// Computes a % (b -1) of 2 large numbers
        /// </summary>
        /// <param name="a">variable a</param>
        /// <param name="b">variable b</param>
        /// <returns></returns>
        public static PinnedArray<byte> ModMinusOne(PinnedArray<byte> a,
                                              PinnedArray<byte> b)
        {
            using (PinnedArray<byte> bMinusOne = (PinnedArray<byte>)b.Clone()) {

            PinnedArray<byte> result = (PinnedArray<byte>)a.Clone();
            // shouldn't have to worry about borrowing because b should be prime and
            // therefore not end in zero
            bMinusOne.Data[bMinusOne.Data.Length - 1]--;
            int bShift = a.Data.Length - b.Data.Length;

            while (bShift >= 0) {
              while (CompareBigInt(result.Data, bMinusOne.Data, bShift) >= 0) {
            result.Data = SubtractBigInt(result.Data, bMinusOne.Data, bShift);
            TrimLeadingZero(result);
              }
              bShift--;
            }

            return result;
              }
        }
Exemplo n.º 44
0
 private void mAddButton_Clicked()
 {
     // TODO - persist start directory (and possibly viewMode)
       using (var dialog = new KeyFileDialog()) {
     dialog.SetDirectory (Environment.GetFolderPath(
                      Environment.SpecialFolder.Personal));
     dialog.Exec ();
     if (dialog.Result  == (int)QDialog.DialogCode.Accepted) {
       var constraints = dialog.GetConstraints ();
       foreach (var file in dialog.SelectedFiles) {
     try {
       KeyFormatter.GetPassphraseCallback passphraseCallback = () =>
       {
         var passphraseDialog = new PassphraseDialog();
         passphraseDialog.Exec();
         if (passphraseDialog.Result == (int)QDialog.DialogCode.Rejected) {
           return null;
         }
         using (var passphrase =
                new PinnedArray<byte>(passphraseDialog.GetPassphrase()))
         {
           var securePassphrase = new SecureString();
           foreach (var b in passphrase.Data) {
             securePassphrase.AppendChar((char)b);
           }
           return securePassphrase;
         }
       };
       mAgent.AddKeyFromFile (file, passphraseCallback, constraints);
     } catch (AgentFailureException) {
       QMessageBox.Critical (this,
                             Tr("Agent Failure"),
                             Tr("Possible causes:") +
                             "<ul>" + "</li>" +
                             "<li>" + Tr("Agent is locked") + "</li>" +
                             "<li>" + Tr("Agent does not support this key type") +
                             "</ul>");
     } catch (KeyFormatterException) {
       QMessageBox.Critical (this,
                             Tr("File format error"),
                             Tr("This file not a recognized private key file") +
                             "<br><br>" +
                             file);
     } catch (Exception ex) {
       Debug.Fail (ex.ToString ());
     }
       }
     }
       }
       if (mAgent is Agent) {
     UpdateUIState ();
       } else {
     ReloadData ();
       }
 }
Exemplo n.º 45
0
        private static void VerifyIntegrity(FileData fileData)
        {
            BlobBuilder builder = new BlobBuilder();
              if (fileData.ppkFileVersion != Version.V1) {
            builder.AddStringBlob(fileData.publicKeyAlgorithm.GetIdentifierString());
            builder.AddStringBlob(fileData.privateKeyAlgorithm.GetIdentifierString());
            builder.AddBlob(Encoding.GetEncoding(1252).GetBytes(fileData.comment));
            builder.AddBlob(fileData.publicKeyBlob);
            builder.AddInt(fileData.privateKeyBlob.Data.Length);
              }
              builder.AddBytes(fileData.privateKeyBlob.Data);

              byte[] computedHash;
              SHA1 sha = SHA1.Create();
              if (fileData.isHMAC) {
            HMAC hmac = HMACSHA1.Create();
            if (fileData.passphrase != null) {
              using (PinnedArray<byte> hashData =
                 new PinnedArray<byte>(cMACKeySalt.Length +
                   fileData.passphrase.Length)) {
            Array.Copy(Encoding.UTF8.GetBytes(cMACKeySalt),
                       hashData.Data, cMACKeySalt.Length);
            IntPtr passphrasePtr =
              Marshal.SecureStringToGlobalAllocUnicode(fileData.passphrase);
            for (int i = 0; i < fileData.passphrase.Length; i++) {
              int unicodeChar = Marshal.ReadInt16(passphrasePtr + i * 2);
              byte ansiChar = Util.UnicodeToAnsi(unicodeChar);
              hashData.Data[cMACKeySalt.Length + i] = ansiChar;
              Marshal.WriteByte(passphrasePtr, i * 2, 0);
            }
            Marshal.ZeroFreeGlobalAllocUnicode(passphrasePtr);
            hmac.Key = sha.ComputeHash(hashData.Data);
              }
            } else {
              hmac.Key = sha.ComputeHash(Encoding.UTF8.GetBytes(cMACKeySalt));
            }
            computedHash = hmac.ComputeHash(builder.GetBlob());
            hmac.Clear();
              } else {
            computedHash = sha.ComputeHash(builder.GetBlob());
              }
              sha.Clear();
              builder.Clear();

              try {
            int macLength = computedHash.Length;
            bool failed = false;
            if (fileData.privateMAC.Length == macLength) {
              for (int i = 0; i < macLength; i++) {
            if (fileData.privateMAC[i] != computedHash[i]) {
              failed = true;
              break;
            }
              }
            } else {
              failed = true;
            }
            if (failed) {
              // private key data should start with 3 bytes with value 0 if it was
              // properly decrypted or does not require decryption
              if ((fileData.privateKeyBlob.Data[0] == 0) &&
              (fileData.privateKeyBlob.Data[1] == 0) &&
              (fileData.privateKeyBlob.Data[2] == 0)) {
            // so if they bytes are there, passphrase decrypted properly and
            // something else is wrong with the file contents
            throw new PpkFormatterException(PpkFormatterException.PpkErrorType.FileCorrupt);
              } else {
            // if the bytes are not zeros, we assume that the data was not
            // properly decrypted because the passphrase was incorrect.
            throw new PpkFormatterException(PpkFormatterException.PpkErrorType.BadPassphrase);
              }
            }
              } catch {
            throw;
              } finally {
            Array.Clear(computedHash, 0, computedHash.Length);
              }
        }
Exemplo n.º 46
0
        private static void DecryptPrivateKey(ref FileData fileData)
        {
            switch (fileData.privateKeyAlgorithm) {

            case PrivateKeyAlgorithm.None:
              return;

            case PrivateKeyAlgorithm.AES256_CBC:

              /* create key from passphrase */

              SHA1 sha = SHA1.Create();
              sha.Initialize();
              List<byte> key = new List<byte>();

              using (PinnedArray<byte> hashData =
                 new PinnedArray<byte>(cPrivateKeyDecryptSalt1.Length +
                                     fileData.passphrase.Length)) {
            Array.Copy(Encoding.UTF8.GetBytes(cPrivateKeyDecryptSalt1),
                       hashData.Data, cPrivateKeyDecryptSalt1.Length);
            IntPtr passphrasePtr =
              Marshal.SecureStringToGlobalAllocUnicode(fileData.passphrase);
            for (int i = 0; i < fileData.passphrase.Length; i++) {
              int unicodeChar = Marshal.ReadInt16(passphrasePtr + i * 2);
              byte ansiChar = Util.UnicodeToAnsi(unicodeChar);
              hashData.Data[cPrivateKeyDecryptSalt1.Length + i] = ansiChar;
              Marshal.WriteByte(passphrasePtr, i, 0);
            }
            Marshal.ZeroFreeGlobalAllocUnicode(passphrasePtr);
            sha.ComputeHash(hashData.Data);
            key.AddRange(sha.Hash);
            Array.Copy(Encoding.UTF8.GetBytes(cPrivateKeyDecryptSalt2),
                       hashData.Data, cPrivateKeyDecryptSalt2.Length);
            sha.ComputeHash(hashData.Data);
            key.AddRange(sha.Hash);
              }
              sha.Clear();
              /* decrypt private key */

              Aes aes = Aes.Create();
              aes.KeySize = 256;
              aes.Mode = CipherMode.CBC;
              aes.Padding = PaddingMode.None;
              int keySize = aes.KeySize / 8; // convert bits to bytes
              key.RemoveRange(keySize, key.Count - keySize); // remove extra bytes
              aes.Key = key.ToArray();
              Util.ClearByteList(key);
              aes.IV = new byte[aes.IV.Length];
              ICryptoTransform decryptor = aes.CreateDecryptor();
              fileData.privateKeyBlob.Data =
            Util.GenericTransform(decryptor, fileData.privateKeyBlob.Data);
              decryptor.Dispose();
              aes.Clear();
              break;

            default:
              throw new PpkFormatterException(PpkFormatterException.PpkErrorType.PrivateKeyEncryption);
              }
        }
Exemplo n.º 47
0
 public override void Serialize(Stream aStream, object aObject)
 {
     /* check for required parameters */
       if (aStream == null) {
     throw new ArgumentNullException("aStream");
       }
       if (aObject == null) {
     throw new ArgumentNullException("aObject");
       }
       PasswordFinder pwFinder = null;
       if (GetPassphraseCallbackMethod != null) {
     pwFinder = new PasswordFinder(GetPassphraseCallbackMethod);
       }
       StreamWriter streamWriter = new StreamWriter(aStream);
       PemWriter writer = new PemWriter(streamWriter);
       PinnedArray<char> passphrase = null;
       if (pwFinder != null) {
     passphrase = new PinnedArray<char>(0);
     passphrase.Data = pwFinder.GetPassword();
       }
       if (passphrase == null) {
     writer.WriteObject(aObject);
       } else {
     writer.WriteObject(aObject, null, passphrase.Data, null);
     passphrase.Dispose();
       }
 }
Exemplo n.º 48
0
 /// <summary>
 /// removes leading element from array if the value of that element is 0
 /// </summary>
 /// <param name="array"></param>
 public static void TrimLeadingZero(PinnedArray<byte> array)
 {
     if (array != null && array.Data != null && array.Data.Length > 0) {
     if (array.Data[0] == 0) {
       PinnedArray<byte> arrayCopy = (PinnedArray<byte>)array.Clone();
       array.Data = new byte[array.Data.Length - 1];
       Array.Copy(arrayCopy.Data, 1, array.Data, 0, array.Data.Length);
       arrayCopy.Dispose();
     }
       }
 }
Exemplo n.º 49
0
        /// <summary>
        /// Answers the message.
        /// </summary>
        /// <param name='messageStream'>Message stream.</param>
        /// <param name="process">The calling process or <c>null</c> if the process
        /// could not be obtained.</param>
        /// <remarks>code based on winpgnt.c from PuTTY source code</remarks>
        public void AnswerMessage(Stream messageStream, Process process = null)
        {
            if (messageStream.CanTimeout) {
            messageStream.ReadTimeout = 5000;
              }
              var messageParser = new BlobParser(messageStream);
              var responseBuilder = new BlobBuilder();
              BlobHeader header;
              try {
            header = messageParser.ReadHeader();

            if (MessageReceived != null) {
              var eventArgs = new MessageReceivedEventArgs(header);
              MessageReceived(this, eventArgs);
              if (eventArgs.Fail) {
            throw new Exception ();
              }
            }
              } catch (Exception) {
            header = new BlobHeader();
            header.Message = Message.UNKNOWN;
            // this will cause the switch statement below to use the default case
            // which returns an error to the stream.
              }

              switch (header.Message) {
            case Message.SSH1_AGENTC_REQUEST_RSA_IDENTITIES:
              /*
               * Reply with SSH1_AGENT_RSA_IDENTITIES_ANSWER.
               */
              try {
            if (header.BlobLength > 1) {
              // ruby net-ssh tries to send a SSH2_AGENT_REQUEST_VERSION message
              // which has the same id number as SSH1_AGENTC_REQUEST_RSA_IDENTITIES
              // with a string tacked on. We need to read the string from the
              // stream, but it is not used for anything.
              messageParser.ReadString ();
            }
            var keyList = ListKeys(SshVersion.SSH1);
            if (FilterKeyListCallback != null) {
              keyList = FilterKeyListCallback(keyList);
            }
            foreach (SshKey key in keyList) {
              responseBuilder.AddBytes(key.GetPublicKeyBlob());
              responseBuilder.AddStringBlob(key.Comment);
            }
            responseBuilder.InsertHeader(Message.SSH1_AGENT_RSA_IDENTITIES_ANSWER,
              keyList.Count);
            // TODO may want to check that there is enough room in the message stream
            break; // succeeded
              } catch (Exception ex) {
            Debug.Fail(ex.ToString());
              }
              goto default; // failed

            case Message.SSH2_AGENTC_REQUEST_IDENTITIES:
              /*
               * Reply with SSH2_AGENT_IDENTITIES_ANSWER.
               */
              try {
            var keyList = ListKeys(SshVersion.SSH2);
            if (FilterKeyListCallback != null) {
              keyList = FilterKeyListCallback(keyList);
            }
            foreach (SshKey key in keyList) {
              responseBuilder.AddBlob(key.GetPublicKeyBlob());
              responseBuilder.AddStringBlob(key.Comment);
            }
            responseBuilder.InsertHeader(Message.SSH2_AGENT_IDENTITIES_ANSWER,
              keyList.Count);
            // TODO may want to check that there is enough room in the message stream
            break; // succeeded
              } catch (Exception ex) {
            Debug.Fail(ex.ToString());
              }
              goto default; // failed

            case Message.SSH1_AGENTC_RSA_CHALLENGE:
              /*
               * Reply with either SSH1_AGENT_RSA_RESPONSE or
               * SSH_AGENT_FAILURE, depending on whether we have that key
               * or not.
               */

              try {
            //Reading publicKey information
            var publicKeyParams = messageParser.ReadSsh1PublicKeyData(true);

            //Searching for Key here
            var matchingKey = mKeyList.Where(key => key.Version == SshVersion.SSH1
                && (key.GetPublicKeyParameters().Equals(publicKeyParams))).Single();

            //Reading challenge
            var encryptedChallenge = messageParser.ReadSsh1BigIntBlob();
            var sessionId = messageParser.ReadBytes(16);

            //Checking responseType field
            if (messageParser.ReadInt() != 1) {
              goto default; //responseType !=1  is not longer supported
            }

            //Answering to the challenge
            var engine = new Pkcs1Encoding(new RsaEngine());
            engine.Init(false /* decrypt */, matchingKey.GetPrivateKeyParameters());

            var decryptedChallenge = engine.ProcessBlock(encryptedChallenge,
                0, encryptedChallenge.Length);

            using (MD5 md5 = MD5.Create()) {
              var md5Buffer = new byte[48];
              decryptedChallenge.CopyTo(md5Buffer, 0);
              sessionId.CopyTo(md5Buffer, 32);

              responseBuilder.AddBytes(md5.ComputeHash(md5Buffer));
              responseBuilder.InsertHeader(Message.SSH1_AGENT_RSA_RESPONSE);
              break;
            }
              } catch (InvalidOperationException) {
            // this is expected if there is not a matching key
              } catch (Exception ex) {
            Debug.Fail(ex.ToString());
              }

              goto default; // failed

            case Message.SSH2_AGENTC_SIGN_REQUEST:
              /*
               * Reply with either SSH2_AGENT_SIGN_RESPONSE or SSH_AGENT_FAILURE,
               * depending on whether we have that key or not.
               */
              try {
            var keyBlob = messageParser.ReadBlob();
            var reqData = messageParser.ReadBlob();
            var flags = new SignRequestFlags();
            try {
              // usually, there are no flags, so parser will throw
              flags = (SignRequestFlags)messageParser.ReadInt();
            } catch { }

            var matchingKey =
              mKeyList.Where(key => key.Version == SshVersion.SSH2 &&
              key.GetPublicKeyBlob().SequenceEqual(keyBlob)).First();
            var confirmConstraints = matchingKey.Constraints
              .Where(constraint => constraint.Type ==
                KeyConstraintType.SSH_AGENT_CONSTRAIN_CONFIRM);
            if (confirmConstraints.Count() > 0) {
              if (!ConfirmUserPermissionCallback.Invoke(matchingKey, process)) {
                goto default;
              }
            }

            /* create signature */
            var signKey = matchingKey;
            var signer = signKey.GetSigner();
            var algName = signKey.Algorithm.GetIdentifierString();
            signer.Init(true, signKey.GetPrivateKeyParameters());
            signer.BlockUpdate(reqData, 0, reqData.Length);
            byte[] signature = signer.GenerateSignature();
            signature = signKey.FormatSignature(signature);
            BlobBuilder signatureBuilder = new BlobBuilder();
            if (!flags.HasFlag(SignRequestFlags.SSH_AGENT_OLD_SIGNATURE)) {
              signatureBuilder.AddStringBlob(algName);
            }
            signatureBuilder.AddBlob(signature);
            responseBuilder.AddBlob(signatureBuilder.GetBlob());
            responseBuilder.InsertHeader(Message.SSH2_AGENT_SIGN_RESPONSE);
            try {
              KeyUsed(this, new KeyUsedEventArgs(signKey, process));
            } catch { }
            break; // succeeded
              } catch (InvalidOperationException) {
            // this is expected if there is not a matching key
              } catch (Exception ex) {
            Debug.Fail(ex.ToString());
              }
              goto default; // failure

            case Message.SSH1_AGENTC_ADD_RSA_IDENTITY:
            case Message.SSH1_AGENTC_ADD_RSA_ID_CONSTRAINED:
              /*
               * Add to the list and return SSH_AGENT_SUCCESS, or
               * SSH_AGENT_FAILURE if the key was malformed.
               */

              if (IsLocked) {
            goto default;
              }

              bool ssh1constrained = (header.Message == Message.SSH1_AGENTC_ADD_RSA_ID_CONSTRAINED);

              try {
            var publicKeyParams = messageParser.ReadSsh1PublicKeyData(false);
            var keyPair = messageParser.ReadSsh1KeyData(publicKeyParams);

            SshKey key = new SshKey(SshVersion.SSH1, keyPair);
            key.Comment = messageParser.ReadString();
            key.Source = "External client";

            if (ssh1constrained) {
              while (messageStream.Position < header.BlobLength + 4) {
                KeyConstraint constraint = new KeyConstraint();
                constraint.Type = (KeyConstraintType)messageParser.ReadByte();
                if (constraint.Type ==
                  KeyConstraintType.SSH_AGENT_CONSTRAIN_LIFETIME) {
                  constraint.Data = messageParser.ReadInt();
                }
                key.AddConstraint(constraint);
              }
            }
            AddKey(key);
            responseBuilder.InsertHeader(Message.SSH_AGENT_SUCCESS);
            break;

              } catch (CallbackNullException) {
            // this is expected
              } catch (Exception ex) {
            Debug.Fail(ex.ToString());
              }

              goto default; // failed

            case Message.SSH2_AGENTC_ADD_IDENTITY:
            case Message.SSH2_AGENTC_ADD_ID_CONSTRAINED:
              /*
               * Add to the list and return SSH_AGENT_SUCCESS, or
               * SSH_AGENT_FAILURE if the key was malformed.
               */

              if (IsLocked) {
            goto default;
              }

              bool constrained = (header.Message ==
              Message.SSH2_AGENTC_ADD_ID_CONSTRAINED);

              try {
            var publicKeyParams = messageParser.ReadSsh2PublicKeyData();
            var keyPair = messageParser.ReadSsh2KeyData(publicKeyParams);
            SshKey key = new SshKey(SshVersion.SSH2, keyPair);
            key.Comment = messageParser.ReadString();
            key.Source = "External client";

            if (constrained) {
              while (messageStream.Position < header.BlobLength + 4) {
                KeyConstraint constraint = new KeyConstraint();
                constraint.Type =
                  (KeyConstraintType)messageParser.ReadByte();
                if (constraint.Type ==
                  KeyConstraintType.SSH_AGENT_CONSTRAIN_LIFETIME) {
                  constraint.Data = messageParser.ReadInt();
                }
                key.AddConstraint(constraint);
              }
            }
            AddKey(key);
            responseBuilder.InsertHeader(Message.SSH_AGENT_SUCCESS);
            break; // success!
              } catch (CallbackNullException) {
            // this is expected
              } catch (Exception ex) {
            Debug.Fail(ex.ToString());
              }
              goto default; // failed

            case Message.SSH1_AGENTC_REMOVE_RSA_IDENTITY:
            case Message.SSH2_AGENTC_REMOVE_IDENTITY:
              /*
               * Remove from the list and return SSH_AGENT_SUCCESS, or
               * perhaps SSH_AGENT_FAILURE if it wasn't in the list to
               * start with.
               */

              if (IsLocked) {
            goto default;
              }

              SshVersion removeVersion;
              byte[] rKeyBlob;
              if (header.Message == Message.SSH1_AGENTC_REMOVE_RSA_IDENTITY) {
            removeVersion = SshVersion.SSH1;
            rKeyBlob = messageParser.ReadBytes(header.BlobLength - 1);
              } else if (header.Message == Message.SSH2_AGENTC_REMOVE_IDENTITY) {
            removeVersion = SshVersion.SSH2;
            rKeyBlob = messageParser.ReadBlob();
              } else {
            Debug.Fail("Should not get here.");
            goto default;
              }

              try {
            var matchingKey = mKeyList.Get(removeVersion, rKeyBlob);
            var startKeyListLength = mKeyList.Count;
            RemoveKey(matchingKey);
            // only succeed if key was removed
            if (mKeyList.Count == startKeyListLength - 1) {
              responseBuilder.InsertHeader(Message.SSH_AGENT_SUCCESS);
              break; //success!
            }
              } catch (Exception ex) {
            Debug.Fail(ex.ToString());
              }
              goto default; // failed

            case Message.SSH1_AGENTC_REMOVE_ALL_RSA_IDENTITIES:
            case Message.SSH2_AGENTC_REMOVE_ALL_IDENTITIES:
              /*
               * Remove all SSH-1 or SSH-2 keys.
               */

              if (IsLocked) {
            goto default;
              }

              SshVersion removeAllVersion;
              if (header.Message == Message.SSH1_AGENTC_REMOVE_ALL_RSA_IDENTITIES) {
            removeAllVersion = SshVersion.SSH1;
              } else if (header.Message == Message.SSH2_AGENTC_REMOVE_ALL_IDENTITIES) {
            removeAllVersion = SshVersion.SSH2;
              } else {
            Debug.Fail("Should not get here.");
            goto default;
              }

              try {
            RemoveAllKeys(removeAllVersion);
            responseBuilder.InsertHeader(Message.SSH_AGENT_SUCCESS);
            break; //success!
              } catch (Exception ex) {
            Debug.Fail(ex.ToString());
              }
              goto default; // failed

            case Message.SSH_AGENTC_LOCK:
              try {
            var passphrase = new PinnedArray<byte>(messageParser.ReadBlob());
            try {
              Lock(passphrase.Data);
            } finally {
              passphrase.Clear();
            }
            if (IsLocked) {
              responseBuilder.InsertHeader(Message.SSH_AGENT_SUCCESS);
              break;
            }
              } catch (AgentLockedException) {
            // This is expected
              } catch (Exception ex) {
            Debug.Fail(ex.ToString());
              }
              goto default;

            case Message.SSH_AGENTC_UNLOCK:
              try {
            var passphrase = new PinnedArray<byte>(messageParser.ReadBlob());
            try {
              Unlock(passphrase.Data);
            } finally {
              passphrase.Clear();
            }
            if (!IsLocked) {
              responseBuilder.InsertHeader(Message.SSH_AGENT_SUCCESS);
              break;
            }
              } catch (AgentLockedException) {
            // This is expected
              } catch (PassphraseException) {
            // This is expected
              } catch (Exception ex) {
            Debug.Fail(ex.ToString());
              }
              goto default;

            default:
              responseBuilder.Clear();
              responseBuilder.InsertHeader(Message.SSH_AGENT_FAILURE);
              break;
              }
              /* write response to stream */
              if (messageStream.CanSeek)
            messageStream.Position = 0;
              messageStream.Write(responseBuilder.GetBlob(), 0, responseBuilder.Length);
              messageStream.Flush();
        }