]> cvs.zerfleddert.de Git - proxmark3-svn/blob - client/graph.c
Fixed issue where -1 size_t was returned
[proxmark3-svn] / client / graph.c
1 //-----------------------------------------------------------------------------
2 // Copyright (C) 2010 iZsh <izsh at fail0verflow.com>
3 //
4 // This code is licensed to you under the terms of the GNU GPL, version 2 or,
5 // at your option, any later version. See the LICENSE.txt file for the text of
6 // the license.
7 //-----------------------------------------------------------------------------
8 // Graph utilities
9 //-----------------------------------------------------------------------------
10
11 #include <stdio.h>
12 #include <stdbool.h>
13 #include <string.h>
14 #include "ui.h"
15 #include "graph.h"
16 #include "lfdemod.h"
17
18 int GraphBuffer[MAX_GRAPH_TRACE_LEN];
19 int GraphTraceLen;
20
21 /* write a bit to the graph */
22 void AppendGraph(int redraw, int clock, int bit)
23 {
24 int i;
25
26 for (i = 0; i < (int)(clock / 2); ++i)
27 GraphBuffer[GraphTraceLen++] = bit ^ 1;
28
29 for (i = (int)(clock / 2); i < clock; ++i)
30 GraphBuffer[GraphTraceLen++] = bit;
31
32 if (redraw)
33 RepaintGraphWindow();
34 }
35
36 // clear out our graph window
37 int ClearGraph(int redraw)
38 {
39 int gtl = GraphTraceLen;
40 memset(GraphBuffer, 0x00, GraphTraceLen);
41
42 GraphTraceLen = 0;
43
44 if (redraw)
45 RepaintGraphWindow();
46
47 return gtl;
48 }
49
50 // DETECT CLOCK NOW IN LFDEMOD.C
51
52 void setGraphBuf(uint8_t *buff, size_t size)
53 {
54 if ( buff == NULL ) return;
55
56 uint16_t i = 0;
57 if ( size > MAX_GRAPH_TRACE_LEN )
58 size = MAX_GRAPH_TRACE_LEN;
59 ClearGraph(0);
60 for (; i < size; ++i){
61 GraphBuffer[i]=buff[i]-128;
62 }
63 GraphTraceLen=size;
64 RepaintGraphWindow();
65 return;
66 }
67 size_t getFromGraphBuf(uint8_t *buff)
68 {
69 if ( buff == NULL ) return 0;
70
71 uint32_t i;
72 for (i=0;i<GraphTraceLen;++i){
73 if (GraphBuffer[i]>127) GraphBuffer[i]=127; //trim
74 if (GraphBuffer[i]<-127) GraphBuffer[i]=-127; //trim
75 buff[i]=(uint8_t)(GraphBuffer[i]+128);
76 }
77 return i;
78 }
79 // Get or auto-detect clock rate
80 int GetClock(const char *str, int peak, int verbose)
81 {
82 int clock;
83 sscanf(str, "%i", &clock);
84 if (!strcmp(str, ""))
85 clock = 0;
86
87 // Auto-detect clock
88 if (!clock)
89 {
90 uint8_t grph[MAX_GRAPH_TRACE_LEN]={0};
91 size_t size = getFromGraphBuf(grph);
92 if ( size == 0 ) {
93 PrintAndLog("Failed to copy from graphbuffer");
94 return -1;
95 }
96 clock = DetectASKClock(grph,size,0);
97 // Only print this message if we're not looping something
98 if (!verbose){
99 PrintAndLog("Auto-detected clock rate: %d", clock);
100 }
101 }
102 return clock;
103 }
104
105 // A simple test to see if there is any data inside Graphbuffer.
106 bool HasGraphData(){
107
108 if ( GraphTraceLen <= 0) {
109 PrintAndLog("No data available, try reading something first");
110 return false;
111 }
112 return true;
113 }
114
115 // Detect high and lows in Grapbuffer.
116 // Only loops the first 256 values.
117 void DetectHighLowInGraph(int *high, int *low, bool addFuzz) {
118
119 uint8_t loopMax = 255;
120 if ( loopMax > GraphTraceLen)
121 loopMax = GraphTraceLen;
122
123 for (uint8_t i = 0; i < loopMax; ++i) {
124 if (GraphBuffer[i] > *high)
125 *high = GraphBuffer[i];
126 else if (GraphBuffer[i] < *low)
127 *low = GraphBuffer[i];
128 }
129
130 //12% fuzz in case highs and lows aren't clipped
131 if (addFuzz) {
132 *high = (int)(*high * .88);
133 *low = (int)(*low * .88);
134 }
135 }
136
137 int GetNRZpskClock(const char *str, int peak, int verbose)
138 {
139 int clock;
140 sscanf(str, "%i", &clock);
141 if (!strcmp(str, ""))
142 clock = 0;
143
144 // Auto-detect clock
145 if (!clock)
146 {
147 uint8_t grph[MAX_GRAPH_TRACE_LEN]={0};
148 size_t size = getFromGraphBuf(grph);
149 if ( size == 0 ) {
150 PrintAndLog("Failed to copy from graphbuffer");
151 return -1;
152 }
153 clock = DetectpskNRZClock(grph,size,0);
154 // Only print this message if we're not looping something
155 if (!verbose){
156 PrintAndLog("Auto-detected clock rate: %d", clock);
157 }
158 }
159 return clock;
160 }
Impressum, Datenschutz