- add sources.
[platform/framework/web/crosswalk.git] / src / ppapi / native_client / src / trusted / plugin / utility.cc
1 /*
2  * Copyright (c) 2011 The Chromium Authors. All rights reserved.
3  * Use of this source code is governed by a BSD-style license that can be
4  * found in the LICENSE file.
5  */
6
7 #include <stdarg.h>
8 #include <stdlib.h>
9 #include <string.h>
10
11 #include "ppapi/native_client/src/trusted/plugin/utility.h"
12
13 namespace plugin {
14
15 int gNaClPluginDebugPrintEnabled = -1;
16 FILE* gNaClPluginLogFile = NULL;
17
18 /*
19  * Prints formatted message to the log.
20  */
21 int NaClPluginPrintLog(const char *format, ...) {
22   if (NULL == gNaClPluginLogFile) {
23     return 0;
24   }
25   va_list arg;
26   int done;
27   va_start(arg, format);
28   done = vfprintf(gNaClPluginLogFile, format, arg);
29   va_end(arg);
30   fflush(gNaClPluginLogFile);
31   return done;
32 }
33
34 /*
35  * Opens file where plugin log should be written. The file name is
36  * taken from NACL_PLUGIN_LOG environment variable.
37  * If environment variable doesn't exist or file can't be opened,
38  * the function returns stdout.
39  */
40 FILE* NaClPluginLogFileEnv() {
41   char* file = getenv("NACL_PLUGIN_LOG");
42   if (NULL != file) {
43     FILE* log_file = fopen(file, "w+");
44     if (NULL == log_file) {
45         return stdout;
46     }
47     return log_file;
48   }
49   return stdout;
50 }
51
52 /*
53  * Currently looks for presence of NACL_PLUGIN_DEBUG and returns
54  * 0 if absent and 1 if present.  In the future we may include notions
55  * of verbosity level.
56  */
57 int NaClPluginDebugPrintCheckEnv() {
58   char* env = getenv("NACL_PLUGIN_DEBUG");
59   return (NULL != env);
60 }
61
62 bool IsValidIdentifierString(const char* strval, uint32_t* length) {
63   // This function is supposed to recognize valid ECMAScript identifiers,
64   // as described in
65   // http://www.ecma-international.org/publications/files/ECMA-ST/ECMA-262.pdf
66   // It is currently restricted to only the ASCII subset.
67   // TODO(sehr): Recognize the full Unicode formulation of identifiers.
68   // TODO(sehr): Make this table-driven if efficiency becomes a problem.
69   if (NULL != length) {
70     *length = 0;
71   }
72   if (NULL == strval) {
73     return false;
74   }
75   static const char* kValidFirstChars =
76       "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz$_";
77   static const char* kValidOtherChars =
78       "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz$_"
79       "0123456789";
80   if (NULL == strchr(kValidFirstChars, strval[0])) {
81     return false;
82   }
83   uint32_t pos;
84   for (pos = 1; ; ++pos) {
85     if (0 == pos) {
86       // Unsigned overflow.
87       return false;
88     }
89     int c = strval[pos];
90     if (0 == c) {
91       break;
92     }
93     if (NULL == strchr(kValidOtherChars, c)) {
94       return false;
95     }
96   }
97   if (NULL != length) {
98     *length = pos;
99   }
100   return true;
101 }
102
103 }  // namespace plugin