home *** CD-ROM | disk | FTP | other *** search
/ hobbes.nmsu.edu 2008 / 2008-06-02_hobbes.nmsu.edu.zip / new / scummc-0.2.0-os2.zip / ScummC / src / scc_fd.c < prev    next >
Encoding:
C/C++ Source or Header  |  2007-11-26  |  4.6 KB  |  219 lines

  1. /* ScummC
  2.  * Copyright (C) 2004-2006  Alban Bedel
  3.  *
  4.  * This program is free software; you can redistribute it and/or
  5.  * modify it under the terms of the GNU General Public License
  6.  * as published by the Free Software Foundation; either version 2
  7.  * of the License, or (at your option) any later version.
  8.  
  9.  * This program is distributed in the hope that it will be useful,
  10.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.  * GNU General Public License for more details.
  13.  
  14.  * You should have received a copy of the GNU General Public License
  15.  * along with this program; if not, write to the Free Software
  16.  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
  17.  *
  18.  */
  19.  
  20. /**
  21.  * @file scc_fd.c
  22.  * @ingroup utils
  23.  * @brief Read/write file with XOR encryption
  24.  */
  25.  
  26. #include "config.h"
  27.  
  28. #include <stdio.h>
  29. #include <stdlib.h>
  30. #include <inttypes.h>
  31. #include <errno.h>
  32. #include <string.h>
  33.  
  34. #include <sys/types.h>
  35. #include <sys/stat.h>
  36. #include <fcntl.h>
  37. #include <unistd.h>
  38. #include <stdarg.h>
  39.  
  40. #include "scc_fd.h"
  41. #include "scc_util.h"
  42.  
  43. scc_fd_t* new_scc_fd(char* path,int flags,uint8_t key) {
  44.   int fd;
  45.   scc_fd_t* scc_fd;
  46.  
  47.   if(flags & O_CREAT)
  48.     fd = open(path,flags,0644);
  49.   else
  50.     fd = open(path,flags);
  51.   if(fd < 0) return NULL;
  52.   scc_fd = malloc(sizeof(scc_fd_t));
  53.   scc_fd->fd = fd;
  54.   scc_fd->enckey = key;
  55.   scc_fd->filename = strdup(path);
  56.  
  57.   return scc_fd;
  58. }
  59.  
  60. int scc_fd_close(scc_fd_t* f) {
  61.   int r = close(f->fd);
  62.   free(f->filename);
  63.   free(f);
  64.   return r;
  65. }
  66.  
  67. int scc_fd_read(scc_fd_t* f,void *buf, size_t count) {
  68.   if(count <= 0) return 0;
  69.   count = read(f->fd,buf,count);
  70.   if(count <= 0) return count;
  71.   else {
  72.     uint8_t* ptr = ((uint8_t*)buf) + count;
  73.     do {
  74.       ptr--;
  75.       *ptr ^= f->enckey;
  76.     } while(ptr != buf);
  77.   }
  78.   return count;
  79. }
  80.  
  81. uint8_t* scc_fd_load(scc_fd_t* f,size_t count) {
  82.   uint8_t* ret = malloc(count);
  83.   int r,pos = 0;
  84.   
  85.   while(pos < count) {
  86.     r = scc_fd_read(f,ret+pos,count - pos);
  87.     if(r <= 0) {
  88.       free(ret);
  89.       return NULL;
  90.     }
  91.     pos += r;
  92.   }
  93.  
  94.   return ret;
  95. }
  96.  
  97. int scc_fd_dump(scc_fd_t* f,char* path,int size) {
  98.   char buf[2048];
  99.   int r=0,w,dfd = open(path,O_CREAT|O_WRONLY,0644);
  100.  
  101.   if(dfd < 0) {
  102.     scc_log(LOG_ERR,"Failed to open %s: %s\n",path,strerror(errno));
  103.     return 0;
  104.   }
  105.  
  106.   while(size > 0) {
  107.     r = scc_fd_read(f,buf,size > 2048 ? 2048 : size);
  108.     if(r < 0) {
  109.       scc_log(LOG_ERR,"Read error: %s\n",strerror(errno));
  110.       close(dfd);
  111.       return 0;
  112.     }
  113.     size -= r;
  114.     while(r > 0) {
  115.       w = write(dfd,buf,r);
  116.       if(w < 0) {
  117.     scc_log(LOG_ERR,"Write error: %s\n",strerror(errno));
  118.     close(dfd);
  119.     return 0;
  120.       }
  121.       if(w < r) memmove(buf,buf+w,r-w);
  122.       r -= w;
  123.     }
  124.   }
  125.   close(dfd);
  126.   return 1;
  127. }
  128.  
  129. off_t scc_fd_seek(scc_fd_t* f, off_t offset, int whence) {
  130.   return lseek(f->fd,offset,whence);
  131. }
  132.  
  133. off_t scc_fd_pos(scc_fd_t* f) {
  134.   return lseek(f->fd,0,SEEK_CUR);
  135. }
  136.  
  137. uint8_t scc_fd_r8(scc_fd_t* f) {
  138.   uint8_t r = 0;
  139.   scc_fd_read(f,&r,1);
  140.   return r;
  141. }
  142.  
  143. uint16_t scc_fd_r16le(scc_fd_t* f) {
  144.   uint16_t a = scc_fd_r8(f);
  145.   uint16_t b = scc_fd_r8(f);
  146.   return a | (b << 8);
  147. }
  148.  
  149. uint32_t scc_fd_r32le(scc_fd_t* f) {
  150.   uint32_t a = scc_fd_r16le(f);
  151.   uint32_t b = scc_fd_r16le(f);
  152.   return (b << 16) | a;
  153. }
  154.  
  155. uint16_t scc_fd_r16be(scc_fd_t* f) {
  156.   uint16_t b = scc_fd_r8(f);
  157.   uint16_t a = scc_fd_r8(f);
  158.   return a | (b << 8);
  159. }
  160.  
  161. uint32_t scc_fd_r32be(scc_fd_t* f) {
  162.   uint32_t b = scc_fd_r16be(f);
  163.   uint32_t a = scc_fd_r16be(f);
  164.   return (b << 16) | a;
  165. }
  166.  
  167. int scc_fd_write(scc_fd_t* f,void *buf, size_t count) {
  168.   int w;
  169.   uint8_t tmp[count];
  170.   uint8_t* ptr = buf;
  171.  
  172.   for(w = 0 ; w < count ; w++)
  173.     tmp[w] = ptr[w] ^ f->enckey;
  174.  
  175.   return write(f->fd,tmp,count);
  176. }
  177.  
  178. int scc_fd_w8(scc_fd_t*f,uint8_t a) {
  179.   return scc_fd_write(f,&a,sizeof(a));
  180. }
  181.  
  182. int scc_fd_w16le(scc_fd_t*f,uint16_t a) {
  183.   scc_fd_w8(f,a & 0xFF);
  184.   return scc_fd_w8(f,(a >> 8) & 0xFF);
  185. }
  186.  
  187. int scc_fd_w32le(scc_fd_t*f,uint32_t a) {
  188.   scc_fd_w16le(f,a & 0xFFFF);
  189.   return scc_fd_w16le(f,(a >> 16) & 0xFFFF);
  190. }
  191.  
  192. int scc_fd_w16be(scc_fd_t*f,uint16_t a) {
  193.   scc_fd_w8(f,(a >> 8) & 0xFF);
  194.   return scc_fd_w8(f,a & 0xFF);
  195. }
  196.  
  197. int scc_fd_w32be(scc_fd_t*f,uint32_t a) {
  198.   scc_fd_w16be(f,(a >> 16) & 0xFFFF);
  199.   return scc_fd_w16be(f,a & 0xFFFF);
  200. }
  201.  
  202. int scc_fd_vprintf(scc_fd_t*f,const char *fmt, va_list ap) {
  203.     char* txt;
  204.     int ret, n = vasprintf(&txt,fmt,ap);
  205.     if(n <= 0) return 0;
  206.     ret = scc_fd_write(f,txt,n);
  207.     free(txt);
  208.     return ret;
  209. }
  210.  
  211. int scc_fd_printf(scc_fd_t*f,const char *fmt, ...) {
  212.     va_list ap;
  213.     int n;
  214.     va_start(ap, fmt);
  215.     n = scc_fd_vprintf(f,fmt,ap);
  216.     va_end(ap);
  217.     return n;
  218. }        
  219.