~ubuntu-branches/ubuntu/quantal/enigmail/quantal-security

« back to all changes in this revision

Viewing changes to mailnews/extensions/enigmail/ipc/modules/subprocess_worker_win.js

  • Committer: Package Import Robot
  • Author(s): Chris Coulson
  • Date: 2013-09-13 16:02:15 UTC
  • mfrom: (0.12.16)
  • Revision ID: package-import@ubuntu.com-20130913160215-u3g8nmwa0pdwagwc
Tags: 2:1.5.2-0ubuntu0.12.10.1
* New upstream release v1.5.2 for Thunderbird 24

* Build enigmail using a stripped down Thunderbird 17 build system, as it's
  now quite difficult to build the way we were doing previously, with the
  latest Firefox build system
* Add debian/patches/no_libxpcom.patch - Don't link against libxpcom, as it
  doesn't exist anymore (but exists in the build system)
* Add debian/patches/use_sdk.patch - Use the SDK version of xpt.py and
  friends
* Drop debian/patches/ipc-pipe_rename.diff (not needed anymore)
* Drop debian/patches/makefile_depth.diff (not needed anymore)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* ***** BEGIN LICENSE BLOCK *****
 
2
 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
 
3
 *
 
4
 * The contents of this file are subject to the Mozilla Public
 
5
 * License Version 1.1 (the "MPL"); you may not use this file
 
6
 * except in compliance with the MPL. You may obtain a copy of
 
7
 * the MPL at http://www.mozilla.org/MPL/
 
8
 *
 
9
 * Software distributed under the MPL is distributed on an "AS
 
10
 * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
 
11
 * implied. See the MPL for the specific language governing
 
12
 * rights and limitations under the MPL.
 
13
 *
 
14
 * The Original Code is subprocess.jsm.
 
15
 *
 
16
 * The Initial Developer of this code is Patrick Brunschwig.
 
17
 * Portions created by Patrick Brunschwig <patrick@enigmail.net>
 
18
 * are Copyright (C) 2011 Patrick Brunschwig.
 
19
 * All Rights Reserved.
 
20
 *
 
21
 * Contributor(s):
 
22
 * Jan Gerber <j@mailb.org>
 
23
 *
 
24
 * Alternatively, the contents of this file may be used under the terms of
 
25
 * either the GNU General Public License Version 2 or later (the "GPL"), or
 
26
 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
 
27
 * in which case the provisions of the GPL or the LGPL are applicable instead
 
28
 * of those above. If you wish to allow use of your version of this file only
 
29
 * under the terms of either the GPL or the LGPL, and not to allow others to
 
30
 * use your version of this file under the terms of the MPL, indicate your
 
31
 * decision by deleting the provisions above and replace them with the notice
 
32
 * and other provisions required by the GPL or the LGPL. If you do not delete
 
33
 * the provisions above, a recipient may use your version of this file under
 
34
 * the terms of any one of the MPL, the GPL or the LGPL.
 
35
 * ***** END LICENSE BLOCK ***** */
 
36
 
 
37
/*
 
38
 * ChromeWorker Object subprocess.jsm on Windows to process stdin/stdout/stderr
 
39
 * on separate threads.
 
40
 *
 
41
 */
 
42
 
 
43
// Being a ChromeWorker object, implicitly uses the following:
 
44
// Components.utils.import("resource://gre/modules/ctypes.jsm");
 
45
 
 
46
'use strict';
 
47
 
 
48
const BufferSize = 1024;
 
49
 
 
50
const BOOL = ctypes.bool;
 
51
const HANDLE = ctypes.size_t;
 
52
const DWORD = ctypes.uint32_t;
 
53
const LPDWORD = DWORD.ptr;
 
54
const PVOID = ctypes.voidptr_t;
 
55
const LPVOID = PVOID;
 
56
 
 
57
/*
 
58
typedef struct _OVERLAPPED {
 
59
  ULONG_PTR Internal;
 
60
  ULONG_PTR InternalHigh;
 
61
  union {
 
62
    struct {
 
63
      DWORD Offset;
 
64
      DWORD OffsetHigh;
 
65
    };
 
66
    PVOID  Pointer;
 
67
  };
 
68
  HANDLE    hEvent;
 
69
} OVERLAPPED, *LPOVERLAPPED;
 
70
*/
 
71
const OVERLAPPED = new ctypes.StructType("OVERLAPPED");
 
72
 
 
73
var ReadFileBuffer = ctypes.char.array(BufferSize);
 
74
var WriteFileBuffer = ctypes.uint8_t.array(BufferSize);
 
75
 
 
76
var kernel32dll = null;
 
77
var libFunc = {};
 
78
 
 
79
function initLib(libName) {
 
80
    if (ctypes.size_t.size == 8) {
 
81
        var WinABI = ctypes.default_abi;
 
82
    } else {
 
83
        var WinABI = ctypes.winapi_abi;
 
84
    }
 
85
 
 
86
    kernel32dll = ctypes.open(libName);
 
87
 
 
88
    /*
 
89
    BOOL WINAPI WriteFile(
 
90
      __in         HANDLE hFile,
 
91
      __in         LPCVOID lpBuffer,
 
92
      __in         DWORD nNumberOfBytesToWrite,
 
93
      __out_opt    LPDWORD lpNumberOfBytesWritten,
 
94
      __inout_opt  LPOVERLAPPED lpOverlapped
 
95
    );
 
96
 
 
97
    NOTE: lpBuffer is declared as array of unsigned int8 instead of char to avoid
 
98
           implicit charset conversion
 
99
    */
 
100
    libFunc.WriteFile = kernel32dll.declare("WriteFile",
 
101
                                        WinABI,
 
102
                                        BOOL,
 
103
                                        HANDLE,
 
104
                                        WriteFileBuffer,
 
105
                                        DWORD,
 
106
                                        LPDWORD,
 
107
                                        OVERLAPPED.ptr
 
108
    );
 
109
 
 
110
    /*
 
111
    BOOL WINAPI ReadFile(
 
112
      __in         HANDLE hFile,
 
113
      __out        LPVOID ReadFileBuffer,
 
114
      __in         DWORD nNumberOfBytesToRead,
 
115
      __out_opt    LPDWORD lpNumberOfBytesRead,
 
116
      __inout_opt  LPOVERLAPPED lpOverlapped
 
117
    );
 
118
    */
 
119
    libFunc.ReadFile = kernel32dll.declare("ReadFile",
 
120
                                        WinABI,
 
121
                                        BOOL,
 
122
                                        HANDLE,
 
123
                                        ReadFileBuffer,
 
124
                                        DWORD,
 
125
                                        LPDWORD,
 
126
                                        OVERLAPPED.ptr
 
127
    );
 
128
 
 
129
    /*
 
130
    BOOL WINAPI CloseHandle(
 
131
      __in  HANDLE hObject
 
132
    );
 
133
    */
 
134
    libFunc.CloseHandle = kernel32dll.declare("CloseHandle",
 
135
                                            WinABI,
 
136
                                            BOOL,
 
137
                                            HANDLE
 
138
    );
 
139
}
 
140
 
 
141
// Windows error codes
 
142
const ERROR_HANDLE_EOF = 38;
 
143
const ERROR_BROKEN_PIPE = 109;
 
144
 
 
145
function writePipe(pipe, data) {
 
146
    var bytesWritten = DWORD(0);
 
147
 
 
148
    var pData = new WriteFileBuffer();
 
149
 
 
150
    var numChunks = Math.floor(data.length / BufferSize);
 
151
    for (var chunk = 0; chunk <= numChunks; chunk ++) {
 
152
        var numBytes = chunk < numChunks ? BufferSize : data.length - chunk * BufferSize;
 
153
        for (var i=0; i < numBytes; i++) {
 
154
            pData[i] = data.charCodeAt(chunk * BufferSize + i) % 256;
 
155
        }
 
156
 
 
157
      var r = libFunc.WriteFile(pipe, pData, numBytes, bytesWritten.address(), null);
 
158
      if (bytesWritten.value != numBytes)
 
159
          throw("error: wrote "+bytesWritten.value+" instead of "+numBytes+" bytes");
 
160
    }
 
161
    postMessage("wrote "+data.length+" bytes of data");
 
162
}
 
163
 
 
164
function readString(data, length, charset) {
 
165
    var r = '';
 
166
    for(var i = 0; i < length; i++) {
 
167
        if(data[i] == 0 && charset != "null") // stop on NULL character for non-binary data
 
168
           break;
 
169
 
 
170
        r += String.fromCharCode(data[i]);
 
171
    }
 
172
 
 
173
    return r;
 
174
}
 
175
 
 
176
 
 
177
function readPipe(pipe, charset, bufferedOutput) {
 
178
    var dataStr = "";
 
179
    while (true) {
 
180
        var bytesRead = DWORD(0);
 
181
        var line = new ReadFileBuffer();
 
182
        var r = libFunc.ReadFile(pipe, line, BufferSize, bytesRead.address(), null);
 
183
 
 
184
        if (!r) {
 
185
            // stop if we get an error (such as EOF reached)
 
186
            let lastErr = ctypes.winLastError;
 
187
            switch (lastErr) {
 
188
                case ERROR_HANDLE_EOF:
 
189
                case ERROR_BROKEN_PIPE:
 
190
                    postMessage({msg: "info", data: "EOF reached"});
 
191
                    break;
 
192
                default:
 
193
                    postMessage({msg: "error", data: "Windows error " + lastErr});
 
194
            }
 
195
            break;
 
196
        }
 
197
 
 
198
        if (bytesRead.value > 0) {
 
199
            var c = readString(line, bytesRead.value, charset);
 
200
            if (!bufferedOutput)
 
201
              postMessage({msg: "data", data: c, count: c.length});
 
202
            else
 
203
              dataStr += c;
 
204
        }
 
205
        else {
 
206
            break;
 
207
        }
 
208
    }
 
209
 
 
210
    if (bufferedOutput)
 
211
      postMessage({msg: "data", data: dataStr, count: dataStr.length});
 
212
 
 
213
    libFunc.CloseHandle(pipe);
 
214
    postMessage({msg: "done"});
 
215
    kernel32dll.close();
 
216
    close();
 
217
}
 
218
 
 
219
onmessage = function (event) {
 
220
    let pipePtr;
 
221
    switch (event.data.msg) {
 
222
    case "init":
 
223
        initLib(event.data.libc);
 
224
        postMessage("InitOK");
 
225
        break;
 
226
    case "write":
 
227
        // data contents:
 
228
        //   msg: 'write'
 
229
        //   data: the data (string) to write
 
230
        //   pipe: ptr to pipe
 
231
        pipePtr = HANDLE.ptr(event.data.pipe);
 
232
        writePipe(pipePtr.contents, event.data.data);
 
233
        postMessage("WriteOK");
 
234
        break;
 
235
    case "read":
 
236
        initLib(event.data.libc);
 
237
        pipePtr = HANDLE.ptr(event.data.pipe);
 
238
        readPipe(pipePtr.contents, event.data.charset, event.data.bufferedOutput);
 
239
        break;
 
240
    case "close":
 
241
        pipePtr = HANDLE.ptr(event.data.pipe);
 
242
        postMessage("closing stdin\n");
 
243
 
 
244
        if (libFunc.CloseHandle(pipePtr.contents)) {
 
245
            postMessage("ClosedOK");
 
246
        }
 
247
        else
 
248
            postMessage("Could not close stdin handle");
 
249
        break;
 
250
    case "stop":
 
251
        kernel32dll.close();
 
252
        close();
 
253
        break;
 
254
    default:
 
255
        throw("error: Unknown command"+event.data.msg+"\n");
 
256
    }
 
257
    return;
 
258
};