OLD | NEW |
(Empty) | |
| 1 /* Copyright (c) 2012 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 |
| 6 #include <assert.h> |
| 7 #include <stdarg.h> |
| 8 #include <stdio.h> |
| 9 #include <stdlib.h> |
| 10 #include <string.h> |
| 11 |
| 12 #include "ppapi/c/pp_errors.h" |
| 13 #include "ppapi/c/pp_module.h" |
| 14 #include "ppapi/c/pp_var.h" |
| 15 #include "ppapi/c/ppb.h" |
| 16 #include "ppapi/c/ppb_instance.h" |
| 17 #include "ppapi/c/ppb_messaging.h" |
| 18 #include "ppapi/c/ppb_var.h" |
| 19 #include "ppapi/c/ppp.h" |
| 20 #include "ppapi/c/ppp_instance.h" |
| 21 #include "ppapi/c/ppp_messaging.h" |
| 22 #include "nacl_mounts/kernel_intercept.h" |
| 23 |
| 24 |
| 25 #define MAX_OPEN_FILES 10 |
| 26 #define MAX_PARAMS 4 |
| 27 #define MIN(a, b) (((a) < (b)) ? (a) : (b)) |
| 28 |
| 29 #if defined(WIN32) |
| 30 #define va_copy(d, s) ((d) = (s)) |
| 31 #endif |
| 32 |
| 33 |
| 34 typedef int (*HandleFunc)(int num_params, char** params, char** output); |
| 35 typedef struct { |
| 36 const char* name; |
| 37 HandleFunc function; |
| 38 } FuncNameMapping; |
| 39 |
| 40 |
| 41 int HandleFopen(int num_params, char** params, char** output); |
| 42 int HandleFwrite(int num_params, char** params, char** output); |
| 43 int HandleFread(int num_params, char** params, char** output); |
| 44 int HandleFseek(int num_params, char** params, char** output); |
| 45 int HandleFclose(int num_params, char** params, char** output); |
| 46 |
| 47 |
| 48 static PPB_Messaging* ppb_messaging_interface = NULL; |
| 49 static PPB_Var* ppb_var_interface = NULL; |
| 50 static FILE* g_OpenFiles[MAX_OPEN_FILES]; |
| 51 static FuncNameMapping g_FunctionMap[] = { |
| 52 { "fopen", HandleFopen }, |
| 53 { "fwrite", HandleFwrite }, |
| 54 { "fread", HandleFread }, |
| 55 { "fseek", HandleFseek }, |
| 56 { "fclose", HandleFclose }, |
| 57 { NULL, NULL }, |
| 58 }; |
| 59 |
| 60 |
| 61 static struct PP_Var CStrToVar(const char* str) { |
| 62 if (ppb_var_interface != NULL) { |
| 63 return ppb_var_interface->VarFromUtf8(str, strlen(str)); |
| 64 } |
| 65 return PP_MakeUndefined(); |
| 66 } |
| 67 |
| 68 static char* VprintfToNewString(const char* format, va_list args) { |
| 69 va_list args_copy; |
| 70 int length; |
| 71 char* buffer; |
| 72 int result; |
| 73 |
| 74 va_copy(args_copy, args); |
| 75 length = vsnprintf(NULL, 0, format, args); |
| 76 buffer = (char*)malloc(length + 1); /* +1 for NULL-terminator. */ |
| 77 result = vsnprintf(&buffer[0], length + 1, format, args_copy); |
| 78 assert(result == length); |
| 79 return buffer; |
| 80 } |
| 81 |
| 82 static char* PrintfToNewString(const char* format, ...) { |
| 83 va_list args; |
| 84 char* result; |
| 85 va_start(args, format); |
| 86 result = VprintfToNewString(format, args); |
| 87 va_end(args); |
| 88 return result; |
| 89 } |
| 90 |
| 91 static struct PP_Var PrintfToVar(const char* format, ...) { |
| 92 if (ppb_var_interface != NULL) { |
| 93 char* string; |
| 94 va_list args; |
| 95 struct PP_Var var; |
| 96 |
| 97 va_start(args, format); |
| 98 string = VprintfToNewString(format, args); |
| 99 va_end(args); |
| 100 |
| 101 var = ppb_var_interface->VarFromUtf8(string, strlen(string)); |
| 102 free(string); |
| 103 |
| 104 return var; |
| 105 } |
| 106 |
| 107 return PP_MakeUndefined(); |
| 108 } |
| 109 |
| 110 static uint32_t VarToCStr(struct PP_Var var, char* buffer, uint32_t length) { |
| 111 if (ppb_var_interface != NULL) { |
| 112 uint32_t var_length; |
| 113 const char* str = ppb_var_interface->VarToUtf8(var, &var_length); |
| 114 /* str is NOT NULL-terminated. Copy using memcpy. */ |
| 115 uint32_t min_length = MIN(var_length, length - 1); |
| 116 memcpy(buffer, str, min_length); |
| 117 buffer[min_length] = 0; |
| 118 |
| 119 return min_length; |
| 120 } |
| 121 |
| 122 return 0; |
| 123 } |
| 124 |
| 125 static int AddFileToMap(FILE* file) { |
| 126 int i; |
| 127 assert(file != NULL); |
| 128 for (i = 0; i < MAX_OPEN_FILES; ++i) { |
| 129 if (g_OpenFiles[i] == NULL) { |
| 130 g_OpenFiles[i] = file; |
| 131 return i; |
| 132 } |
| 133 } |
| 134 |
| 135 return -1; |
| 136 } |
| 137 |
| 138 static void RemoveFileFromMap(int i) { |
| 139 assert(i >= 0 && i < MAX_OPEN_FILES); |
| 140 g_OpenFiles[i] = NULL; |
| 141 } |
| 142 |
| 143 static FILE* GetFileFromMap(int i) { |
| 144 assert(i >= 0 && i < MAX_OPEN_FILES); |
| 145 return g_OpenFiles[i]; |
| 146 } |
| 147 |
| 148 static FILE* GetFileFromIndexString(const char* s, int* file_index) { |
| 149 char* endptr; |
| 150 int result = strtol(s, &endptr, 10); |
| 151 if (endptr != s + strlen(s)) { |
| 152 /* Garbage at the end of the number...? */ |
| 153 return NULL; |
| 154 } |
| 155 |
| 156 if (file_index) |
| 157 *file_index = result; |
| 158 |
| 159 return GetFileFromMap(result); |
| 160 } |
| 161 |
| 162 int HandleFopen(int num_params, char** params, char** output) { |
| 163 FILE* file; |
| 164 int file_index; |
| 165 const char* filename; |
| 166 const char* mode; |
| 167 |
| 168 if (num_params != 2) { |
| 169 *output = PrintfToNewString("Error: fopen takes 2 parameters."); |
| 170 return 1; |
| 171 } |
| 172 |
| 173 filename = params[0]; |
| 174 mode = params[1]; |
| 175 |
| 176 file = fopen(filename, mode); |
| 177 if (!file) { |
| 178 *output = PrintfToNewString("Error: fopen returned a NULL FILE*."); |
| 179 return 2; |
| 180 } |
| 181 |
| 182 file_index = AddFileToMap(file); |
| 183 if (file_index == -1) { |
| 184 *output = PrintfToNewString( |
| 185 "Error: Example only allows %d open file handles.", MAX_OPEN_FILES); |
| 186 return 3; |
| 187 } |
| 188 |
| 189 *output = PrintfToNewString("fopen\1%s\1%d", filename, file_index); |
| 190 return 0; |
| 191 } |
| 192 |
| 193 int HandleFwrite(int num_params, char** params, char** output) { |
| 194 FILE* file; |
| 195 const char* file_index_string; |
| 196 const char* data; |
| 197 size_t data_len; |
| 198 size_t bytes_written; |
| 199 |
| 200 if (num_params != 2) { |
| 201 *output = PrintfToNewString("Error: fwrite takes 2 parameters."); |
| 202 return 1; |
| 203 } |
| 204 |
| 205 file_index_string = params[0]; |
| 206 file = GetFileFromIndexString(file_index_string, NULL); |
| 207 data = params[1]; |
| 208 data_len = strlen(data); |
| 209 |
| 210 if (!file) { |
| 211 *output = PrintfToNewString("Error: Unknown file handle %s.", |
| 212 file_index_string); |
| 213 return 2; |
| 214 } |
| 215 |
| 216 bytes_written = fwrite(data, 1, data_len, file); |
| 217 |
| 218 *output = PrintfToNewString("fwrite\1%s\1%d", file_index_string, |
| 219 bytes_written); |
| 220 return 0; |
| 221 } |
| 222 |
| 223 int HandleFread(int num_params, char** params, char** output) { |
| 224 FILE* file; |
| 225 const char* file_index_string; |
| 226 char* buffer; |
| 227 size_t data_len; |
| 228 size_t bytes_read; |
| 229 |
| 230 if (num_params != 2) { |
| 231 *output = PrintfToNewString("Error: fread takes 2 parameters."); |
| 232 return 1; |
| 233 } |
| 234 |
| 235 file_index_string = params[0]; |
| 236 file = GetFileFromIndexString(file_index_string, NULL); |
| 237 data_len = strtol(params[1], NULL, 10); |
| 238 |
| 239 if (!file) { |
| 240 *output = PrintfToNewString("Error: Unknown file handle %s.", |
| 241 file_index_string); |
| 242 return 2; |
| 243 } |
| 244 |
| 245 buffer = (char*)malloc(data_len + 1); |
| 246 bytes_read = fread(buffer, 1, data_len, file); |
| 247 buffer[bytes_read] = 0; |
| 248 |
| 249 *output = PrintfToNewString("fread\1%s\1%s", file_index_string, buffer); |
| 250 free(buffer); |
| 251 return 0; |
| 252 } |
| 253 |
| 254 int HandleFseek(int num_params, char** params, char** output) { |
| 255 FILE* file; |
| 256 const char* file_index_string; |
| 257 long offset; |
| 258 int whence; |
| 259 int result; |
| 260 |
| 261 if (num_params != 3) { |
| 262 *output = PrintfToNewString("Error: fseek takes 3 parameters."); |
| 263 return 1; |
| 264 } |
| 265 |
| 266 file_index_string = params[0]; |
| 267 file = GetFileFromIndexString(file_index_string, NULL); |
| 268 offset = strtol(params[1], NULL, 10); |
| 269 whence = strtol(params[2], NULL, 10); |
| 270 |
| 271 if (!file) { |
| 272 *output = PrintfToNewString("Error: Unknown file handle %s.", |
| 273 file_index_string); |
| 274 return 2; |
| 275 } |
| 276 |
| 277 result = fseek(file, offset, whence); |
| 278 if (result) { |
| 279 *output = PrintfToNewString("Error: fseek returned error %d.", result); |
| 280 return 3; |
| 281 } |
| 282 |
| 283 *output = PrintfToNewString("fseek\1%d", file_index_string); |
| 284 return 0; |
| 285 } |
| 286 |
| 287 int HandleFclose(int num_params, char** params, char** output) { |
| 288 FILE* file; |
| 289 int file_index; |
| 290 const char* file_index_string; |
| 291 int result; |
| 292 |
| 293 if (num_params != 1) { |
| 294 *output = PrintfToNewString("Error: fclose takes 1 parameters."); |
| 295 return 1; |
| 296 } |
| 297 |
| 298 file_index_string = params[0]; |
| 299 file = GetFileFromIndexString(file_index_string, &file_index); |
| 300 if (!file) { |
| 301 *output = PrintfToNewString("Error: Unknown file handle %s.", |
| 302 file_index_string); |
| 303 return 2; |
| 304 } |
| 305 |
| 306 result = fclose(file); |
| 307 if (result) { |
| 308 *output = PrintfToNewString("Error: fclose returned error %d.", result); |
| 309 return 3; |
| 310 } |
| 311 |
| 312 RemoveFileFromMap(file_index); |
| 313 |
| 314 *output = PrintfToNewString("fclose\1%s", file_index_string); |
| 315 return 0; |
| 316 } |
| 317 |
| 318 static size_t ParseMessage(char* message, |
| 319 char** out_function, |
| 320 char** out_params, |
| 321 size_t max_params) { |
| 322 char* separator; |
| 323 char* param_start; |
| 324 size_t num_params = 0; |
| 325 |
| 326 /* Parse the message: function\1param1\1param2\1param3,... */ |
| 327 *out_function = &message[0]; |
| 328 |
| 329 separator = strchr(message, 1); |
| 330 if (!separator) { |
| 331 return num_params; |
| 332 } |
| 333 |
| 334 *separator = 0; /* NULL-terminate function. */ |
| 335 |
| 336 while (separator && num_params < max_params) { |
| 337 param_start = separator + 1; |
| 338 separator = strchr(param_start, 1); |
| 339 if (separator) { |
| 340 *separator = 0; |
| 341 out_params[num_params++] = param_start; |
| 342 } |
| 343 } |
| 344 |
| 345 out_params[num_params++] = param_start; |
| 346 |
| 347 return num_params; |
| 348 } |
| 349 |
| 350 static HandleFunc GetFunctionByName(const char* function_name) { |
| 351 FuncNameMapping* map_iter = g_FunctionMap; |
| 352 for (; map_iter->name; ++map_iter) { |
| 353 if (strcmp(map_iter->name, function_name) == 0) { |
| 354 return map_iter->function; |
| 355 } |
| 356 } |
| 357 |
| 358 return NULL; |
| 359 } |
| 360 |
| 361 |
| 362 static PP_Bool Instance_DidCreate(PP_Instance instance, |
| 363 uint32_t argc, |
| 364 const char* argn[], |
| 365 const char* argv[]) { |
| 366 |
| 367 ki_init(NULL); |
| 368 return PP_TRUE; |
| 369 } |
| 370 |
| 371 |
| 372 static void Instance_DidDestroy(PP_Instance instance) { |
| 373 } |
| 374 |
| 375 static void Instance_DidChangeView(PP_Instance instance, |
| 376 PP_Resource view_resource) { |
| 377 } |
| 378 |
| 379 static void Instance_DidChangeFocus(PP_Instance instance, |
| 380 PP_Bool has_focus) { |
| 381 } |
| 382 |
| 383 static PP_Bool Instance_HandleDocumentLoad(PP_Instance instance, |
| 384 PP_Resource url_loader) { |
| 385 /* NaCl modules do not need to handle the document load function. */ |
| 386 return PP_FALSE; |
| 387 } |
| 388 |
| 389 static void Messaging_HandleMessage(PP_Instance instance, |
| 390 struct PP_Var message) { |
| 391 char buffer[1024]; |
| 392 char* function_name; |
| 393 char* params[MAX_PARAMS]; |
| 394 size_t num_params; |
| 395 char* output = NULL; |
| 396 int result; |
| 397 HandleFunc function; |
| 398 |
| 399 VarToCStr(message, &buffer[0], 1024); |
| 400 |
| 401 num_params = ParseMessage(buffer, &function_name, ¶ms[0], MAX_PARAMS); |
| 402 |
| 403 function = GetFunctionByName(function_name); |
| 404 if (!function) { |
| 405 /* Function name wasn't found. Error. */ |
| 406 ppb_messaging_interface->PostMessage( |
| 407 instance, PrintfToVar("Error: Unknown function \"%s\"", function)); |
| 408 } |
| 409 |
| 410 result = (*function)(num_params, ¶ms[0], &output); |
| 411 if (result != 0) { |
| 412 /* Error. */ |
| 413 struct PP_Var var; |
| 414 if (output != NULL) { |
| 415 var = PrintfToVar( |
| 416 "Error: Function \"%s\" returned error %d. " |
| 417 "Additional output: %s", function_name, result, output); |
| 418 } else { |
| 419 var = PrintfToVar("Error: Function \"%s\" returned error %d.", |
| 420 function_name, result); |
| 421 } |
| 422 |
| 423 ppb_messaging_interface->PostMessage(instance, var); |
| 424 return; |
| 425 } |
| 426 |
| 427 if (output != NULL) { |
| 428 /* Function returned an output string. Send it to JavaScript. */ |
| 429 ppb_messaging_interface->PostMessage(instance, CStrToVar(output)); |
| 430 free(output); |
| 431 } |
| 432 } |
| 433 |
| 434 PP_EXPORT int32_t PPP_InitializeModule(PP_Module a_module_id, |
| 435 PPB_GetInterface get_browser) { |
| 436 ppb_messaging_interface = |
| 437 (PPB_Messaging*)(get_browser(PPB_MESSAGING_INTERFACE)); |
| 438 ppb_var_interface = (PPB_Var*)(get_browser(PPB_VAR_INTERFACE)); |
| 439 return PP_OK; |
| 440 } |
| 441 |
| 442 |
| 443 PP_EXPORT const void* PPP_GetInterface(const char* interface_name) { |
| 444 if (strcmp(interface_name, PPP_INSTANCE_INTERFACE) == 0) { |
| 445 static PPP_Instance instance_interface = { |
| 446 &Instance_DidCreate, |
| 447 &Instance_DidDestroy, |
| 448 &Instance_DidChangeView, |
| 449 &Instance_DidChangeFocus, |
| 450 &Instance_HandleDocumentLoad, |
| 451 }; |
| 452 return &instance_interface; |
| 453 } else if (strcmp(interface_name, PPP_MESSAGING_INTERFACE) == 0) { |
| 454 static PPP_Messaging messaging_interface = { |
| 455 &Messaging_HandleMessage, |
| 456 }; |
| 457 return &messaging_interface; |
| 458 } |
| 459 return NULL; |
| 460 } |
| 461 |
| 462 |
| 463 PP_EXPORT void PPP_ShutdownModule() { |
| 464 } |
OLD | NEW |