Você está na página 1de 11

Magnetic Stripe Reading

Joseph Battaglia
sephail@sephail.net http://www.sephail.net

Originally appearing in 2600 Magazine, Spring 2005

Introduction
Good magnetic stripe readers are hard to come by. Most are expensive, only capable of reading one or two tracks, and have inconvenient interfaces. In this article I will describe the process of making an extremely cheap, simple, and reliable single-track reader from parts that are readily available. We will be interfacing the reader to the microphone input of a sound card, which is very convenient for use with most laptops and desktops. I will not be discussing the theory and concepts of magnetic stripe technology and the assumption is made that you are somewhat familiar with the topic. For a simplistic overview of magnetic stripe technology that is easy to read and understand, I recommend that you read the classic article "CardO-Rama: Magnetic Stripe Technology and Beyond" by Count Zero, which can be found quickly by doing a web search for keywords in the title.

Materials
Below is a list of materials you'll need to construct the reader. - Magnetic head Magnetic heads are extremely common. Discarded cassette tape players contain magnetic heads of almost the exact size needed (the small difference won't matter for our application). Simply obtain a discarded cassette tape player and remove the magnetic head without damaging it. These heads are usually secured with one or two screws which can be useful when building the reader, so don't discard them. - 3.5mm mono phone plug (with 2-conductor wire) You can find this on a discarded monaural earphone or in an electronics store. - Soldering iron with solder Optional: - Wood (or other sturdy material) base to mount magnetic head - Ruler or other straight edge to slide cards on

Construction
The actual hardware design is incredibly simple. The interface consists of simply connecting the output of the magnetic head directly to the mic input of a sound card. Solder the wire connecting the 3.5mm mono phone plug (base and tip) to the leads of the magnetic stripe head. Polarity does not matter. I recommend that you mount the head in a way that makes it easy to swipe a card over it with a constant velocity. This is where your custom hardware ingenuity comes in. Mount a ruler (or other straight edge) perpendicular to the magnetic head, with the reading solenoid (usually visible as a

black rectangle on the head) at the correct distance from the base for the corresponding track. Track 1 starts at 0.223" from the bottom of the card, Track 2 starts at 0.333", and Track 3 starts at 0.443". Alternatively, you can purchase a surplus reader with no interface (i.e., scrapped or with a cheap TTL interface) and follow the same instructions with the exception that the magnetic head will already be mounted. Most surplus readers come preset to Track 2, although it is usually a simple hardware mod to move it to the track you'd like to read. This will save you the trouble of building a custom swiping mechanism and will also improve the reliability of the reads. There are surplus readers that can be purchased for less than $10 US at various online merchants.

Software
In this project, the software does all the heavy lifting. The "dab" utility included in this article takes the raw DSP data from your sound card, decodes the FSK (frequency shift keying - a.k.a. Atkin Biphase) modulation from the magnetic stripe, and outputs the binary data. Additionally, you can decode the binary data using the "dmsb" utility to output the ASCII characters and perform an LRC check to verify the integrity of the data, provided that the stripe conforms to the specifications described in ISO 7811, 7813, and optionally ISO 4909 (for the uncommon Track 3). Becoming familiar with these specifications will help you understand the contents of the magnetic stripe when viewing the decoded data. The provided software is more proof-of-concept than production code, and should be treated as such. That said, it does its job well. It is open source and released under the MIT license. Feel free to contribute.

Requirements
- Linux (or the desire to port to another operating system) - A configured 16-bit sound card - Access to the /dev/dsp device - libsndfile Note that "dab" can also take input from any audio file supported by libsndfile. However, it must be a clean sample that starts at the beginning of the file. This is useful to eliminate the requirement of a sound card and allow samples to be recorded from another device (e.g., an MP3 player/recorder) and decoded at another time.

Obtaining
dab.c (v0.7)- Decode Atkin Biphase dmsb.c (v0.1)- Decode (standard) Magnetic Stripe Binary Code is available from the Code Listing appendices and http://www.sephail.net/articles/magstripe

Compiling
Edit any configuration #defines near the top of the dab.c file and proceed to compile the source with the following commands:
cc dab.c -o dab -lsndfile cc dmsb.c -o dmsb

Usage
Usage: dab [OPTIONS] -a, -d, -f, -h, -m, -s, -t, -%, --auto-thres --de%ice --file --hel --ma)-le%el --silent --threshold --%ersion Set auto-thres ercentage !default: "#$ &e%ice to read audio data from !default: 'de%'ds $ (ile to read audio data from !use instead of -d$ Print hel information Sho*s the ma)imum le%el !use to determine threshold$ No %erbose messages Set silence threshold !default: automatic detect$ Print %ersion information

Usage: dmsb [OPTIONS] -+, -h, -%, --%erbose --hel --%ersion +erbose messages Print hel information Print %ersion information

dmsb *ill *ait on stdin for ra* magnetic stri e data !string of #s and ,s follo*ed b- a ne*line$ and rint the decoded data to stdout.

Be sure that the mic is set as the recording device for your sound card (using a utility such as aumix or your preferred mixer). Standard usage on the command line with the hardware interfaced directly to the sound card (mic in) will be as follows with standard cards:
.'dab . .'dmsb

Pictures
My original reader. With this reader I would use a ruler as a track guide. This way I could not only read the three standard tracks, but also data on non-standard cards, some of which have tracks in odd positions such as through the middle of the card.

My current reader, made of a modified surplus reader which is only capable of reading the three standard tracks.

Examples
Below are some examples of a few (hopefully) less common cards as to get an idea of the sort of data you're likely to find.

Park Inn (Berlin-Alexanderplatz) Door Key Cards


/oom: 0##1 2hec3out &ate: ,0'"#'0##4 2ard , Trac3 0 &ata: 56,#,,60##1#,#7,0,"#,04###,0##########8 2ard 0 Trac3 0 &ata: 56,#,,60##1#0#7,0,"#,04###,0##########8 /oom: 0##6 2hec3out &ate: ,0'"#'0##4 2ard , Trac3 0 &ata: 56,#,,60##6#,#,1#0"#,04###,0##########8 2ard 0 Trac3 0 &ata: 56,#,,60##6#0#,1#0"#,04###,0##########8

SEPTA Monthly TransPass Cards


9onth: No%ember 0##4 Serial: ##,41: Trac3 0 &ata: 5#,#,##,,#,#4,,"##4#####,41:8 9onth: ;une 0##" Serial: ##040, Trac3 0 &ata: 5#,#,###1#,#"#1"##"#####040,8 9onth: ;anuar- 0##0 Serial: #0<<," Trac3 0 &ata: 5#,#,###,#,#0#,",#0####0<<,"8

Sony Connect Cash Cards


2ard Number: 1#"6:, #,#410 ,,"4617 PIN: 7#,4 Trac3 , &ata: =>1#"6:,#,#410,,"4617??47,0#####4#8 Trac3 0 &ata: 51#"6:,#,#410,,"4617@47,0#####4#8

2ard Number: 1#"6:, #,#410 ,,"00<0 PIN: 6717 Trac3 , &ata: =>1#"6:,#,#410,,"00<0??47,0###<,4:8 Trac3 0 &ata: 51#"6:,#,#410,,"00<0@47,0###<,4:8

Starbucks Cards
2ard Number: 1#,6 #1," 0:,6 <401 Trac3 , &ata: =>1#,#616#1,"0:,6<?##4#'9O9S&AB#4?06#,###4####1##,<401 Trac3 0 &ata: 51#,#616#1,"0:,6<@06#,###4####1##,<4018 2ard Number: 1#,4 640, 61": 7607 Trac3 , &ata: =>1#,#614640,61"::?##0:'CD2EUSI+C>0>#4?06#,###4####1##,7607 Trac3 0 &ata: 51#,#614640,61"::@06#,###4####1##,76078 2ard Number: 1#,4 640, 1"#0 6:6: Trac3 , &ata: =>1#,#614640,61"::?##0:'CD2EUSI+C>0>#4?06#,###4####1##,7607 Trac3 0 &ata: 51#,#614640,1"#0:@06#,###4####1##,6:6:8 8

Conclusion
This project was originally started for the New York City MetroCard decoding project that you may have heard about on Off The Hook. Nearly all commercial readers are unable to dump the raw data as it exists on the MetroCard and, even if they could, they are priced way above our (and most hobbyists') budget limitations. This solution has worked very well for us and can aid you in reverseengineering cards that you may have as well. The "dmsb" application available online can be used for simply decoding standard cards that you have laying around as well. While my construction example demonstrates a fairly straightforward and typical use of a magnetic stripe reader, many other uses can be considered. For instance, since all the data obtained from the reader itself is audio, the device can be interfaced to a digital audio recording device, such as one of the many MP3 (and other codec) player/recorders on the market. You could then set the device to record, interfaced the same way with the magnetic stripe reader, and have a stand-alone reader small enough to fit in your pocket. Later, you'd view and edit the captured audio file, saving the clean waveform to a standard .wav file to be analyzed with "dab" (which, in fact, has this capability). You can even construct the reader in an inconspicuous way, so onlookers would never realize the device's capability. How is this significant? Reading boarding passes with magnetic stripes is a perfect application. These are generally only available in the waiting area of airports. They're issued at check-in and collected when you board, leaving a very small time margin during which the stripe can be scanned. In my case, I had been flagged for additional security and the infamous "SSSS" was printed on my pass. Using my reader, I was able to duck into a bathroom and quickly read the data into my mp3 player/recorder for later analysis. (I discovered a mysterious code on track 2 (normally blank) which read: "C 13190-2******" as well as an "S" at the end of the passenger data on track 1.) But there are other more sinister applications. What if one of the waiters at your favorite restaurant built this device and swiped the card of everyone who pays with credit? From the data obtained, an exact clone of the credit card could be created. Credit card fraud would quickly become out of control if this were commonplace. The same principle could be applied to reverse-engineering an unknown magnetic stripe technology. While individual card samples are often much more difficult to obtain, scanning samples as you obtain them enables you to gather samples at an astonishing rate. This way, supporters can loan you cards to scan on the spot. I have personally used this method for the

MetroCard decoding project and it works extremely well. I could go on and on with more examples of the implications of this sort of design, but I'd like to hear back from the readers as to what other ideas may have been thought up. All feedback is appreciated and, time permitting, all questions will be answered. Hopefully this project makes you realize how certain types of technology are priced way above what they have to be to keep them away from "us" because of the fear of malicious use. I also hope it encourages more projects like this to surface so we can learn about and use technology without the restrictions imposed upon us by big corporations.

Code Listing (dab.c)


'F dab.c - &ecode Ai3en >i hase Y 2o -right !c$ 0##4-0##6 ;ose h >attaglia Gse hailHse hail.netI return n tr5 2ode contributions ' atches: 9i3e 2astleman GmlcH01##.comI Cd Jandasie*icK G*andedHbreathemail.netI Y e)it!CDITS(AIEU/C$5

'F co - a string *ith out of memor- chec3ing Permission is hereb- granted, free of charge, to an- erson obtaining a co [string] string to co of this soft*are and associated documentation files !the LSoft*areL$, to returns ne*l- allocated co - of string F' deal in the Soft*are *ithout restriction, including *ithout limitation the char F)strdu !char Fstring$ rights to use, co -, modif-, merge, ublish, distribute, sublicense, and'or W sell co ies of the Soft*are, and to ermit ersons to *hom the Soft*are is char F tr5 furnished to do so, subMect to the follo*ing conditions: tr @ )malloc!strlen!string$ Z ,$5 The abo%e co -right notice and this ermission notice shall be included in strc -! tr, string$5 all co ies or substantial ortions of the Soft*are. return tr5 TNC SO(TJA/C IS P/O+I&C& LAS ISL, JITNOUT JA//ANTB O( ANB OIN&, CDP/CSS O/ Y I9PEIC&, IN2EU&INP >UT NOT EI9ITC& TO TNC JA//ANTICS O( 9C/2NANTA>IEITB, (ITNCSS (O/ A PA/TI2UEA/ PU/POSC AN& NONIN(/INPC9CNT. IN NO C+CNT SNAEE TNC AUTNO/S O/ 2OPB/IPNT NOE&C/S >C EIA>EC (O/ ANB 2EAI9, &A9APCS O/ OTNC/ 'F read *ith error chec3ing EIA>IEITB, JNCTNC/ IN AN A2TION O( 2ONT/A2T, TO/T O/ OTNC/JISC, A/ISINP [fd] file descri tor to read from (/O9, OUT O( O/ IN 2ONNC2TION JITN TNC SO(TJA/C O/ TNC USC O/ OTNC/ &CAEINPS [buf] buffer IN TNC SO(TJA/C. [count] b-tes to read returns b-tes read F' 2hangelog: ssiKeSt )read!int fd, %oid Fbuf, siKeSt count$ #., !Se 0##4$: W QaudiomagQ released int ret%al5 #.0 !Oct 0##4$: 01## 9etro2ard decoding roMect started ret%al @ read!fd, buf, count$5 changed name from QaudiomagQ to QdabQ if !ret%al @@ -,$ W no* reRuires onl- one Lcloc3ingL bit error!Lread!$L$5 o timiKed for reading non-standard cards !eg. 9etro2ards$ e)it!CDITS(AIEU/C$5 #." !No% 0##4$: Y im ro%ed decoding algorithm added ma)Sle%el functionalitreturn ret%al5 #.4 !&ec 0##4$: Y fi)ed bug *hen calculating threshold from ercentage #.6 !&ec 0##4$: 'FFFFFFFFFF end function *ra ers FFFFFFFFFF' im ro%ed decoding algorithm im ro%ed automatic threshold detection added su ort for reading from a file *ith libsndfile !9i3e 2.$ #.1 !;an 0##6$: 'FFFFFFFFFF %ersion functions FFFFFFFFFF' fi)ed bro3en flags im ro%ed libsndfile use 'F rints %ersion #.: !Aug 0##6$: [stream] out ut stream F' fi)ed otential segmentation fault !Cd J.$ %oid rintS%ersion!(IEC Fstream$ W 2om iling: f rintf!stream, Ldab - &ecode Ai3en >i haseXnL$5 cc dab.c -o dab -lsndfile f rintf!stream, L+ersion =sXnL, +C/SION$5 F' f rintf!stream, L2o -right !c$ 0##4-0##6 L$5 f rintf!stream, L;ose h >attaglia Gse hailHse hail.netIXnL$5 Y Tinclude Gfcntl.hI Tinclude Ggeto t.hI Tinclude Gsndfile.hI 'F rints %ersion and hel Tinclude Gstdio.hI [stream] out ut stream Tinclude Gstdlib.hI [e)ec] string containing the name of the rogram e)ecutable F' Tinclude Gstring.hI %oid rintShel !(IEC Fstream, char Fe)ec$ Tinclude Gs-s'ioctl.hI W Tinclude Gs-s'soundcard.hI rintS%ersion!stream$5 Tinclude Gs-s'stat.hI f rintf!stream, LXnUsage: =s [OPTIONS]XnXnL, e)ec$5 Tinclude Gs-s't- es.hI f rintf!stream, L -a, --auto-thres Set auto-thres ercentageXnL$5 Tinclude Gunistd.hI f rintf!stream, L !default: =d$XnL, AUTOSTN/CS$5 f rintf!stream, L -d, --de%ice &e%ice to read audio data fromXnL$5 'FFF defaults FFF' f rintf!stream, L !default: =s$XnL, &C+I2C$5 Tdefine &C+I2C L'de%'ds L 'F default sound card de%ice F' f rintf!stream, L -f, --file (ile to read audio data fromXnL$5 Tdefine SA9PECS/ATC ,70### 'F default sam le rate !hK$ F' f rintf!stream, L !use instead of -d$XnL$5 Tdefine SIECN2CSTN/CS 6### 'F initial silence threshold F' f rintf!stream, L -h, --hel Print hel informationXnL$5 'FFF end defaults FFF' f rintf!stream, L -m, --ma)-le%el Sho*s the ma)imum le%elXnL$5 f rintf!stream, L !use to determine threshold$XnL$5 'F Tdefine &ISA>ECS+2 F' f rintf!stream, L -s, --silent No %erbose messagesXnL$5 f rintf!stream, L -t, --threshold Set silence thresholdXnL$5 Tdefine AUTOSTN/CS "# 'F ct of highest %alue to set silenceSthres to F' f rintf!stream, L !default: automatic detect$XnL$5 Tdefine >U(SSIUC ,#04 'F buffer siKe F' f rintf!stream, L -%, --%ersion Print %ersion informationXnL$5 Tdefine CN&SECNPTN 0## 'F msec of silence to determine end of sam le F' Y Tdefine (/CVSTN/CS 1# 'F freRuenc- threshold ! ct$ F' Tdefine 9ADSTC/9 1# 'F sec before termination of rintSma)Sle%el!$ F' 'FFFFFFFFFF end %ersion functions FFFFFFFFFF' Tdefine +C/SION L#.:L 'F %ersion F' short int Fsam le @ NUEE5 int sam leSsiKe @ #5 'FFFFFFFFFF ds functions FFFFFFFFFF'

'FFFFFFFFFF function *ra

ers FFFFFFFFFF'

'F allocate memor- *ith out of memor- chec3ing [siKe] allocate siKe b-tes returns ointer to allocated memor- F' %oid F)malloc!siKeSt siKe$ W %oid F tr5 tr @ malloc!siKe$5 if ! tr @@ NUEE$ W f rintf!stderr, LOut of memor-.XnL$5 e)it!CDITS(AIEU/C$5 Y return Y 'F reallocate memor- *ith out of memor- chec3ing [ tr] memor- to reallocate [siKe] allocate siKe b-tes returns ointer to reallocated memor- F' %oid F)realloc!%oid F tr, siKeSt siKe$ W %oid Fn tr5 n tr @ realloc! tr, siKe$5 if !n tr @@ NUEE$ W f rintf!stderr, LOut of memor-.XnL$5 tr5

'F sets the de%ice arameters [fd] file descri tor to set ioctls on [%erbose] rints %erbose messages if true returns sam le rate F' int ds Sinit!int fd, int %erbose$ W int ch, fmt, sr5 if !%erbose$ f rintf!stderr, LFFF Setting audio de%ice arameters:XnL$5

'F set audio format F' if !%erbose$ f rintf!stderr, L (ormat: A(9TSS,1SECXnL$5 fmt @ A(9TSS,1SEC5 if !ioctl!fd, SN&2TES&SPSSCT(9T, [fmt$ @@ -,$ W error!LSN&2TES&SPSSCT(9TL$5 e)it!CDITS(AIEU/C$5 Y if !fmt \@ A(9TSS,1SEC$ W f rintf!stderr, LFFF Crror: &e%ice does not su e)it!CDITS(AIEU/C$5 Y 'F set audio channels F' if !%erbose$ f rintf!stderr, L 2hannels: ,XnL$5 ch @ #5 if !ioctl!fd, SN&2TES&SPSSTC/CO, [ch$ @@ -,$ W error!LSN&2TES&SPSSTC/COL$5 e)it!CDITS(AIEU/C$5 Y if !ch \@ #$ W

ort A(9TSS,1SECXnL$5

f rintf!stderr, LFFF Crror: &e%ice does not su e)it!CDITS(AIEU/C$5 Y

ort monaural recordingXnL$5

eos @ #5 Y Y else eos @ #5 Y Y 'FFFFFFFFFF end ds functions FFFFFFFFFF'

'F set sam le rate F' if !%erbose$ f rintf!stderr, L Sam le rate: =dXnL, SA9PECS/ATC$5 sr @ SA9PECS/ATC5 if !ioctl!fd, SN&2TES&SPSSPCC&, [sr$ @@ -,$ W error!LSN&2TES&SPSSPCC&L$5 e)it!CDITS(AIEU/C$5 Y if !sr \@ SA9PECS/ATC$ f rintf!stderr, LFFF Jarning: Nighest su orted sam le rate is =dXnL, sr$5 return sr5 Y 'F rints the ma)imum ds le%el to aid in setting the silence threshold [fd] file descri tor to read from [sam leSrate] sam le rate of de%ice F' %oid rintSma)Sle%el!int fd, int sam leSrate$ W int i5 short int buf, last @ #5 rintf!LTerminating after =d seconds...XnL, 9ADSTC/9$5 for !i @ #5 i G sam leSrate F 9ADSTC/95 iZZ$ W 'F read from fd F' )read!fd, [buf, siKeof !short int$$5 'F ta3e absolute %alue F' if !buf G #$ buf @ -buf5 'F rint if highest le%el F' if !buf I last$ W rintf!L9a)imum le%el: =dXrL, buf$5 fflush!stdout$5 last @ buf5 Y Y rintf!LXnL$5 Y 'F finds the ma)imum %alue in sam le FF global FF [sam le] sam le [sam leSsiKe] number of frames in sam le F' short int e%aluateSma)!%oid$ W int i5 short int ma) @ #5 for !i @ #5 i G sam leSsiKe5 iZZ$ W if !sam le[i] I ma)$ ma) @ sam le[i]5 Y return ma)5 Y 'F auses until the ds le%el is abo%e the silence threshold [fd] file descri tor to read from [silenceSthres] silence threshold F' %oid silenceS ause!int fd, int silenceSthres$ W short int buf @ #5 'F loo *hile silent F' *hile !buf G silenceSthres$ W 'F read from fd F' )read!fd, [buf, siKeof !short int$$5 'F absolute %alue F' if !buf G #$ buf @ -buf5 Y Y 'F gets a sam le, terminating *hen the in ut goes belo* the silence threshold [fd] file descri tor to read from [sam leSrate] sam le rate of de%ice [silenceSthres] silence threshold FF global FF [sam le] sam le [sam leSsiKe] number of frames in sam le F' %oid getSds !int fd, int sam leSrate, int silenceSthres$ W int count @ #, eos @ #, i5 short buf5 sam leSsiKe @ #5 'F *ait for sam le F' silenceS ause!fd, silenceSthres$5 *hile !\eos$ W 'F fill buffer F' sam le @ )realloc!sam le, siKeof !short int$ F !>U(SSIUC F !count Z ,$$$5 for !i @ #5 i G >U(SSIUC5 iZZ$ W )read!fd, [buf, siKeof !short int$$5 sam le[i Z !count F >U(SSIUC$] @ buf5 Y countZZ5 sam leSsiKe @ count F >U(SSIUC5 'F chec3 for silence F' eos @ ,5 if !sam leSsiKe I !sam leSrate F CN&SECNPTN$ ' ,###$ W for !i @ #5 i G !sam leSrate F CN&SECNPTN$ ' ,###5 iZZ$ buf @ sam le[!count F >U(SSIUC$ - i - ,]5 if !buf G #$ buf @ -buf5 if !buf I silenceSthres$

'FFFFFFFFFF begin sndfile functions FFFFFFFFFF' 'F o en the file [fd] file to o en [%erbose] %erbosit- flag FF global FF [sam leSsiKe] number of frames in the file F' SN&(IEC FsndfileSinit!int fd, int %erbose$ W SN&(IEC Fsndfile5 S(SIN(O sfinfo5 'F clear sfinfo structure F' memset![sfinfo, #, siKeof!sfinfo$$5 'F set sndfile from file descri tor F' sndfile @ sfSo enSfd!fd, S(9S/CA&, [sfinfo, #$5 if !sndfile @@ NUEE$ W f rintf!stderr, LFFF Crror: sfSo enSfd!$ failedXnL$5 e)it!CDITS(AIEU/C$5 Y 'F rint some statistics F' if !%erbose$ W f rintf!stderr, LFFF In ut file format:XnL L (rames: =iXnL L Sam le /ate: =iXnL L 2hannels: =iXnL L (ormat: #)=#<)XnL L Sections: =iXnL L See3able: =iXnL, !int$sfinfo.frames, sfinfo.sam lerate, sfinfo.channels, sfinfo.format, sfinfo.sections, sfinfo.see3able$5 Y 'F ensure that the file is monaural F' if !sfinfo.channels \@ ,$ W f rintf!stderr, LFFF Crror: Onl- monaural files are su e)it!CDITS(AIEU/C$5 Y 'F set sam le siKe F' sam leSsiKe @ sfinfo.frames5 return sndfile5 Y 'F read in data from libsndfile [sndfile] SN&(IEC ointer from sfSo en!$ or sfSo enSfd!$ FF global FF [sam le] sam le [sam leSsiKe] number of frames in sam le F' %oid getSsndfile!SN&(IEC Fsndfile$ W sfScountSt count5 'F allocate memor- for sam le F' sam le @ )malloc!siKeof!short int$ F sam leSsiKe$5 'F read in sam le F' count @ sfSreadSshort!sndfile, sam le, sam leSsiKe$5 if !count \@ sam leSsiKe$ W f rintf!stderr, LFFF Jarning: e) ected =i frames, read =i.XnL, sam leSsiKe, !int$count$5 sam leSsiKe @ count5 Y Y 'FFFFFFFFFF end sndfile functions FFFFFFFFFF'

ortedXnL$5

'F decodes ai3en bi hase and rints binar[freRSthres] freRuenc- threshold FF global FF [sam le] sam le [sam leSsiKe] number of frames in sam le F' %oid decodeSai3enSbi hase!int freRSthres, int silenceSthres$ W int i @ #, ea3 @ #, ea3 @ #5 int F ea3s @ NUEE, ea3sSsiKe @ #5 int Kerobl5 'F absolute %alue F' for !i @ #5 i G sam leSsiKe5 iZZ$ if !sam le[i] G #$ sam le[i] @ -sam le[i]5 'F store ea3 differences F' i @ #5 *hile !i G sam leSsiKe$ W 'F old ea3 %alue F' ea3 @ ea35 'F find ea3s F' *hile !i G sam leSsiKe [[ sam le[i] G@ silenceSthres$ iZZ5 ea3 @ #5 *hile !i G sam leSsiKe [[ sam le[i] I silenceSthres$ W if !sam le[i] I sam le[ ea3]$ ea3 @ i5 iZZ5 Y if ! ea3 ea3 I #$ W ea3s @ )realloc! ea3s, siKeof!int$ F ! ea3sSsiKe Z ,$$5 ea3s[ ea3sSsiKe] @ ea3 ea35 ea3sSsiKeZZ5 Y Y 'F decode ai3en bi hase allo*ing for freRuenc- de%iation based on freRSthres F'

'F ignore first t*o ea3s and last ea3 F' if ! ea3sSsiKe G 0$ W f rintf!stderr, LFFF Crror: No data detectedXnL$5 e)it!CDITS(AIEU/C$5 Y Kerobl @ ea3s[0]5 for !i @ 05 i G ea3sSsiKe - ,5 iZZ$ W if ! ea3s[i] G !!Kerobl ' 0$ Z !freRSthres F !Kerobl ' 0$ ' ,##$$ [[ ea3s[i] I !!Kerobl ' 0$ - !freRSthres F !Kerobl ' 0$ ' ,##$$$ W if ! ea3s[i Z ,] G !!Kerobl ' 0$ Z !freRSthres F !Kerobl ' 0$ ' ,##$$ [[ ea3s[i Z ,] I !!Kerobl ' 0$ - !freRSthres F !Kerobl ' 0$ ' ,##$$$ W rintf!L,L$5 Kerobl @ ea3s[i] F 05 iZZ5 Y Y else if ! ea3s[i] G !Kerobl Z !freRSthres F Kerobl ' ,##$$ [[ ea3s[i] I !Kerobl - !freRSthres F Kerobl ' ,##$$$ W rintf!L#L$5 Tifndef &ISA>ECS+2 Kerobl @ ea3s[i]5 Tendif Y Y rintf!LXnL$5 Y

silenceSthres @ atoi!o targ$5 brea35 'F %ersion F' case Q%Q: rintS%ersion!stdout$5 e)it!CDITSSU22CSS$5 brea35 'F default F' default: rintShel !stderr, arg%[#]$5 e)it!CDITS(AIEU/C$5 brea35 Y Y 'F rint %ersion F' if !%erbose$ W rintS%ersion!stderr$5 f rintf!stderr, LXnL$5 Y 'F chec3 for incorrect use of command-line arguments F' if !useSsndfile [[ ma)Sle%el$ W f rintf!stderr, LFFF Crror: -f and -m s*itches do not mi)\XnL$5 e)it!CDITS(AIEU/C$5 Y 'F set default if no de%ice is s ecified F' if !filename @@ NUEE$ filename @ )strdu !&C+I2C$5 'F o en de%ice for reading F' if !%erbose$ f rintf!stderr, LFFF O ening =sXnL, filename$5 fd @ o en!filename, OS/&ONEB$5 if !fd @@ -,$ W error!Lo en!$L$5 e)it!CDITS(AIEU/C$5 Y 'F o en sndfile or set de%ice arameters F' if !useSsndfile$ sndfile @ sndfileSinit!fd, %erbose$5 else sam leSrate @ ds Sinit!fd, %erbose$5 'F sho* user ma)imum ds le%el F' if !ma)Sle%el$ W rintSma)Sle%el!fd, sam leSrate$5 e)it!CDITSSU22CSS$5 Y 'F silenceSthres sanit- chec3 F' if !\silenceSthres$ W f rintf!stderr, LFFF Crror: In%alid silence thresholdXnL$5 e)it!CDITS(AIEU/C$5 Y 'F read sam le F' if !useSsndfile$ getSsndfile!sndfile$5 else W if !%erbose$ f rintf!stderr, LFFF Jaiting for sam le...XnL$5 getSds !fd, sam leSrate, silenceSthres$5 Y 'F automaticall- set threshold F' if !autoSthres$ silenceSthres @ autoSthres F e%aluateSma)!$ ' ,##5 'F rint silence threshold F' if !%erbose$ f rintf!stderr, LFFF Silence threshold: =d !=d== of ma)$XnL, silenceSthres, autoSthres$5 'F decode ai3en bi hase F' decodeSai3enSbi hase!(/CVSTN/CS, silenceSthres$5 'F close file F' close!fd$5 'F free memor- F' free!sam le$5 e)it!CDITSSU22CSS$5 return #5 Y 'F end dab.c F'

'F main F' int main!int argc, char Farg%[]$ W int fd5 SN&(IEC Fsndfile @ NUEE5 'F configuration %ariables F' char Ffilename @ NUEE5 int autoSthres @ AUTOSTN/CS, ma)Sle%el @ #, useSsndfile @ #, %erbose @ ,5 int sam leSrate @ SA9PECS/ATC, silenceSthres @ SIECN2CSTN/CS5 'F geto t %ariables F' int ch, o tionSinde)5 static struct o tion longSo tions[] @ W WLauto-thresL, #, #, QaQY, WLde%iceL, ,, #, QdQY, WLfileL, ,, #, QfQY, WLhel L, #, #, QhQY, WLma)-le%elL, #, #, QmQY, WLsilentL, #, #, QsQY, WLthresholdL, ,, #, QtQY, WL%ersionL, #, #, Q%QY, W #, #, #, # Y Y5 'F rocess command line arguments F' *hile !,$ W ch @ geto tSlong!argc, arg%, La:d:f:hmst:%L, longSo tions, [o tionSinde)$5 if !ch @@ -,$ brea35 s*itch !ch$ W 'F auto-thres F' case QaQ: autoSthres @ atoi!o targ$5 brea35 'F de%ice F' case QdQ: filename @ )strdu !o targ$5 brea35 'F file F' case QfQ: filename @ )strdu !o targ$5 useSsndfile @ ,5 brea35 'F hel F' case QhQ: rintShel !stdout, arg%[#]$5 e)it!CDITSSU22CSS$5 brea35 'F ma)-le%el F' case QmQ: ma)Sle%el @ ,5 brea35 'F silent F' case QsQ: %erbose @ #5 brea35 'F threshold F' case QtQ: autoSthres @ #5

Code Listing (dmsb.c)


'F dmsb.c - &ecodes !standard$ 9agnetic Stri e >inar2o -right !c$ 0##4 ;ose h >attaglia Gse hailHse hail.netI f f f f rintf!stream, rintf!stream, rintf!stream, rintf!stream, Ldmsb *ill *ait on stdin for ra* magnetic stri e L$5 Ldata !string of #s and ,sXnL$5 Lfollo*ed b- a ne*line$ and rint the decoded data to L$5 Lstdout.XnL$5 Permission is hereb- granted, free of charge, to an- erson obtaining a co - Y of this soft*are and associated documentation files !the LSoft*areL$, to deal in the Soft*are *ithout restriction, including *ithout limitation the 'FFFFFFFFFF end %ersion functions FFFFFFFFFF' rights to use, co -, modif-, merge, ublish, distribute, sublicense, and'or sell co ies of the Soft*are, and to ermit ersons to *hom the Soft*are is furnished to do so, subMect to the follo*ing conditions: 'FFFFFFFFFF string functions FFFFFFFFFF' The abo%e co -right notice and this ermission notice shall be included in all co ies or substantial ortions of the Soft*are. 'F returns a ointer to the re%ersed string [string] string to re%erse TNC SO(TJA/C IS P/O+I&C& LAS ISL, JITNOUT JA//ANTB O( ANB OIN&, CDP/CSS O/ returns ne*l- allocated re%ersed string F' I9PEIC&, IN2EU&INP >UT NOT EI9ITC& TO TNC JA//ANTICS O( 9C/2NANTA>IEITB, char Fre%erseSstring!char Fstring$ (ITNCSS (O/ A PA/TI2UEA/ PU/POSC AN& NONIN(/INPC9CNT. IN NO C+CNT SNAEE TNC W AUTNO/S O/ 2OPB/IPNT NOE&C/S >C EIA>EC (O/ ANB 2EAI9, &A9APCS O/ OTNC/ char Frstring5 EIA>IEITB, JNCTNC/ IN AN A2TION O( 2ONT/A2T, TO/T O/ OTNC/JISC, A/ISINP int i, stringSlen5 (/O9, OUT O( O/ IN 2ONNC2TION JITN TNC SO(TJA/C O/ TNC USC O/ OTNC/ &CAEINPS IN TNC SO(TJA/C. stringSlen @ strlen!string$5 'F record string length F' 2om iling: cc dmsb.c -o dmsb F' for !i @ #5 i G stringSlen5 iZZ$ 'F re%erse string and store in rstring F' rstring[i] @ string[stringSlen - i - ,]5 Tdefine >U(SSIUC 0#4< Tdefine +C/SION L#.,L Tinclude Tinclude Tinclude Tinclude Ggeto t.hI Gstdlib.hI Gstdio.hI Gstring.hI Y 'FFFFFFFFFF end string functions FFFFFFFFFF' rstring[stringSlen] @ QX#Q5 'F terminate rstring F' return rstring5 'F return rstring F' 'F allocate memor- for rstring F' rstring @ )malloc!stringSlen Z ,$5

'FFFFFFFFFF function *ra

ers FFFFFFFFFF'

'FFFFFFFFFF 'F

arsing functions FFFFFFFFFF'

'F allocate memor- *ith out of memor- chec3ing [siKe] allocate siKe b-tes returns ointer to allocated memor- F' %oid F)malloc!siKeSt siKe$ W %oid F tr5 tr @ malloc!siKe$5 if ! tr @@ NUEE$ W f rintf!stderr, LOut of memor-.XnL$5 e)it!CDITS(AIEU/C$5 Y return Y 'F reallocate memor- *ith out of memor- chec3ing [ tr] memor- to reallocate [siKe] allocate siKe b-tes returns ointer to reallocated memor- F' %oid F)realloc!%oid F tr, siKeSt siKe$ W %oid Fn tr5 n tr @ realloc! tr, siKe$5 if !n tr @@ NUEE$ W f rintf!stderr, LOut of memor-.XnL$5 e)it!CDITS(AIEU/C$5 Y return n tr5 Y 'F co - a string *ith out of memor- chec3ing [string] string to co returns ne*l- allocated co - of string F' char F)strdu !char Fstring$ W char F tr5 tr @ )malloc!strlen!string$ Z ,$5 strc -! tr, string$5 return Y 'FFFFFFFFFF end function *ra ers FFFFFFFFFF' tr5 tr5

arse A>A format ra* bits and return a ointer to the decoded string [bitstring] string to decode returns decoded string F' char F arseSA>A!char Fbitstring$ W char FdecodedSstring, FlrcSstart, FstartSdecode, Fstring5 char lrc[] @ W,, ,, #, ,, #Y5 'F initial condition is E/2 of the start sentinel F' int asciichr, charcnt @ #, i, M5 'F ma3e a co - of bitstring and store it in string F' string @ )strdu !bitstring$5 'F loo3 for start sentinel F' if !!startSdecode @ strstr!string, L,,#,#L$$ @@ NUEE$ W free!string$5 'F free string memor- F' return NUEE5 'F could not find start sentinel F' Y 'F set startSdecode to first bit !start of first b-te$ after start sentinel F' startSdecode Z@ 65 'F loo3 for end sentinel F' if !!lrcSstart @ strstr!string, L,,,,,L$$ @@ NUEE$ W free!string$5 'F free string memor- F' return NUEE5 'F could not find end sentinel F' Y 'F must be a multi le of 6 F' *hile !!strlen!startSdecode$ - strlen!lrcSstart$$ = 6$ 'F search again F' if !!lrcSstart @ strstr!ZZlrcSstart, L,,,,,L$$ @@ NUEE$ W free!string$5 'F free string memor- F' return NUEE5 'F cound not find end sentinel F' Y lrcSstart[#] @ QX#Q5 'F terminate startSdecode at end sentinel F' lrcSstart Z@ 65 'F set the ointer to the E/2 F' if !lrcSstart[6] \@ QX#Q$ 'F terminate E/2 if not alread- F' lrcSstart[6] @ QX#Q5 'F allocate memor- for decodedSstring F' decodedSstring @ )malloc!!strlen!startSdecode$ ' 6$ Z "$5 decodedSstring[charcntZZ] @ Q5Q5 'F add start sentinel F' 'F decode each set of bits, chec3 decodedSstring F' *hile !strlen!startSdecode$$ W arit-, chec3 E/2, and add to

'FFFFFFFFFF %ersion functions FFFFFFFFFF' 'F rint %ersion information [stream] out ut stream F' %oid rintS%ersion!(IEC Fstream$ W f rintf!stream, Ldmsb - &ecode !standard$ 9agnetic Stri e >inar-XnL$5 f rintf!stream, L+ersion =sXnL, +C/SION$5 f rintf!stream, L2o -right !c$ 0##4 ;ose h >attaglia Gse hailHse hail.netIXnL$5 Y 'F rint hel information [stream] out ut stream [e)ec] string containing the name of the rogram e)ecutable F' %oid rintShel !(IEC Fstream, char Fe)ec$ W rintS%ersion!stream$5 f rintf!stream, LXnUsage: =s [OPTIONS]XnL, e)ec$5 f rintf!stream, LXnL$5 f rintf!stream, L -+, --%erbose +erbose messagesXnL$5 f rintf!stream, LXnL$5 f rintf!stream, L -h, --hel Print hel informationXnL$5 f rintf!stream, L -%, --%ersion Print %ersion informationXnL$5 f rintf!stream, LXnL$5

for !i @ #, M @ #5 i G 45 iZZ$ 'F chec3 arit- F' if !startSdecode[i] @@ Q,Q$ MZZ5 if !!!M = 0$ [[ startSdecode[4] @@ Q,Q$ .. !\!M = 0$ [[ startSdecode[4] @@ Q#Q$$ W free!string$5 'F free string memor- F' free!decodedSstring$5 'F free decodedSstring memor- F' return NUEE5 'F failed arit- chec3 F' Y asciichr asciichr asciichr asciichr asciichr @ 4<5 'F generate ascii %alue Z@ startSdecode[#] @@ Q,Q 8 , Z@ startSdecode[,] @@ Q,Q 8 0 Z@ startSdecode[0] @@ Q,Q 8 4 Z@ startSdecode["] @@ Q,Q 8 < from bits F' : #5 : #5 : #5 : #5

decodedSstring[charcntZZ] @ asciichr5 'F add character to decodedSstring F' for !i @ #5 i G 45 iZZ$ 'F calculate E/2 F' lrc[i] @ lrc[i] ? !startSdecode[i] @@ Q,Q$ 8 , : #5 startSdecode Z@ 65 'F increment startSdecode to ne)t b-te F' Y decodedSstring[charcntZZ] @ Q8Q5 'F add end sentinel F' decodedSstring[charcnt] @ QX#Q5 'F terminate decodedSstring F' for !i @ #5 i G 45 iZZ$ 'F calculate 2/2 of end sentinel F'

lrc[i] @ lrc[i] ? ,5 for !i @ #, M @ #5 i G 45 iZZ$ 'F set E/2 if !lrc[i]$ MZZ5 if !\!M = 0$$ lrc[4] @ ,5 else lrc[4] @ #5 arit- bit F' Y

free!string$5 'F free string memor- F' return decodedSstring5

'FFFFFFFFFF end

arsing functions FFFFFFFFFF'

for !i @ #5 i G 65 iZZ$ 'F chec3 2/2 F' if !!lrc[i] [[ lrcSstart[i] @@ Q#Q$ .. !\lrc[i] [[ lrcSstart[i] @@ Q,Q$$ W free!string$5 'F free string memor- F' free!decodedSstring$5 'F free decodedSstring memor- F' return NUEE5 'F failed 2/2 chec3 F' Y free!string$5 'F free string memor- F' return decodedSstring5 Y 'F arse IATA format ra* bits and return a ointer to the decoded string [bitstring] string to decode returns decoded string F' char F arseSIATA!char Fbitstring$ W char FdecodedSstring, FlrcSstart, FstartSdecode, Fstring5 char lrc[] @ W,, #, ,, #, #, #, ,Y5 'F initial condition is E/2 of the start sentinel F' int asciichr, charcnt @ #, i, M5 'F ma3e a co - of bitstring and store it in string F' string @ )strdu !bitstring$5 'F loo3 for start sentinel F' if !!startSdecode @ strstr!string, L,#,###,L$$ @@ NUEE$ W free!string$5 'F free string memor- F' return NUEE5 'F could not find start sentinel F' Y 'F set startSdecode to first bit !start of first b-te$ after start sentinel F' startSdecode Z@ :5 'F loo3 for end sentinel F' if !!lrcSstart @ strstr!string, L,,,,,##L$$ @@ NUEE$ W free!string$5 'F free string memor- F' return NUEE5 'F could not find end sentinel F' Y 'F must be a multi le of : F' *hile !!strlen!startSdecode$ - strlen!lrcSstart$$ = :$ 'F search again F' if !!lrcSstart @ strstr!ZZlrcSstart, L,,,,,##L$$ @@ NUEE$ W free!string$5 'F free string memor- F' return NUEE5 'F cound not find end sentinel F' Y lrcSstart[#] @ QX#Q5 'F terminate startSdecode at end sentinel F' lrcSstart Z@ :5 'F set the ointer to the E/2 F' if !lrcSstart[:] \@ QX#Q$ 'F terminate E/2 if not alread- F' lrcSstart[:] @ QX#Q5 'F allocate memor- for decodedSstring F' decodedSstring @ )malloc!!strlen!startSdecode$ ' :$ Z "$5 decodedSstring[charcntZZ] @ Q=Q5 'F add start sentinel F' 'F decode each set of bits, chec3 decodedSstring F' *hile !strlen!startSdecode$$ W arit-, chec3 E/2, and add to

int main!int argc, char Farg%[]$ W char buf[>U(SSIUC], Frbuf, FdecodedSdata5 int %erbose @ #5 int ch, o tionSinde)5 static struct WL%erboseL, WLhel L , WL%ersionL, W # , Y5 o tion longSo tions[] @ W #, #, Q+QY, #, #, QhQY, #, #, Q%QY, #, #, # Y

*hile !!ch @ geto tSlong!argc, arg%, L+h%L, longSo tions, [o tionSinde)$$ \@ -,$ W s*itch !ch$ W case Q+Q: 'F %erbose F' %erbose @ ,5 brea35 case QhQ: 'F hel F' rintShel !stdout, arg%[#]$5 e)it!CDITSSU22CSS$5 brea35 case Q%Q: 'F %ersion F' rintS%ersion!stdout$5 e)it!CDITSSU22CSS$5 brea35 default: 'F in%alid o tion F' rintShel !stderr, arg%[#]$5 e)it!CDITS(AIEU/C$5 brea35 Y Y if !%erbose$ W rintS%ersion!stderr$5 f rintf!stderr, LJaiting for data on stdin...XnL$5 Y fgets!buf, >U(SSIUC, stdin$5 'F get string from stdin F' if !%erbose$ W f rintf!stderr, LTr-ing to decode using A>A...L$5 fflush!stderr$5 Y if !!decodedSdata @ arseSA>A!buf$$ \@ NUEE$ W 'F tr- A>A F' if !%erbose$ W f rintf!stderr, LsuccessXnL$5 f rintf!stderr, LA>A format detected:XnL$5 Y rintf!L=sXnL, decodedSdata$5 'F rint decoded data F' e)it!CDITSSU22CSS$5 Y if !%erbose$ W f rintf!stderr, Lre%ersing bits...L$5 fflush!stderr$5 Y rbuf @ re%erseSstring!buf$5 'F re%erse string and tr- again F' if !!decodedSdata @ arseSA>A!rbuf$$ \@ NUEE$ W 'F tr- A>A F' if !%erbose$ W f rintf!stderr, LsuccessXnL$5 f rintf!stderr, LA>A format detected !bits re%ersed$:XnL$5 Y rintf!L=sXnL, decodedSdata$5 e)it!CDITSSU22CSS$5 Y if !%erbose$ f rintf!stderr, LfailedXnL$5 if !%erbose$ W f rintf!stderr, LTr-ing to decode using IATA...L$5 fflush!stderr$5 Y if !!decodedSdata @ arseSIATA!buf$$ \@ NUEE$ W 'F tr- IATA F' if !%erbose$ W f rintf!stderr, LsuccessXnL$5 f rintf!stderr, LIATA format detected:XnL$5 Y rintf!L=sXnL, decodedSdata$5 'F rint decoded data F' e)it!CDITSSU22CSS$5 Y if !%erbose$ W f rintf!stderr, Lre%ersing bits...L$5 fflush!stderr$5 Y if !!decodedSdata @ arseSIATA!rbuf$$ \@ NUEE$ W 'F tr- IATA *ith re%erse F' if !%erbose$ W f rintf!stderr, LsuccessXnL$5 f rintf!stderr, LIATA format detected !bits re%ersed$:XnL$5 Y rintf!L=sXnL, decodedSdata$5 e)it!CDITSSU22CSS$5 Y if !%erbose$ f rintf!stderr, LfailedXnL$5 rintf!L&etection failedXnL$5 e)it!CDITS(AIEU/C$5 return #5 Y 'F end dmsb.c F'

for !i @ #, M @ #5 i G 15 iZZ$ 'F chec3 arit- F' if !startSdecode[i] @@ Q,Q$ MZZ5 if !!!M = 0$ [[ startSdecode[1] @@ Q,Q$ .. !\!M = 0$ [[ startSdecode[1] @@ Q#Q$$ W free!string$5 'F free string memor- F' free!decodedSstring$5 'F free decodedSstring memor- F' return NUEE5 'F failed arit- chec3 F' Y asciichr asciichr asciichr asciichr asciichr asciichr asciichr @ "05 'F generate ascii %alue from bits F' Z@ startSdecode[#] @@ Q,Q 8 , : #5 Z@ startSdecode[,] @@ Q,Q 8 0 : #5 Z@ startSdecode[0] @@ Q,Q 8 4 : #5 Z@ startSdecode["] @@ Q,Q 8 < : #5 Z@ startSdecode[4] @@ Q,Q 8 ,1 : #5 Z@ startSdecode[6] @@ Q,Q 8 "0 : #5

decodedSstring[charcntZZ] @ asciichr5 'F add character to decodedSstring F' for !i @ #5 i G 15 iZZ$ 'F calculate E/2 F' lrc[i] @ lrc[i] ? !startSdecode[i] @@ Q,Q$ 8 , : #5 startSdecode Z@ :5 'F increment startSdecode to ne)t b-te F' Y decodedSstring[charcntZZ] @ Q8Q5 'F add end sentinel F' decodedSstring[charcnt] @ QX#Q5 'F terminate decodedSstring F' for !i @ #5 i G 65 iZZ$ 'F calculate 2/2 of end sentinel F' lrc[i] @ lrc[i] ? ,5 lrc[6] @ lrc[6] ? #5 for !i @ #, M @ #5 i G 15 iZZ$ 'F set E/2 if !lrc[i]$ MZZ5 if !\!M = 0$$ lrc[1] @ ,5 else lrc[1] @ #5 arit- bit F'

for !i @ #5 i G :5 iZZ$ 'F chec3 2/2 F' if !!lrc[i] [[ lrcSstart[i] @@ Q#Q$ .. !\lrc[i] [[ lrcSstart[i] @@ Q,Q$$ W free!string$5 'F free string memor- F' free!decodedSstring$5 'F free decodedSstring memor- F' return NUEE5 'F failed 2/2 chec3 F' Y

Você também pode gostar