public void Encode(ByteBuffer buffer) { byte[] bytes = this.uuid.ToByteArray(); buffer.Validate(true, bytes.Length); Buffer.BlockCopy(bytes, 0, buffer.Buffer, buffer.WritePos, bytes.Length); buffer.Append(bytes.Length); }
public static ByteBuffer ReadFrameBuffer(ITransport transport, byte[] sizeBuffer, uint maxFrameSize) { ReadBuffer(transport, sizeBuffer, 0, FixedWidth.UInt); int size = AmqpBitConverter.ReadInt(sizeBuffer, 0); if ((uint)size > maxFrameSize) { throw new AmqpException(ErrorCode.InvalidField, Fx.Format(SRAmqp.InvalidFrameSize, size, maxFrameSize)); } ByteBuffer frameBuffer = new ByteBuffer(size, true); AmqpBitConverter.WriteInt(frameBuffer, size); ReadBuffer(transport, frameBuffer.Buffer, frameBuffer.Length, frameBuffer.Size); frameBuffer.Append(frameBuffer.Size); return frameBuffer; }
public override bool WriteAsync(TransportAsyncCallbackArgs args) { Fx.Assert(this.writeState.Args == null, "Cannot write when a write is still in progress"); ArraySegment<byte> buffer; if (args.Buffer != null) { buffer = new ArraySegment<byte>(args.Buffer, args.Offset, args.Count); this.writeState.Args = args; } else { Fx.Assert(args.ByteBufferList != null, "Buffer list should not be null when buffer is null"); if (args.ByteBufferList.Count == 1) { ByteBuffer byteBuffer = args.ByteBufferList[0]; buffer = new ArraySegment<byte>(byteBuffer.Buffer, byteBuffer.Offset, byteBuffer.Length); this.writeState.Args = args; } else { // Copy all buffers into one big buffer to avoid SSL overhead Fx.Assert(args.Count > 0, "args.Count should be set"); ByteBuffer temp = new ByteBuffer(args.Count, false, false); for (int i = 0; i < args.ByteBufferList.Count; ++i) { ByteBuffer byteBuffer = args.ByteBufferList[i]; Buffer.BlockCopy(byteBuffer.Buffer, byteBuffer.Offset, temp.Buffer, temp.Length, byteBuffer.Length); temp.Append(byteBuffer.Length); } buffer = new ArraySegment<byte>(temp.Buffer, 0, temp.Length); this.writeState.Args = args; this.writeState.Buffer = temp; } } IAsyncResult result = this.sslStream.BeginWrite(buffer.Array, buffer.Offset, buffer.Count, onWriteComplete, this); bool completedSynchronously = result.CompletedSynchronously; if (completedSynchronously) { this.HandleOperationComplete(result, true, true); } return !completedSynchronously; }
public static void WriteFloat(ByteBuffer buffer, float data) { buffer.EnsureSize(FixedWidth.Float); fixed (byte* d = &buffer.Buffer[buffer.End]) { byte* p = (byte*)&data; d[0] = p[3]; d[1] = p[2]; d[2] = p[1]; d[3] = p[0]; } buffer.Append(FixedWidth.Float); }
public static void WriteUShort(ByteBuffer buffer, ushort data) { buffer.EnsureSize(FixedWidth.UShort); fixed (byte* d = &buffer.Buffer[buffer.End]) { byte* p = (byte*)&data; d[0] = p[1]; d[1] = p[0]; } buffer.Append(FixedWidth.UShort); }
public static void WriteUByte(ByteBuffer buffer, byte data) { buffer.EnsureSize(FixedWidth.UByte); buffer.Buffer[buffer.End] = data; buffer.Append(FixedWidth.UByte); }
static void Encode(AmqpSymbol value, int width, ByteBuffer buffer) { int stringSize = Encoding.ASCII.GetByteCount(value.Value); if (width == 0) { width = AmqpEncoding.GetEncodeWidthBySize(stringSize); } if (width == FixedWidth.UByte) { AmqpBitConverter.WriteUByte(buffer, (byte)FormatCode.Symbol8); AmqpBitConverter.WriteUByte(buffer, (byte)stringSize); } else { AmqpBitConverter.WriteUByte(buffer, (byte)FormatCode.Symbol32); AmqpBitConverter.WriteUInt(buffer, (uint)stringSize); } buffer.EnsureSize(stringSize); int written = Encoding.ASCII.GetBytes(value.Value, 0, value.Value.Length, buffer.Buffer, buffer.End); buffer.Append(written); }
/** * Signs a PDF where space was already reserved. * @param reader the original PDF * @param fieldName the field to sign. It must be the last field * @param outs the output PDF * @param externalSignatureContainer the signature container doing the actual signing. Only the * method ExternalSignatureContainer.sign is used * @throws DocumentException * @throws IOException * @throws GeneralSecurityException */ public static void SignDeferred(PdfReader reader, String fieldName, Stream outs, IExternalSignatureContainer externalSignatureContainer) { AcroFields af = reader.AcroFields; PdfDictionary v = af.GetSignatureDictionary(fieldName); if (v == null) throw new DocumentException("No field"); if (!af.SignatureCoversWholeDocument(fieldName)) throw new DocumentException("Not the last signature"); PdfArray b = v.GetAsArray(PdfName.BYTERANGE); long[] gaps = b.AsLongArray(); if (b.Size != 4 || gaps[0] != 0) throw new DocumentException("Single exclusion space supported"); IRandomAccessSource readerSource = reader.SafeFile.CreateSourceView(); Stream rg = new RASInputStream(new RandomAccessSourceFactory().CreateRanged(readerSource, gaps)); byte[] signedContent = externalSignatureContainer.Sign(rg); int spaceAvailable = (int)(gaps[2] - gaps[1]) - 2; if ((spaceAvailable & 1) != 0) throw new DocumentException("Gap is not a multiple of 2"); spaceAvailable /= 2; if (spaceAvailable < signedContent.Length) throw new DocumentException("Not enough space"); StreamUtil.CopyBytes(readerSource, 0, gaps[1] + 1, outs); ByteBuffer bb = new ByteBuffer(spaceAvailable * 2); foreach (byte bi in signedContent) { bb.AppendHex(bi); } int remain = (spaceAvailable - signedContent.Length) * 2; for (int k = 0; k < remain; ++k) { bb.Append((byte)48); } bb.WriteTo(outs); StreamUtil.CopyBytes(readerSource, gaps[2] - 1, gaps[3] + 1, outs); }
public override bool WriteAsync(TransportAsyncCallbackArgs args) { Fx.Assert(this.writeState.Args == null, "Cannot write when a write is still in progress"); Windows.Storage.Streams.IBuffer ibuffer; if (args.Buffer != null) { this.writeState.Args = args; ibuffer = args.Buffer.AsBuffer(args.Offset, args.Count); } else { Fx.Assert(args.ByteBufferList != null, "Buffer list should not be null when buffer is null"); ArraySegment<byte> buffer; if (args.ByteBufferList.Count == 1) { ByteBuffer byteBuffer = args.ByteBufferList[0]; buffer = new ArraySegment<byte>(byteBuffer.Buffer, byteBuffer.Offset, byteBuffer.Length); this.writeState.Args = args; } else { // Copy all buffers into one big buffer to avoid SSL overhead Fx.Assert(args.Count > 0, "args.Count should be set"); ByteBuffer temp = new ByteBuffer(args.Count, false, false); for (int i = 0; i < args.ByteBufferList.Count; ++i) { ByteBuffer byteBuffer = args.ByteBufferList[i]; Buffer.BlockCopy(byteBuffer.Buffer, byteBuffer.Offset, temp.Buffer, temp.Length, byteBuffer.Length); temp.Append(byteBuffer.Length); } buffer = new ArraySegment<byte>(temp.Buffer, 0, temp.Length); this.writeState.Args = args; this.writeState.Buffer = temp; } ibuffer = buffer.Array.AsBuffer(0, buffer.Count); } var t = this.socket.OutputStream.WriteAsync(ibuffer).AsTask(); t.ContinueWith(completion => { var args2 = this.readState.Args; if (completion.IsFaulted) { if (Fx.IsFatal(completion.Exception)) { throw completion.Exception; } args2.Exception = completion.Exception; } else { args2 = this.writeState.Args; ByteBuffer buffer = this.writeState.Buffer; this.writeState.Reset(); if (buffer != null) { buffer.Dispose(); } Fx.Assert(args2.Count == completion.Result, "completion must have the same write count"); args2.BytesTransfered = args2.Count; } args2.CompletedSynchronously = false; Action<TransportAsyncCallbackArgs> callback = args2.CompletedCallback; if (callback != null) { args2.CompletedCallback(args2); } }); return true; }
public static void WriteBytes(ByteBuffer buffer, byte[] data, int offset, int count) { buffer.Validate(true, count); Buffer.BlockCopy(data, offset, buffer.Buffer, buffer.WritePos, count); buffer.Append(count); }
public static void WriteFloat(ByteBuffer buffer, float data) { buffer.Validate(true, FixedWidth.Float); fixed (byte* d = &buffer.Buffer[buffer.WritePos]) { byte* p = (byte*)&data; d[0] = p[3]; d[1] = p[2]; d[2] = p[1]; d[3] = p[0]; } buffer.Append(FixedWidth.Float); }
public static void WriteULong(ByteBuffer buffer, ulong data) { buffer.Validate(true, FixedWidth.ULong); fixed (byte* d = &buffer.Buffer[buffer.WritePos]) { byte* p = (byte*)&data; d[0] = p[7]; d[1] = p[6]; d[2] = p[5]; d[3] = p[4]; d[4] = p[3]; d[5] = p[2]; d[6] = p[1]; d[7] = p[0]; } buffer.Append(FixedWidth.ULong); }
public static void WriteUShort(ByteBuffer buffer, ushort data) { buffer.Validate(true, FixedWidth.UShort); fixed (byte* d = &buffer.Buffer[buffer.WritePos]) { byte* p = (byte*)&data; d[0] = p[1]; d[1] = p[0]; } buffer.Append(FixedWidth.UShort); }
public static void WriteUByte(ByteBuffer buffer, byte data) { buffer.Validate(true, FixedWidth.UByte); buffer.Buffer[buffer.WritePos] = data; buffer.Append(FixedWidth.UByte); }
public static void WriteDouble(ByteBuffer buffer, double data) { buffer.EnsureSize(FixedWidth.Double); fixed (byte* d = &buffer.Buffer[buffer.End]) { byte* p = (byte*)&data; d[0] = p[7]; d[1] = p[6]; d[2] = p[5]; d[3] = p[4]; d[4] = p[3]; d[5] = p[2]; d[6] = p[1]; d[7] = p[0]; } buffer.Append(FixedWidth.Double); }
public static void WriteUuid(ByteBuffer buffer, Guid data) { buffer.EnsureSize(FixedWidth.Uuid); fixed (byte* d = &buffer.Buffer[buffer.End]) { byte* p = (byte*)&data; d[0] = p[3]; d[1] = p[2]; d[2] = p[1]; d[3] = p[0]; d[4] = p[5]; d[5] = p[4]; d[6] = p[7]; d[7] = p[6]; *((ulong*)&d[8]) = *((ulong*)&p[8]); } buffer.Append(FixedWidth.Uuid); }
/** * Converts an annotation structure item to a Form XObject annotation. * @param item the structure item * @throws IOException */ virtual protected void ConvertToXObject(StructureObject item) { PdfDictionary structElem = item.GetStructElem(); if (structElem == null) return; PdfDictionary dict = item.GetObjAsDict(); if (dict == null || !dict.CheckType(PdfName.ANNOT)) return; PdfDictionary ap = dict.GetAsDict(PdfName.AP); if (ap == null) return; PdfNumber structParent = dict.GetAsNumber(PdfName.STRUCTPARENT); if (structParent == null) return; PdfStream stream = ap.GetAsStream(PdfName.N); if (stream == null) return; stream.Put(PdfName.STRUCTPARENT, structParent); PdfIndirectReference xobjr = ap.GetAsIndirectObject(PdfName.N); if (xobjr == null) return; // remove the annotation from the page for (int i = 0; i < annots.Length; i++) { PdfIndirectReference annotref = annots.GetAsIndirectObject(i); if (item.GetObjRef().Number == annotref.Number) { annots.Remove(i); break; } } // replace the existing attributes by a PrintField attribute PdfDictionary attribute = new PdfDictionary(); attribute.Put(PdfName.O, PdfName.PRINTFIELD); PdfString description = dict.GetAsString(PdfName.TU); if (description == null) description = dict.GetAsString(PdfName.T); if (PdfName.BTN.Equals(dict.Get(PdfName.FT))) { PdfNumber fflags = dict.GetAsNumber(PdfName.FF); if (fflags != null) { int ff = fflags.IntValue; if ((ff & PdfFormField.FF_PUSHBUTTON) != 0) attribute.Put(PdfName.ROLE, PdfName.PB); // I don't think the condition below will ever be true if ((ff & PdfFormField.FF_RADIO) != 0) attribute.Put(PdfName.ROLE, PdfName.rb); else attribute.Put(PdfName.ROLE, PdfName.CB); } } else { attribute.Put(PdfName.ROLE, PdfName.TV); } attribute.Put(PdfName.DESC, description); // Updating the values of the StructElem dictionary PdfString t = structElem.GetAsString(PdfName.T); if (t == null || t.ToString().Trim().Length == 0) structElem.Put(PdfName.T, dict.GetAsString(PdfName.T)); structElem.Put(PdfName.A, attribute); structElem.Put(PdfName.S, PdfName.P); structElem.Put(PdfName.PG, pageref); // Defining a new MCID int mcid = items.ProcessMCID(structParents, item.GetRef()); LOGGER.Info("Using MCID " + mcid); structElem.Put(PdfName.K, new PdfNumber(mcid)); // removing the annotation from the parent tree items.RemoveFromParentTree(structParent); // Adding the XObject to the page PdfName xobj = new PdfName("XObj" + structParent.IntValue); LOGGER.Info("Creating XObject with name " + xobj); xobjects.Put(xobj, xobjr); PdfArray array = dict.GetAsArray(PdfName.RECT); // Getting the position of the annotation Rectangle rect = new Rectangle( array.GetAsNumber(0).FloatValue, array.GetAsNumber(1).FloatValue, array.GetAsNumber(2).FloatValue, array.GetAsNumber(3).FloatValue); rect.Normalize(); // A Do operator is forbidden inside a text block if (inText && !btWrite) { LOGGER.Debug("Introducing extra ET"); byte[] bytes = Encoding.ASCII.GetBytes("ET\n"); baos.Write(bytes, 0, bytes.Length); etExtra = true; } // Writing the marked-content sequence with the Do operator // Note that the position assumes that the CTM wasn't changed in the graphics state // TODO: do the math if the CTM did change! ByteBuffer buf = new ByteBuffer(); buf.Append("/P <</MCID "); buf.Append(mcid); buf.Append(">> BDC\n"); buf.Append("q 1 0 0 1 "); buf.Append(rect.Left.ToString(CultureInfo.InvariantCulture)); buf.Append(" "); buf.Append(rect.Bottom.ToString(CultureInfo.InvariantCulture)); buf.Append(" cm "); buf.Append(xobj.GetBytes()); buf.Append(" Do Q\n"); buf.Append("EMC\n"); buf.Flush(); buf.WriteTo(baos); // if we were inside a text block, we've introduced an ET, so we'll need to write a BT if (inText) btWrite = true; }