Skip to content

Instantly share code, notes, and snippets.

@meqif
Created June 15, 2009 18:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save meqif/130256 to your computer and use it in GitHub Desktop.
Save meqif/130256 to your computer and use it in GitHub Desktop.
CueCat decoder
/*
* CueCat Decoder
*
* Thanks to:
* - http://oilcan.org/cuecat/base64.html
* - LibraryThing for shipping my CueCat
*
* Copyright (c) 2009 Ricardo Martins <ricardo@scarybox.net>
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <assert.h>
#define XOR 'C'
const char *base64 = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789+-";
char *decode64(const char *orig)
{
int i, j;
size_t length = strlen(orig);
char *in, *out, *head;
union x {
char data[3];
int as_integer;
} alpha;
in = malloc(length - (length%4) + 4 + 1);
memset(in, '=', length - (length%4) + 4);
strcpy(in, orig);
out = calloc(length+1, sizeof(char));
head = out;
for (i = 0; i < length; i++) {
for (j = 0; in[i] != '=' && in[i] != base64[j]; j++);
in[i] = j;
}
for (i = 0; i < length; i += 4) {
alpha.as_integer = in[i] << 18;
alpha.as_integer |= in[i+1] << 12;
alpha.as_integer |= in[i+2] << 6;
alpha.as_integer |= in[i+3];
*(head++) = alpha.data[2];
*(head++) = alpha.data[1];
*(head++) = alpha.data[0];
}
*head = 0;
free(in);
return out;
}
/*
* Returns just the barcode data, ignoring the CueCat's serial number and the
* type of the barcode
*/
static char *getData(const char *input)
{
int nSeen;
char *data;
const char *pos = NULL;
for (nSeen = 0; *input != '\0'; input++) {
if (*input == '.') {
nSeen++;
if (nSeen == 3)
pos = input + 1;
}
}
assert(pos != NULL);
size_t size = input - pos - 1;
data = calloc(size+1, sizeof(char));
strncpy(data, pos, size);
return data;
}
/* Decodes the data encrypted by the CueCat */
static char *decode(char *in)
{
int i;
char *decoded = decode64(in);
for (i = 0; i < strlen(decoded); i++) {
decoded[i] ^= XOR;
}
return decoded;
}
int main(void)
{
char *input, *data, *decoded;
input = calloc(255, sizeof(char));
scanf("%254s", input);
data = getData(input);
decoded = decode(data);
printf("%s\n", decoded);
free(input);
free(data);
free(decoded);
return 0;
}
// vim: et ts=4 sw=4 sts=4
#!/usr/bin/env ruby
#
# (C)opyright 2009 Ricardo Martins <ricardo at scarybox dot net>
# Licensed under the MIT/X11 License. See LICENSE file for license details.
require 'base64'
def decode(str)
data = str.split('.')[3]
dec64 = Base64.decode64(data.swapcase)
decoded = String.new
dec64.each_byte {|i| decoded << (i^'C'[0]) }
return decoded
end
input = gets
puts decode(input)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment