4577424 [rkeene@sledge /home/rkeene/devel/dact]$ cat -n dendian.c
 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 
34 int write_de(const int dst, const uint32_t num, const int sze) {
35     unsigned char buf[4]={0, 0, 0, 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 
53 int read_de(const int src, void *dest, const int sze, const int out_sze) {
54     unsigned char ch;
55     uint32_t ret=0;
56     int i;
57 
58     for (i=0;i<sze;i++) {
59         if (read(src, &ch, 1)<=0) {
60             PERROR("read");
61             return(-1);
62         }
63         ret|=ch<<(8*(sze-i-1));
64     }
65     memcpy(dest, &ret, out_sze);
66 
67     return(sze);
68 }
4577425 [rkeene@sledge /home/rkeene/devel/dact]$

Click here to go back to the directory listing.
Click here to download this file.
last modified: 2004-04-04 07:01:51