William Reynolds
/
LPC_Bootloader
Embed:
(wiki syntax)
Show/hide line numbers
UUEncode.cpp
00001 #include "mbed.h" 00002 #include "LPC.h" 00003 00004 //ENCODING FUNCTIONS BELOW 00005 int SerialBuffered::UUEncode() { //Encodes the next 45 bytes in the file into a UUEncoded string with checksum and LF/CR characters ready to be sent to the board 00006 int loop=0; int i; checksum=0; 00007 for (int a=0; a<=63; a++) uuline[a]=0; 00008 for (i=0; i<15; i++) { 00009 if ((ch1=fgetc(f)) == EOF) //Padded with 0x00 making life easier when the end of the file is reached 00010 ch1=0x00; 00011 if ((ch2=fgetc(f)) == EOF) 00012 ch2=0x00; 00013 if ((ch3=fgetc(f)) == EOF) 00014 ch3=0x00; 00015 checksum +=3; 00016 Encode3(); 00017 uuline[1+(loop*4)]=n1; 00018 uuline[2+(loop*4)]=n2; 00019 uuline[3+(loop*4)]=n3; 00020 uuline[4+(loop*4)]=n4; 00021 loop++; 00022 } 00023 uuline[0] = checksum+32; 00024 uuline[61] = 0x0D; 00025 uuline[62] = 0x0A; 00026 uuline[63] = 0x00; 00027 return 0; 00028 } 00029 00030 int SerialBuffered:: Encode3() { //Does the actual raw encoding. Laid out in a less compact way to explicitly show the process going on 00031 n1 = 0, n2 = 0, n3 = 0, n4 = 0; 00032 sum20 += ch1+ch2+ch3; 00033 if ((ch1-128)>=0) {ch1-=128; n1+=32;} 00034 if ((ch1-64)>=0) {ch1-=64; n1+=16;} 00035 if ((ch1-32)>=0) {ch1-=32; n1+=8;} 00036 if ((ch1-16)>=0) {ch1-=16; n1+=4;} 00037 if ((ch1-8)>=0) {ch1-=8; n1+=2;} 00038 if ((ch1-4)>=0) {ch1-=4; n1+=1;} 00039 00040 if ((ch1-2)>=0) {ch1-=2; n2+=32;} 00041 if ((ch1-1)>=0) {ch1-=1; n2+=16;} 00042 if ((ch2-128)>=0) {ch2-=128; n2+=8;} 00043 if ((ch2-64)>=0) {ch2-=64; n2+=4;} 00044 if ((ch2-32)>=0) {ch2-=32; n2+=2;} 00045 if ((ch2-16)>=0) {ch2-=16; n2+=1;} 00046 00047 if ((ch2-8)>=0) {ch2-=8; n3+=32;} 00048 if ((ch2-4)>=0) {ch2-=4; n3+=16;} 00049 if ((ch2-2)>=0) {ch2-=2; n3+=8;} 00050 if ((ch2-1)>=0) {ch2-=1; n3+=4;} 00051 if ((ch3-128)>=0) {ch3-=128; n3+=2;} 00052 if ((ch3-64)>=0) {ch3-=64; n3+=1;} 00053 00054 if ((ch3-32)>=0) {ch3-=32; n4+=32;} 00055 if ((ch3-16)>=0) {ch3-=16; n4+=16;} 00056 if ((ch3-8)>=0) {ch3-=8; n4+=8;} 00057 if ((ch3-4)>=0) {ch3-=4; n4+=4;} 00058 if ((ch3-2)>=0) {ch3-=2; n4+=2;} 00059 if ((ch3-1)>=0) {ch3-=1; n4+=1;} 00060 00061 if (n1 == 0x00) n1=0x60; 00062 else n1+=0x20; 00063 if (n2 == 0x00) n2=0x60; 00064 else n2+=0x20; 00065 if (n3 == 0x00) n3=0x60; 00066 else n3+=0x20; 00067 if (n4 == 0x00) n4=0x60; 00068 else n4+=0x20; 00069 return 0; 00070 } 00071 00072 int SerialBuffered::FirstEncode() { //Function to add two's complement of the first 7 DWORDs into the space occupied by the 8th DWORD (DWORDs 1-8 should add to zero) 00073 long int precheck = 0; 00074 int a; 00075 for (a=0; a<9; a++) { 00076 ch1 = fgetc(f); ch2 = fgetc(f); ch3 = fgetc(f); 00077 sum[a*3]=ch1; sum[(a*3)+1]=ch2; sum[(a*3)+2]=ch3; 00078 } 00079 ch1 = fgetc(f); fgetc(f); fgetc(f); fgetc(f); fgetc(f); //Ignores the 4 bytes which are to be overwritten 00080 sum[27] = ch1; 00081 00082 for (a=0; a<7; a++) { 00083 sum1[a*4] = sum[a*4+3]; 00084 sum1[a*4+1] = sum[a*4+2]; 00085 sum1[a*4+2] = sum[a*4+1]; 00086 sum1[a*4+3] = sum[a*4]; 00087 precheck += (sum1[a*4]*0x1000000) + (sum1[a*4+1]*0x10000) + (sum1[a*4+2]*0x100) + sum1[a*4+3]; 00088 } 00089 precheck = ~precheck+1; //Takes the two's complement of the checksum 00090 sum[28] = precheck & 0xFF; 00091 sum[29] = (precheck >> 8) & 0xFF; 00092 sum[30] = (precheck >>16) & 0xFF; 00093 sum[31] = (precheck >>24) & 0xFF; 00094 sum[32] = fgetc(f); 00095 for (int a=33; a<46; a++) sum[a] = fgetc(f); 00096 fseek(f, 0, SEEK_END); 00097 filesize = ftell(f); 00098 fclose(f); 00099 f=fopen("/fs/delete.bin", "w"); //Opens a temporary file for writing to 00100 fwrite (sum, 1, sizeof(sum), f); //Writes the checksum-added and encoded bytes 00101 fclose(f); 00102 f=fopen("/fs/delete.bin", "r"); //Opens the original binary file again for reading 00103 UUEncode(); 00104 return 0; 00105 } 00106 00107 int SerialBuffered::EndUUEncode() { //Encodes the last 124 bytes of a 1024 byte block 00108 int loop=0; int i; checksum=0; 00109 for (int a=0; a<=50; a++) enduuline[a]=0; 00110 for (i=1; i<=12; i++) { 00111 if ((ch1=fgetc(f)) == EOF) 00112 ch1=0x00; 00113 if ((ch2=fgetc(f)) == EOF) 00114 ch2=0x00; 00115 if ((ch3=fgetc(f)) == EOF) 00116 ch3=0x00; 00117 checksum +=3; 00118 if (loop==11) { 00119 checksum-=2; 00120 fseek (f, -2, SEEK_CUR); 00121 ch2=0x00; ch3=0x00; 00122 } 00123 Encode3(); 00124 enduuline[1+(loop*4)]=n1; 00125 enduuline[2+(loop*4)]=n2; 00126 enduuline[3+(loop*4)]=n3; 00127 enduuline[4+(loop*4)]=n4; 00128 loop++; 00129 } 00130 enduuline[0] = checksum+32; 00131 enduuline[49] = 0x0D; 00132 enduuline[50] = 0x0A; 00133 enduuline[51] = 0x00; 00134 return 0; 00135 }
Generated on Tue Jul 19 2022 22:34:47 by 1.7.2