xref: /freebsd/usr.sbin/vidcontrol/decode.c (revision eba230afba4932f02a1ca44efc797cf7499a5cb0)
17363955dSSøren Schmidt /*-
2*1de7b4b8SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
3*1de7b4b8SPedro F. Giffuni  *
4f76b3199SUlrich Spörlein  * Copyright (c) 1994 Søren Schmidt
57363955dSSøren Schmidt  * All rights reserved.
67363955dSSøren Schmidt  *
77363955dSSøren Schmidt  * Redistribution and use in source and binary forms, with or without
87363955dSSøren Schmidt  * modification, are permitted provided that the following conditions
97363955dSSøren Schmidt  * are met:
107363955dSSøren Schmidt  * 1. Redistributions of source code must retain the above copyright
11a926a37bSSøren Schmidt  *    notice, this list of conditions and the following disclaimer,
12a926a37bSSøren Schmidt  *    in this position and unchanged.
137363955dSSøren Schmidt  * 2. Redistributions in binary form must reproduce the above copyright
147363955dSSøren Schmidt  *    notice, this list of conditions and the following disclaimer in the
157363955dSSøren Schmidt  *    documentation and/or other materials provided with the distribution.
167363955dSSøren Schmidt  * 3. The name of the author may not be used to endorse or promote products
1721dc7d4fSJens Schweikhardt  *    derived from this software without specific prior written permission
187363955dSSøren Schmidt  *
197363955dSSøren Schmidt  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
207363955dSSøren Schmidt  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
217363955dSSøren Schmidt  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
227363955dSSøren Schmidt  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
237363955dSSøren Schmidt  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
247363955dSSøren Schmidt  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
257363955dSSøren Schmidt  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
267363955dSSøren Schmidt  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
277363955dSSøren Schmidt  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
287363955dSSøren Schmidt  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
297363955dSSøren Schmidt  */
307363955dSSøren Schmidt 
317363955dSSøren Schmidt #include <stdio.h>
32bfc214e3SBrian Somers #include <string.h>
33bfc214e3SBrian Somers #include "decode.h"
347363955dSSøren Schmidt 
decode(FILE * fd,char * buffer,int len)355e0db936SMaxim Sobolev int decode(FILE *fd, char *buffer, int len)
367363955dSSøren Schmidt {
375e0db936SMaxim Sobolev 	int n, pos = 0, tpos;
385e0db936SMaxim Sobolev 	char *bp, *p;
395e0db936SMaxim Sobolev 	char tbuffer[3];
407363955dSSøren Schmidt 	char temp[128];
417363955dSSøren Schmidt 
427363955dSSøren Schmidt #define	DEC(c)	(((c) - ' ') & 0x3f)
437363955dSSøren Schmidt 
447363955dSSøren Schmidt 	do {
457363955dSSøren Schmidt 		if (!fgets(temp, sizeof(temp), fd))
467363955dSSøren Schmidt 			return(0);
477363955dSSøren Schmidt 	} while (strncmp(temp, "begin ", 6));
489a03b27dSXin LI 	sscanf(temp, "begin %o %s", (unsigned *)&n, temp);
495e0db936SMaxim Sobolev 	bp = buffer;
507363955dSSøren Schmidt 	for (;;) {
517363955dSSøren Schmidt 		if (!fgets(p = temp, sizeof(temp), fd))
527363955dSSøren Schmidt 			return(0);
537363955dSSøren Schmidt 		if ((n = DEC(*p)) <= 0)
547363955dSSøren Schmidt 			break;
555e0db936SMaxim Sobolev 		for (++p; n > 0; p += 4, n -= 3) {
565e0db936SMaxim Sobolev 			tpos = 0;
577363955dSSøren Schmidt 			if (n >= 3) {
585e0db936SMaxim Sobolev 				tbuffer[tpos++] = DEC(p[0])<<2 | DEC(p[1])>>4;
595e0db936SMaxim Sobolev 				tbuffer[tpos++] = DEC(p[1])<<4 | DEC(p[2])>>2;
605e0db936SMaxim Sobolev 				tbuffer[tpos++] = DEC(p[2])<<6 | DEC(p[3]);
617363955dSSøren Schmidt 			}
627363955dSSøren Schmidt 			else {
637363955dSSøren Schmidt 				if (n >= 1) {
645e0db936SMaxim Sobolev 					tbuffer[tpos++] =
657363955dSSøren Schmidt 						DEC(p[0])<<2 | DEC(p[1])>>4;
667363955dSSøren Schmidt 				}
677363955dSSøren Schmidt 				if (n >= 2) {
685e0db936SMaxim Sobolev 					tbuffer[tpos++] =
697363955dSSøren Schmidt 						DEC(p[1])<<4 | DEC(p[2])>>2;
707363955dSSøren Schmidt 				}
717363955dSSøren Schmidt 				if (n >= 3) {
725e0db936SMaxim Sobolev 					tbuffer[tpos++] =
737363955dSSøren Schmidt 						DEC(p[2])<<6 | DEC(p[3]);
747363955dSSøren Schmidt 				}
757363955dSSøren Schmidt 			}
765e0db936SMaxim Sobolev 			if (tpos == 0)
775e0db936SMaxim Sobolev 				continue;
785e0db936SMaxim Sobolev 			if (tpos + pos > len) {
795e0db936SMaxim Sobolev 				tpos = len - pos;
805e0db936SMaxim Sobolev 				/*
815e0db936SMaxim Sobolev 				 * Arrange return value > len to indicate
825e0db936SMaxim Sobolev 				 * overflow.
835e0db936SMaxim Sobolev 				 */
845e0db936SMaxim Sobolev 				pos++;
855e0db936SMaxim Sobolev 			}
865e0db936SMaxim Sobolev 			bcopy(tbuffer, bp, tpos);
875e0db936SMaxim Sobolev 			pos += tpos;
885e0db936SMaxim Sobolev 			bp += tpos;
895e0db936SMaxim Sobolev 			if (pos > len)
905e0db936SMaxim Sobolev 				return(pos);
915e0db936SMaxim Sobolev 		}
927363955dSSøren Schmidt 	}
937363955dSSøren Schmidt 	if (!fgets(temp, sizeof(temp), fd) || strcmp(temp, "end\n"))
947363955dSSøren Schmidt 		return(0);
957363955dSSøren Schmidt 	return(pos);
967363955dSSøren Schmidt }
97