ia64/xen-unstable

view tools/libxc/xg_private.c @ 9730:74ee53209cca

Fix whitespace in libxc. Tabs are manually fixed.
Trailing whitespace removed with:
perl -p -i -e 's/\s+$/\n/g' tools/libxc/*.[ch]

Signed-off-by: Hollis Blanchard <hollisb@us.ibm.com>
author kaf24@firebug.cl.cam.ac.uk
date Sat Apr 15 10:07:31 2006 +0100 (2006-04-15)
parents eceb7ffe1e67
children 414dabe82a31
line source
1 /******************************************************************************
2 * xg_private.c
3 *
4 * Helper functions for the rest of the library.
5 */
7 #include <stdlib.h>
8 #include <unistd.h>
9 #include <zlib.h>
11 #include "xg_private.h"
13 char *xc_read_image(const char *filename, unsigned long *size)
14 {
15 int kernel_fd = -1;
16 gzFile kernel_gfd = NULL;
17 char *image = NULL;
18 unsigned int bytes;
20 if ( (filename == NULL) || (size == NULL) )
21 return NULL;
23 if ( (kernel_fd = open(filename, O_RDONLY)) < 0 )
24 {
25 PERROR("Could not open kernel image");
26 goto out;
27 }
29 if ( (*size = xc_get_filesz(kernel_fd)) == 0 )
30 {
31 PERROR("Could not read kernel image");
32 goto out;
33 }
35 if ( (kernel_gfd = gzdopen(kernel_fd, "rb")) == NULL )
36 {
37 PERROR("Could not allocate decompression state for state file");
38 goto out;
39 }
41 if ( (image = malloc(*size)) == NULL )
42 {
43 PERROR("Could not allocate memory for kernel image");
44 goto out;
45 }
47 if ( (bytes = gzread(kernel_gfd, image, *size)) != *size )
48 {
49 PERROR("Error reading kernel image, could not"
50 " read the whole image (%d != %ld).", bytes, *size);
51 free(image);
52 image = NULL;
53 }
55 out:
56 if ( kernel_gfd != NULL )
57 gzclose(kernel_gfd);
58 else if ( kernel_fd >= 0 )
59 close(kernel_fd);
60 return image;
61 }
63 char *xc_inflate_buffer(const char *in_buf, unsigned long in_size,
64 unsigned long *out_size)
65 {
66 int sts;
67 z_stream zStream;
68 unsigned long out_len;
69 char *out_buf;
71 /* Not compressed? Then return the original buffer. */
72 if ( ((unsigned char)in_buf[0] != 0x1F) ||
73 ((unsigned char)in_buf[1] != 0x8B) )
74 {
75 if ( out_size != NULL )
76 *out_size = in_size;
77 return (char *)in_buf;
78 }
80 out_len = (unsigned char)in_buf[in_size-4] +
81 (256 * ((unsigned char)in_buf[in_size-3] +
82 (256 * ((unsigned char)in_buf[in_size-2] +
83 (256 * (unsigned char)in_buf[in_size-1])))));
85 bzero(&zStream, sizeof(zStream));
86 out_buf = malloc(out_len + 16); /* Leave a little extra space */
87 if ( out_buf == NULL )
88 {
89 ERROR("Error mallocing buffer\n");
90 return NULL;
91 }
93 zStream.next_in = (unsigned char *)in_buf;
94 zStream.avail_in = in_size;
95 zStream.next_out = (unsigned char *)out_buf;
96 zStream.avail_out = out_len+16;
97 sts = inflateInit2(&zStream, (MAX_WBITS+32)); /* +32 means "handle gzip" */
98 if ( sts != Z_OK )
99 {
100 ERROR("inflateInit failed, sts %d\n", sts);
101 free(out_buf);
102 return NULL;
103 }
105 /* Inflate in one pass/call */
106 sts = inflate(&zStream, Z_FINISH);
107 if ( sts != Z_STREAM_END )
108 {
109 ERROR("inflate failed, sts %d\n", sts);
110 free(out_buf);
111 return NULL;
112 }
114 if ( out_size != NULL )
115 *out_size = out_len;
117 return out_buf;
118 }
120 /*******************/
122 int pin_table(
123 int xc_handle, unsigned int type, unsigned long mfn, domid_t dom)
124 {
125 struct mmuext_op op;
127 op.cmd = type;
128 op.arg1.mfn = mfn;
130 if ( xc_mmuext_op(xc_handle, &op, 1, dom) < 0 )
131 return 1;
133 return 0;
134 }
136 /* This is shared between save and restore, and may generally be useful. */
137 unsigned long csum_page(void *page)
138 {
139 int i;
140 unsigned long *p = page;
141 unsigned long long sum=0;
143 for ( i = 0; i < (PAGE_SIZE/sizeof(unsigned long)); i++ )
144 sum += p[i];
146 return sum ^ (sum>>32);
147 }