Header
Product Comparison: USB I2C | USB SPI | USB GPIO
  • My Account
  • Shopping Cart
  • Products
  • My Account
  • About
  • Products
  • PC-I2C/SPI/GPIO Adapters
  • Main Page
  • Classes
  • Files
  • File List
  • File Members

encoder_buffer.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (C) 2007 by Diolan                                          *
00003  *   www.diolan.com                                                        *
00004  *                                                                         *
00005  *   This program is free software; you can redistribute it and/or modify  *
00006  *   it under the terms of the GNU General Public License as published by  *
00007  *   the Free Software Foundation; either version 2 of the License, or     *
00008  *   (at your option) any later version.                                   *
00009  *                                                                         *
00010  *   This program is distributed in the hope that it will be useful,       *
00011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00013  *   GNU General Public License for more details.                          *
00014  *                                                                         *
00015  *   You should have received a copy of the GNU General Public License     *
00016  *   along with this program; if not, write to the                         *
00017  *   Free Software Foundation, Inc.,                                       *
00018  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
00019  ***************************************************************************/
00020 
00021 #include "osdep/osdep.h"
00022 
00023 #include "encoder.h"
00024 #include "encoder_buffer.h"
00025 
00026 #pragma warning(disable:4307)
00027 
00028 const XTEA_KEY_T XTEA_ITER = 64;
00029 const XTEA_KEY_T DELTA = 0x9E3779B9;
00030 
00031 void EncoderBuffer::encode(XTEA_KEY_T key[4])
00032 {
00033  XTEA_KEY_T sum;
00034  XTEA_KEY_T data[2];
00035 
00036  size_t address;
00037 
00038  FragBuffer::iterator it = begin();
00039  while (it != end())
00040  {
00041   address = this->address(it);
00042   address &= 0xFFFFFFF8U;
00043 
00044   if (read((unsigned char *)&data[0], 8, address, true) == 0)
00045    continue;
00046   sum = 0;
00047   for(XTEA_KEY_T i = 0; i < XTEA_ITER; i++ )
00048   {
00049 
00050    data[0] += (((data[1]<<4) ^ (data[1]>>5)) + data[1]) ^ (sum + key[sum&0x03]);
00051    sum+= DELTA;
00052    data[1] += (((data[0]<<4) ^ (data[0]>>5)) + data[0]) ^ (sum + key[(sum>>11)&0x03]);
00053   }
00054   write((unsigned char *)&data[0], 8, address);
00055   it = lower_bound(address + 8);
00056  }
00057 }
00058 
00059 void EncoderBuffer::decode(XTEA_KEY_T key[4])
00060 {
00061  XTEA_KEY_T sum;
00062  XTEA_KEY_T data[2];
00063 
00064  size_t address;
00065 
00066  FragBuffer::iterator it = begin();
00067  while (it != end())
00068  {
00069   address = this->address(it);
00070   address &= 0xFFFFFFF8U;
00071 
00072   if (read((unsigned char *)&data[0], 8, address, true) == 0)
00073    continue;
00074 
00075   sum = DELTA * XTEA_ITER;
00076   for(XTEA_KEY_T i = 0; i < XTEA_ITER; i++ )
00077   {
00078    data[1] -= (((data[0]<<4) ^ (data[0]>>5)) + data[0]) ^ (sum + key[(sum>>11)&0x03]);
00079    sum-= DELTA;
00080    data[0] -= (((data[1]<<4) ^ (data[1]>>5)) + data[1]) ^ (sum + key[sum&0x03]);
00081   }
00082   write((unsigned char *)&data[0], 8, address);
00083   it = lower_bound(address + 8);
00084  }
00085 }
00086 
00087 /* End of file */
  • Products
  • PC-I2C/SPI/GPIO Adapters
  • Comparison
  • PC-I2C/SPI/GPIO Adapter DLN-1
  • USB-I2C/SPI/GPIO Adapter DLN-2
  • Multiprotocol Master DLN-4M
  • Multiprotocol Slave DLN-4S
  • Downloads
  • Software
  • Documentation
  • SW Tools & Examples
  • Development Boards
  • Open Source Projects
  • Discontinued Products
  • API Documentation
  • Downloads
  • SW Tools and Examples
footer top footer bottom
© Copyright Diolan 2006-2012