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.

FileSharingRecord.java 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. package org.apache.poi.hssf.record;
  16. import java.util.Map;
  17. import java.util.function.Supplier;
  18. import org.apache.poi.util.GenericRecordUtil;
  19. import org.apache.poi.util.LittleEndianOutput;
  20. import org.apache.poi.util.StringUtil;
  21. /**
  22. * Stores the encrypted readonly for a workbook (write protect).<p>
  23. * This functionality is accessed from the options dialog box available when performing 'Save As'.
  24. */
  25. public final class FileSharingRecord extends StandardRecord {
  26. public static final short sid = 0x005B;
  27. private short field_1_readonly;
  28. private short field_2_password;
  29. private byte field_3_username_unicode_options;
  30. private String field_3_username_value;
  31. public FileSharingRecord() {}
  32. public FileSharingRecord(FileSharingRecord other) {
  33. super(other);
  34. field_1_readonly = other.field_1_readonly;
  35. field_2_password = other.field_2_password;
  36. field_3_username_unicode_options = other.field_3_username_unicode_options;
  37. field_3_username_value = other.field_3_username_value;
  38. }
  39. public FileSharingRecord(RecordInputStream in) {
  40. field_1_readonly = in.readShort();
  41. field_2_password = in.readShort();
  42. int nameLen = in.readShort();
  43. if(nameLen > 0) {
  44. // TODO - Current examples(3) from junits only have zero length username.
  45. field_3_username_unicode_options = in.readByte();
  46. field_3_username_value = in.readCompressedUnicode(nameLen);
  47. } else {
  48. field_3_username_value = "";
  49. }
  50. }
  51. /**
  52. * set the readonly flag
  53. *
  54. * @param readonly 1 for true, not 1 for false
  55. */
  56. public void setReadOnly(short readonly) {
  57. field_1_readonly = readonly;
  58. }
  59. /**
  60. * get the readonly
  61. *
  62. * @return short representing if this is read only (1 = true)
  63. */
  64. public short getReadOnly() {
  65. return field_1_readonly;
  66. }
  67. /**
  68. * @param password hashed password
  69. */
  70. public void setPassword(short password) {
  71. field_2_password = password;
  72. }
  73. /**
  74. * @return password hashed with hashPassword() (very lame)
  75. */
  76. public short getPassword() {
  77. return field_2_password;
  78. }
  79. /**
  80. * @return username of the user that created the file
  81. */
  82. public String getUsername() {
  83. return field_3_username_value;
  84. }
  85. /**
  86. * @param username of the user that created the file
  87. */
  88. public void setUsername(String username) {
  89. field_3_username_value = username;
  90. }
  91. public void serialize(LittleEndianOutput out) {
  92. // TODO - junit
  93. out.writeShort(getReadOnly());
  94. out.writeShort(getPassword());
  95. out.writeShort(field_3_username_value.length());
  96. if(field_3_username_value.length() > 0) {
  97. out.writeByte(field_3_username_unicode_options);
  98. StringUtil.putCompressedUnicode(getUsername(), out);
  99. }
  100. }
  101. protected int getDataSize() {
  102. int nameLen = field_3_username_value.length();
  103. if (nameLen < 1) {
  104. return 6;
  105. }
  106. return 7+nameLen;
  107. }
  108. public short getSid() {
  109. return sid;
  110. }
  111. @Override
  112. public FileSharingRecord copy() {
  113. return new FileSharingRecord(this);
  114. }
  115. @Override
  116. public HSSFRecordTypes getGenericRecordType() {
  117. return HSSFRecordTypes.FILE_SHARING;
  118. }
  119. @Override
  120. public Map<String, Supplier<?>> getGenericProperties() {
  121. return GenericRecordUtil.getGenericProperties(
  122. "readOnly", this::getReadOnly,
  123. "password", this::getPassword,
  124. "username", this::getUsername
  125. );
  126. }
  127. }