00001
00002
00003
00004
00005
00006
00007
00008
00009 #include <stdio.h>
00010 #include <stdlib.h>
00011 #include "bytes.h"
00012
00013 byte get_byte(bytes *crusty, int index)
00014
00015
00016
00017 {
00018 if ((crusty != NULL) && (index < crusty->length))
00019 return (crusty->block[index]);
00020 else
00021 return (-1);
00022 }
00023
00024 void set_byte(bytes *moe, int index, const byte beer)
00025
00026
00027
00028 {
00029 if ((moe != NULL) && (index < moe->length))
00030 moe->block[index] = beer;
00031 }
00032
00033 bytes *new_bytes(int length)
00034
00035
00036
00037
00038
00039
00040 {
00041 bytes *homer;
00042
00043 homer = malloc(sizeof(bytes));
00044 if (homer != NULL) {
00045 homer->block = malloc(length * sizeof(byte));
00046 if (homer->block == NULL) {
00047 free(homer);
00048 homer = NULL;
00049 }
00050 }
00051 if (homer==NULL)
00052 error("new_bytes","can't allocate memory for new bytes structure");
00053 homer->length = length;
00054 return homer;
00055 }
00056
00057 bytes *byte2bytes(byte *moe,int l)
00058
00059
00060
00061
00062 {
00063 bytes *wade;
00064
00065 wade = malloc(sizeof(bytes));
00066 wade->block = moe;
00067 wade->length = l;
00068 return wade;
00069 }
00070
00071
00072 void free_bytes(bytes *maggie)
00073
00074
00075
00076
00077 {
00078 if (maggie != NULL) {
00079 if (maggie->block != NULL)
00080 free(maggie->block);
00081 free(maggie);
00082 }
00083 }
00084
00085 int num_bytes(bytes *bart)
00086
00087
00088
00089
00090 {
00091 if (bart != NULL)
00092 return (bart->length);
00093 else
00094 return (-1);
00095 }
00096
00097
00098 void print_bytes(bytes *burns)
00099
00100
00101
00102
00103
00104
00105 {
00106 int i, j;
00107
00108 if (burns != NULL) {
00109 printf("%d bytes:\n", burns->length);
00110
00111 for (i=0; i < ((burns->length + 15) / 16); i++) {
00112 for (j=0; j < 16; j++) {
00113 if ((i*16+j) < burns->length)
00114 printf("%X ", burns->block[i*16+j]);
00115 else
00116 printf(" ");
00117 }
00118 printf(": ");
00119 for (j=0; j < 16; j++) {
00120 if ((i*16+j) < burns->length) {
00121 if (burns->block[i*16+j] >= 32)
00122 putchar(burns->block[i*16+j]);
00123 else
00124 putchar('.');
00125 } else {
00126 putchar(' ');
00127 }
00128 }
00129 printf("\n");
00130 }
00131 }
00132 }
00133
00134 bytes *cat_bytes(bytes *homer, bytes *marge)
00135
00136
00137
00138
00139 {
00140 byte *newblock;
00141 int i,j;
00142
00143 if ((homer != NULL) && (marge != NULL)) {
00144
00145 newblock = malloc(homer->length + marge->length);
00146 if (newblock != NULL) {
00147 j=0;
00148 for (i=0; i<homer->length; i++)
00149 newblock[j++]=homer->block[i];
00150 for (i=0; i<marge->length; i++)
00151 newblock[j++]=marge->block[i];
00152 if (homer->block != NULL)
00153 free(homer->block);
00154 homer->block=newblock;
00155 homer->length=j;
00156 }
00157 else
00158 error("cat_bytes","can't allocate new block");
00159 }
00160 return (homer);
00161 }
00162
00163 void copy_bytes(bytes *lisa, bytes *nelson)
00164
00165
00166
00167
00168
00169 {
00170 int i;
00171
00172 if (lisa->length == nelson->length) {
00173 for (i=0; i<lisa->length; i++) {
00174 lisa->block[i] = nelson->block[i];
00175 }
00176 }
00177 }
00178
00179
00180 bytes *duplicate_bytes(bytes *smithers)
00181
00182
00183
00184
00185
00186 {
00187 bytes *sideshow_bob;
00188
00189 sideshow_bob = new_bytes(num_bytes(smithers));
00190 if (sideshow_bob != NULL) {
00191 copy_bytes(sideshow_bob, smithers);
00192 }
00193 return (sideshow_bob);
00194 }
00195