summaryrefslogtreecommitdiff
path: root/utils/common.c
diff options
context:
space:
mode:
authorlaforge <laforge@e0336214-984f-0b4b-a45f-81c69e1f0ede>2006-11-28 10:06:24 +0000
committerlaforge <laforge@e0336214-984f-0b4b-a45f-81c69e1f0ede>2006-11-28 10:06:24 +0000
commit2da53a43badc7bd578ab5df850648f14807aecd2 (patch)
tree7041dc431c25664d9ba280a02504157407eb774d /utils/common.c
parentc972071a03825d8fe595d33f9128b4341b0478bd (diff)
- fix mifare write support
- make mifare auth more reliable - add mifare permission / access bit en/decoding - some more 15693 bits - add new 'mifare-tool' program git-svn-id: https://svn.gnumonks.org/trunk/librfid@1928 e0336214-984f-0b4b-a45f-81c69e1f0ede
Diffstat (limited to 'utils/common.c')
-rw-r--r--utils/common.c125
1 files changed, 125 insertions, 0 deletions
diff --git a/utils/common.c b/utils/common.c
new file mode 100644
index 0000000..2966ab4
--- /dev/null
+++ b/utils/common.c
@@ -0,0 +1,125 @@
+#include <stdio.h>
+#include <unistd.h>
+#include <stdlib.h>
+#include <string.h>
+#include <errno.h>
+#include <libgen.h>
+
+#include <librfid/rfid.h>
+#include <librfid/rfid_scan.h>
+#include <librfid/rfid_reader.h>
+#include <librfid/rfid_layer2.h>
+#include <librfid/rfid_protocol.h>
+
+#include <librfid/rfid_protocol_mifare_classic.h>
+#include <librfid/rfid_protocol_mifare_ul.h>
+
+#include "librfid-tool.h"
+
+const char *
+hexdump(const void *data, unsigned int len)
+{
+ static char string[1024];
+ unsigned char *d = (unsigned char *) data;
+ unsigned int i, left;
+
+ string[0] = '\0';
+ left = sizeof(string);
+ for (i = 0; len--; i += 3) {
+ if (i >= sizeof(string) -4)
+ break;
+ snprintf(string+i, 4, " %02x", *d++);
+ }
+ return string;
+}
+
+static char parse_hexdigit(const char hexdigit)
+{
+ if (hexdigit <= '9' && hexdigit >= '0')
+ return hexdigit - '0';
+ if (hexdigit <= 'f' && hexdigit >= 'a')
+ return 10 + (hexdigit - 'a');
+
+ return 0;
+}
+
+int
+hexread(unsigned char *result, const unsigned char *in, unsigned int len)
+{
+ const unsigned char *pos;
+ char dig1, dig2;
+ unsigned char *res = result;
+
+ for (pos = in; pos-in <= len-2; pos+=2) {
+ if (*pos == ':') {
+ pos++;
+ continue;
+ }
+ dig1 = *pos;
+ dig2 = *(pos+1);
+
+ *res++ = parse_hexdigit(dig1) << 4 | parse_hexdigit(dig2);
+ }
+
+ return (res - result);
+}
+
+struct rfid_reader_handle *rh;
+struct rfid_layer2_handle *l2h;
+struct rfid_protocol_handle *ph;
+
+int reader_init(void)
+{
+ printf("opening reader handle\n");
+ rh = rfid_reader_open(NULL, RFID_READER_CM5121);
+ if (!rh) {
+ fprintf(stderr, "No Omnikey Cardman 5121 found\n");
+ rh = rfid_reader_open(NULL, RFID_READER_OPENPCD);
+ if (!rh) {
+ fprintf(stderr, "No OpenPCD found either\n");
+ return -1;
+ }
+ }
+ return 0;
+}
+
+int l2_init(int layer2)
+{
+ unsigned char buf[0x3f];
+ int rc;
+
+ printf("opening layer2 handle\n");
+ l2h = rfid_layer2_init(rh, layer2);
+ if (!l2h) {
+ fprintf(stderr, "error during iso14443a_init\n");
+ return -1;
+ }
+
+ printf("running layer2 anticol\n");
+ rc = rfid_layer2_open(l2h);
+ if (rc < 0) {
+ fprintf(stderr, "error during layer2_open\n");
+ return rc;
+ }
+
+ return 0;
+}
+
+int l3_init(int protocol)
+{
+ printf("running layer3 (ats)\n");
+ ph = rfid_protocol_init(l2h, protocol);
+ if (!ph) {
+ fprintf(stderr, "error during protocol_init\n");
+ return -1;
+ }
+ if (rfid_protocol_open(ph) < 0) {
+ fprintf(stderr, "error during protocol_open\n");
+ return -1;
+ }
+
+ printf("we now have layer3 up and running\n");
+
+ return 0;
+}
+
personal git repositories of Harald Welte. Your mileage may vary