1 /*
2 * Copyright (C) 2001, 2002, and 2003 Roy Keene
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 *
18 * email: dact@rkeene.org
19 */
20
21 #include "dact.h"
22 #include <stdio.h>
23 #ifdef HAVE_UNISTD_H
24 #include <unistd.h>
25 #endif
26 #ifdef HAVE_STDLIB_H
27 #include <stdlib.h>
28 #endif
29 #ifdef HAVE_STRING_H
30 #include <string.h>
31 #endif
32 #include "dendian.h"
33
write_de(const int dst,const uint64_t num,const int sze)34 int write_de(const int dst, const uint64_t num, const int sze) {
35 unsigned char buf[8]={0};
36 int i,x=0,v;
37
38 for (i=0;i<sze;i++) {
39 buf[sze-i-1]=((num&(0xff<<(i*8)))>>(i*8));
40 }
41
42 for (i=0;i<sze;i++) {
43 if ((v=write(dst, &buf[i], 1))<=0) {
44 PERROR("write");
45 return(-1);
46 }
47 x+=v;
48 }
49
50 return(x);
51 }
52
read_de(const int src,void * dest,const int sze,const int out_sze)53 int read_de(const int src, void *dest, const int sze, const int out_sze) {
54 unsigned char ch;
55 uint64_t ret=0;
56 uint64_t tmpvar64;
57 uint32_t tmpvar32;
58 uint16_t tmpvar16;
59 uint8_t tmpvar8;
60 int i;
61
62 for (i=0;i<sze;i++) {
63 if (read(src, &ch, 1)<=0) {
64 PERROR("read");
65 return(-1);
66 }
67 ret|=((uint64_t) ch)<<(8*(sze-i-1));
68 }
69 switch (out_sze) {
70 case 1: tmpvar8=ret; memcpy(dest, &tmpvar8, out_sze); break;
71 case 2: tmpvar16=ret; memcpy(dest, &tmpvar16, out_sze); break;
72 case 4: tmpvar32=ret; memcpy(dest, &tmpvar32, out_sze); break;
73 case 8: tmpvar64=ret; memcpy(dest, &tmpvar64, out_sze); break;
74 };
75
76 return(sze);
77 }
78