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/debug_stub/util.h"
16 :
17 : using std::string;
18 :
19 : namespace gdb_rsp {
20 :
21 15038 : bool NibbleToInt(char ch, int *val) {
22 : // Check for nibble of a-f
23 18444 : if ((ch >= 'a') && (ch <= 'f')) {
24 4988 : if (val) *val = (ch - 'a' + 10);
25 2494 : return true;
26 : }
27 :
28 : // Check for nibble of A-F
29 13631 : if ((ch >= 'A') && (ch <= 'F')) {
30 12 : if (val) *val = (ch - 'A' + 10);
31 6 : return true;
32 : }
33 :
34 : // Check for nibble of 0-9
35 24025 : if ((ch >= '0') && (ch <= '9')) {
36 20790 : if (val) *val = (ch - '0');
37 10395 : return true;
38 : }
39 :
40 : // Not a valid nibble representation
41 2143 : return false;
42 15038 : }
43 :
44 51420 : bool IntToNibble(int nibble, char *ch) {
45 : // Verify this value fits in a nibble
46 51916 : if (nibble != (nibble & 0xF)) return false;
47 :
48 50924 : nibble &= 0xF;
49 50924 : if (nibble < 10) {
50 85576 : if (ch) *ch = '0' + nibble;
51 42788 : } else {
52 : // Although uppercase maybe more readible GDB
53 : // expects lower case values.
54 16272 : if (ch) *ch = 'a' + (nibble - 10);
55 : }
56 :
57 50924 : return true;
58 51420 : }
59 :
60 0 : bool NibblesToByte(const char *inStr, int *outInt) {
61 0 : int o1, o2;
62 :
63 0 : if (NibbleToInt(inStr[0], &o1) && NibbleToInt(inStr[1], &o2)) {
64 0 : *outInt = (o1 << 4) + o2;
65 0 : return true;
66 : }
67 :
68 0 : return false;
69 0 : }
70 :
71 209 : stringvec StringSplit(const string &instr, const char *delim) {
72 209 : int count = 0;
73 209 : const char *in = instr.data();
74 209 : const char *start = in;
75 :
76 418 : stringvec ovec;
77 :
78 : // Check if we have nothing to do
79 209 : if (NULL == in) return ovec;
80 209 : if (NULL == delim) {
81 0 : ovec.push_back(string(in));
82 0 : return ovec;
83 : }
84 :
85 888 : while (*in) {
86 470 : int len = 0;
87 : // Toss all preceeding delimiters
88 2982 : while (*in && strchr(delim, *in)) in++;
89 :
90 : // If we still have something to process
91 470 : if (*in) {
92 441 : std::string token;
93 441 : start = in;
94 441 : len = 0;
95 : // Keep moving forward for all valid chars
96 11535 : while (*in && (strchr(delim, *in) == NULL)) {
97 3377 : len++;
98 3377 : in++;
99 3377 : }
100 :
101 : // Build this token and add it to the array.
102 1323 : ovec.resize(count + 1);
103 882 : ovec[count].assign(start, len);
104 441 : count++;
105 882 : }
106 470 : }
107 :
108 209 : return ovec;
109 418 : }
110 :
111 :
112 :
113 : } // End of namespace gdb_rsp
114 :
|