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 be
4 : * 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 1 : bool NibbleToInt(char ch, int *val) {
22 : // Check for nibble of a-f
23 1 : if ((ch >= 'a') && (ch <= 'f')) {
24 1 : if (val) *val = (ch - 'a' + 10);
25 1 : return true;
26 : }
27 :
28 : // Check for nibble of A-F
29 1 : if ((ch >= 'A') && (ch <= 'F')) {
30 1 : if (val) *val = (ch - 'A' + 10);
31 1 : return true;
32 : }
33 :
34 : // Check for nibble of 0-9
35 1 : if ((ch >= '0') && (ch <= '9')) {
36 1 : if (val) *val = (ch - '0');
37 1 : return true;
38 : }
39 :
40 : // Not a valid nibble representation
41 1 : return false;
42 1 : }
43 :
44 1 : bool IntToNibble(int nibble, char *ch) {
45 : // Verify this value fits in a nibble
46 1 : if (nibble != (nibble & 0xF)) return false;
47 :
48 1 : nibble &= 0xF;
49 1 : if (nibble < 10) {
50 1 : if (ch) *ch = '0' + nibble;
51 1 : } else {
52 : // Although uppercase maybe more readible GDB
53 : // expects lower case values.
54 1 : if (ch) *ch = 'a' + (nibble - 10);
55 : }
56 :
57 1 : return true;
58 1 : }
59 :
60 0 : bool NibblesToByte(const char *inStr, int *outInt) {
61 : 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 0 : stringvec StringSplit(const string &instr, const char *delim) {
72 0 : int count = 0;
73 0 : const char *in = instr.data();
74 0 : const char *start = in;
75 :
76 0 : stringvec ovec;
77 :
78 : // Check if we have nothing to do
79 0 : if (NULL == in) return ovec;
80 0 : if (NULL == delim) {
81 0 : ovec.push_back(string(in));
82 0 : return ovec;
83 : }
84 :
85 0 : while (*in) {
86 0 : int len = 0;
87 : // Toss all preceeding delimiters
88 0 : while (*in && strchr(delim, *in)) in++;
89 :
90 : // If we still have something to process
91 0 : if (*in) {
92 0 : std::string token;
93 0 : start = in;
94 0 : len = 0;
95 : // Keep moving forward for all valid chars
96 0 : while (*in && (strchr(delim, *in) == NULL)) {
97 0 : len++;
98 0 : in++;
99 0 : }
100 :
101 : // Build this token and add it to the array.
102 0 : ovec.resize(count + 1);
103 0 : ovec[count].assign(start, len);
104 0 : count++;
105 0 : }
106 0 : }
107 :
108 0 : return ovec;
109 0 : }
110 :
111 :
112 :
113 : } // End of namespace gdb_rsp
114 :
|