2 * This file is part of mslib.
3 * mslib is free software: you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation, either version 3 of the License, or
6 * (at your option) any later version.
8 * mslib is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with nosebus. If not, see <http://www.gnu.org/licenses/>.
19 #ifdef __cplusplus /* If this is a C++ compiler, use C linkage */
30 #define PEAK_THRESHOLD 500
33 #define MAX_BITSTREAM_LEN 1024
35 #define ABA_SS "11010"
36 #define ABA_ES "11111"
37 #define MAX_ABA_LEN 41
38 #define ABA_CHAR_LEN 5
39 #define ABA_ASCII_OFFSET 48
41 #define IATA_SS "1010001"
42 #define IATA_ES "1111100"
43 #define MAX_IATA_LEN 83 // some reports say max len is 79, but AAMVA says 82 ...
44 #define IATA_CHAR_LEN 7
45 #define IATA_ASCII_OFFSET 32
47 #define NUM_ASCII_OFFSET 48
49 /* This char will be put into the stream whenever the parity bit doesn't match.
50 * The default char, | (PIPE), is chosen because it does not appear in either
51 * of the two encoding schemes. */
86 /* internal helper function, do not use */
89 /* Create an msData object using the given 16bit array of PCM data
90 * pcmData - A 16bit signed array of PCM data.
91 * pcmDataLen - the number of elements in pcmData
92 * Returns an msData object on success and NULL on failure */
93 msData *ms_create( const short *pcmData, int pcmDataLen );
95 /* Garbage Collection */
97 /* Frees all data allocated for the given msData objected
98 * Always returns NULL.
99 * This function will not free any data that mslib did not allocate itself */
100 msData *ms_free( msData *ms );
102 void ms_reinit( msData *ms );
104 /* internal helper function, do not use */
105 void ms_free_peakList( msData *ms );
107 /* Configuration functions */
109 /* Set the peakThreshold value.
110 * The value determines the minimum amplitude to be considered a peak
111 * You probably can stay with the default value */
112 void ms_set_peakThreshold( msData *ms, int peakThreshold );
114 /* Set the peakOffset value.
115 * The value determines the offset to use when looking for intersects
116 * You probably can stay with the default value */
117 void ms_set_peakOffset( msData *ms, int peakOffset );
120 /* Get the bit stream.
121 * This would be the binary data that the peaks are decoded to.
122 * Can only be run after ms_decode_peaks(). Format is a NULL-terminated
124 * Returns NULL on error (such as if ms_decode_peaks() has not been run) */
125 const char *ms_get_bitStream( msData *ms );
127 /* Get the char stream.
128 * This is the decoded binary data that is human readable.
129 * Can only be run after ms_decode_bits(). Format is a NULL-terminated
131 * Returns NULL on error (such as if ms_decode_bits() has not been run) */
132 const char *ms_get_charStream( msData *ms );
134 /* Reverses a string in place */
135 void strrev( char *str );
139 /* Find Peaks Functions */
141 /* internal helper function, do not use */
142 int ms_range( int a, int b1, int b2 );
144 /* Finds the peaks in the stream.
145 * This should be run immediately after ms_create().
146 * Compares the stream against an offset of itself and searches for intersects.
147 * Intersections are marked as peaks.
149 void ms_peaks_find( msData *ms );
151 /* Finds the peaks in the stream.
152 * Marks the apex of any amplitude apex as a peak.
154 void ms_peaks_find_walk( msData *ms );
156 /* Filters the list of peaks found using a grouping method based on signedness
158 * This typically should be run after ms_peaks_find().
160 void ms_peaks_filter_group( msData *ms );
162 /* internal helper function, do not use */
163 LListH *_ms_peaks_filter_groupFind( msData *ms, LListH *groupList );
166 /* Peak Decoding Functions */
168 /* internal helper function, do not use */
169 char _ms_closer( int *oneClock, int dif );
171 /* This decodes the peaks found through the ms_peaks*() functions.
172 * It should be run after them.
174 void ms_decode_peaks( msData *ms );
176 /* Bit Decoding functions */
178 /* This determines the type of encoding used: ABA or IATA.
179 * At present, only ABA is supported (as found on track 2 of most cards).
181 * This is generally used as a helper function internally, but can be run
182 * directly. It will return 1 on error and 0 on success.
183 * Success means it was able to detect either ABA or IATA. */
184 int ms_decode_typeDetect( msData *ms );
186 /* This decodes the bits into the charstream.
187 * It should be run after ms_decode_peaks().
188 * The encoding is auto-detected using ms_decode_typeDetect()
189 * Returns -1 on error, 0 on success, and from the enum ms_dataType UNKNOWN (9)
190 * if the card format was not detected (de facto error) */
191 int ms_decode_bits( msData *ms );
193 /* internal helper function, do not use */
194 char _ms_decode_bits_char( char *bitStream, char *LRC, ms_dataType type );
197 * This saves the stream to the given filename with ".pcm" appended.
198 * It also saves the contents of the peakList to the filename with a ".peaks"
201 * gnuplot is ideal for viewing the contents:
202 * plot "x.pcm" binary format="%int16" using l 1 w l, "x.peaks"
204 void ms_save( msData *ms, const char *filename );
206 #ifdef __cplusplus /* If this is a C++ compiler, end C linkage */