Beispiel #1
0
        /// <summary>
        /// Get the reference of a cloud block blob.
        /// </summary>
        /// <param name="cloudBlobContainer">The container to get the blob reference from</param>
        /// <param name="blobName">Name of the desired blob</param>
        /// <param name="directoryName">Name of the virtual directory</param>
        /// <param name="path">Path of the virtual directory / blob</param>
        /// <returns></returns>
        public static CloudBlockBlob GetCloudBlockBlobReference(this CloudBlobContainer cloudBlobContainer, string blobName, string directoryName = null, string[] path = null)
        {
            #region validation

            if (cloudBlobContainer == null)
            {
                throw new ArgumentNullException(nameof(cloudBlobContainer));
            }

            if (string.IsNullOrEmpty(blobName))
            {
                throw new ArgumentNullException(blobName);
            }

            #endregion

            string fullBlobPath;

            if (string.IsNullOrEmpty(directoryName))
            {
                fullBlobPath = BlobUtilities.GetPath(blobName, path);
            }
            else
            {
                string fullDirectoryPath = BlobUtilities.GetPath(directoryName, path);

                fullBlobPath = BlobUtilities.GetPath(blobName, fullDirectoryPath);
            }

            return(cloudBlobContainer.GetBlockBlobReference(fullBlobPath));
        }
Beispiel #2
0
        /// <exception cref="ArgumentOutOfRangeException">Range specified by <paramref name="start"/> and <paramref name="byteCount"/> falls outside of the bounds of the buffer content.</exception>
        /// <exception cref="InvalidOperationException">Content is not available, the builder has been linked with another one.</exception>
        public byte[] ToArray(int start, int byteCount)
        {
            BlobUtilities.ValidateRange(Count, start, byteCount);

            var result = new byte[byteCount];

            int chunkStartPosition = 0;
            int resultOffset       = 0;

            foreach (var chunk in GetChunks())
            {
                int chunkEndPosition = chunkStartPosition + chunk.Length;

                if (chunkEndPosition > start)
                {
                    int bytesToCopy = Math.Min(chunk.Length, result.Length - resultOffset);
                    if (bytesToCopy == 0)
                    {
                        break;
                    }

                    Array.Copy(chunk._buffer, Math.Max(start - chunkStartPosition, 0), result, resultOffset, bytesToCopy);

                    resultOffset += bytesToCopy;
                }

                chunkStartPosition = chunkEndPosition;
            }

            Debug.Assert(resultOffset == result.Length);
            return(result);
        }
Beispiel #3
0
        /// <exception cref="ArgumentNullException"><paramref name="buffer"/> is null.</exception>
        /// <exception cref="ArgumentOutOfRangeException">Range specified by <paramref name="start"/> and <paramref name="byteCount"/> falls outside of the bounds of the <paramref name="buffer"/>.</exception>
        /// <exception cref="InvalidOperationException">Builder is not writable, it has been linked with another one.</exception>
        public unsafe void WriteBytes(byte[] buffer, int start, int byteCount)
        {
            if (buffer == null)
            {
                throw new ArgumentNullException(nameof(buffer));
            }

            BlobUtilities.ValidateRange(buffer.Length, start, byteCount);

            if (!IsHead)
            {
                ThrowHeadRequired();
            }

            // an empty array has no element pointer:
            if (buffer.Length == 0)
            {
                return;
            }

            fixed(byte *ptr = buffer)
            {
                WriteBytesUnchecked(ptr + start, byteCount);
            }
        }
        /// <summary>
        /// Returns the content of the entire memory block.
        /// </summary>
        /// <remarks>
        /// Does not check bounds.
        ///
        /// Only creates a copy of the data if they are not represented by a managed byte array,
        /// or if the specified range doesn't span the entire block.
        /// </remarks>
        public unsafe virtual ImmutableArray <byte> GetContentUnchecked(int start, int length)
        {
            var result = BlobUtilities.ReadImmutableBytes(Pointer + start, length);

            GC.KeepAlive(this);
            return(result);
        }
Beispiel #5
0
        /// <exception cref="ArgumentNullException"><paramref name="buffer"/> is null.</exception>
        /// <exception cref="ArgumentOutOfRangeException">Range specified by <paramref name="start"/> and <paramref name="byteCount"/> falls outside of the bounds of the <paramref name="buffer"/>.</exception>
        /// <exception cref="InvalidOperationException">Builder is not writable, it has been linked with another one.</exception>
        public unsafe void WriteBytes(byte[] buffer, int start, int byteCount)
        {
            if (buffer == null)
            {
                Throw.ArgumentNull(nameof(buffer));
            }

            BlobUtilities.ValidateRange(buffer.Length, start, byteCount, nameof(byteCount));

            if (!IsHead)
            {
                Throw.InvalidOperationBuilderAlreadyLinked();
            }

            // an empty array has no element pointer:
            if (buffer.Length == 0)
            {
                return;
            }

            fixed(byte *ptr = &buffer[0])
            {
                WriteBytesUnchecked(ptr + start, byteCount);
            }
        }
Beispiel #6
0
        /// <exception cref="ArgumentOutOfRangeException">Range specified by <paramref name="start"/> and <paramref name="byteCount"/> falls outside of the bounds of the buffer content.</exception>
        public byte[] ToArray(int start, int byteCount)
        {
            BlobUtilities.ValidateRange(Length, start, byteCount, nameof(byteCount));

            var result = new byte[byteCount];
            Array.Copy(_buffer, _start + start, result, 0, byteCount);
            return result;
        }
Beispiel #7
0
 private int ComputeSizeOfDebugDirectoryData()
 {
     // The debug directory data is only needed if this.EmitPdb.
     return((!EmitPdb) ? 0 :
            4 +            // 4B signature "RSDS"
            16 +           // GUID
            sizeof(uint) + // Age
            Math.Max(BlobUtilities.GetUTF8ByteCount(PdbPathOpt) + 1, MinPdbPath));
 }
Beispiel #8
0
        /// <exception cref="ArgumentOutOfRangeException">Range specified by <paramref name="start"/> and <paramref name="byteCount"/> falls outside of the bounds of the buffer content.</exception>
        public byte[] ToArray(int start, int byteCount)
        {
            BlobUtilities.ValidateRange(Length, start, byteCount);

            var result = new byte[byteCount];

            Buffer.BlockCopy(_buffer, _start + start, result, 0, byteCount);
            return(result);
        }
        public async Task ComputeNodeUploadLogs()
        {
            Func <Task> test = async() =>
            {
                using (BatchClient batchCli = TestUtilities.OpenBatchClientFromEnvironmentAsync().Result)
                {
                    const string containerName = "computenodelogscontainer";

                    // Generate a storage container URL
                    StagingStorageAccount storageAccount  = TestUtilities.GetStorageCredentialsFromEnvironment();
                    BlobServiceClient     blobClient      = BlobUtilities.GetBlobServiceClient(storageAccount);
                    BlobContainerClient   containerClient = BlobUtilities.GetBlobContainerClient(containerName, blobClient, storageAccount);

                    try
                    {
                        containerClient.CreateIfNotExists();
                        string sasUri = BlobUtilities.GetWriteableSasUri(containerClient, storageAccount);

                        var blobs = containerClient.GetAllBlobs();

                        // Ensure that there are no items in the container to begin with
                        Assert.Empty(blobs);

                        var startTime = DateTime.UtcNow.Subtract(TimeSpan.FromMinutes(5));

                        var node   = batchCli.PoolOperations.ListComputeNodes(this.poolFixture.PoolId).First();
                        var result = batchCli.PoolOperations.UploadComputeNodeBatchServiceLogs(
                            this.poolFixture.PoolId,
                            node.Id,
                            sasUri,
                            startTime);

                        Assert.NotEqual(0, result.NumberOfFilesUploaded);
                        Assert.NotEmpty(result.VirtualDirectoryName);

                        // Allow up to 2m for files to get uploaded
                        DateTime timeoutAt = DateTime.UtcNow.AddMinutes(2);
                        while (DateTime.UtcNow < timeoutAt)
                        {
                            blobs = containerClient.GetAllBlobs();
                            if (blobs.Any())
                            {
                                break;
                            }
                        }

                        Assert.NotEmpty(blobs);
                    }
                    finally
                    {
                        await containerClient.DeleteIfExistsAsync();
                    }
                }
            };

            await SynchronizationContextHelper.RunTestAsync(test, TestTimeout);
        }
        /// <exception cref="ImageFormatLimitationException">The remaining space on the heap is too small to fit the string.</exception>
        public UserStringHandle ReserveUserString(int length, out Blob fixup)
        {
            int offset        = GetNewUserStringHeapOffset(length);
            int encodedLength = BlobUtilities.GetUserStringByteLength(length);

            fixup = _userStringWriter.ReserveBytes(BlobWriterImpl.GetCompressedIntegerSize(encodedLength) + encodedLength);
            new BlobWriter(fixup).WriteBytes(0, fixup.Length);
            return(MetadataTokens.UserStringHandle(offset));
        }
        private static async Task CopyBlobAsync(BlobContainerClient container, BlobContainerClient destContainer, JPOFileInfo info, ILogger log)
        {
            try {
                // Get the name of the first blob in the container to use as the source.
                string blobName = info.fileName;

                // Create a BlobClient representing the source blob to copy.
                BlobClient sourceBlob = container.GetBlobClient(blobName);

                // Ensure that the source blob exists.
                if (await sourceBlob.ExistsAsync())
                {
                    // Lease the source blob for the copy operation to prevent another client from modifying it.
                    BlobLeaseClient lease = sourceBlob.GetBlobLeaseClient();

                    // Specifying -1 for the lease interval creates an infinite lease.
                    //await lease.AcquireAsync(TimeSpan.FromSeconds(100));

                    // Get the source blob's properties and display the lease state.
                    BlobProperties sourceProperties = await sourceBlob.GetPropertiesAsync();

                    log.LoggerInfo($"Lease state: {sourceProperties.LeaseState}", info);

                    Uri blob_sas_uri = BlobUtilities.GetServiceSASUriForBlob(sourceBlob, container.Name, null);

                    // Get a BlobClient representing the destination blob
                    BlobClient destBlob = destContainer.GetBlobClient(blobName);//destContainer.GetBlobClient(blob_sas_uri.ToString());

                    // Start the copy operation.
                    await destBlob.StartCopyFromUriAsync(blob_sas_uri);

                    // Get the destination blob's properties and display the copy status.
                    BlobProperties destProperties = await destBlob.GetPropertiesAsync();

                    // Update the source blob's properties.
                    sourceProperties = await sourceBlob.GetPropertiesAsync();

                    if (sourceProperties.LeaseState == LeaseState.Leased)
                    {
                        // Break the lease on the source blob.
                        await lease.BreakAsync();

                        // Update the source blob's properties to check the lease state.
                        sourceProperties = await sourceBlob.GetPropertiesAsync();
                    }
                }
            }
            catch (RequestFailedException ex) {
                log.LoggerError($"RequestFailedException: {ex.Message}", ex?.StackTrace, info);
                Console.WriteLine(ex.Message);
                Console.ReadLine();
                throw;
            }
        }
        private static unsafe byte[] ReadBuffer(byte *buffer, int bufferSize)
        {
            byte *p = buffer + bufferSize - 1;

            while (p >= buffer && *p == 0xfe)
            {
                p--;
            }

            return(BlobUtilities.ReadBytes(buffer, (int)(p + 1 - buffer)));
        }
        public async Task <IActionResult> Get(string documentId)
        {
            var file = await BlobUtilities.DownloadFileFromBlob(documentId);

            if (file == null)
            {
                return(new NotFoundResult());
            }

            return(File(file, "application/pdf", documentId));
        }
Beispiel #14
0
        /// <summary>
        /// Writes string in User String (#US) heap format (see ECMA-335-II 24.2.4 #US and #Blob heaps):
        /// </summary>
        /// <remarks>
        /// The string is UTF16 encoded and prefixed by the its size in bytes.
        ///
        /// This final byte holds the value 1 if and only if any UTF16 character within the string has any bit set in its top byte,
        /// or its low byte is any of the following: 0x01-0x08, 0x0E-0x1F, 0x27, 0x2D, 0x7F. Otherwise, it holds 0.
        /// The 1 signifies Unicode characters that require handling beyond that normally provided for 8-bit encoding sets.
        /// </remarks>
        /// <exception cref="InvalidOperationException">Builder is not writable, it has been linked with another one.</exception>
        public void WriteUserString(string value)
        {
            if (value is null)
            {
                Throw.ArgumentNull(nameof(value));
            }

            WriteCompressedInteger(BlobUtilities.GetUserStringByteLength(value.Length));
            WriteUTF16(value);
            WriteByte(BlobUtilities.GetUserStringTrailingByte(value));
        }
Beispiel #15
0
        /// <summary>
        /// Writes string in User String (#US) heap format (see ECMA-335-II 24.2.4 #US and #Blob heaps):
        /// </summary>
        /// <remarks>
        /// The string is UTF16 encoded and prefixed by the its size in bytes.
        ///
        /// This final byte holds the value 1 if and only if any UTF16 character within the string has any bit set in its top byte,
        /// or its low byte is any of the following: 0x01–0x08, 0x0E–0x1F, 0x27, 0x2D, 0x7F. Otherwise, it holds 0.
        /// The 1 signifies Unicode characters that require handling beyond that normally provided for 8-bit encoding sets.
        /// </remarks>
        /// <exception cref="InvalidOperationException">Builder is not writable, it has been linked with another one.</exception>
        public void WriteUserString(string value)
        {
            if (value == null)
            {
                throw new ArgumentNullException(nameof(value));
            }

            WriteCompressedInteger(BlobUtilities.GetUserStringByteLength(value.Length));
            WriteUTF16(value);
            WriteByte(BlobUtilities.GetUserStringTrailingByte(value));
        }
Beispiel #16
0
        private void TestGetUTF8ByteCount(int expectedCount, string expectedRemainder, string str, int charCount, int byteLimit)
        {
            fixed(char *ptr = str)
            {
                char *remainderPtr;

                Assert.Equal(expectedCount, BlobUtilities.GetUTF8ByteCount(ptr, charCount, byteLimit, out remainderPtr));

                string remainder = new string(remainderPtr);

                Assert.Equal(expectedRemainder, remainder);
            }
        }
Beispiel #17
0
        /// <summary>
        /// Reserves space on the User String heap for a string of specified length.
        /// </summary>
        /// <param name="length">The number of characters to reserve.</param>
        /// <param name="reservedUserString">
        /// <see cref="Blob"/> representing the entire User String blob (including its length and terminal character).
        /// Use <see cref="BlobWriter.WriteUserString(string)"/> to fill in the content.
        /// </param>
        /// <returns>
        /// Handle to the reserved User String.
        /// May be used in <see cref="InstructionEncoder.LoadString(UserStringHandle)"/>.
        /// </returns>
        /// <exception cref="ImageFormatLimitationException">The remaining space on the heap is too small to fit the string.</exception>
        /// <exception cref="ArgumentOutOfRangeException"><paramref name="length"/> is negative.</exception>
        public UserStringHandle ReserveUserString(int length, out Blob reservedUserString)
        {
            if (length < 0)
            {
                Throw.ArgumentOutOfRange(nameof(length));
            }

            var handle        = GetNewUserStringHandle();
            int encodedLength = BlobUtilities.GetUserStringByteLength(length);

            reservedUserString = _userStringBuilder.ReserveBytes(BlobWriterImpl.GetCompressedIntegerSize(encodedLength) + encodedLength);
            return(handle);
        }
Beispiel #18
0
        /// <summary>
        /// Returns a cloud blob directory reference at a specified location
        /// </summary>
        /// <param name="cloudBlobContainer">Container reference to get the directory reference from</param>
        /// <param name="path">Path to the cloud blob directory</param>
        /// <returns>Cloud blob directory reference at the desired location</returns>
        public static CloudBlobDirectory GetCloudBlobDirectoryReference(this CloudBlobContainer cloudBlobContainer, string[] path = null)
        {
            #region validation

            if (cloudBlobContainer == null)
            {
                throw new ArgumentNullException(nameof(cloudBlobContainer));
            }
            #endregion

            string fullPath = BlobUtilities.GetPath(path);

            return(cloudBlobContainer.GetDirectoryReference(fullPath));
        }
        /// <summary>
        /// Create a container if doesn't exist, setting permission with policy, and return assosciated SAS signature
        /// </summary>
        /// <param name="account">Storage account</param>
        /// <param name="Key">Storage account key</param>
        /// <param name="blobUri">Blob endpoint URI</param>
        /// <param name="containerName">Name of the container to be created</param>
        /// <param name="policy">Name for the policy</param>
        /// <param name="start">Start time of the policy</param>
        /// <param name="end">Expire time of the policy</param>
        /// <param name="permissions">Blob access permissions</param>
        /// <returns>the SAS for the container, in full URI format.</returns>.
        private static async Task <string> CreateContainerWithPolicySASIfNotExistAsync(string account, string key, Uri blobUri, string containerName, string policy, DateTime start, DateTime end, string permissions)
        {
            // 1. form the credentail and initial client
            StagingStorageAccount      stagingCredentials  = new StagingStorageAccount(account, key, blobUri.ToString());
            StorageSharedKeyCredential shardKeyCredentials = new StorageSharedKeyCredential(account, key);
            BlobContainerClient        containerClient     = BlobUtilities.GetBlobContainerClient(containerName, stagingCredentials);

            // 2. create container if it doesn't exist
            containerClient.CreateIfNotExists();

            // 3. validate policy, create/overwrite if doesn't match
            BlobSignedIdentifier identifier = new BlobSignedIdentifier
            {
                Id           = policy,
                AccessPolicy = new BlobAccessPolicy
                {
                    Permissions = permissions,
                    StartsOn    = start,
                    ExpiresOn   = end,
                },
            };

            var  accessPolicy = (await containerClient.GetAccessPolicyAsync()).Value;
            bool policyFound  = accessPolicy.SignedIdentifiers.Any(i => i == identifier);

            if (policyFound == false)
            {
                await containerClient.SetAccessPolicyAsync(PublicAccessType.BlobContainer, permissions : new List <BlobSignedIdentifier> {
                    identifier
                });
            }

            BlobSasBuilder sasBuilder = new BlobSasBuilder
            {
                BlobContainerName = containerName,
                StartsOn          = start,
                ExpiresOn         = end,
            };

            sasBuilder.SetPermissions(permissions);
            BlobUriBuilder builder = new BlobUriBuilder(containerClient.Uri)
            {
                Sas = sasBuilder.ToSasQueryParameters(shardKeyCredentials)
            };
            string fullSas = builder.ToString();

            return(fullSas);
        }
Beispiel #20
0
        private void WriteUTF8(string str, int start, int length, bool allowUnpairedSurrogates, bool prependSize)
        {
            fixed (char* strPtr = str)
            {
                char* charPtr = strPtr + start;
                int byteCount = BlobUtilities.GetUTF8ByteCount(charPtr, length);

                if (prependSize)
                {
                    WriteCompressedInteger(byteCount);
                }

                int startOffset = Advance(byteCount);
                _buffer.WriteUTF8(startOffset, charPtr, length, byteCount, allowUnpairedSurrogates);
            }
        }
Beispiel #21
0
        /// <summary>
        /// Stage a single file.
        /// </summary>
        private async static Task StageOneFileAsync(FileToStage stageThisFile, SequentialFileStagingArtifact seqArtifacts)
        {
            StagingStorageAccount storecreds = stageThisFile.StagingStorageAccount;
            string containerName             = seqArtifacts.BlobContainerCreated;

            // TODO: this flattens all files to the top of the compute node/task relative file directory. solve the hiearchy problem (virt dirs?)
            string blobName = Path.GetFileName(stageThisFile.LocalFileToStage);

            BlobContainerClient blobContainerClient = BlobUtilities.GetBlobContainerClient(containerName, storecreds);
            BlockBlobClient     blobClient          = blobContainerClient.GetBlockBlobClient(blobName);

            bool doesBlobExist = await blobClient.ExistsAsync();

            bool mustUploadBlob = true; // we do not re-upload blobs if they have already been uploaded

            if (doesBlobExist)          // if the blob exists, compare
            {
                FileInfo fi = new FileInfo(stageThisFile.LocalFileToStage);

                var properties = await blobClient.GetPropertiesAsync();

                var length = properties.Value.ContentLength;
                // since we don't have a hash of the contents... we check length
                if (length == fi.Length)
                {
                    mustUploadBlob = false;
                }
            }

            if (mustUploadBlob)
            {
                using FileStream stream = new FileStream(stageThisFile.LocalFileToStage, FileMode.Open);

                // upload the file
                Task uploadTask = blobClient.UploadAsync(stream);

                await uploadTask.ConfigureAwait(continueOnCapturedContext : false);
            }

            // get the SAS for the blob
            string blobSAS      = ConstructBlobSource(seqArtifacts.DefaultContainerSAS, blobName);
            string nodeFileName = stageThisFile.NodeFileName;

            // create a new ResourceFile and populate it.  This file is now staged!
            stageThisFile.StagedFiles = new ResourceFile[] { ResourceFile.FromUrl(blobSAS, nodeFileName) };
        }
Beispiel #22
0
        public void MoveDirectoryTest(string directoryName, string[] sourcePath, string[] targetPath)
        {
            // Arrange
            string relativeAddress = BlobUtilities.GetPath(directoryName, sourcePath);

            CloudBlobDirectory sourceCloudBlobDirectory = _cloudBlobContainer.GetDirectoryReference(relativeAddress);

            int expectedChildCount = TaskUtilities
                                     .ExecuteSync(
                sourceCloudBlobDirectory.ListBlobsSegmentedAsync(
                    useFlatBlobListing: true,
                    blobListingDetails: BlobListingDetails.None,
                    maxResults: null,
                    currentToken: new BlobContinuationToken(),
                    options: new BlobRequestOptions(),
                    operationContext: new OperationContext()
                    )
                )
                                     .Results.Count();

            // Act
            _fileContainer.MoveDirectory(directoryName, sourcePath, targetPath);

            string path = BlobUtilities.GetPath(directoryName, targetPath);

            CloudBlobDirectory targetCloudBlobDirectory = _cloudBlobContainer.GetDirectoryReference(path);

            int actualChildCount = TaskUtilities
                                   .ExecuteSync(
                targetCloudBlobDirectory.ListBlobsSegmentedAsync(
                    useFlatBlobListing: true,
                    blobListingDetails: BlobListingDetails.None,
                    maxResults: null,
                    currentToken: new BlobContinuationToken(),
                    options: new BlobRequestOptions(),
                    operationContext: new OperationContext()
                    )
                )
                                   .Results.Count();

            // Assert
            Assert.IsFalse(_fileContainer.ExistsDirectory(directoryName, sourcePath));

            Assert.AreEqual(expectedChildCount, actualChildCount);
        }
Beispiel #23
0
            public void GetSharedAccessSignature_WithBlob_Returns_The_Correct_Token()
            {
                // Arrange
                var dummyContainer = new CloudBlobContainer(
                    new Uri("http://test-storage.org"),
                    new StorageCredentials("fakeaccount",
                                           Convert.ToBase64String(Encoding.Unicode.GetBytes("fakekeyval")), "fakekeyname"));

                var blobUtilities = new BlobUtilities();
                var permissions   = SharedAccessBlobPermissions.Read;
                var expiryTime    = DateTimeOffset.UtcNow.AddDays(1);

                // Act
                var result = blobUtilities.GetSharedAccessSignature(dummyContainer, "blob", expiryTime, permissions);

                // Assert
                Assert.That(result, Is.Not.Null);
            }
        /// <summary>
        /// Fills in stringIndexMap with data from stringIndex and write to stringWriter.
        /// Releases stringIndex as the stringTable is sealed after this point.
        /// </summary>
        private static ImmutableArray <int> SerializeStringHeap(
            BlobBuilder heapBuilder,
            Dictionary <string, StringHandle> strings,
            int stringHeapStartOffset)
        {
            // Sort by suffix and remove stringIndex
            var sorted = new List <KeyValuePair <string, StringHandle> >(strings);

            sorted.Sort(SuffixSort.Instance);

            // Create VirtIdx to Idx map and add entry for empty string
            int totalCount = sorted.Count + 1;
            var stringVirtualIndexToHeapOffsetMap = ImmutableArray.CreateBuilder <int>(totalCount);

            stringVirtualIndexToHeapOffsetMap.Count = totalCount;

            stringVirtualIndexToHeapOffsetMap[0] = 0;
            heapBuilder.WriteByte(0);

            // Find strings that can be folded
            string prev = string.Empty;

            foreach (KeyValuePair <string, StringHandle> entry in sorted)
            {
                int position = stringHeapStartOffset + heapBuilder.Count;

                // It is important to use ordinal comparison otherwise we'll use the current culture!
                if (prev.EndsWith(entry.Key, StringComparison.Ordinal) && !BlobUtilities.IsLowSurrogateChar(entry.Key[0]))
                {
                    // Map over the tail of prev string. Watch for null-terminator of prev string.
                    stringVirtualIndexToHeapOffsetMap[entry.Value.GetWriterVirtualIndex()] = position - (BlobUtilities.GetUTF8ByteCount(entry.Key) + 1);
                }
                else
                {
                    stringVirtualIndexToHeapOffsetMap[entry.Value.GetWriterVirtualIndex()] = position;
                    heapBuilder.WriteUTF8(entry.Key, allowUnpairedSurrogates: false);
                    heapBuilder.WriteByte(0);
                }

                prev = entry.Key;
            }

            return(stringVirtualIndexToHeapOffsetMap.MoveToImmutable());
        }
Beispiel #25
0
        /// <summary>
        /// Creates a builder of a metadata root.
        /// </summary>
        /// <param name="tablesAndHeaps">
        /// Builder populated with metadata entities stored in tables and values stored in heaps.
        /// The entities and values will be enumerated when serializing the metadata root.
        /// </param>
        /// <param name="metadataVersion">
        /// The version string written to the metadata header. The default value is "v4.0.30319".
        /// </param>
        /// <exception cref="ArgumentNullException"><paramref name="tablesAndHeaps"/> is null.</exception>
        /// <exception cref="ArgumentException"><paramref name="metadataVersion"/> is too long (the number of bytes when UTF8-encoded must be less than 255).</exception>
        public MetadataRootBuilder(MetadataBuilder tablesAndHeaps, string metadataVersion = null)
        {
            if (tablesAndHeaps == null)
            {
                Throw.ArgumentNull(nameof(tablesAndHeaps));
            }

            Debug.Assert(BlobUtilities.GetUTF8ByteCount(DefaultMetadataVersionString) == DefaultMetadataVersionString.Length);
            int metadataVersionByteCount = metadataVersion != null?BlobUtilities.GetUTF8ByteCount(metadataVersion) : DefaultMetadataVersionString.Length;

            if (metadataVersionByteCount > MetadataSizes.MaxMetadataVersionByteCount)
            {
                Throw.InvalidArgument(SR.MetadataVersionTooLong, nameof(metadataVersion));
            }

            _tablesAndHeaps     = tablesAndHeaps;
            MetadataVersion     = metadataVersion ?? DefaultMetadataVersionString;
            _serializedMetadata = tablesAndHeaps.GetSerializedMetadata(EmptyRowCounts, metadataVersionByteCount, isStandaloneDebugMetadata: false);
        }
Beispiel #26
0
        /// <exception cref="ArgumentNullException"><paramref name="buffer"/> is null.</exception>
        /// <exception cref="ArgumentOutOfRangeException">Range specified by <paramref name="start"/> and <paramref name="byteCount"/> falls outside of the bounds of the <paramref name="buffer"/>.</exception>
        public unsafe void WriteBytes(byte[] buffer, int start, int byteCount)
        {
            if (buffer == null)
            {
                Throw.ArgumentNull(nameof(buffer));
            }

            BlobUtilities.ValidateRange(buffer.Length, start, byteCount, nameof(byteCount));

            // an empty array has no element pointer:
            if (buffer.Length == 0)
            {
                return;
            }

            fixed (byte* ptr = &buffer[0])
            {
                WriteBytes(ptr + start, byteCount);
            }
        }
Beispiel #27
0
        internal unsafe void WriteUTF8(string str, int start, int length, bool allowUnpairedSurrogates, bool prependSize)
        {
            Debug.Assert(start >= 0);
            Debug.Assert(length >= 0);
            Debug.Assert(start + length <= str.Length);

            if (!IsHead)
            {
                Throw.InvalidOperationBuilderAlreadyLinked();
            }

            fixed(char *strPtr = str)
            {
                char *currentPtr = strPtr + start;
                char *nextPtr;

                // the max size of compressed int is 4B:
                int byteLimit = FreeBytes - (prependSize ? sizeof(uint) : 0);

                int bytesToCurrent = BlobUtilities.GetUTF8ByteCount(currentPtr, length, byteLimit, out nextPtr);
                int charsToCurrent = (int)(nextPtr - currentPtr);
                int charsToNext    = length - charsToCurrent;
                int bytesToNext    = BlobUtilities.GetUTF8ByteCount(nextPtr, charsToNext);

                if (prependSize)
                {
                    WriteCompressedInteger(bytesToCurrent + bytesToNext);
                }

                _buffer.WriteUTF8(Length, currentPtr, charsToCurrent, bytesToCurrent, allowUnpairedSurrogates);
                AddLength(bytesToCurrent);

                if (bytesToNext > 0)
                {
                    Expand(bytesToNext);

                    _buffer.WriteUTF8(0, nextPtr, charsToNext, bytesToNext, allowUnpairedSurrogates);
                    AddLength(bytesToNext);
                }
            }
        }
Beispiel #28
0
        /// <summary>
        /// Creates a builder of a Portable PDB image.
        /// </summary>
        /// <param name="tablesAndHeaps">
        /// Builder populated with debug metadata entities stored in tables and values stored in heaps.
        /// The entities and values will be enumerated when serializing the Portable PDB image.
        /// </param>
        /// <param name="typeSystemRowCounts">
        /// Row counts of all tables that the associated type-system metadata contain.
        /// Each slot in the array corresponds to a table (<see cref="TableIndex"/>).
        /// The length of the array must be equal to <see cref="MetadataTokens.TableCount"/>.
        /// </param>
        /// <param name="entryPoint">
        /// Entry point method definition handle.
        /// </param>
        /// <param name="idProvider">
        /// Function calculating id of content represented as a sequence of blobs.
        /// If not specified a default function that ignores the content and returns current time-based content id is used
        /// (<see cref="BlobContentId.GetTimeBasedProvider()"/>).
        /// You must specify a deterministic function to produce a deterministic Portable PDB image.
        /// </param>
        /// <exception cref="ArgumentNullException"><paramref name="tablesAndHeaps"/> or <paramref name="typeSystemRowCounts"/> is null.</exception>
        public PortablePdbBuilder(
            MetadataBuilder tablesAndHeaps,
            ImmutableArray <int> typeSystemRowCounts,
            MethodDefinitionHandle entryPoint,
            Func <IEnumerable <Blob>, BlobContentId>?idProvider = null)
        {
            if (tablesAndHeaps == null)
            {
                Throw.ArgumentNull(nameof(tablesAndHeaps));
            }

            ValidateTypeSystemRowCounts(typeSystemRowCounts);

            _builder    = tablesAndHeaps;
            _entryPoint = entryPoint;

            Debug.Assert(BlobUtilities.GetUTF8ByteCount(MetadataVersion) == MetadataVersion.Length);
            _serializedMetadata = tablesAndHeaps.GetSerializedMetadata(typeSystemRowCounts, MetadataVersion.Length, isStandaloneDebugMetadata: true);

            IdProvider = idProvider ?? BlobContentId.GetTimeBasedProvider();
        }
        /// <summary>
        /// Fills in stringIndexMap with data from stringIndex and write to stringWriter.
        /// Releases stringIndex as the stringTable is sealed after this point.
        /// </summary>
        private void SerializeStringHeap()
        {
            // Sort by suffix and remove stringIndex
            var sorted = new List <KeyValuePair <string, StringHandle> >(_strings);

            sorted.Sort(new SuffixSort());
            _strings = null;

            _stringWriter = new BlobBuilder(1024);

            // Create VirtIdx to Idx map and add entry for empty string
            _stringIndexToResolvedOffsetMap = new int[sorted.Count + 1];

            _stringIndexToResolvedOffsetMap[0] = 0;
            _stringWriter.WriteByte(0);

            // Find strings that can be folded
            string prev = string.Empty;

            foreach (KeyValuePair <string, StringHandle> entry in sorted)
            {
                int position = _stringHeapStartOffset + _stringWriter.Position;

                // It is important to use ordinal comparison otherwise we'll use the current culture!
                if (prev.EndsWith(entry.Key, StringComparison.Ordinal) && !BlobUtilities.IsLowSurrogateChar(entry.Key[0]))
                {
                    // Map over the tail of prev string. Watch for null-terminator of prev string.
                    _stringIndexToResolvedOffsetMap[MetadataTokens.GetHeapOffset(entry.Value)] = position - (BlobUtilities.GetUTF8ByteCount(entry.Key) + 1);
                }
                else
                {
                    _stringIndexToResolvedOffsetMap[MetadataTokens.GetHeapOffset(entry.Value)] = position;
                    _stringWriter.WriteUTF8(entry.Key, allowUnpairedSurrogates: false);
                    _stringWriter.WriteByte(0);
                }

                prev = entry.Key;
            }
        }
        private async Task <bool> SaveToAzureBlob(BlobDetails blobDetails)
        {
            MemoryStream ms   = null;
            IFormFile    file = null;

            try
            {
                if (CloudStorageAccount.TryParse(_azureBlobConfig.Value.ConnectionString, out CloudStorageAccount storageAccount))
                {
                    CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();

                    CloudBlobContainer container = BlobUtilities.GetBlobContainer(blobClient, blobDetails.Type, _azureBlobConfig);

                    CloudBlockBlob blockBlob = container.GetBlockBlobReference(BlobUtilities.GenerateUniqueAadhaarImageName(blobDetails.CustomerID, blobDetails.Type));

                    using (ms = new MemoryStream())
                    {
                        file = blobDetails.BlobFile;
                        file.CopyTo(ms);
                        var fileBytes = ms.ToArray();
                        await blockBlob.UploadFromByteArrayAsync(fileBytes, 0, fileBytes.Length);
                    }
                    return(true);
                }
                else
                {
                    return(false);
                }
            }
            catch
            {
                return(false);
            }
            //finally
            //{
            //    await ms.DisposeAsync();
            //}
        }