summaryrefslogtreecommitdiffstats
path: root/chrome/browser/resources/file_manager/js/byte_reader.js
blob: 772973ac0949680d566cb43edaed28932dd93bac (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

ByteReader = function(arrayBuffer) {
  this.buf_ = arrayBuffer;
  this.view_ = new DataView(arrayBuffer);
  this.pos_ = 0;
  this.seekStack_ = [];
  this.setByteOrder(ByteReader.BIG_ENDIAN);
};

// Static const and methods.

ByteReader.LITTLE_ENDIAN = 0;  // Intel, 0x1234 is [0x34, 0x12]
ByteReader.BIG_ENDIAN = 1;  // Motorola, 0x1234 is [0x12, 0x34]

ByteReader.SEEK_BEG = 0;  // Seek relative to the beginning of the buffer.
ByteReader.SEEK_CUR = 1;  // Seek relative to the current position.
ByteReader.SEEK_END = 2;  // Seek relative to the end of the buffer.

/**
 * Throw an error if (0 > pos >= end) or if (pos + size > end).
 *
 * Static utility function.
 */
ByteReader.validateRead = function(pos, size, end) {
  if (pos < 0 || pos >= end)
    throw 'Invalid read position';

  if (pos + size > end)
    throw 'Read past end of buffer';
};

/**
 * Read as a sequence of characters, returning them as a single string.
 *
 * This is a static utility function.  There is a member function with the
 * same name which side-effects the current read position.
 */
ByteReader.readString = function(dataView, pos, size, opt_end) {
  ByteReader.validateRead(pos, size, opt_end || dataView.byteLength);

  var codes = [];

  for (var i = 0; i < size; ++i)
    codes.push(dataView.getUint8(pos + i));

  return String.fromCharCode.apply(null, codes);
};

/**
 * Read as a sequence of characters, returning them as a single string.
 *
 * This is a static utility function.  There is a member function with the
 * same name which side-effects the current read position.
 */
ByteReader.readNullTerminatedString = function(dataView, pos, size, opt_end) {
  ByteReader.validateRead(pos, size, opt_end || dataView.byteLength);

  var codes = [];

  for (var i = 0; i < size; ++i) {
    var code = dataView.getUint8(pos + i);
    if (code == 0) break;
    codes.push(code);
  }

  return String.fromCharCode.apply(null, codes);
};

ByteReader.base64Alphabet_ =
    ('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/').
    split('');

/**
 * Read as a sequence of bytes, returning them as a single base64 encoded
 * string.
 *
 * This is a static utility function.  There is a member function with the
 * same name which side-effects the current read position.
 */
ByteReader.readBase64 = function(dataView, pos, size, opt_end) {
  ByteReader.validateRead(pos, size, opt_end || dataView.byteLength);

  var rv = [];
  var chars = [];
  var padding = 0;

  for (var i = 0; i < size; /* incremented inside */) {
    var bits = dataView.getUint8(pos + (i++)) << 16;

    if (i < size) {
      bits |= dataView.getUint8(pos + (i++)) << 8;

      if (i < size) {
        bits |= dataView.getUint8(pos + (i++));
      } else {
        padding = 1;
      }
    } else {
      padding = 2;
    }

    chars[3] = ByteReader.base64Alphabet_[bits & 63];
    chars[2] = ByteReader.base64Alphabet_[(bits >> 6) & 63];
    chars[1] = ByteReader.base64Alphabet_[(bits >> 12) & 63];
    chars[0] = ByteReader.base64Alphabet_[(bits >> 18) & 63];

    rv.push.apply(rv, chars);
  }

  if (padding > 0)
    rv[rv.length - 1] = '=';
  if (padding > 1)
    rv[rv.length - 2] = '=';

  return rv.join('');
};

/**
 * Read as an image encoded in a data url.
 *
 * This is a static utility function.  There is a member function with the
 * same name which side-effects the current read position.
 */
ByteReader.readImage = function(dataView, pos, size, opt_end) {
  opt_end = opt_end || dataView.byteLength;
  ByteReader.validateRead(pos, size, opt_end);

  var format;
  if (ByteReader.readString(dataView, pos, 4, opt_end) == '\x89PNG') {
    format = 'png';
  } else if (dataView.getUint16(pos, false) == 0xFFD8) {  // Always big endian.
    format = 'jpeg';
  } else {
    format = 'unknown';
  }

  var b64 = ByteReader.readBase64(dataView, pos, size, opt_end);
  return 'data:image/' + format + ';base64,' + b64;
};

// Instance methods.

/**
 * Return true if the current position is past the end of the buffer.
 */
ByteReader.prototype.eof = function() {
  return this.pos_ >= this.view_.byteLength;
};

/**
 * Return true if the current position is before the beginning of the buffer.
 */
ByteReader.prototype.bof = function() {
  return this.pos_ < 0;
};

/**
 * Return true if the current position is oustide the buffer.
 */
ByteReader.prototype.beof = function() {
  return this.pos_ >= this.view_.byteLength || this.pos_ < 0;
};

/**
 * Set the expected byte ordering for future reads.
 */
ByteReader.prototype.setByteOrder = function(order) {
  this.littleEndian_ = order == ByteReader.LITTLE_ENDIAN;
};

/**
 * Throw an error if the reader is at an invalid position, or if a read a read
 * of |size| would put it in one.
 *
 * You may optionally pass opt_end to override what is considered to be the
 * end of the buffer.
 */
ByteReader.prototype.validateRead = function(size, opt_end) {
  if (typeof opt_end == 'undefined')
    opt_end = this.view_.byteLength;

  ByteReader.validateRead(this.view_, this.pos_, size, opt_end);
};

ByteReader.prototype.readScalar = function(width, opt_signed, opt_end) {
  var method = opt_signed ? 'getInt' : 'getUint';

  switch (width) {
    case 1:
      method += '8';
      break;

    case 2:
      method += '16';
      break;

    case 4:
      method += '32';
      break;

    case 8:
      method += '64';
      break;

    default:
      throw 'Invalid width: ' + width;
      break;
  }

  this.validateRead(width, opt_end);
  var rv = this.view_[method](this.pos_, this.littleEndian_);
  this.pos_ += width;
  return rv;
}

/**
 * Read as a sequence of characters, returning them as a single string.
 *
 * Adjusts the current position on success.  Throws an exception if the
 * read would go past the end of the buffer.
 */
ByteReader.prototype.readString = function(size, opt_end) {
  var rv = ByteReader.readString(this.view_, this.pos_, size, opt_end);
  this.pos_ += size;
  return rv;
};


/**
 * Read as a sequence of characters, returning them as a single string.
 *
 * Adjusts the current position on success.  Throws an exception if the
 * read would go past the end of the buffer.
 */
ByteReader.prototype.readNullTerminatedString = function(size, opt_end) {
  var rv = ByteReader.readNullTerminatedString(this.view_,
                                               this.pos_,
                                               size,
                                               opt_end);
  this.pos_ += rv.length;

  if (rv.length < size) {
    // If we've stopped reading because we found '0' but didn't hit size limit
    // then we should skip additional '0' character
    this.pos_++;
  }

  return rv;
};


/**
 * Read as an array of numbers.
 *
 * Adjusts the current position on success.  Throws an exception if the
 * read would go past the end of the buffer.
 */
ByteReader.prototype.readSlice = function(size, opt_end,
                                          opt_arrayConstructor) {
  this.validateRead(width, opt_end);

  var arrayConstructor = opt_arrayConstructor || Uint8Array;
  var slice = new arrayConstructor(this.buf_, this.pos_, size);
  this.pos_ += size;

  return slice;
};

/**
 * Read as a sequence of bytes, returning them as a single base64 encoded
 * string.
 *
 * Adjusts the current position on success.  Throws an exception if the
 * read would go past the end of the buffer.
 */
ByteReader.prototype.readBase64 = function(size, opt_end) {
  var rv = ByteReader.readBase64(this.view_, this.pos_, size, opt_end);
  this.pos_ += size;
  return rv;
};

/**
 * Read an image returning it as a data url.
 *
 * Adjusts the current position on success.  Throws an exception if the
 * read would go past the end of the buffer.
 */
ByteReader.prototype.readImage = function(size, opt_end) {
  var rv = ByteReader.readImage(this.view_, this.pos_, size, opt_end);
  this.pos_ += size;
  return rv;
};

/**
 * Seek to a give position relative to opt_seekStart.
 */
ByteReader.prototype.seek = function(pos, opt_seekStart, opt_end) {
  opt_end = opt_end || this.view_.byteLength;

  var newPos;
  if (opt_seekStart == ByteReader.SEEK_CUR) {
    newPos = this.pos_ + pos;
  } else if (opt_seekStart == ByteReader.SEEK_END) {
    newPos = opt_end + pos;
  } else {
    newPos = pos;
  }

  if (newPos < 0 || newPos >= this.view_.byteLength)
    throw 'Seek outside of buffer: ' + (newPos - opt_end);

  this.pos_ = newPos;
};

/**
 * Seek to a given position relative to opt_seekStart, saving the current
 * position.
 *
 * Recover the current position with a call to seekPop.
 */
ByteReader.prototype.pushSeek = function(pos, opt_seekStart) {
  var oldPos = this.pos_;
  this.seek(pos, opt_seekStart);
  // Alter the seekStack_ after the call to seek(), in case it throws.
  this.seekStack_.push(oldPos);
};

/**
 * Undo a previous seekPush.
 */
ByteReader.prototype.popSeek = function() {
  this.seek(this.seekStack_.pop());
};

/**
 * Return the current read position.
 */
ByteReader.prototype.tell = function() {
  return this.pos_;
};