📄 uucodec.c
字号:
/***************************************** Copyright (c) 2001-2002 Sigma Designs, Inc. All Rights Reserved Proprietary and Confidential *****************************************//* This file is part of the boot loader *//* * uucodec.c * * decode uuencoded stream * * first revision by Ho Lee 11/01/2002 *//* * uuencode : encode a binary stream using only printing ASCII characters (0x21 - 0x60) * * first line : "begin [permission] [filename]" * stream : "[length of the line] [stream]" * encode(ch) : ch = (ch == 0) ? 0x60 : ch + 0x20 * maximum length = 45 (0x2d + 0x20 = 0x4d = 'M') * stream : 3 bytes => 4 printing bytes * 8 8 8 => 6 6 6 6 * last line - 1 : 0x60 (length = 0) * last line : "end" */#include "uart.h"#include "util.h"#define MAX_BUF_LEN 1024#define MAX_RETRIES 10#define DECODE(x) (((x) == 0x60) ? 0 : ((x) - 0x20))#define ISVALID(x) ((x) > 0x20 && (x) <= 0x60)// read uuencoded stream from UART and decoded it// buf : buffer where the decoded stream will be saved to // maxlen : maximum length of buffer. 0 for no limitint serial_uudecode(unsigned char *buf, int maxlen){ int nretry, nlines = 0, nbytes = 0, nloop = 0, len; int i, error = 0, full = 0; unsigned char str[MAX_BUF_LEN], *cp, decode[3]; // look for the begin of the stream for (nretry = 0; nretry < MAX_RETRIES; ++nretry) { GetRawString(str, MAX_BUF_LEN); if (strncmp(str, "begin ", 6) == 0) break; } if (nretry == MAX_RETRIES) { PrintUart("\r\n### Timeout ###\r\n", -1); return 0; } // decode the stream body for (;;) { GetRawString(str, MAX_BUF_LEN); // print out progress // print '.' per 0x40 (64) lines (2880 bytes) // print new line per 0x40 '.' : 4096 lines (184320 bytes) if ((nlines++ & 0x3f) == 0) { if ((nloop++ & 0x3f) == 0) if (nloop != 1) PrintUart("\r\n", -1); PrintChar('.'); } // decode from printing stream to binary stream for (i = 0; str[i] != 0 && str[i] != '\r' && str[i] != '\n'; ++i) { if (!ISVALID(str[i])) { error = 1; break; } str[i] = DECODE(str[i]); } if (i == 0 || error) break; str[i] = 0; // if the length of the current line is 0, the next line must have "end" string if ((len = str[0]) == 0) { GetRawString(str, MAX_BUF_LEN); if (strncmp(str, "end", 3) == 0) { PrintUart("\r\n", -1); return nbytes; } else { PrintUart("\r\n### No 'end' ###\r\n", -1); return -1; } } // decode 4 byte of stream => 3 bytes of original stream for (cp = str + 1; len > 0 && !full; len -= 3, cp += 4) { decode[0] = (cp[0] << 2) | (cp[1] >> 4); decode[1] = (cp[1] << 4) | (cp[2] >> 2); decode[2] = (cp[2] << 6) | (cp[3]); for (i = 0; i < 3 && i < len; ++i) { *buf++ = decode[i]; if (++nbytes == maxlen) { full = 1; break; } } } } PrintUart("\r\n### Invalid data ###\r\n", -1); return -1;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -