1// Licensed to the .NET Foundation under one or more agreements.
2// The .NET Foundation licenses this file to you under the MIT license.
3// See the LICENSE file in the project root for more information.
4//
5// File: CompressedInteger.inl
6//
7
8//
9// Class code:MetaData::CompressedInteger provides secure access to a compressed integer (as defined in CLI
10// ECMA specification). The integer is compressed into 1, 2 or 4 bytes. See code:CompressedInteger#Format
11// for full format description.
12//
13// ======================================================================================
14
15#pragma once
16
17#include "compressedinteger.h"
18
19namespace MetaData
20{
21
22// --------------------------------------------------------------------------------------
23//
24// Returns TRUE if the value (nValue) fits into 1-byte, 2-bytes or 4-bytes encoding and fills
25// *pcbEncodingSize with 1, 2 or 4.
26// Returns FALSE if the value cannot be encoded as compressed integer, doesn't fill *pcbEncodingSize then.
27//
28__checkReturn
29//static
30inline
31BOOL
32CompressedInteger::GetEncodingSize(
33 UINT32 nValue,
34 __out UINT32 *pcbEncodingSize)
35{
36 // Does it fit into 1-byte encoding?
37 if (nValue <= const_Max1Byte)
38 { // The value fits into 1 byte (binary format 0xxx xxxx)
39 *pcbEncodingSize = 1;
40 return TRUE;
41 }
42 // Does it fit into 2-bytes encoding?
43 if (nValue <= const_Max2Bytes)
44 { // The value fits into 2 bytes (binary format 10xx xxxx yyyy yyyy)
45 *pcbEncodingSize = 2;
46 return TRUE;
47 }
48 // Does it fit into 4-bytes encoding?
49 if (nValue <= const_Max4Bytes)
50 { // The value fits into 4 bytes (binary format 110x xxxx yyyy yyyy zzzz zzzz wwww wwww)
51 *pcbEncodingSize = 4;
52 return TRUE;
53 }
54 // The value cannot be encoded as compressed integer
55 return FALSE;
56} // CompressedInteger::GetEncodingSize
57
58// --------------------------------------------------------------------------------------
59//
60// Returns TRUE if the value (nValue) fits into 1-byte, 2-bytes or 4-bytes encoding and fills
61// *pcbEncodingSize with 1, 2 or 4 and *pnEncodedValue with the encoded value.
62// Returns FALSE if the value cannot be encoded as compressed integer, doesn't fill *pcbEncodingSize
63// nor *pnEncodedValue then.
64//
65__checkReturn
66//static
67inline
68BOOL
69CompressedInteger::Encode(
70 UINT32 nValue,
71 __out UINT32 *pnEncodedValue,
72 __out UINT32 *pcbEncodingSize)
73{
74 // Does it fit into 1-byte encoding?
75 if (nValue <= const_Max1Byte)
76 { // The value fits into 1 byte (binary format 0xxx xxxx)
77 *pnEncodedValue = nValue;
78 *pcbEncodingSize = 1;
79 return TRUE;
80 }
81 // Does it fit into 2-bytes encoding?
82 if (nValue <= const_Max2Bytes)
83 { // The value fits into 2 bytes (binary format 10xx xxxx yyyy yyyy)
84 *pnEncodedValue = 0x8000 | nValue;
85 *pcbEncodingSize = 2;
86 return TRUE;
87 }
88 // Does it fit into 4-bytes encoding?
89 if (nValue <= const_Max4Bytes)
90 { // The value fits into 4 bytes (binary format 110x xxxx yyyy yyyy zzzz zzzz wwww wwww)
91 *pnEncodedValue = 0xC0000000 | nValue;
92 *pcbEncodingSize = 4;
93 return TRUE;
94 }
95 // The value cannot be encoded as compressed integer
96 return FALSE;
97} // CompressedInteger::Encode
98
99}; // namespace MetaData
100