Source: util/signed-blob.js

  1. /**
  2. * Copyright (C) 2013 Regents of the University of California.
  3. * @author: Jeff Thompson <jefft0@remap.ucla.edu>
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU Lesser General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. * A copy of the GNU Lesser General Public License is in the file COPYING.
  18. */
  19. /** @ignore */
  20. var Blob = require('./blob.js').Blob;
  21. /**
  22. * A SignedBlob extends Blob to keep the offsets of a signed portion (e.g., the
  23. * bytes of Data packet). This inherits from Blob, including Blob.size and Blob.buf.
  24. * @param {Blob|Buffer|Array<number>} value (optional) If value is a Blob, take
  25. * another pointer to the Buffer without copying. If value is a Buffer or byte
  26. * array, copy to create a new Buffer. If omitted, buf() will return null.
  27. * @param {number} signedPortionBeginOffset (optional) The offset in the
  28. * encoding of the beginning of the signed portion. If omitted, set to 0.
  29. * @param {number} signedPortionEndOffset (optional) The offset in the encoding
  30. * of the end of the signed portion. If omitted, set to 0.
  31. * @constructor
  32. */
  33. var SignedBlob = function SignedBlob(value, signedPortionBeginOffset, signedPortionEndOffset)
  34. {
  35. // Call the base constructor.
  36. Blob.call(this, value);
  37. if (this.buffer == null) {
  38. this.signedPortionBeginOffset = 0;
  39. this.signedPortionEndOffset = 0;
  40. }
  41. else if (typeof value === 'object' && value instanceof SignedBlob) {
  42. // Copy the SignedBlob, allowing override for offsets.
  43. this.signedPortionBeginOffset = signedPortionBeginOffset == null ?
  44. value.signedPortionBeginOffset : signedPortionBeginOffset;
  45. this.signedPortionEndOffset = signedPortionEndOffset == null ?
  46. value.signedPortionEndOffset : signedPortionEndOffset;
  47. }
  48. else {
  49. this.signedPortionBeginOffset = signedPortionBeginOffset || 0;
  50. this.signedPortionEndOffset = signedPortionEndOffset || 0;
  51. }
  52. if (this.buffer == null)
  53. this.signedBuffer = null;
  54. else
  55. this.signedBuffer = this.buffer.slice
  56. (this.signedPortionBeginOffset, this.signedPortionEndOffset);
  57. };
  58. SignedBlob.prototype = new Blob();
  59. SignedBlob.prototype.name = "SignedBlob";
  60. exports.SignedBlob = SignedBlob;
  61. /**
  62. * Return the length of the signed portion of the immutable byte array.
  63. * @return {number} The length of the signed portion. If signedBuf() is null,
  64. * return 0.
  65. */
  66. SignedBlob.prototype.signedSize = function()
  67. {
  68. if (this.signedBuffer != null)
  69. return this.signedBuffer.length;
  70. else
  71. return 0;
  72. };
  73. /**
  74. * Return a the signed portion of the immutable byte array.
  75. * @return {Buffer} A slice into the Buffer which is the signed portion.
  76. * If the pointer to the array is null, return null.
  77. */
  78. SignedBlob.prototype.signedBuf = function() { return this.signedBuffer; };
  79. /**
  80. * Return the offset in the array of the beginning of the signed portion.
  81. * @return {number} The offset in the array.
  82. */
  83. SignedBlob.prototype.getSignedPortionBeginOffset = function()
  84. {
  85. return this.signedPortionBeginOffset;
  86. };
  87. /**
  88. * Return the offset in the array of the end of the signed portion.
  89. * @return {number} The offset in the array.
  90. */
  91. SignedBlob.prototype.getSignedPortionEndOffset = function()
  92. {
  93. return this.signedPortionEndOffset;
  94. };