OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 define("mojo/public/js/bindings/validator", [ | |
6 "mojo/public/js/bindings/codec", | |
7 ], function(codec) { | |
8 | |
9 var validationError = { | |
10 NONE: 'VALIDATION_ERROR_NONE', | |
11 MISALIGNED_OBJECT: 'VALIDATION_ERROR_MISALIGNED_OBJECT', | |
12 ILLEGAL_MEMORY_RANGE: 'VALIDATION_ERROR_ILLEGAL_MEMORY_RANGE', | |
13 UNEXPECTED_STRUCT_HEADER: 'VALIDATION_ERROR_UNEXPECTED_STRUCT_HEADER', | |
14 UNEXPECTED_ARRAY_HEADER: 'VALIDATION_ERROR_UNEXPECTED_ARRAY_HEADER', | |
15 ILLEGAL_HANDLE: 'VALIDATION_ERROR_ILLEGAL_HANDLE', | |
16 ILLEGAL_POINTER: 'VALIDATION_ERROR_ILLEGAL_POINTER', | |
17 MESSAGE_HEADER_INVALID_FLAG_COMBINATION: | |
18 'VALIDATION_ERROR_MESSAGE_HEADER_INVALID_FLAG_COMBINATION', | |
19 MESSAGE_HEADER_MISSING_REQUEST_ID: | |
20 'VALIDATION_ERROR_MESSAGE_HEADER_MISSING_REQUEST_ID' | |
21 }; | |
22 | |
23 function Validator(message) { | |
24 this.message = message; | |
25 this.offset = 0; | |
26 } | |
27 | |
28 Object.defineProperty(Validator.prototype, "offsetLimit", { | |
29 get: function() { return this.message.buffer.byteLength; } | |
30 }); | |
31 | |
32 // True if we can safely allocate a block of bytes from start to | |
33 // to start + numBytes. | |
34 Validator.prototype.isValidRange = function(start, numBytes) { | |
35 // Only positive JavaScript integers that are less than 2^53 | |
36 // (Number.MAX_SAFE_INTEGER) can be represented exactly. | |
37 if (start < this.offset || numBytes <= 0 || | |
38 !Number.isSafeInteger(start) || | |
39 !Number.isSafeInteger(numBytes)) | |
40 return false; | |
41 | |
42 var newOffset = start + numBytes; | |
43 if (!Number.isSafeInteger(newOffset) || newOffset > this.offsetLimit) | |
44 return false; | |
45 | |
46 return true; | |
47 } | |
48 | |
49 Validator.prototype.claimRange = function(start, numBytes) { | |
50 if (this.isValidRange(start, numBytes)) { | |
51 this.offset += start + numBytes; | |
yzshen1
2014/07/30 18:36:07
This line needs to be changed, too.
hansmuller
2014/07/30 19:13:31
Yes. Sorry to have missed that.
| |
52 return true; | |
53 } | |
54 return false; | |
55 } | |
56 | |
57 Validator.prototype.validateStructHeader = | |
58 function(offset, minNumBytes, minNumFields) { | |
59 if (!codec.isAligned(offset)) | |
60 return validationError.MISALIGNED_OBJECT; | |
61 | |
62 if (!this.isValidRange(offset, codec.kStructHeaderSize)) | |
63 return validationError.ILLEGAL_MEMORY_RANGE; | |
64 | |
65 var numBytes = this.message.buffer.getUint32(offset); | |
66 var numFields = this.message.buffer.getUint32(offset + 4); | |
67 | |
68 if (numBytes < minNumBytes || numFields < minNumFields) | |
69 return validationError.UNEXPECTED_STRUCT_HEADER; | |
70 | |
71 if (!this.claimRange(offset, numBytes)) | |
72 return validationError.ILLEGAL_MEMORY_RANGE; | |
73 | |
74 return validationError.NONE; | |
75 } | |
76 | |
77 Validator.prototype.validateMessageHeader = function() { | |
78 var numBytes = this.message.getHeaderNumBytes(); | |
79 var numFields = this.message.getHeaderNumFields(); | |
80 | |
81 var validNumFieldsAndNumBytes = | |
82 (numFields == 2 && numBytes == codec.kMessageHeaderSize) || | |
83 (numFields == 3 && | |
84 numBytes == codec.kMessageWithRequestIDHeaderSize) || | |
85 (numFields > 3 && | |
86 numBytes >= codec.kMessageWithRequestIDHeaderSize); | |
87 if (!validNumFieldsAndNumBytes) | |
88 return validationError.UNEXPECTED_STRUCT_HEADER; | |
89 | |
90 var expectsResponse = this.message.expectsResponse(); | |
91 var isResponse = this.message.isResponse(); | |
92 | |
93 if (numFields == 2 && (expectsResponse || isResponse)) | |
94 return validationError.MESSAGE_HEADER_MISSING_REQUEST_ID; | |
95 | |
96 if (isResponse && expectsResponse) | |
97 return validationError.MESSAGE_HEADER_INVALID_FLAG_COMBINATION; | |
98 | |
99 return validationError.NONE; | |
100 } | |
101 | |
102 Validator.prototype.validateMessage = function() { | |
103 var err = this.validateStructHeader(0, codec.kStructHeaderSize, 2); | |
104 if (err != validationError.NONE) | |
105 return err; | |
106 | |
107 return this.validateMessageHeader(); | |
108 } | |
109 | |
110 var exports = {}; | |
111 exports.validationError = validationError; | |
112 exports.Validator = Validator; | |
113 return exports; | |
114 }); | |
OLD | NEW |