Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Revision 0:3b5e1025cbd6, committed 2010-09-13
- Comitter:
- iZsh
- Date:
- Mon Sep 13 22:19:39 2010 +0000
- Commit message:
- Not tested IRL yet
Changed in this revision
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/BarcodeLED.cpp Mon Sep 13 22:19:39 2010 +0000
@@ -0,0 +1,237 @@
+/* Copyright (c) 2010 iZsh - izsh at fail0verflow.com
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+#include "BarcodeLED.h"
+
+const char * BarcodeLED::m_Barcode39[] = {
+ "1113313111", "3113111131", "1133111131", "3133111111", "1113311131", "3113311111", "1133311111",
+ "1113113131", "3113113111", "1133113111", "3111131131", "1131131131", "3131131111", "1111331131",
+ "3111331111", "1131331111", "1111133131", "3111133111", "1131133111", "1111333111", "3111111331",
+ "1131111331", "3131111311", "1111311331", "3111311311", "1131311311", "1111113331", "3111113311",
+ "1131113311", "1111313311", "3311111131", "1331111131", "3331111111", "1311311131", "3311311111",
+ "1331311111", "1311113131", "3311113111", "1331113111", "1313131111", "1313111311", "1311131311",
+ "1113131311", "1311313111"
+};
+
+const char * BarcodeLED::m_Barcode128[] = {
+ "212222", "222122", "222221", "121223", "121322", "131222", "122213", "122312", "132212",
+ "221213", "221312", "231212", "112232", "122132", "122231", "113222", "123122", "123221",
+ "223211", "221132", "221231", "213212", "223112", "312131", "311222", "321122", "321221",
+ "312212", "322112", "322211", "212123", "212321", "232121", "111323", "131123", "131321",
+ "112313", "132113", "132311", "211313", "231113", "231311", "112133", "112331", "132131",
+ "113123", "113321", "133121", "313121", "211331", "231131", "213113", "213311", "213131",
+ "311123", "311321", "331121", "312113", "312311", "332111", "314111", "221411", "431111",
+ "111224", "111422", "121124", "121421", "141122", "141221", "112214", "112412", "122114",
+ "122411", "142112", "142211", "241211", "221114", "413111", "241112", "134111", "111242",
+ "121142", "121241", "114212", "124112", "124211", "411212", "421112", "421211", "212141",
+ "214121", "412121", "111143", "111341", "131141", "114113", "114311", "411113", "411311",
+ "113141", "114131", "311141", "411131", "211412", "211214", "211232", "2331112"
+};
+
+BarcodeLED::BarcodeLED(const PinName Led, const CodeType Codetype, const int BaseDelay)
+ : m_Led(DigitalOut(Led)), m_Codetype(Codetype), m_BaseDelay(BaseDelay), m_Verbose(false)
+{
+ m_Led = 1;
+}
+
+BarcodeLED::CodeType BarcodeLED::GetCodetype()
+{
+ return m_Codetype;
+}
+
+void BarcodeLED::SetCodetype(const CodeType Codetype)
+{
+ m_Codetype = Codetype;
+}
+
+int BarcodeLED::GetBaseDelay()
+{
+ return m_BaseDelay;
+}
+
+void BarcodeLED::SetBaseDelay(const int BaseDelay)
+{
+ m_BaseDelay = BaseDelay;
+}
+
+int BarcodeLED::GetVerbose()
+{
+ return m_Verbose;
+}
+
+void BarcodeLED::SetVerbose(const bool Verbose)
+{
+ m_Verbose = Verbose;
+}
+
+void BarcodeLED::Emit(const char Str[])
+{
+ switch (m_Codetype) {
+ case Code128a:
+ case Code128b:
+ case Code128c:
+ EmitCode128(Str);
+ break;
+ case Code39:
+ default:
+ EmitCode39(Str);
+ break;
+ }
+}
+
+void BarcodeLED::EmitCode39(const char Str[])
+{
+ if (m_Verbose)
+ printf("sending \"%s\"\r\n", Str);
+ // Let's build a sequence string
+ // We do that (instead of sending the chars on the fly
+ // because that's probably better timing-wise (not relying
+ // on inlining) and because it gets easier that way to reverse
+ // the sequence (to increase reliabilty)
+ int len = strlen(Str);
+ // Reserve the memory on the stack. Hopefully no one will
+ // call this function with an insane string length
+ int size = (len + 2) * 10 + 1; // |start|seq|stop|null|
+ char seq[size];
+ memset(seq, '\0', size);
+ // And build the seq
+ strncat(seq, "1311313111", size - 1); // Code39 start
+ for (int i = 0; i < len; ++i)
+ strncat(seq, m_Barcode39[ASCII2Code39(Str[i])], size - (i + 1) * 10 - 1);
+ strncat(seq, "1311313111", size - (len + 1) * 10 - 1); // Code39 stop
+ if (m_Verbose)
+ printf("=> %s\r\n", seq);
+
+ // Emit
+ m_Led = 1;
+ wait_us(m_BaseDelay * 100); // long whitespace
+ FlashSeq(seq); // Seq
+ m_Led = 1;
+ wait_us(m_BaseDelay * 25); // short whitespace
+ FlashSeq(RevStr(seq)); // Rev Seq
+ m_Led = 1;
+ wait_us(m_BaseDelay * 100); // long whitespace
+}
+
+void BarcodeLED::EmitCode128(const char Str[])
+{
+ if (m_Verbose)
+ printf("sending \"%s\"\r\n", Str);
+ int len = strlen(Str);
+ int size = len * 6 + 6 * 2 + 7 + 1; // |start|seq|checksum|stop|null|
+ char seq[size];
+ memset(seq, '\0', size);
+ // build the seq
+ strncat(seq, m_Barcode128[m_Codetype], size - 1); // Code128 start
+ for (int i = 0; i < len; ++i)
+ strncat(seq, m_Barcode128[ASCII2Code128(Str[i])], size - (i + 1) * 6 - 1);
+ strncat(seq, m_Barcode128[Code128Checksum(Str)], size - (len + 1) * 6 - 1);
+ strncat(seq, "2331112", size - (len + 2) * 6 - 1); // Code128 stop
+ if (m_Verbose)
+ printf("=> %s\r\n", seq);
+
+ // Emit
+ m_Led = 1;
+ wait_us(m_BaseDelay * 100); // long whitespace
+ FlashSeq(seq); // Seq
+ m_Led = 1;
+ wait_us(m_BaseDelay * 25); // short whitespace
+ FlashSeq(RevStr(seq)); // Rev Seq
+ m_Led = 1;
+ wait_us(m_BaseDelay * 100); // long whitespace
+}
+
+// Convert an ASCII code to Code39
+// It doesn't support the full ascii table yet
+// That would need to use a sequence of two Code39 for that
+// See http://en.wikipedia.org/wiki/Code_39
+int BarcodeLED::ASCII2Code39(const char C)
+{
+ if (C >= '0' && C <= '9')
+ return C - '0';
+ // Case is ignored for now
+ if (C >= 'A' && C <= 'Z')
+ return C - 55;
+ if (C >= 'a' && C <= 'z')
+ return C - 87;
+ // Punctuation and alike
+ switch (C) {
+ case '-':
+ return 36;
+ case '.':
+ return 37;
+ case ' ':
+ return 38;
+ case '$':
+ return 39;
+ case '/':
+ return 40;
+ case '+':
+ return 41;
+ case '%':
+ return 42;
+ case '*':
+ return 43;
+ }
+ return 0;
+}
+
+int BarcodeLED::ASCII2Code128(const char C)
+{
+ if (C == ' ')
+ return 0;
+ if (C >= 33 && C <= 126)
+ return C - 32;
+ if (C >= 145)
+ return C - 50;
+ if (C <= 31) // Code128a
+ return C + 64;
+ return 0;
+}
+
+char * BarcodeLED::RevStr(char Str[])
+{
+ int len = strlen(Str);
+ int s = 0;
+ int e = len - 1;
+ while (s < e) {
+ char c = Str[s];
+ Str[s] = Str[e];
+ Str[e] = c;
+ ++s;
+ --e;
+ }
+ return Str;
+}
+
+void BarcodeLED::FlashSeq(const char Seq[])
+{
+ int b = 0;
+ for (int i = 0; Seq[i] != '\0'; ++i) {
+ m_Led = b;
+ wait_us(m_BaseDelay * (Seq[i] - '0'));
+ b = !b;
+ }
+}
+
+int BarcodeLED::Code128Checksum(const char Str[])
+{
+ long checksum = m_Codetype;
+ for (int i = 0; Str[i] != '\0'; ++i)
+ checksum += (i + 1) * ASCII2Code128(Str[i]);
+ return checksum % 103;
+}
+
+
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/BarcodeLED.h Mon Sep 13 22:19:39 2010 +0000
@@ -0,0 +1,68 @@
+/* Copyright (c) 2010 iZsh - izsh at fail0verflow.com
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+ #ifndef IZSH_MBED_BARCODELED_H
+ #define IZSH_MBED_BARCODELED_H
+
+ #include "mbed.h"
+
+ class BarcodeLED
+ {
+ public:
+ enum CodeType
+ {
+ Code39,
+ Code128a = 103,
+ Code128b = 104,
+ Code128c = 105,
+ };
+
+ BarcodeLED(const PinName Led, const CodeType Codetype = Code39, const int BaseDelay = 20);
+
+ CodeType GetCodetype();
+ void SetCodetype(const CodeType Codetype);
+
+ int GetBaseDelay();
+ void SetBaseDelay(const int BaseDelay);
+
+ int GetVerbose();
+ void SetVerbose(const bool Verbose);
+
+ void Emit(const char Str[]);
+
+ private:
+
+ void EmitCode39(const char Str[]);
+ void EmitCode128(const char Str[]);
+
+ int ASCII2Code39(const char C);
+ int ASCII2Code128(const char C);
+
+ char * RevStr(char Str[]);
+ void FlashSeq(const char Seq[]);
+
+ int Code128Checksum(const char Str[]);
+
+ DigitalOut m_Led;
+ CodeType m_Codetype;
+ int m_BaseDelay;
+ bool m_Verbose;
+
+ static const char * m_Barcode39[];
+ static const char * m_Barcode128[];
+ };
+
+
+ #endif
\ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp Mon Sep 13 22:19:39 2010 +0000
@@ -0,0 +1,111 @@
+/* Copyright (c) 2010 iZsh - izsh at fail0verflow.com
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+// Based on
+// http://www.irongeek.com/i.php?page=security/barcode-flashing-led-fuzzer-bruteforcer-injector
+
+#include "mbed.h"
+#include "BarcodeLED.h"
+
+typedef struct
+{
+ const char * str;
+ const char * descr;
+} str_t;
+
+void readline(char Buffer[], int Size)
+{
+ int i = 0;
+ char c;
+
+ while ((c = fgetc(stdin)) != '\r' && i < Size - 1) {
+ putc(c, stdout);
+ Buffer[i++] = c;
+ }
+ Buffer[i] = '\0';
+}
+
+void prompt(const str_t Barcodes[])
+{
+ printf("\r\n");
+ printf("=============\r\n");
+ printf("command list:\r\n");
+ printf("=============\r\n");
+ printf("code <39 | 128a | 128b | 128c>\r\n");
+ printf("preset <num>\r\n");
+ for (int i = 0; i < 6; ++i)
+ printf("\t%d: %s\r\n", i, Barcodes[i].descr);
+ printf("send <string>\r\n");
+ printf("> ");
+}
+
+// Very primitive parsing...
+void parse(const char Line[], const str_t Barcodes[], BarcodeLED & Barcode)
+{
+ printf("\r\n");
+ if (!strcmp(Line, "code 39")) {
+ Barcode.SetCodetype(BarcodeLED::Code39);
+ printf("Setting barcode to Code39\r\n");
+ } else if (!strcmp(Line, "code 128a")) {
+ Barcode.SetCodetype(BarcodeLED::Code128a);
+ printf("Setting barcode to Code128a\r\n");
+ } else if (!strcmp(Line, "code 128b")) {
+ Barcode.SetCodetype(BarcodeLED::Code128b);
+ printf("Setting barcode to Code128b\r\n");
+ } else if (!strcmp(Line, "code 128c")) {
+ Barcode.SetCodetype(BarcodeLED::Code128c);
+ printf("Setting barcode to Code128c\r\n");
+ } else if (!strncmp(Line, "preset ", 7)) {
+ int num = -1;
+ sscanf(Line, "preset %d", &num);
+ if (num != -1 && num < 6)
+ Barcode.Emit(Barcodes[num].str);
+ else
+ printf("=> Unknown string ID!\r\n");
+ } else if (!strncmp(Line, "send ", 5)) {
+ Barcode.Emit(Line + 5);
+ } else {
+ printf("=> Command not found!\r\n");
+ }
+}
+
+int main()
+{
+ BarcodeLED barcode(LED1, BarcodeLED::Code39);
+ DigitalOut led(LED4);
+
+ str_t barcodes[] = {
+ {"0123456789", "numeric"},
+ {"abc123", "simple alphanum"},
+ {"<script>alert(\"Hello World!\")</script>", "XSS"},
+ {"' or 1=1 -- ", "SQL injection"},
+ {"X5O!P%@AP[4\\PZX54(P^)7CC)7}$EICAR-STANDARD-ANTIVIRUS-TEST-FILE!$H+H*", "EICAR test string"},
+ {"TRY TO PASTE v", "Ctrl-V in 128a"},
+ };
+
+ barcode.SetVerbose(true);
+
+ while(1) {
+ char buffer[512];
+ memset(buffer, '\0', 512);
+ prompt(barcodes);
+ readline(buffer, 512);
+ parse(buffer, barcodes, barcode);
+ led = 1;
+ wait(0.2);
+ led = 0;
+ wait(0.2);
+ }
+}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed.bld Mon Sep 13 22:19:39 2010 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/mbed/builds/e2ac27c8e93e