1 : /*
2 : * Copyright 2010 The Native Client Authors. All rights reserved.
3 : * Use of this source code is governed by a BSD-style license that can
4 : * be found in the LICENSE file.
5 : */
6 :
7 : #include <stdio.h>
8 : #include <stdlib.h>
9 : #include <stdarg.h>
10 : #include <string.h>
11 :
12 : #include <string>
13 : #include <vector>
14 :
15 : #include "native_client/src/trusted/gdb_rsp/util.h"
16 : #include "native_client/src/trusted/port/std_types.h"
17 :
18 : using std::string;
19 :
20 : namespace gdb_rsp {
21 :
22 688 : bool NibbleToInt(char ch, int *val) {
23 : // Check for nibble of a-f
24 688 : if ((ch >= 'a') && (ch <= 'f')) {
25 42 : if (val) *val = (ch - 'a' + 10);
26 42 : return true;
27 : }
28 :
29 : // Check for nibble of A-F
30 646 : if ((ch >= 'A') && (ch <= 'F')) {
31 6 : if (val) *val = (ch - 'A' + 10);
32 6 : return true;
33 : }
34 :
35 : // Check for nibble of 0-9
36 640 : if ((ch >= '0') && (ch <= '9')) {
37 404 : if (val) *val = (ch - '0');
38 404 : return true;
39 : }
40 :
41 : // Not a valid nibble representation
42 236 : return false;
43 : }
44 :
45 688 : bool IntToNibble(int nibble, char *ch) {
46 : // Verify this value fits in a nibble
47 688 : if (nibble != (nibble & 0xF)) return false;
48 :
49 192 : nibble &= 0xF;
50 192 : if (nibble < 10) {
51 155 : if (ch) *ch = '0' + nibble;
52 : } else {
53 : // Although uppercase maybe more readible GDB
54 : // expects lower case values.
55 37 : if (ch) *ch = 'a' + (nibble - 10);
56 : }
57 :
58 192 : return true;
59 : }
60 :
61 3 : bool NibblesToByte(const char *inStr, int *outInt) {
62 : int o1, o2;
63 :
64 3 : if (NibbleToInt(inStr[0], &o1) && NibbleToInt(inStr[1], &o2)) {
65 3 : *outInt = (o1 << 4) + o2;
66 3 : return true;
67 : }
68 :
69 0 : return false;
70 : }
71 :
72 : #ifdef WIN32
73 : int snprintf(char *buf, size_t size, const char *fmt, ...) {
74 : va_list argptr;
75 : va_start(argptr, fmt);
76 :
77 : int len = vsnprintf(buf, size, fmt, argptr);
78 : return len;
79 : }
80 : #endif
81 :
82 13 : stringvec StringSplit(const string &instr, const char *delim) {
83 13 : int count = 0;
84 13 : const char *in = instr.data();
85 13 : const char *start = in;
86 :
87 13 : stringvec ovec;
88 :
89 : // Check if we have nothing to do
90 13 : if (NULL == in) return ovec;
91 13 : if (NULL == delim) {
92 0 : ovec.push_back(string(in));
93 0 : return ovec;
94 : }
95 :
96 49 : while (*in) {
97 23 : int len = 0;
98 : // Toss all preceeding delimiters
99 23 : while (*in && strchr(delim, *in)) in++;
100 :
101 : // If we still have something to process
102 23 : if (*in) {
103 21 : std::string token;
104 21 : start = in;
105 21 : len = 0;
106 : // Keep moving forward for all valid chars
107 264 : while (*in && (strchr(delim, *in) == NULL)) {
108 222 : len++;
109 222 : in++;
110 : }
111 :
112 : // Build this token and add it to the array.
113 21 : ovec.resize(count + 1);
114 21 : ovec[count].assign(start, len);
115 21 : count++;
116 : }
117 : }
118 :
119 13 : return ovec;
120 : }
121 :
122 :
123 :
124 : } // End of namespace gdb_rsp
125 :
|