a .NET library that can read/write Office formats without Microsoft Office installed. No COM+, no interop.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

143 lines
4.8 KiB

  1. /* ====================================================================
  2. Licensed to the Apache Software Foundation (ASF) Under one or more
  3. contributor license agreements. See the NOTICE file distributed with
  4. this work for Additional information regarding copyright ownership.
  5. The ASF licenses this file to You Under the Apache License, Version 2.0
  6. (the "License"); you may not use this file except in compliance with
  7. the License. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed Under the License is distributed on an "AS Is" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations Under the License.
  14. ==================================================================== */
  15. using System;
  16. using NPOI.Util;
  17. namespace NPOI.HPSF
  18. {
  19. public class Array
  20. {
  21. internal sealed class ArrayDimension
  22. {
  23. public const int SIZE = 8;
  24. private int _indexOffset;
  25. internal long _size;
  26. public ArrayDimension(byte[] data, int offset)
  27. {
  28. _size = LittleEndian.GetUInt(data, offset);
  29. _indexOffset = LittleEndian.GetInt(data, offset
  30. + LittleEndian.INT_SIZE);
  31. }
  32. }
  33. internal sealed class ArrayHeader
  34. {
  35. private readonly ArrayDimension[] _dimensions;
  36. internal int _type;
  37. public ArrayHeader(byte[] data, int startOffset)
  38. {
  39. int offset = startOffset;
  40. _type = LittleEndian.GetInt(data, offset);
  41. offset += LittleEndian.INT_SIZE;
  42. long numDimensionsUnsigned = LittleEndian.GetUInt(data, offset);
  43. offset += LittleEndian.INT_SIZE;
  44. if(!(1 <= numDimensionsUnsigned && numDimensionsUnsigned <= 31))
  45. throw new IllegalPropertySetDataException(
  46. "Array dimension number " + numDimensionsUnsigned
  47. + " is not in [1; 31] range");
  48. int numDimensions = (int) numDimensionsUnsigned;
  49. _dimensions = new ArrayDimension[numDimensions];
  50. for(int i = 0; i < numDimensions; i++)
  51. {
  52. _dimensions[i] = new ArrayDimension(data, offset);
  53. offset += ArrayDimension.SIZE;
  54. }
  55. }
  56. public long NumberOfScalarValues
  57. {
  58. get
  59. {
  60. long result = 1;
  61. foreach(ArrayDimension dimension in _dimensions)
  62. result *= dimension._size;
  63. return result;
  64. }
  65. }
  66. public int Size
  67. {
  68. get
  69. {
  70. return LittleEndian.INT_SIZE * 2 + _dimensions.Length
  71. * ArrayDimension.SIZE;
  72. }
  73. }
  74. public int Type
  75. {
  76. get { return _type; }
  77. }
  78. }
  79. private ArrayHeader _header;
  80. private TypedPropertyValue[] _values;
  81. public Array()
  82. {
  83. }
  84. public Array(byte[] data, int offset)
  85. {
  86. Read(data, offset);
  87. }
  88. public int Read(byte[] data, int startOffset)
  89. {
  90. int offset = startOffset;
  91. _header = new ArrayHeader(data, offset);
  92. offset += _header.Size;
  93. long numberOfScalarsLong = _header.NumberOfScalarValues;
  94. if(numberOfScalarsLong > int.MaxValue)
  95. throw new InvalidOperationException(
  96. "Sorry, but POI can't store array of properties with size of "
  97. + numberOfScalarsLong + " in memory");
  98. int numberOfScalars = (int) numberOfScalarsLong;
  99. _values = new TypedPropertyValue[numberOfScalars];
  100. int type = _header._type;
  101. if(type == Variant.VT_VARIANT)
  102. {
  103. for(int i = 0; i < numberOfScalars; i++)
  104. {
  105. TypedPropertyValue typedPropertyValue = new TypedPropertyValue();
  106. offset += typedPropertyValue.Read(data, offset);
  107. }
  108. }
  109. else
  110. {
  111. for(int i = 0; i < numberOfScalars; i++)
  112. {
  113. TypedPropertyValue typedPropertyValue = new TypedPropertyValue(
  114. type, null);
  115. offset += typedPropertyValue.ReadValuePadded(data, offset);
  116. }
  117. }
  118. return offset - startOffset;
  119. }
  120. }
  121. }